Example #1
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
Example #2
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
Example #3
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
Example #4
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