Пример #1
0
def command_create(args):
    base_template = args.base_template
    service_name = args.name or base_template
    destination_dir = os.path.join(os.getcwd(), service_name)
    if os.path.exists(destination_dir):
        raise ArmadaCommandException('Destination dir {destination_dir} already exists.'.format(**locals()))
    command_list_code, command_list_out, command_list_err = execute_local_command('armada list armada -q | head -1')
    if command_list_code != 0:
        raise ArmadaCommandException('Could not get Armada container id:\n{command_list_err}'.format(**locals()))
    path_to_base_template = os.path.join('/opt/armada-docker/microservice_templates', base_template)
    armada_container_id = command_list_out.strip()
    temp_dir = tempfile.mkdtemp()
    try:
        command_cp = 'docker cp {armada_container_id}:{path_to_base_template} {temp_dir}'.format(**locals())
        command_cp_code, command_cp_out, command_cp_err = execute_local_command(command_cp)
        if command_cp_code != 0:
            raise ArmadaCommandException('Could not get microservice template:\n{command_cp_err}'.format(**locals()))
        os.rename(os.path.join(temp_dir, base_template), destination_dir)
        if service_name != base_template:
            upper_template = base_template.upper()
            template_name_variable = '_{upper_template}_'.format(**locals())
            _replace_in_path(destination_dir, template_name_variable, service_name)
        dockyard_address = dockyard.get_dockyard_address()
        template_dockyard_variable = '_DOCKYARD_ADDRESS_'
        _replace_in_path(destination_dir, template_dockyard_variable, dockyard_address)

        print('Service {service_name} has been created in {destination_dir}.'.format(**locals()))
    finally:
        shutil.rmtree(temp_dir)
Пример #2
0
def command_push(args):
    image_name = args.microservice_name
    if not image_name:
        raise ArmadaCommandException('ERROR: Please specify microservice_name argument'
                                     ' or set MICROSERVICE_NAME environment variable')
    dockyard_alias = args.dockyard
    image = ArmadaImage(image_name, dockyard_alias)

    if '/' not in image_name:
        if not ArmadaImage(image.microservice_name, 'local').exists():
            raise Exception('Image {image.microservice_name} does not exist. Typo?'.format(**locals()))
        dockyard_string = image.dockyard_address or ''
        dockyard_string += ' (alias: {dockyard_alias})'.format(**locals()) if dockyard_alias else ''
        print('Pushing microservice {image.microservice_name} to dockyard: {dockyard_string}...'.format(
            **locals()))
        tag_command = 'docker tag -f {image.microservice_name} {image.image_path}'.format(**locals())
        assert execute_local_command(tag_command, stream_output=True, retries=1)[0] == 0
    else:
        # If command was called with [docker_registry_address]/[microservice_name] and no -d/--dockyard, then simply
        # mimic 'docker push' behavior (without tagging).
        print('Pushing image {image}...'.format(**locals()))

    dockyard_dict = dockyard.get_dockyard_dict(dockyard_alias)
    did_print = print_dockyard_unavailability_warning(dockyard_dict.get("address"),
                                dockyard_dict.get("user"),
                                dockyard_dict.get("password"),
                                "ERROR! Cannot push to dockyard!")
    retries = 0 if did_print else 3
    login_to_dockyard(dockyard_alias)
    push_command = 'docker push {image.image_path}'.format(**locals())
    assert execute_local_command(push_command, stream_output=True, retries=retries)[0] == 0
Пример #3
0
def command_push(args):
    if not args.image_path:
        raise ArmadaCommandException('ERROR: Please specify image_path argument'
                                     ' or set MICROSERVICE_NAME environment variable')
    dockyard_alias = args.dockyard
    image = ArmadaImage(args.image_path, dockyard_alias)

    if '/' not in args.image_path:
        if not ArmadaImage(image.image_name, 'local').exists():
            raise Exception('Image {} does not exist. Typo?'.format(image.image_name))
        dockyard_string = image.dockyard.url or ''
        dockyard_string += ' (alias: {})'.format(dockyard_alias) if dockyard_alias else ''
        print('Pushing image {} to dockyard: {}...'.format(image.image_name, dockyard_string))
        tag_command = 'docker tag -f {} {}'.format(image.image_name, image.image_path)
        assert execute_local_command(tag_command, stream_output=True, retries=1)[0] == 0
    else:
        # If command was called with [docker_registry_address]/[image_name] and no -d/--dockyard, then simply
        # mimic 'docker push' behavior (without tagging).
        print('Pushing image {}...'.format(image))

    dockyard_dict = dockyard.get_dockyard_dict(dockyard_alias)
    did_print = False
    d = dockyard_factory(dockyard_dict.get('address'), dockyard_dict.get('user'), dockyard_dict.get('password'))
    if d.is_http():
        did_print = print_http_dockyard_unavailability_warning(
            dockyard_dict['address'],
            dockyard_alias,
            "ERROR! Cannot push to dockyard!",
        )

    retries = 0 if did_print else 3
    login_to_dockyard(dockyard_alias)
    push_command = 'docker push {}'.format(image.image_path)
    assert execute_local_command(push_command, stream_output=True, retries=retries)[0] == 0
Пример #4
0
def command_build(args):
    if args.squash:
        chain_run_commands()
    base_image_name = _get_base_image_name()
    dockyard_alias = args.dockyard or dockyard.get_dockyard_alias(base_image_name, is_run_locally=True)

    try:
        image = ArmadaImageFactory(args.microservice_name, dockyard_alias, os.environ.get('MICROSERVICE_NAME'))
    except InvalidImagePathException:
        raise ValueError('No microservice name supplied.')

    if not os.path.exists('Dockerfile'):
        print('ERROR: Dockerfile not found in current directory', file=sys.stderr)
        return

    base_image = ArmadaImageFactory(base_image_name, dockyard_alias)
    if base_image.is_remote():
        if not base_image.exists():
            if dockyard_alias == DOCKYARD_FALLBACK_ALIAS:
                was_fallback_dockyard = True
            else:
                print('Base image {base_image} not found. Searching in official Armada dockyard...'.format(**locals()))
                dockyard_alias = DOCKYARD_FALLBACK_ALIAS
                base_image = ArmadaImageFactory(base_image_name, dockyard_alias)
                was_fallback_dockyard = False
            if was_fallback_dockyard or not base_image.exists():
                print('Base image {base_image} not found. Aborting.'.format(**locals()))
                sys.exit(1)
        dockyard_dict = dockyard.get_dockyard_dict(dockyard_alias)
        did_print = False
        d = dockyard_factory(dockyard_dict.get('address'), dockyard_dict.get('user'), dockyard_dict.get('password'))
        if d.is_http():
            did_print = print_http_dockyard_unavailability_warning(
                dockyard_dict['address'],
                dockyard_alias,
                "ERROR! Cannot pull from dockyard!",
            )
        retries = 0 if did_print else 3
        base_image_path = base_image.image_path
        if is_verbose():
            print('Fetching base image: "{base_image_path}".\n'.format(**locals()))

        pull_command = 'docker pull {base_image_path}'.format(**locals())

        assert execute_local_command(pull_command, stream_output=True, retries=retries)[0] == 0
        if base_image_path != base_image_name:
            if is_verbose():
                print('Tagging "{base_image_path}" as "{base_image_name}"\n'.format(**locals()))
            tag_command = docker_backend.build_tag_command(base_image_path, base_image_name)
            assert execute_local_command(tag_command, stream_output=True, retries=1)[0] == 0

    build_command = 'docker build -t {} .'.format(image.image_name_with_tag)
    assert execute_local_command(build_command, stream_output=True)[0] == 0

    if args.squash:
        os.rename('Dockerfile.tmp', 'Dockerfile')
        squash_command = 'docker-squash {} -t {}'.format(image.image_name_with_tag,
                                                         image.image_name_with_tag)
        assert execute_local_command(squash_command, stream_output=True)[0] == 0
Пример #5
0
def command_build(args):
    microservice_name = args.microservice_name or os.environ.get(
        'MICROSERVICE_NAME')
    if not microservice_name:
        raise ValueError('No microservice name supplied.')
    if not os.path.exists('Dockerfile'):
        print('ERROR: Dockerfile not found in current directory',
              file=sys.stderr)
        return
    base_image_name = _get_base_image_name()
    dockyard_alias = args.dockyard or dockyard.get_dockyard_alias(
        base_image_name, is_run_locally=True)

    base_image = ArmadaImage(base_image_name, dockyard_alias)

    if base_image.is_remote():
        if not base_image.exists():
            if dockyard_alias == DOCKYARD_FALLBACK_ALIAS:
                was_fallback_dockyard = True
            else:
                print(
                    'Base image {base_image} not found. Searching in official Armada dockyard...'
                    .format(**locals()))
                dockyard_alias = DOCKYARD_FALLBACK_ALIAS
                base_image = ArmadaImage(base_image_name, dockyard_alias)
                was_fallback_dockyard = False
            if was_fallback_dockyard or not base_image.exists():
                print('Base image {base_image} not found. Aborting.'.format(
                    **locals()))
                sys.exit(1)
        dockyard_dict = dockyard.get_dockyard_dict(dockyard_alias)
        did_print = False
        d = dockyard_factory(dockyard_dict.get('address'),
                             dockyard_dict.get('user'),
                             dockyard_dict.get('password'))
        if d.is_http():
            did_print = print_http_dockyard_unavailability_warning(
                dockyard_dict['address'],
                dockyard_alias,
                "ERROR! Cannot pull from dockyard!",
            )
        retries = 0 if did_print else 3
        base_image_path = base_image.image_path
        pull_command = 'docker pull {base_image_path}'.format(**locals())

        assert execute_local_command(pull_command,
                                     stream_output=True,
                                     retries=retries)[0] == 0
        if base_image_path != base_image_name:
            tag_command = 'docker tag -f {base_image_path} {base_image_name}'.format(
                **locals())
            assert execute_local_command(tag_command,
                                         stream_output=True,
                                         retries=1)[0] == 0

    build_command = 'docker build -t {microservice_name} .'.format(**locals())
    assert execute_local_command(build_command, stream_output=True)[0] == 0
Пример #6
0
def command_push(args):
    try:
        source_image = ArmadaImageFactory(args.image_path, 'local',
                                          os.environ.get('MICROSERVICE_NAME'))
    except InvalidImagePathException:
        raise ArmadaCommandException(
            'ERROR: Please specify image_path argument'
            ' or set MICROSERVICE_NAME environment variable')

    notify_about_detected_dev_environment(source_image.image_name)

    dockyard_alias = args.dockyard
    if not source_image.dockyard_address:
        if not source_image.exists():
            raise Exception('Image {} does not exist. Typo?'.format(
                source_image.image_name_with_tag))
        destination_image = ArmadaImageFactory(
            source_image.image_name_with_tag, dockyard_alias)
        dockyard_string = destination_image.dockyard.url or ''
        dockyard_string += ' (alias: {})'.format(
            dockyard_alias) if dockyard_alias else ''
        print('Pushing image {} to dockyard: {}...'.format(
            source_image.image_name_with_tag, dockyard_string))
        tag_command = "docker tag {} {}".format(
            source_image.image_name_with_tag,
            destination_image.image_path_with_tag)
        print(tag_command)
        assert execute_local_command(tag_command,
                                     stream_output=True,
                                     retries=1)[0] == 0
    else:
        # If command was called with [docker_registry_address]/[image_name] and no -d/--dockyard, then simply
        # mimic 'docker push' behavior (without tagging).
        print('Pushing image {}...'.format(source_image.image_path_with_tag))
        destination_image = source_image

    dockyard_dict = dockyard.get_dockyard_dict(dockyard_alias)
    did_print = False
    d = dockyard_factory(dockyard_dict.get('address'),
                         dockyard_dict.get('user'),
                         dockyard_dict.get('password'))
    if d.is_http():
        did_print = print_http_dockyard_unavailability_warning(
            dockyard_dict['address'],
            dockyard_alias,
            "ERROR! Cannot push to dockyard!",
        )

    retries = 0 if did_print else 3
    login_to_dockyard(dockyard_alias)
    push_command = 'docker push {}'.format(
        destination_image.image_path_with_tag)
    assert execute_local_command(push_command,
                                 stream_output=True,
                                 retries=retries)[0] == 0
Пример #7
0
def command_build(args):
    dockerfile_path = args.file
    if not os.path.exists(dockerfile_path):
        raise ArmadaCommandException(
            'ERROR: {} not found.'.format(dockerfile_path))

    source_base_image_paths = _get_base_image_paths(dockerfile_path)
    dockyard_alias = args.dockyard or get_default_alias()

    try:
        image = ArmadaImageFactory(args.microservice_name, dockyard_alias,
                                   os.environ.get('MICROSERVICE_NAME'))
    except InvalidImagePathException:
        raise ArmadaCommandException('No microservice name supplied.')

    notify_about_detected_dev_environment(image.image_name)

    for source_base_image_path in source_base_image_paths:
        source_dockyard_address = split_image_path(source_base_image_path)[0]
        if source_dockyard_address:
            base_image = ArmadaImageFactory(source_base_image_path,
                                            args.dockyard)
        else:
            print_warning(
                'Using image name only as base image ("FROM {}") has been deprecated. '
                'Consider providing full dockyard/docker registry address, '
                'e.g.: "FROM dockyard.armada.sh/microservice".'.format(
                    source_base_image_path))
            base_image = ArmadaImageFactory(source_base_image_path,
                                            dockyard_alias)

        base_image_path = _pull_base_image(base_image, source_base_image_path,
                                           dockyard_alias)

        if base_image_path != source_base_image_path:
            if is_verbose():
                print(
                    'Tagging "{base_image_path}" as "{source_base_image_path}"\n'
                    .format(**locals()))

            tag_command = "docker tag {} {}".format(base_image_path,
                                                    source_base_image_path)
            assert execute_local_command(tag_command,
                                         stream_output=True,
                                         retries=1)[0] == 0

    build_command = _generate_build_command(args, dockerfile_path, image)
    assert execute_local_command(' '.join(build_command),
                                 stream_output=True)[0] == 0
Пример #8
0
def command_build(args):
    microservice_name = args.microservice_name or os.environ.get('MICROSERVICE_NAME')
    if not microservice_name:
        raise ValueError('No microservice name supplied.')
    if not os.path.exists('Dockerfile'):
        print('ERROR: Dockerfile not found in current directory', file=sys.stderr)
        return
    base_image_name = _get_base_image_name()
    dockyard_alias = args.dockyard or dockyard.get_dockyard_alias(base_image_name, is_run_locally=True)

    base_image = ArmadaImage(base_image_name, dockyard_alias)

    if base_image.is_remote():
        if not base_image.exists():
            if dockyard_alias == DOCKYARD_FALLBACK_ALIAS:
                was_fallback_dockyard = True
            else:
                print('Base image {base_image} not found. Searching in official Armada dockyard...'.format(**locals()))
                dockyard_alias = DOCKYARD_FALLBACK_ALIAS
                base_image = ArmadaImage(base_image_name, dockyard_alias)
                was_fallback_dockyard = False
            if was_fallback_dockyard or not base_image.exists():
                print('Base image {base_image} not found. Aborting.'.format(**locals()))
                sys.exit(1)
        dockyard_dict = dockyard.get_dockyard_dict(dockyard_alias)
        did_print = False
        d = dockyard_factory(dockyard_dict.get('address'), dockyard_dict.get('user'), dockyard_dict.get('password'))
        if d.is_http():
            did_print = print_http_dockyard_unavailability_warning(
                dockyard_dict['address'],
                dockyard_alias,
                "ERROR! Cannot pull from dockyard!",
            )
        retries = 0 if did_print else 3
        base_image_path = base_image.image_path
        pull_command = 'docker pull {base_image_path}'.format(**locals())

        assert execute_local_command(pull_command, stream_output=True, retries=retries)[0] == 0
        if base_image_path != base_image_name:
            tag_command = 'docker tag -f {base_image_path} {base_image_name}'.format(**locals())
            assert execute_local_command(tag_command, stream_output=True, retries=1)[0] == 0

    build_command = 'docker build -t {microservice_name} .'.format(**locals())
    assert execute_local_command(build_command, stream_output=True)[0] == 0
Пример #9
0
def command_create(args):
    base_template = _get_template_name(args.base_template)
    service_name = args.name or base_template
    destination_dir = os.path.join(os.getcwd(), service_name)
    if os.path.exists(destination_dir):
        raise ArmadaCommandException(
            'Destination dir {destination_dir} already exists.'.format(
                **locals()))
    command_list_code, command_list_out, command_list_err = execute_local_command(
        'armada list armada -q | head -1')

    if command_list_code != 0:
        raise ArmadaCommandException(
            'Could not get Armada container id:\n{command_list_err}'.format(
                **locals()))
    path_to_base_template = os.path.join('/opt/templates', base_template)
    armada_container_id = command_list_out.strip()
    temp_dir = tempfile.mkdtemp()
    try:
        command_cp = 'docker cp {armada_container_id}:{path_to_base_template} {temp_dir}'.format(
            **locals())
        command_cp_code, command_cp_out, command_cp_err = execute_local_command(
            command_cp)
        if command_cp_code != 0:
            raise ArmadaCommandException(
                'Could not get microservice template:\n{command_cp_err}'.
                format(**locals()))
        shutil.move(os.path.join(temp_dir, base_template), destination_dir)
        if service_name != base_template:
            upper_template = base_template.upper()
            template_name_variable = '_{upper_template}_'.format(**locals())
            _replace_in_path(destination_dir, template_name_variable,
                             service_name)
        dockyard_address = dockyard.get_dockyard_address()
        template_dockyard_variable = '_DOCKYARD_ADDRESS_'
        _replace_in_path(destination_dir, template_dockyard_variable,
                         dockyard_address)

        print(
            'Service {service_name} has been created in {destination_dir} from {args.base_template} template.'
            .format(**locals()))
    finally:
        shutil.rmtree(temp_dir)
Пример #10
0
def login_to_dockyard(dockyard_alias):
    dockyard_dict = dockyard.get_dockyard_dict(dockyard_alias)
    if not dockyard_dict:
        raise ArmadaCommandException("Couldn't read configuration for dockyard alias {0}.".format(dockyard_alias))

    dockyard_user = dockyard_dict.get('user')
    dockyard_password = dockyard_dict.get('password')
    if dockyard_user and dockyard_password:
        dockyard_address = dockyard_dict.get('address')
        current_user_email = '{0}@{1}'.format(pwd.getpwuid(os.getuid()).pw_name, socket.gethostname())
        login_command = 'docker login --username="******" --password="******" --email="{current_user_email}" {dockyard_address}'.format(**locals())
        if execute_local_command(login_command)[0] != 0:
            raise ArmadaCommandException('ERROR: Could not login to dockyard with alias {dockyard_alias}.'.format(**locals()))
Пример #11
0
def command_push(args):
    try:
        source_image = ArmadaImageFactory(args.image_path, "local", os.environ.get("MICROSERVICE_NAME"))
    except InvalidImagePathException:
        raise ArmadaCommandException(
            "ERROR: Please specify image_path argument" " or set MICROSERVICE_NAME environment variable"
        )
    dockyard_alias = args.dockyard
    if not source_image.dockyard_address:
        if not source_image.exists():
            raise Exception("Image {} does not exist. Typo?".format(source_image.image_name_with_tag))
        destination_image = ArmadaImageFactory(source_image.image_name_with_tag, dockyard_alias)
        dockyard_string = destination_image.dockyard.url or ""
        dockyard_string += " (alias: {})".format(dockyard_alias) if dockyard_alias else ""
        print("Pushing image {} to dockyard: {}...".format(source_image.image_name_with_tag, dockyard_string))
        tag_command = docker_backend.build_tag_command(
            source_image.image_name_with_tag, destination_image.image_path_with_tag
        )
        print(tag_command)
        assert execute_local_command(tag_command, stream_output=True, retries=1)[0] == 0
    else:
        # If command was called with [docker_registry_address]/[image_name] and no -d/--dockyard, then simply
        # mimic 'docker push' behavior (without tagging).
        print("Pushing image {}...".format(source_image.image_path_with_tag))
        destination_image = source_image

    dockyard_dict = dockyard.get_dockyard_dict(dockyard_alias)
    did_print = False
    d = dockyard_factory(dockyard_dict.get("address"), dockyard_dict.get("user"), dockyard_dict.get("password"))
    if d.is_http():
        did_print = print_http_dockyard_unavailability_warning(
            dockyard_dict["address"], dockyard_alias, "ERROR! Cannot push to dockyard!"
        )

    retries = 0 if did_print else 3
    login_to_dockyard(dockyard_alias)
    push_command = "docker push {}".format(destination_image.image_path_with_tag)
    assert execute_local_command(push_command, stream_output=True, retries=retries)[0] == 0
Пример #12
0
def _pull_base_image(base_image, source_base_image_path, dockyard_alias):
    if base_image.is_remote():
        did_print = False
        if not base_image.dockyard_address:
            if not base_image.exists():
                if dockyard_alias == DOCKYARD_FALLBACK_ALIAS:
                    was_fallback_dockyard = True
                else:
                    print('Base image {base_image} not found. '
                          'Searching in official Armada dockyard...'.format(
                              **locals()))
                    dockyard_alias = DOCKYARD_FALLBACK_ALIAS
                    base_image = ArmadaImageFactory(source_base_image_path,
                                                    dockyard_alias)
                    was_fallback_dockyard = False
                if was_fallback_dockyard or not base_image.exists():
                    raise ArmadaCommandException(
                        'Base image {base_image} not found. Aborting.'.format(
                            **locals()))
            dockyard_dict = dockyard.get_dockyard_dict(dockyard_alias)

            d = dockyard_factory(dockyard_dict.get('address'),
                                 dockyard_dict.get('user'),
                                 dockyard_dict.get('password'))
            if d.is_http():
                did_print = print_http_dockyard_unavailability_warning(
                    dockyard_dict['address'],
                    dockyard_alias,
                    "ERROR! Cannot pull from dockyard!",
                )
        retries = 0 if did_print else 3

        base_image_path = base_image.image_path_with_tag
        if is_verbose():
            print('Fetching base image: "{base_image_path}".\n'.format(
                **locals()))

        pull_command = 'docker pull {base_image_path}'.format(**locals())

        assert execute_local_command(pull_command,
                                     stream_output=True,
                                     retries=retries)[0] == 0
    else:
        base_image = ArmadaImageFactory(base_image.image_name, 'local')
        if not base_image.exists():
            raise ArmadaCommandException(
                'Base image {base_image} not found. Aborting.'.format(
                    **locals()))
        base_image_path = base_image.image_path_with_tag
    return base_image_path
Пример #13
0
def login_to_dockyard(dockyard_alias):
    dockyard_dict = dockyard.get_dockyard_dict(dockyard_alias)
    if not dockyard_dict:
        raise ArmadaCommandException("Couldn't read configuration for dockyard alias {0}.".format(dockyard_alias))

    dockyard_user = dockyard_dict.get('user')
    dockyard_password = dockyard_dict.get('password')
    if dockyard_user and dockyard_password:
        dockyard_address = dockyard_dict.get('address')
        login_command = ('docker login --username="******" --password="******" '
                         '{dockyard_address}').format(**locals())
        if execute_local_command(login_command)[0] != 0:
            raise ArmadaCommandException(
                'ERROR: Could not login to dockyard with alias {dockyard_alias}.'.format(**locals()))
Пример #14
0
def login_to_dockyard(dockyard_alias):
    dockyard_dict = dockyard.get_dockyard_dict(dockyard_alias)
    if not dockyard_dict:
        raise ArmadaCommandException("Couldn't read configuration for dockyard alias {0}.".format(dockyard_alias))

    dockyard_user = dockyard_dict.get('user')
    dockyard_password = dockyard_dict.get('password')
    if dockyard_user and dockyard_password:
        dockyard_address = dockyard_dict.get('address')
        current_user_email = '{0}@{1}'.format(pwd.getpwuid(os.getuid()).pw_name, socket.gethostname())
        login_command = ('docker login --username="******" --password="******" '
                         '--email="{current_user_email}" {dockyard_address}').format(**locals())
        if execute_local_command(login_command)[0] != 0:
            raise ArmadaCommandException(
                'ERROR: Could not login to dockyard with alias {dockyard_alias}.'.format(**locals()))
Пример #15
0
def command_build(args):
    dockerfile_path = args.file
    if args.squash:
        if dockerfile_path != 'Dockerfile':
            raise ArmadaCommandException(
                'You cannot use --file flag together with -s/--squash.')
        chain_run_commands()

    if not os.path.exists(dockerfile_path):
        print('ERROR: {} not found.'.format(dockerfile_path), file=sys.stderr)
        return

    base_image_name = _get_base_image_name(dockerfile_path)
    dockyard_alias = args.dockyard or dockyard.get_dockyard_alias(
        base_image_name, is_run_locally=True)

    try:
        image = ArmadaImageFactory(args.microservice_name, dockyard_alias,
                                   os.environ.get('MICROSERVICE_NAME'))
    except InvalidImagePathException:
        raise ValueError('No microservice name supplied.')

    base_image = ArmadaImageFactory(base_image_name, dockyard_alias)
    if base_image.is_remote():
        if not base_image.exists():
            if dockyard_alias == DOCKYARD_FALLBACK_ALIAS:
                was_fallback_dockyard = True
            else:
                print(
                    'Base image {base_image} not found. Searching in official Armada dockyard...'
                    .format(**locals()))
                dockyard_alias = DOCKYARD_FALLBACK_ALIAS
                base_image = ArmadaImageFactory(base_image_name,
                                                dockyard_alias)
                was_fallback_dockyard = False
            if was_fallback_dockyard or not base_image.exists():
                print('Base image {base_image} not found. Aborting.'.format(
                    **locals()))
                sys.exit(1)
        dockyard_dict = dockyard.get_dockyard_dict(dockyard_alias)
        did_print = False
        d = dockyard_factory(dockyard_dict.get('address'),
                             dockyard_dict.get('user'),
                             dockyard_dict.get('password'))
        if d.is_http():
            did_print = print_http_dockyard_unavailability_warning(
                dockyard_dict['address'],
                dockyard_alias,
                "ERROR! Cannot pull from dockyard!",
            )
        retries = 0 if did_print else 3
        base_image_path = base_image.image_path_with_tag
        if is_verbose():
            print('Fetching base image: "{base_image_path}".\n'.format(
                **locals()))

        pull_command = 'docker pull {base_image_path}'.format(**locals())

        assert execute_local_command(pull_command,
                                     stream_output=True,
                                     retries=retries)[0] == 0
        if base_image_path != base_image_name:
            if is_verbose():
                print('Tagging "{base_image_path}" as "{base_image_name}"\n'.
                      format(**locals()))
            tag_command = docker_backend.build_tag_command(
                base_image_path, base_image_name)
            assert execute_local_command(tag_command,
                                         stream_output=True,
                                         retries=1)[0] == 0

    build_command = 'docker build -f {} -t {} .'.format(
        dockerfile_path, image.image_name_with_tag)
    assert execute_local_command(build_command, stream_output=True)[0] == 0

    if args.squash:
        os.rename('Dockerfile.tmp', 'Dockerfile')
        squash_command = 'docker-squash {} -t {}'.format(
            image.image_name_with_tag, image.image_name_with_tag)
        assert execute_local_command(squash_command,
                                     stream_output=True)[0] == 0
Пример #16
0
def command_build(args):
    dockerfile_path = args.file
    if not os.path.exists(dockerfile_path):
        raise ArmadaCommandException(
            'ERROR: {} not found.'.format(dockerfile_path))

    base_image_name = _get_base_image_name(dockerfile_path)
    dockyard_alias = args.dockyard or dockyard.get_dockyard_alias(
        base_image_name, is_run_locally=True)

    try:
        image = ArmadaImageFactory(args.microservice_name, dockyard_alias,
                                   os.environ.get('MICROSERVICE_NAME'))
    except InvalidImagePathException:
        raise ArmadaCommandException('No microservice name supplied.')

    notify_about_detected_dev_environment(image.image_name)

    base_image = ArmadaImageFactory(base_image_name, dockyard_alias)
    if base_image.is_remote():
        if not base_image.exists():
            if dockyard_alias == DOCKYARD_FALLBACK_ALIAS:
                was_fallback_dockyard = True
            else:
                print(
                    'Base image {base_image} not found. Searching in official Armada dockyard...'
                    .format(**locals()))
                dockyard_alias = DOCKYARD_FALLBACK_ALIAS
                base_image = ArmadaImageFactory(base_image_name,
                                                dockyard_alias)
                was_fallback_dockyard = False
            if was_fallback_dockyard or not base_image.exists():
                raise ArmadaCommandException(
                    'Base image {base_image} not found. Aborting.'.format(
                        **locals()))
        dockyard_dict = dockyard.get_dockyard_dict(dockyard_alias)
        did_print = False
        d = dockyard_factory(dockyard_dict.get('address'),
                             dockyard_dict.get('user'),
                             dockyard_dict.get('password'))
        if d.is_http():
            did_print = print_http_dockyard_unavailability_warning(
                dockyard_dict['address'],
                dockyard_alias,
                "ERROR! Cannot pull from dockyard!",
            )
        retries = 0 if did_print else 3
        base_image_path = base_image.image_path_with_tag
        if is_verbose():
            print('Fetching base image: "{base_image_path}".\n'.format(
                **locals()))

        pull_command = 'docker pull {base_image_path}'.format(**locals())

        assert execute_local_command(pull_command,
                                     stream_output=True,
                                     retries=retries)[0] == 0
        if base_image_path != base_image_name:
            if is_verbose():
                print('Tagging "{base_image_path}" as "{base_image_name}"\n'.
                      format(**locals()))

            tag_command = "docker tag {} {}".format(base_image_path,
                                                    base_image_name)
            assert execute_local_command(tag_command,
                                         stream_output=True,
                                         retries=1)[0] == 0

    build_command = 'docker build {} -f {} -t {} .'.format(
        '--squash' if args.squash else '', dockerfile_path,
        image.image_name_with_tag)
    assert execute_local_command(build_command, stream_output=True)[0] == 0