コード例 #1
0
ファイル: command_ssh.py プロジェクト: ruo91/armada
def command_ssh(args):
    microservice_name = args.microservice_name or os.environ['MICROSERVICE_NAME']
    if not microservice_name:
        raise ValueError('No microservice name supplied.')

    instances = armada_utils.get_matched_containers(microservice_name)
    instances_count = len(instances)
    if instances_count > 1:
        raise armada_utils.ArmadaCommandException(
            'There are too many ({instances_count}) matching containers. '
            'Provide more specific container_id or microservice name.'.format(**locals()))
    instance = instances[0]

    service_id = instance['ServiceID']
    container_id = service_id.split(':')[0]
    payload = {'container_id': container_id}

    result = json.loads(armada_api.get('ssh-address', payload, ship_name=instance['Node']))

    if result['status'] != 'ok':
        raise armada_utils.ArmadaCommandException('armada API error: {0}'.format(result['error']))
    ssh_host, ssh_port = result['ssh'].split(':')

    print("Connecting to {0} at {1}:{2}...".format(instance['ServiceName'], ssh_host, ssh_port))

    docker_key_file = os.path.join(os.path.dirname(os.path.realpath(__file__)), '..', 'keys/docker.key')

    if args.command:
        command = 'sudo ' + ' '.join(args.command)
    else:
        command = 'sudo -i'
    ssh_command = ('ssh -t -p {ssh_port} -i {docker_key_file} -o StrictHostKeyChecking=no docker@{ssh_host} '
                   '"{command}"').format(**locals())
    subprocess.call(ssh_command, shell=True)
コード例 #2
0
def command_diagnose(args):
    microservice_name = args.microservice_name

    script = "diagnose.sh"
    if args.logs:
        script = "logs.sh"
    diagnostic_command = ("armada ssh -i {microservice_name} "
                          "bash < /opt/armada/armada_command/diagnostic_scripts/{script}").format(**locals())
    exit_code = subprocess.call(diagnostic_command, shell=True)
    if exit_code != 0:
        instances = get_matched_containers(microservice_name)
        if instances is not None and len(instances) == 1:
            instance = instances[0]
            status = instance['Status']
            if status == 'recovering':
                params = instance['params']
                print('RESTART_CONTAINER_PARAMETERS:')
                print(json.dumps(params, indent=4, sort_keys=True))
            elif status in ['crashed', 'not-recovered']:
                params = instance['params']
                print('RESTART_CONTAINER_PARAMETERS:')
                print(json.dumps(params, indent=4, sort_keys=True))
                print('')
                container_id = instance['container_id']
                print('Docker logs of container_id: {}'.format(container_id))
                diagnostic_command = ("docker logs {}".format(container_id))
                subprocess.call(diagnostic_command, shell=True)
コード例 #3
0
def command_diagnose(args):
    microservice_name = args.microservice_name

    script = "diagnose.sh"
    if args.logs:
        script = "logs.sh"
    diagnostic_command = (
        "armada ssh -i {microservice_name} "
        "bash < /opt/armada/armada_command/diagnostic_scripts/{script}"
    ).format(**locals())
    exit_code = subprocess.call(diagnostic_command, shell=True)
    if exit_code != 0:
        instances = get_matched_containers(microservice_name)
        if instances is not None and len(instances) == 1:
            instance = instances[0]
            status = instance['Status']
            if status == 'recovering':
                params = instance['params']
                print('RESTART_CONTAINER_PARAMETERS:')
                print(json.dumps(params, indent=4, sort_keys=True))
            elif status in ['crashed', 'not-recovered']:
                params = instance['params']
                print('RESTART_CONTAINER_PARAMETERS:')
                print(json.dumps(params, indent=4, sort_keys=True))
                print('')
                container_id = instance['container_id']
                print('Docker logs of container_id: {}'.format(container_id))
                diagnostic_command = ("docker logs {}".format(container_id))
                subprocess.call(diagnostic_command, shell=True)
コード例 #4
0
ファイル: command_restart.py プロジェクト: ruo91/armada
def command_restart(args):
    if args.verbose:
        global verbose
        verbose = True
    microservice_name = args.microservice_name or os.environ['MICROSERVICE_NAME']
    if not microservice_name:
        raise ValueError('No microservice name supplied.')

    instances = armada_utils.get_matched_containers(microservice_name)

    instances_count = len(instances)

    if instances_count > 1:
        if not args.all:
            raise armada_utils.ArmadaCommandException(
                'There are too many ({instances_count}) matching containers. '
                'Provide more specific container_id or microservice name or use -a/--all flag.'.format(**locals()))
        print('Restarting {instances_count} services {microservice_name}...'.format(**locals()))
    else:
        print('Restarting service {microservice_name}...'.format(**locals()))

    were_errors = False
    for i, instance in enumerate(instances):
        try:
            if instances_count > 1:
                print('[{0}/{1}]'.format(i + 1, instances_count))
            container_id = instance['ServiceID'].split(':')[0]
            is_run_locally = armada_utils.is_local_container(container_id)
            if is_run_locally:
                result = json.loads(armada_api.get('env/{container_id}/ARMADA_RUN_COMMAND'.format(**locals())))
                if result['status'] == 'ok':
                    stop_command = 'armada stop {container_id}'.format(**locals())
                    run_command = base64.b64decode(result['value'])
                    assert armada_utils.execute_local_command(stop_command, stream_output=True, retries=3)[0] == 0
                    assert armada_utils.execute_local_command(run_command, stream_output=True, retries=5)[0] == 0
                    if instances_count > 1:
                        print()
                else:
                    raise armada_utils.ArmadaCommandException('ERROR: {0}'.format(result['error']))
            else:
                payload = {'container_id': container_id}

                result = armada_api.post('restart', payload, ship_name=instance['Node'])

                if result['status'] == 'ok':
                    new_container_id = result['container_id']
                    print('Service has been restarted and is running in container {new_container_id} '
                          'available at addresses:'.format(**locals()))
                    for service_address, docker_port in result['endpoints'].iteritems():
                        print('  {0} ({1})'.format(service_address, docker_port))
                    if instances_count > 1:
                        print()
                else:
                    raise armada_utils.ArmadaCommandException('ERROR: {0}'.format(result['error']))
        except:
            traceback.print_exc()
            were_errors = True
    if were_errors:
        sys.exit(1)
コード例 #5
0
ファイル: command_ssh.py プロジェクト: zerofudge/armada
def command_ssh(args):
    microservice_name = args.microservice_name or os.environ["MICROSERVICE_NAME"]
    if not microservice_name:
        raise ValueError("No microservice name supplied.")

    instances = armada_utils.get_matched_containers(microservice_name)
    instances_count = len(instances)
    if instances_count > 1:
        raise armada_utils.ArmadaCommandException(
            "There are too many ({instances_count}) matching containers. "
            "Provide more specific container_id or microservice name.".format(**locals())
        )
    instance = instances[0]

    if "kv_index" in instance:
        raise armada_utils.ArmadaCommandException("Cannot connect to not running service.")

    service_id = instance["ServiceID"]
    container_id = service_id.split(":")[0]
    payload = {"container_id": container_id}

    is_local = False
    local_microservices_ids = set(consul_query("agent/services").keys())
    if container_id in local_microservices_ids:
        is_local = True

    if args.command:
        command = " ".join(args.command)
    else:
        command = "bash"
        args.tty = True
        args.interactive = True

    tty = "-t" if args.tty else ""
    interactive = "-i" if args.interactive else ""
    term = os.environ.get("TERM") or "dummy"

    command = pipes.quote(command)
    docker_command = "docker exec {interactive} {tty} {container_id} env TERM={term} " "sh -c {command}".format(
        **locals()
    )

    if is_local:
        print("Connecting to {0}...".format(instance["ServiceName"]))
        ssh_args = shlex.split(docker_command)
    else:
        ssh_host = instance["Address"]
        docker_key_file = os.path.join(os.path.dirname(os.path.realpath(__file__)), "..", "keys/docker.key")
        remote_ssh_chunk = "ssh -t {tty} -p 2201 -i {docker_key_file} -o StrictHostKeyChecking=no docker@{ssh_host}".format(
            **locals()
        )
        ssh_args = shlex.split(remote_ssh_chunk)
        ssh_args.extend(("sudo", docker_command))
        print("Connecting to {0} on host {1}...".format(instance["ServiceName"], ssh_host))

    os.execvp(ssh_args[0], ssh_args)
コード例 #6
0
def command_restart(args):
    microservice_handle = args.microservice_handle or os.environ['MICROSERVICE_NAME']
    if not microservice_handle:
        raise ValueError('No microservice name or container id supplied.')
    armada_utils.notify_about_detected_dev_environment(microservice_handle)

    instances = armada_utils.get_matched_containers(microservice_handle)

    instances_count = len(instances)

    if instances_count > 1:
        if not args.all:
            raise armada_utils.ArmadaCommandException(
                'There are too many ({instances_count}) matching containers. '
                'Provide more specific container_id or microservice name or use -a/--all flag.'.format(**locals()))
        print('Restarting {instances_count} services {microservice_handle}...'.format(**locals()))
    else:
        microservice_name = instances[0]['ServiceName']
        container_id = instances[0]["ServiceID"].split(':')[0]
        print('Restarting service {microservice_name} ({container_id})...'.format(**locals()))

    were_errors = False
    for i, instance in enumerate(instances):
        try:
            if instances_count > 1:
                print('[{0}/{1}]'.format(i + 1, instances_count))

            container_id = instance['ServiceID'].split(':')[0]

            payload = {'container_id': container_id}
            if args.ship:
                payload['target_ship'] = args.ship
                payload['force'] = args.force

            print('Checking if there is new image version. May take few minutes if download is needed...')
            ship_name = instance['Address']
            result = armada_api.post('restart', payload, ship_name=ship_name)

            if result['status'] == 'ok':
                new_container_id = result['container_id']
                print('Service has been restarted and is running in container {new_container_id} '
                      'available at addresses:'.format(**locals()))
                for service_address, docker_port in result['endpoints'].iteritems():
                    print('  {0} ({1})'.format(service_address, docker_port))
                if instances_count > 1:
                    print()
            else:
                raise armada_utils.ArmadaCommandException(result['error'])
        except armada_utils.ArmadaCommandException as e:
            print("ArmadaCommandException: {0}".format(str(e)))
            were_errors = True
        except:
            traceback.print_exc()
            were_errors = True
    if were_errors:
        sys.exit(1)
コード例 #7
0
def command_ssh(args):
    microservice_name = args.microservice_name or os.environ[
        'MICROSERVICE_NAME']
    if not microservice_name:
        raise ValueError('No microservice name supplied.')

    instances = armada_utils.get_matched_containers(microservice_name)
    instances_count = len(instances)
    if instances_count > 1:
        raise armada_utils.ArmadaCommandException(
            'There are too many ({instances_count}) matching containers. '
            'Provide more specific container_id or microservice name.'.format(
                **locals()))
    instance = instances[0]

    service_id = instance['ServiceID']
    container_id = service_id.split(':')[0]
    payload = {'container_id': container_id}

    is_local = False
    local_microservices_ids = set(consul_query('agent/services').keys())
    if container_id in local_microservices_ids:
        is_local = True

    if args.command:
        command = ' '.join(args.command)
    else:
        command = 'bash'
        args.tty = True
        args.interactive = True

    tty = '-t' if args.tty else ''
    interactive = '-i' if args.interactive else ''
    term = os.environ.get('TERM') or 'dummy'

    command = pipes.quote(command)
    docker_command = 'docker exec {interactive} {tty} {container_id} env TERM={term} ' \
                     'sh -c {command}'.format(**locals())

    if is_local:
        print("Connecting to {0}...".format(instance['ServiceName']))
        ssh_args = shlex.split(docker_command)
    else:
        ssh_host = instance['Address']
        docker_key_file = os.path.join(
            os.path.dirname(os.path.realpath(__file__)), '..',
            'keys/docker.key')
        remote_ssh_chunk = 'ssh -t {tty} -p 2201 -i {docker_key_file} -o StrictHostKeyChecking=no docker@{ssh_host}'\
            .format(**locals())
        ssh_args = shlex.split(remote_ssh_chunk)
        ssh_args.extend(('sudo', docker_command))
        print("Connecting to {0} on host {1}...".format(
            instance['ServiceName'], ssh_host))

    os.execvp(ssh_args[0], ssh_args)
コード例 #8
0
ファイル: command_restart.py プロジェクト: labuzm/armada
def command_restart(args):
    microservice_handle = args.microservice_handle or os.environ['MICROSERVICE_NAME']
    if not microservice_handle:
        raise ValueError('No microservice name or container id supplied.')

    instances = armada_utils.get_matched_containers(microservice_handle)

    instances_count = len(instances)

    if instances_count > 1:
        if not args.all:
            raise armada_utils.ArmadaCommandException(
                'There are too many ({instances_count}) matching containers. '
                'Provide more specific container_id or microservice name or use -a/--all flag.'.format(**locals()))
        print('Restarting {instances_count} services {microservice_handle}...'.format(**locals()))
    else:
        microservice_name = instances[0]['ServiceName']
        container_id = instances[0]["ServiceID"].split(':')[0]
        print('Restarting service {microservice_name} ({container_id})...'.format(**locals()))

    were_errors = False
    for i, instance in enumerate(instances):
        try:
            if instances_count > 1:
                print('[{0}/{1}]'.format(i + 1, instances_count))

            container_id = instance['ServiceID'].split(':')[0]

            payload = {'container_id': container_id}
            if args.ship:
                payload['target_ship'] = args.ship
                payload['force'] = args.force

            print('Checking if there is new image version. May take few minutes if download is needed...')
            ship_name = instance['Address']
            result = armada_api.post('restart', payload, ship_name=ship_name)

            if result['status'] == 'ok':
                new_container_id = result['container_id']
                print('Service has been restarted and is running in container {new_container_id} '
                      'available at addresses:'.format(**locals()))
                for service_address, docker_port in result['endpoints'].iteritems():
                    print('  {0} ({1})'.format(service_address, docker_port))
                if instances_count > 1:
                    print()
            else:
                raise armada_utils.ArmadaCommandException(result['error'])
        except armada_utils.ArmadaCommandException as e:
            print("ArmadaCommandException: {0}".format(str(e)))
            were_errors = True
        except:
            traceback.print_exc()
            were_errors = True
    if were_errors:
        sys.exit(1)
コード例 #9
0
ファイル: command_stop.py プロジェクト: chenlFly/armada
def command_stop(args):
    microservice_handle = args.microservice_handle or os.environ[
        'MICROSERVICE_NAME']
    if not microservice_handle:
        raise ValueError('No microservice name or container id supplied.')

    instances = armada_utils.get_matched_containers(microservice_handle)
    instances_count = len(instances)

    if instances_count > 1:
        if not args.all:
            raise armada_utils.ArmadaCommandException(
                'There are too many ({instances_count}) matching containers. '
                'Provide more specific container_id or microservice name or use -a/--all flag.'
                .format(**locals()))
        print('Stopping {instances_count} services {microservice_handle}...'.
              format(**locals()))
    else:
        microservice_name = instances[0]['ServiceName']
        container_id = instances[0]["ServiceID"].split(':')[0]
        print(
            'Stopping service {microservice_name} ({container_id})...'.format(
                **locals()))

    were_errors = False
    for i, instance in enumerate(instances):
        try:
            if instances_count > 1:
                print('[{0}/{1}]'.format(i + 1, instances_count))
            if 'kv_index' in instance:
                kv.kv_remove('service/{}/{}'.format(instance['ServiceName'],
                                                    instance['kv_index']))
                print('Service {} has been removed.'.format(
                    instance['ServiceName']))
            else:
                container_id = instance['ServiceID'].split(':')[0]
                payload = {'container_id': container_id}
                ship_name = instance['Address']
                result = armada_api.post('stop', payload, ship_name=ship_name)

                if result['status'] == 'ok':
                    print('Service {container_id} has been stopped.'.format(
                        **locals()))
                    if instances_count > 1:
                        print()
                else:
                    raise ArmadaCommandException('Stopping error: {0}'.format(
                        result['error']))
        except:
            traceback.print_exc()
            were_errors = True

    if were_errors:
        sys.exit(1)
コード例 #10
0
ファイル: command_ssh.py プロジェクト: yanghongkjxy/armada
def command_ssh(args):
    microservice_name = args.microservice_name or os.environ['MICROSERVICE_NAME']
    if not microservice_name:
        raise ValueError('No microservice name supplied.')

    instances = armada_utils.get_matched_containers(microservice_name)
    instances_count = len(instances)
    if instances_count > 1:
        raise armada_utils.ArmadaCommandException(
            'There are too many ({instances_count}) matching containers. '
            'Provide more specific container_id or microservice name.'.format(**locals()))
    instance = instances[0]

    service_id = instance['ServiceID']
    container_id = service_id.split(':')[0]
    payload = {'container_id': container_id}

    is_local = False
    local_microservices_ids = set(consul_query('agent/services').keys())
    if container_id in local_microservices_ids:
        is_local = True

    if args.command:
        command = ' '.join(args.command)
    else:
        command = 'bash'
        args.tty = True
        args.interactive = True

    tty = '-t' if args.tty else ''
    interactive = '-i' if args.interactive else ''
    term = os.environ.get('TERM') or 'dummy'

    command = pipes.quote(command)
    docker_command = 'docker exec {interactive} {tty} {container_id} env TERM={term} ' \
                     'sh -c {command}'.format(**locals())

    if is_local:
        print("Connecting to {0}...".format(instance['ServiceName']))
        ssh_args = shlex.split(docker_command)
    else:
        ssh_host = instance['Address']
        docker_key_file = os.path.join(os.path.dirname(os.path.realpath(__file__)), '..', 'keys/docker.key')
        remote_ssh_chunk = 'ssh -t {tty} -p 2201 -i {docker_key_file} -o StrictHostKeyChecking=no docker@{ssh_host}' \
            .format(**locals())
        ssh_args = shlex.split(remote_ssh_chunk)
        ssh_args.extend(('sudo', docker_command))
        print("Connecting to {0} on host {1}...".format(instance['ServiceName'], ssh_host))

    os.execvp(ssh_args[0], ssh_args)
コード例 #11
0
ファイル: command_ssh.py プロジェクト: biggtfish/armada
def command_ssh(args):
    microservice_name = args.microservice_name or os.environ['MICROSERVICE_NAME']
    if not microservice_name:
        raise ValueError('No microservice name supplied.')

    instances = armada_utils.get_matched_containers(microservice_name)
    instances_count = len(instances)
    if instances_count > 1:
        raise armada_utils.ArmadaCommandException(
            'There are too many ({instances_count}) matching containers. '
            'Provide more specific container_id or microservice name.'.format(**locals()))
    instance = instances[0]

    service_id = instance['ServiceID']
    container_id = service_id.split(':')[0]
    payload = {'container_id': container_id}

    is_local = False
    local_microservices_ids = set(consul_query('agent/services').keys())
    if container_id in local_microservices_ids:
        is_local = True

    if not is_local:
        result = json.loads(armada_api.get('ssh-address', payload, ship_name=instance['Node']))

        if result['status'] != 'ok':
            raise armada_utils.ArmadaCommandException('armada API error: {0}'.format(result['error']))
        ssh_host = result['ssh'].split(':')[0]

        docker_key_file = os.path.join(os.path.dirname(os.path.realpath(__file__)), '..', 'keys/docker.key')

    tty = '-t'
    if args.command:
        command = ' '.join(args.command)
        if command.startswith('bash'):
            tty = ''
    else:
        command = 'bash'

    ssh_command = 'docker exec -i {tty} {container_id} env TERM=$TERM {command}'.format(**locals())
    if is_local:
        print("Connecting to {0}...".format(instance['ServiceName']))
    else:
        ssh_command = 'ssh -t {tty} -p 2201 -i {docker_key_file} -o StrictHostKeyChecking=no docker@{ssh_host} sudo {ssh_command}'.format(**locals())
        print("Connecting to {0} on host {1}...".format(instance['ServiceName'], ssh_host))

    subprocess.call(ssh_command, shell=True)
コード例 #12
0
ファイル: command_stop.py プロジェクト: zerofudge/armada
def command_stop(args):
    microservice_handle = args.microservice_handle or os.environ['MICROSERVICE_NAME']
    if not microservice_handle:
        raise ValueError('No microservice name or container id supplied.')

    instances = armada_utils.get_matched_containers(microservice_handle)
    instances_count = len(instances)

    if instances_count > 1:
        if not args.all:
            raise armada_utils.ArmadaCommandException(
                'There are too many ({instances_count}) matching containers. '
                'Provide more specific container_id or microservice name or use -a/--all flag.'.format(**locals()))
        print('Stopping {instances_count} services {microservice_handle}...'.format(**locals()))
    else:
        microservice_name = instances[0]['ServiceName']
        container_id = instances[0]["ServiceID"].split(':')[0]
        print('Stopping service {microservice_name} ({container_id})...'.format(**locals()))

    were_errors = False
    for i, instance in enumerate(instances):
        try:
            if instances_count > 1:
                print('[{0}/{1}]'.format(i + 1, instances_count))
            if 'kv_index' in instance:
                kv.kv_remove('service/{}/{}'.format(instance['ServiceName'], instance['kv_index']))
                print('Service {} has been removed.'.format(instance['ServiceName']))
            else:
                container_id = instance['ServiceID'].split(':')[0]
                payload = {'container_id': container_id}
                ship_name = instance['Address']
                result = armada_api.post('stop', payload, ship_name=ship_name)

                if result['status'] == 'ok':
                    print('Service {container_id} has been stopped.'.format(**locals()))
                    if instances_count > 1:
                        print()
                else:
                    raise ArmadaCommandException('Stopping error: {0}'.format(result['error']))
        except:
            traceback.print_exc()
            were_errors = True

    if were_errors:
        sys.exit(1)
コード例 #13
0
ファイル: command_restart.py プロジェクト: sceeter89/armada
def command_restart(args):
    if args.verbose:
        global verbose
        verbose = True
    microservice_handle = args.microservice_handle or os.environ["MICROSERVICE_NAME"]
    if not microservice_handle:
        raise ValueError("No microservice name or container id supplied.")

    instances = armada_utils.get_matched_containers(microservice_handle)

    instances_count = len(instances)

    if instances_count > 1:
        if not args.all:
            raise armada_utils.ArmadaCommandException(
                "There are too many ({instances_count}) matching containers. "
                "Provide more specific container_id or microservice name or use -a/--all flag.".format(**locals())
            )
        print("Restarting {instances_count} services {microservice_handle}...".format(**locals()))
    else:
        microservice_name = instances[0]["ServiceName"]
        container_id = instances[0]["ServiceID"].split(":")[0]
        print("Restarting service {microservice_name} ({container_id})...".format(**locals()))

    were_errors = False
    for i, instance in enumerate(instances):
        try:
            if instances_count > 1:
                print("[{0}/{1}]".format(i + 1, instances_count))
            container_id = instance["ServiceID"].split(":")[0]
            is_run_locally = armada_utils.is_local_container(container_id)
            if is_run_locally:
                result = json.loads(armada_api.get("env/{container_id}/ARMADA_RUN_COMMAND".format(**locals())))
                if result["status"] == "ok":
                    stop_command = "armada stop {container_id}".format(**locals())
                    run_command = base64.b64decode(result["value"])
                    assert armada_utils.execute_local_command(stop_command, stream_output=True, retries=3)[0] == 0
                    assert armada_utils.execute_local_command(run_command, stream_output=True, retries=5)[0] == 0
                    if instances_count > 1:
                        print()
                else:
                    raise armada_utils.ArmadaCommandException("ERROR: {0}".format(result["error"]))
            else:
                payload = {"container_id": container_id}

                result = armada_api.post("restart", payload, ship_name=instance["Node"])

                if result["status"] == "ok":
                    new_container_id = result["container_id"]
                    print(
                        "Service has been restarted and is running in container {new_container_id} "
                        "available at addresses:".format(**locals())
                    )
                    for service_address, docker_port in result["endpoints"].iteritems():
                        print("  {0} ({1})".format(service_address, docker_port))
                    if instances_count > 1:
                        print()
                else:
                    raise armada_utils.ArmadaCommandException("ERROR: {0}".format(result["error"]))
        except:
            traceback.print_exc()
            were_errors = True
    if were_errors:
        sys.exit(1)
コード例 #14
0
def command_restart(args):
    microservice_handle = args.microservice_handle or os.environ['MICROSERVICE_NAME']
    if not microservice_handle:
        raise ValueError('No microservice name or container id supplied.')

    instances = armada_utils.get_matched_containers(microservice_handle)

    instances_count = len(instances)

    if instances_count > 1:
        if not args.all:
            raise armada_utils.ArmadaCommandException(
                'There are too many ({instances_count}) matching containers. '
                'Provide more specific container_id or microservice name or use -a/--all flag.'.format(**locals()))
        print('Restarting {instances_count} services {microservice_handle}...'.format(**locals()))
    else:
        microservice_name = instances[0]['ServiceName']
        container_id = instances[0]["ServiceID"].split(':')[0]
        print('Restarting service {microservice_name} ({container_id})...'.format(**locals()))

    were_errors = False
    for i, instance in enumerate(instances):
        try:
            if instances_count > 1:
                print('[{0}/{1}]'.format(i + 1, instances_count))

            if 'kv_index' in instance:
                if not instance['params']:
                    raise armada_utils.ArmadaCommandException(
                        'There is no run command available for service {}.'.format(instance['ServiceName']))
                run_command = instance['params']['run_command']
                with suppress_version_check():
                    assert armada_utils.execute_local_command(run_command, stream_output=True, retries=5)[0] == 0
                    kv.kv_remove('service/{}/{}'.format(instance['ServiceName'], instance['kv_index']))
                if instances_count > 1:
                    print()
                continue

            container_id = instance['ServiceID'].split(':')[0]
            is_run_locally = armada_utils.is_local_container(container_id) and not args.ship

            if is_run_locally:
                result = json.loads(armada_api.get('env/{container_id}/ARMADA_RUN_COMMAND'.format(**locals())))
                if result['status'] == 'ok':
                    stop_command = 'armada stop {container_id}'.format(**locals())
                    run_command = base64.b64decode(result['value'])
                    with suppress_version_check():
                        assert armada_utils.execute_local_command(stop_command, stream_output=True, retries=3)[0] == 0
                        assert armada_utils.execute_local_command(run_command, stream_output=True, retries=5)[0] == 0
                    if instances_count > 1:
                        print()
                else:
                    raise armada_utils.ArmadaCommandException(result['error'])
            else:
                payload = {'container_id': container_id}
                if args.ship:
                    payload['target_ship'] = args.ship
                    payload['force'] = args.force

                print('Checking if there is new image version. May take few minutes if download is needed...')
                ship_name = instance['Address']
                result = armada_api.post('restart', payload, ship_name=ship_name)

                if result['status'] == 'ok':
                    new_container_id = result['container_id']
                    print('Service has been restarted and is running in container {new_container_id} '
                          'available at addresses:'.format(**locals()))
                    for service_address, docker_port in result['endpoints'].iteritems():
                        print('  {0} ({1})'.format(service_address, docker_port))
                    if instances_count > 1:
                        print()
                else:
                    raise armada_utils.ArmadaCommandException(result['error'])
        except armada_utils.ArmadaCommandException as e:
            print("ArmadaCommandException: {0}".format(str(e)))
            were_errors = True
        except:
            traceback.print_exc()
            were_errors = True
    if were_errors:
        sys.exit(1)
コード例 #15
0
ファイル: command_ssh.py プロジェクト: zhengxiongwei/armada
def command_ssh(args):
    microservice_name = args.microservice_name or os.environ[
        'MICROSERVICE_NAME']
    if not microservice_name:
        raise ValueError('No microservice name supplied.')
    armada_utils.notify_about_detected_dev_environment(microservice_name)

    instances = [
        i for i in armada_utils.get_matched_containers(microservice_name)
        if 'Status' not in i
    ]

    if args.local:
        instances = [
            i for i in instances
            if armada_utils.is_local_container(i['ServiceID'])
        ]

    instances_count = len(instances)

    if instances_count < 1:
        raise armada_utils.ArmadaCommandException(
            'There are no running containers with microservice: '
            '{microservice_name}'.format(**locals()))

    if instances_count > 1:
        if args.no_prompt:
            raise armada_utils.ArmadaCommandException(
                'There are too many ({instances_count}) matching containers. '
                'Provide more specific container_id or microservice name.'.
                format(**locals()))
        instance = prompt_select_instance(instances)
    else:
        instance = instances[0]

    if 'Status' in instance:
        raise armada_utils.ArmadaCommandException(
            'Cannot connect to not running service.')

    service_id = instance['ServiceID']
    container_id = service_id.split(':')[0]
    payload = {'container_id': container_id}

    is_local = armada_utils.is_local_container(container_id)

    if args.command:
        command = ' '.join(args.command)
    else:
        command = 'bash'
        args.tty = True
        args.interactive = True

    tty = '-t' if args.tty else ''
    interactive = '-i' if args.interactive else ''
    term = os.environ.get('TERM') or 'dummy'

    command = pipes.quote(command)
    docker_command = 'docker exec {interactive} {tty} {container_id} env TERM={term} ' \
                     'sh -c {command}'.format(**locals())

    if is_local:
        print("Connecting to {0}...".format(instance['ServiceName']))
        ssh_args = shlex.split(docker_command)
    else:
        ssh_host = instance['Address']
        docker_key_file = os.path.join(
            os.path.dirname(os.path.realpath(__file__)), '..',
            'keys/docker.key')
        remote_ssh_chunk = 'ssh -t {tty} -p 2201 -i {docker_key_file} -o StrictHostKeyChecking=no docker@{ssh_host}' \
            .format(**locals())
        ssh_args = shlex.split(remote_ssh_chunk)
        ssh_args.extend(('sudo', docker_command))
        print("Connecting to {0} on host {1}...".format(
            instance['ServiceName'], ssh_host))

    os.execvp(ssh_args[0], ssh_args)