示例#1
0
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)
示例#2
0
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)
示例#3
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)