Exemple #1
0
 def __init__(self, dockyard_address, image_name, image_tag, dockyard_alias):
     self._dockyard_dict = {}
     if not dockyard_address:
         self._dockyard_dict = dockyard.get_dockyard_dict(dockyard_alias)
         dockyard_address = self._dockyard_dict['address']
     self.dockyard_alias = dockyard_alias
     super(RemoteArmadaImage, self).__init__(dockyard_address, image_name, image_tag)
Exemple #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
Exemple #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
Exemple #4
0
    def __init__(self, microservice_name_or_image_path, dockyard_alias=None):
        self.dockyard_address, self.microservice_name = ArmadaImage.__split_image_path(microservice_name_or_image_path)
        self.dockyard_alias = dockyard_alias
        self.dockyard_dict = None
        self.dockyard_auth = None
        self.dockyard_url = None
        if self.dockyard_address:
            if dockyard_alias:
                raise ArmadaCommandException('Ambiguous dockyard. Please specify either -d/--dockyard '
                                             'or dockyard_ip[:port]/microservice_name')
            self.image_path = microservice_name_or_image_path
        elif dockyard_alias == 'local':
            self.image_path = microservice_name_or_image_path
        else:
            self.dockyard_dict = dockyard.get_dockyard_dict(self.dockyard_alias)
            self.dockyard_address = self.dockyard_dict['address']
            self.image_path = self.dockyard_address + '/' + self.microservice_name
        if self.dockyard_address:
            if self.dockyard_dict and 'user' in self.dockyard_dict and 'password' in self.dockyard_dict:
                self.dockyard_auth = (self.dockyard_dict['user'], self.dockyard_dict['password'])

            if '://' in self.dockyard_address:
                self.dockyard_url = self.dockyard_address
            else:
                if self.dockyard_auth or is_dockyard_address_accessible("https://" + self.dockyard_address):
                    protocol = 'https'
                else:
                    protocol = 'http'
                self.dockyard_url = protocol + '://' + self.dockyard_address
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
Exemple #6
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
Exemple #7
0
 def dockyard(self):
     if self._dockyard is None:
         dockyard_address = self.dockyard_address
         if not dockyard_address:
             self._dockyard_dict = dockyard.get_dockyard_dict(self.dockyard_alias)
             dockyard_address = self._dockyard_dict['address']
         self._dockyard = dockyard.remote_dockyard_factory(dockyard_address,
                                                           self._dockyard_dict.get('user'),
                                                           self._dockyard_dict.get('password'))
     return self._dockyard
Exemple #8
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
Exemple #9
0
    def __init__(self, dockyard_address, image_name, image_tag, dockyard_alias):
        self.image_name, self.image_tag = image_name, image_tag
        dockyard_dict = {}
        if not dockyard_address:
            dockyard_dict = dockyard.get_dockyard_dict(dockyard_alias)
            dockyard_address = dockyard_dict['address']

        self.image_path = '{}/{}'.format(dockyard_address, self.image_name)
        self.dockyard = dockyard.remote_dockyard_factory(dockyard_address,
                                                         dockyard_dict.get('user'),
                                                         dockyard_dict.get('password'))
Exemple #10
0
    def __init__(self, dockyard_address, image_name, image_tag,
                 dockyard_alias):
        self.image_name, self.image_tag = image_name, image_tag
        dockyard_dict = {}
        if not dockyard_address:
            dockyard_dict = dockyard.get_dockyard_dict(dockyard_alias)
            dockyard_address = dockyard_dict['address']

        self.image_path = '{}/{}'.format(dockyard_address, self.image_name)
        self.dockyard = dockyard.remote_dockyard_factory(
            dockyard_address, dockyard_dict.get('user'),
            dockyard_dict.get('password'))
Exemple #11
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()))
Exemple #12
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()))
Exemple #13
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
Exemple #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()))
Exemple #15
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
Exemple #16
0
    def __init__(self, image_path, dockyard_alias=None):
        self.dockyard = None
        dockyard_address, self.image_name = ArmadaImage.__split_image_path(image_path)
        dockyard_dict = {}

        if dockyard_address:
            if dockyard_alias:
                raise ArmadaCommandException('Ambiguous dockyard. Please specify either -d/--dockyard '
                                             'or dockyard_hostname[:port]/image_name')
        elif dockyard_alias == 'local':
            pass
        else:
            dockyard_dict = dockyard.get_dockyard_dict(dockyard_alias)
            dockyard_address = dockyard_dict['address']
            image_path = dockyard_address + '/' + self.image_name

        self.image_path = image_path
        self.dockyard = dockyard_factory(dockyard_address,
                                         dockyard_dict.get('user'),
                                         dockyard_dict.get('password'))
Exemple #17
0
    def __init__(self, image_path, dockyard_alias=None):
        self.dockyard = None
        dockyard_address, self.image_name = ArmadaImage.__split_image_path(
            image_path)
        dockyard_dict = {}

        if dockyard_address:
            if dockyard_alias:
                raise ArmadaCommandException(
                    'Ambiguous dockyard. Please specify either -d/--dockyard '
                    'or dockyard_hostname[:port]/image_name')
        elif dockyard_alias == 'local':
            pass
        else:
            dockyard_dict = dockyard.get_dockyard_dict(dockyard_alias)
            dockyard_address = dockyard_dict['address']
            image_path = dockyard_address + '/' + self.image_name

        self.image_path = image_path
        self.dockyard = dockyard_factory(dockyard_address,
                                         dockyard_dict.get('user'),
                                         dockyard_dict.get('password'))
Exemple #18
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
Exemple #19
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
Exemple #20
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