'Run a one-off command.'

def run(self, name, image, entrypoint, command):

    self._create_container(name, image, command, copy.deepcopy(RUN_TEMPLATE), entrypoint=entrypoint)
    self._put_unit(name, {'desiredState': 'launched'})
    state = self._wait_for_container_state(name)
    try:
        machineID = state.get('machineID')
        machines = self._get_machines()
        if (not machines):
            raise RuntimeError('no    available    hosts    to    run    command')
        primaryIP = None
        for m in machines.get('machines', []):
            if (m['id'] == machineID):
                primaryIP = m['primaryIP']
        if (not primaryIP):
            raise RuntimeError('could    not    find    host')
        file_obj = cStringIO.StringIO(base64.b64decode(self.pkey))
        pkey = paramiko.RSAKey(file_obj=file_obj)
        ssh = paramiko.SSHClient()
        ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
        ssh.connect(primaryIP, username='core', pkey=pkey)
        tran = ssh.get_transport()
        def _do_ssh(cmd):
            with tran.open_session() as chan:
                chan.exec_command(cmd)
                while (not chan.exit_status_ready()):
                    time.sleep(1)
                out = chan.makefile()
                output = out.read()
                rc = chan.recv_exit_status()
                return (rc, output)
        while True:
            (rc, _) = _do_ssh('docker    inspect    {name}'.format(**locals()))
            if (rc == 0):
                break
            time.sleep(1)
        else:
            raise RuntimeError('failed    to    create    container')
        for _ in xrange(2):
            (_rc, _output) = _do_ssh('docker    inspect    {name}'.format(**locals()))
            if (_rc != 0):
                raise RuntimeError('failed    to    inspect    container')
            _container = json.loads(_output)
            started_at = _container[0]['State']['StartedAt']
            if (not started_at.startswith('0001')):
                break
            time.sleep(1)
        else:
            raise RuntimeError('container    failed    to    start')
        for _ in xrange(1200):
            (_rc, _output) = _do_ssh('docker    inspect    {name}'.format(**locals()))
            if (_rc != 0):
                raise RuntimeError('failed    to    inspect    container')
            _container = json.loads(_output)
            finished_at = _container[0]['State']['FinishedAt']
            if (not finished_at.startswith('0001')):
                break
            time.sleep(1)
        else:
            raise RuntimeError('container    timed    out')
        (_rc, output) = _do_ssh('docker    logs    {name}'.format(**locals()))
        if (_rc != 0):
            raise RuntimeError('could    not    attach    to    container')
        (_rc, _output) = _do_ssh('docker    inspect    {name}'.format(**locals()))
        if (_rc != 0):
            raise RuntimeError('could    not    determine    exit    code')
        container = json.loads(_output)
        rc = container[0]['State']['ExitCode']
    finally:
        self._destroy_container(name)
        self._wait_for_destroy(name)
    return (rc, output)
