Ejemplo n.º 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
Ejemplo n.º 2
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
Ejemplo n.º 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')

    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
Ejemplo n.º 4
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
Ejemplo n.º 5
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
Ejemplo n.º 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
Ejemplo n.º 7
0
def command_run(args):
    try:
        image = ArmadaImageFactory(args.microservice_name, 'local',
                                   os.environ.get('MICROSERVICE_NAME'))
    except InvalidImagePathException:
        raise ArmadaCommandException(
            'ERROR: Please specify microservice_name argument'
            ' or set MICROSERVICE_NAME environment variable')
    ship = args.ship
    is_run_locally = ship is None
    dockyard_alias = args.dockyard or dockyard.get_dockyard_alias(
        image.image_name, is_run_locally)

    vagrant_dev = _is_vagrant_dev(args.hidden_vagrant_dev, dockyard_alias,
                                  image.image_name)

    dockyard_alias, image = _find_dockyard_with_image(
        vagrant_dev, args.hidden_is_restart, dockyard_alias,
        image.image_name_with_tag)

    _print_run_info(image, dockyard_alias, ship, args.rename)

    payload = RunPayload()
    payload.update_image_path(image.image_path_with_tag)
    payload.update_dockyard(dockyard_alias)
    if vagrant_dev:
        payload.update_vagrant(args.dynamic_ports, args.publish,
                               args.use_latest_image_code, image.image_name)
    payload.update_environment(args.e)
    payload.update_ports(args.publish)
    payload.update_volumes(args.volumes)
    payload.update_microservice_vars(args.rename, args.env, args.app_id)
    payload.update_run_command(vagrant_dev, args.env, image.image_name)
    payload.update_resource_limits(args.cpu_shares, args.memory,
                                   args.memory_swap, args.cgroup_parent)
    payload.update_configs(args.configs)

    if is_verbose():
        print('payload: {0}'.format(payload))

    warn_if_hit_crontab_environment_variable_length(payload.get('environment'))

    print(
        'Checking if there is new image version. May take few minutes if download is needed...'
    )
    result = armada_api.post('run', payload.data(), ship_name=ship)
    _handle_result(result, args.hidden_is_restart)
Ejemplo n.º 8
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
Ejemplo n.º 9
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
Ejemplo n.º 10
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