def test_generate_updated_tags(self): scratchpad = { 'tags': [{ constants.IMAGE_TAG_KEY: 'tag1', constants.IMAGE_ID_KEY: 'image1' }, { constants.IMAGE_TAG_KEY: 'tag2', constants.IMAGE_ID_KEY: 'image2' }, { constants.IMAGE_TAG_KEY: 'tag-existing', constants.IMAGE_ID_KEY: 'image-existing' }] } new_tags = {'tag3': 'image3', 'tag-existing': 'image-new'} update_tags = tags.generate_updated_tags(scratchpad, new_tags) expected_update_tags = [{ constants.IMAGE_TAG_KEY: 'tag1', constants.IMAGE_ID_KEY: 'image1' }, { constants.IMAGE_TAG_KEY: 'tag2', constants.IMAGE_ID_KEY: 'image2' }, { constants.IMAGE_TAG_KEY: 'tag-existing', constants.IMAGE_ID_KEY: 'image-new' }, { constants.IMAGE_TAG_KEY: 'tag3', constants.IMAGE_ID_KEY: 'image3' }] self.assertEqual(update_tags, expected_update_tags)
def test_generate_updated_tags_empty_newtags(self): scratchpad = {'tags': [{constants.IMAGE_TAG_KEY: 'tag1', constants.IMAGE_ID_KEY: 'image1'}, {constants.IMAGE_TAG_KEY: 'tag2', constants.IMAGE_ID_KEY: 'image2'}, {constants.IMAGE_TAG_KEY: 'tag-existing', constants.IMAGE_ID_KEY: 'image-existing'}]} new_tags = {} update_tags = tags.generate_updated_tags(scratchpad, new_tags) self.assertEqual(update_tags, scratchpad['tags'])
def update_tags(repo_id, new_tags): """ Gets the current scratchpad's tags and updates them with the new_tags :param repo_id: unique ID of a repository :type repo_id: basestring :param new_tags: dictionary of tag:image_id :type new_tags: dict """ repo_manager = factory.repo_manager() scratchpad = repo_manager.get_repo_scratchpad(repo_id) new_tags = tags.generate_updated_tags(scratchpad, new_tags) repo_manager.update_repo_scratchpad(repo_id, {'tags': new_tags})
def finalize(self): """ Update the tags on the repository object. This method is called after process_main has been called. This will be called even if process_main or initialize raises an exceptions. """ super(SaveImages, self).finalize() # Get an updated copy of the repo so that we can update the tags repo = self.get_repo().repo_obj _logger.debug('updating tags for repo {repo_id}'.format(repo_id=repo.repo_id)) if self.parent.tags: new_tags = tags.generate_updated_tags(repo.scratchpad, self.parent.tags) platform_models.Repository.objects(repo_id=repo.repo_id).\ update_one(set__scratchpad__tags=new_tags)
def update_tags(repo_id, new_tags): """ Gets the current scratchpad's tags and updates them with the new_tags :param repo_id: unique ID of a repository :type repo_id: basestring :param new_tags: dictionary of tag:image_id :type new_tags: dict """ repo_obj = model.Repository.objects.get_repo_or_missing_resource(repo_id) new_tags = tags.generate_updated_tags(repo_obj.scratchpad, new_tags) repo_obj.scratchpad['tags'] = new_tags repo_obj.save()
def finalize(self): """ Update the tags on the repository object. This method is called after process_main has been called. This will be called even if process_main or initialize raises an exceptions. """ super(SaveImages, self).finalize() # Get an updated copy of the repo so that we can update the tags repo = self.get_repo().repo_obj _logger.debug( 'updating v1 tags for repo {repo_id}'.format(repo_id=repo.repo_id)) if self.parent.v1_tags: new_tags = tags.generate_updated_tags(repo.scratchpad, self.parent.v1_tags) platform_models.Repository.objects(repo_id=repo.repo_id).\ update_one(set__scratchpad__tags=new_tags)
def test_generate_updated_tags_empty_newtags(self): scratchpad = { 'tags': [{ constants.IMAGE_TAG_KEY: 'tag1', constants.IMAGE_ID_KEY: 'image1' }, { constants.IMAGE_TAG_KEY: 'tag2', constants.IMAGE_ID_KEY: 'image2' }, { constants.IMAGE_TAG_KEY: 'tag-existing', constants.IMAGE_ID_KEY: 'image-existing' }] } new_tags = {} update_tags = tags.generate_updated_tags(scratchpad, new_tags) self.assertEqual(update_tags, scratchpad['tags'])
def test_generate_updated_tags(self): scratchpad = {'tags': [{constants.IMAGE_TAG_KEY: 'tag1', constants.IMAGE_ID_KEY: 'image1'}, {constants.IMAGE_TAG_KEY: 'tag2', constants.IMAGE_ID_KEY: 'image2'}, {constants.IMAGE_TAG_KEY: 'tag-existing', constants.IMAGE_ID_KEY: 'image-existing'}]} new_tags = {'tag3': 'image3', 'tag-existing': 'image-new'} update_tags = tags.generate_updated_tags(scratchpad, new_tags) expected_update_tags = [{constants.IMAGE_TAG_KEY: 'tag1', constants.IMAGE_ID_KEY: 'image1'}, {constants.IMAGE_TAG_KEY: 'tag2', constants.IMAGE_ID_KEY: 'image2'}, {constants.IMAGE_TAG_KEY: 'tag-existing', constants.IMAGE_ID_KEY: 'image-new'}, {constants.IMAGE_TAG_KEY: 'tag3', constants.IMAGE_ID_KEY: 'image3'}] self.assertEqual(update_tags, expected_update_tags)
def run(self, **kwargs): arg_utils.convert_removed_options(kwargs) importer_config = self.parse_user_input(kwargs) name = kwargs.pop(OPT_UPSTREAM_NAME.keyword, None) if name is not None: importer_config[constants.CONFIG_KEY_UPSTREAM_NAME] = name if importer_config: kwargs['importer_config'] = importer_config # Update distributor configuration web_config = {} export_config = {} value = kwargs.pop(OPT_PROTECTED.keyword, None) if value is not None: web_config[constants.CONFIG_KEY_PROTECTED] = value export_config[constants.CONFIG_KEY_PROTECTED] = value value = kwargs.pop(OPT_REDIRECT_URL.keyword, None) if value is not None: web_config[constants.CONFIG_KEY_REDIRECT_URL] = value export_config[constants.CONFIG_KEY_REDIRECT_URL] = value value = kwargs.pop(OPT_REPO_REGISTRY_ID.keyword, None) if value is not None: web_config[constants.CONFIG_KEY_REPO_REGISTRY_ID] = value export_config[constants.CONFIG_KEY_REPO_REGISTRY_ID] = value value = kwargs.pop(OPT_AUTO_PUBLISH.keyword, None) if value is not None: web_config['auto_publish'] = value if web_config or export_config: kwargs['distributor_configs'] = {} if web_config: kwargs['distributor_configs'][constants.CLI_WEB_DISTRIBUTOR_ID] = web_config if export_config: kwargs['distributor_configs'][constants.CLI_EXPORT_DISTRIBUTOR_ID] = export_config # Update Tags repo_id = kwargs.get(OPTION_REPO_ID.keyword) response = self.context.server.repo.repository(repo_id).response_body scratchpad = response.get(u'scratchpad', {}) image_tags = scratchpad.get(u'tags', []) user_tags = kwargs.get(OPTION_TAG.keyword) if user_tags: user_tags = kwargs.pop(OPTION_TAG.keyword) for tag, image_id in user_tags: if len(image_id) < 6: msg = _('The image id, (%s), must be at least 6 characters.') self.prompt.render_failure_message(msg % image_id) return # Ensure the specified images exist in the repo images_requested = set([image_id for tag, image_id in user_tags]) images = ['^%s' % image_id for image_id in images_requested] image_regex = '|'.join(images) search_criteria = { 'type_ids': constants.IMAGE_TYPE_ID, 'match': [['image_id', image_regex]], 'fields': ['image_id'] } response = self.context.server.repo_unit.search(repo_id, **search_criteria).\ response_body if len(response) != len(images): images_found = set([x[u'metadata'][u'image_id'] for x in response]) missing_images = images_requested.difference(images_found) msg = _('Unable to create tag in repository. The following image(s) do not ' 'exist in the repository: %s.') self.prompt.render_failure_message(msg % ', '.join(missing_images)) return # Get the full image id from the returned values and save in tags_to_update dictionary tags_to_update = {} for image in response: found_image_id = image[u'metadata'][u'image_id'] for tag, image_id in user_tags: if found_image_id.startswith(image_id): tags_to_update[tag] = found_image_id # Create a list of tag dictionaries that can be saved on the repo scratchpad # using the original tags and new tags specified by the user image_tags = tags.generate_updated_tags(scratchpad, tags_to_update) scratchpad[u'tags'] = image_tags kwargs[u'scratchpad'] = scratchpad remove_tags = kwargs.get(OPTION_REMOVE_TAG.keyword) if remove_tags: kwargs.pop(OPTION_REMOVE_TAG.keyword) for tag in remove_tags: # For each tag in remove_tags, remove the respective tag dictionary # for matching tag. for image_tag in image_tags[:]: if tag == image_tag[constants.IMAGE_TAG_KEY]: image_tags.remove(image_tag) scratchpad[u'tags'] = image_tags kwargs[u'scratchpad'] = scratchpad super(UpdateDockerRepositoryCommand, self).run(**kwargs)
def run(self, **kwargs): arg_utils.convert_removed_options(kwargs) importer_config = self.parse_user_input(kwargs) name = kwargs.pop(OPT_UPSTREAM_NAME.keyword, None) if name is not None: importer_config[constants.CONFIG_KEY_UPSTREAM_NAME] = name if importer_config: kwargs['importer_config'] = importer_config # Update distributor configuration web_config = {} export_config = {} value = kwargs.pop(OPT_PROTECTED.keyword, None) if value is not None: web_config[constants.CONFIG_KEY_PROTECTED] = value export_config[constants.CONFIG_KEY_PROTECTED] = value value = kwargs.pop(OPT_REDIRECT_URL.keyword, None) if value is not None: web_config[constants.CONFIG_KEY_REDIRECT_URL] = value export_config[constants.CONFIG_KEY_REDIRECT_URL] = value value = kwargs.pop(OPT_REPO_REGISTRY_ID.keyword, None) if value is not None: web_config[constants.CONFIG_KEY_REPO_REGISTRY_ID] = value export_config[constants.CONFIG_KEY_REPO_REGISTRY_ID] = value value = kwargs.pop(OPT_AUTO_PUBLISH.keyword, None) if value is not None: web_config['auto_publish'] = value if web_config or export_config: kwargs['distributor_configs'] = {} if web_config: kwargs['distributor_configs'][ constants.CLI_WEB_DISTRIBUTOR_ID] = web_config if export_config: kwargs['distributor_configs'][ constants.CLI_EXPORT_DISTRIBUTOR_ID] = export_config # Update Tags repo_id = kwargs.get(OPTION_REPO_ID.keyword) response = self.context.server.repo.repository(repo_id).response_body scratchpad = response.get(u'scratchpad', {}) image_tags = scratchpad.get(u'tags', []) user_tags = kwargs.get(OPTION_TAG.keyword) if user_tags: user_tags = kwargs.pop(OPTION_TAG.keyword) for tag, image_id in user_tags: if len(image_id) < 6: msg = _( 'The image id, (%s), must be at least 6 characters.') self.prompt.render_failure_message(msg % image_id) return # Ensure the specified images exist in the repo images_requested = set([image_id for tag, image_id in user_tags]) images = ['^%s' % image_id for image_id in images_requested] image_regex = '|'.join(images) search_criteria = { 'type_ids': constants.IMAGE_TYPE_ID, 'match': [['image_id', image_regex]], 'fields': ['image_id'] } response = self.context.server.repo_unit.search(repo_id, **search_criteria).\ response_body if len(response) != len(images): images_found = set( [x[u'metadata'][u'image_id'] for x in response]) missing_images = images_requested.difference(images_found) msg = _( 'Unable to create tag in repository. The following image(s) do not ' 'exist in the repository: %s.') self.prompt.render_failure_message(msg % ', '.join(missing_images)) return # Get the full image id from the returned values and save in tags_to_update dictionary tags_to_update = {} for image in response: found_image_id = image[u'metadata'][u'image_id'] for tag, image_id in user_tags: if found_image_id.startswith(image_id): tags_to_update[tag] = found_image_id # Create a list of tag dictionaries that can be saved on the repo scratchpad # using the original tags and new tags specified by the user image_tags = tags.generate_updated_tags(scratchpad, tags_to_update) scratchpad[u'tags'] = image_tags kwargs[u'scratchpad'] = scratchpad remove_tags = kwargs.get(OPTION_REMOVE_TAG.keyword) if remove_tags: kwargs.pop(OPTION_REMOVE_TAG.keyword) for tag in remove_tags: # For each tag in remove_tags, remove the respective tag dictionary # for matching tag. for image_tag in image_tags[:]: if tag == image_tag[constants.IMAGE_TAG_KEY]: image_tags.remove(image_tag) scratchpad[u'tags'] = image_tags kwargs[u'scratchpad'] = scratchpad super(UpdateDockerRepositoryCommand, self).run(**kwargs)