Beispiel #1
0
def _find_dockyard_with_image(vagrant_dev, is_restart, dockyard_alias,
                              microservice_name):
    image = ArmadaImageFactory(microservice_name, dockyard_alias)

    if vagrant_dev and is_restart:
        local_image = ArmadaImageFactory(image.image_name_with_tag, 'local')
        image = select_latest_image(image, local_image)
        if image == local_image:
            dockyard_alias = 'local'

    if vagrant_dev and not image.exists():
        print(
            'Image {image} not found. Searching in default dockyard...'.format(
                **locals()))
        dockyard_alias = get_default()
        image = ArmadaImageFactory(image.image_name_with_tag, dockyard_alias)

    if not image.exists():
        if dockyard_alias == DOCKYARD_FALLBACK_ALIAS:
            was_fallback_dockyard = True
        else:
            print(
                'Image {} not found. Searching in official Armada dockyard...'.
                format(image.image_name_with_tag))
            dockyard_alias = DOCKYARD_FALLBACK_ALIAS
            image = ArmadaImageFactory(image.image_name_with_tag,
                                       dockyard_alias)
            was_fallback_dockyard = False
        if was_fallback_dockyard or not image.exists():
            raise ArmadaCommandException(
                'Image {} not found. Aborting.'.format(
                    image.image_path_with_tag))

    return dockyard_alias, image
Beispiel #2
0
def _find_dockyard_with_image(vagrant_dev, is_restart, dockyard_alias, microservice_name):
    image = ArmadaImageFactory(microservice_name, dockyard_alias)

    if vagrant_dev and is_restart:
        local_image = ArmadaImageFactory(image.image_name_with_tag, 'local')
        image = select_latest_image(image, local_image)
        if image == local_image:
            dockyard_alias = 'local'

    if vagrant_dev and not image.exists():
        print('Image {image} not found. Searching in default dockyard...'.format(**locals()))
        dockyard_alias = get_default()
        image = ArmadaImageFactory(image.image_name_with_tag, dockyard_alias)

    if not image.exists():
        if dockyard_alias == DOCKYARD_FALLBACK_ALIAS:
            was_fallback_dockyard = True
        else:
            print('Image {} not found. Searching in official Armada dockyard...'.format(image.image_name_with_tag))
            dockyard_alias = DOCKYARD_FALLBACK_ALIAS
            image = ArmadaImageFactory(image.image_name_with_tag, dockyard_alias)
            was_fallback_dockyard = False
        if was_fallback_dockyard or not image.exists():
            raise ArmadaCommandException('Image {} not found. Aborting.'.format(image.image_path_with_tag))

    return dockyard_alias, image
Beispiel #3
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
Beispiel #4
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
Beispiel #5
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
Beispiel #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"
        )
    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
Beispiel #7
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
Beispiel #8
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