def test_resolve_dependencies_by_unit(self): # Setup report = 'dep report' mock_plugins.MOCK_IMPORTER.resolve_dependencies.return_value = report unit_id_1 = manager_factory.content_manager().add_content_unit( 'type-1', None, {'key-1': 'v1'}) unit_id_2 = manager_factory.content_manager().add_content_unit( 'type-1', None, {'key-1': 'v2'}) association_manager = manager_factory.repo_unit_association_manager() association_manager.associate_unit_by_id(self.repo_id, 'type-1', unit_id_1) association_manager.associate_unit_by_id(self.repo_id, 'type-1', unit_id_2) # Test result = self.manager.resolve_dependencies_by_units( self.repo_id, [], {}) # Verify self.assertEqual(result, report) self.assertEqual( 1, mock_plugins.MOCK_IMPORTER.resolve_dependencies.call_count) args = mock_plugins.MOCK_IMPORTER.resolve_dependencies.call_args[0] self.assertEqual(args[0].id, self.repo_id) self.assertEqual(len(args[1]), 0) self.assertTrue(isinstance(args[2], DependencyResolutionConduit)) self.assertTrue(isinstance(args[3], PluginCallConfiguration))
def test_resolve_dependencies_by_unit(self): # Setup report = 'dep report' mock_plugins.MOCK_IMPORTER.resolve_dependencies.return_value = report unit_id_1 = manager_factory.content_manager().add_content_unit('type-1', None, {'key-1' : 'v1'}) unit_id_2 = manager_factory.content_manager().add_content_unit('type-1', None, {'key-1' : 'v2'}) association_manager = manager_factory.repo_unit_association_manager() association_manager.associate_unit_by_id(self.repo_id, 'type-1', unit_id_1, 'user', 'admin') association_manager.associate_unit_by_id(self.repo_id, 'type-1', unit_id_2, 'user', 'admin') # Test result = self.manager.resolve_dependencies_by_units(self.repo_id, [], {}) # Verify self.assertEqual(result, report) self.assertEqual(1, mock_plugins.MOCK_IMPORTER.resolve_dependencies.call_count) args = mock_plugins.MOCK_IMPORTER.resolve_dependencies.call_args[0] self.assertEqual(args[0].id, self.repo_id) self.assertEqual(len(args[1]), 0) self.assertTrue(isinstance(args[2], DependencyResolutionConduit)) self.assertTrue(isinstance(args[3], PluginCallConfiguration))
def test_resolve_dependencies_by_criteria(self): # Setup report = 'dep report' mock_plugins.MOCK_IMPORTER.resolve_dependencies.return_value = report unit_id_1 = manager_factory.content_manager().add_content_unit( 'type-1', None, {'key-1': 'unit-id-1'}) unit_id_2 = manager_factory.content_manager().add_content_unit( 'type-1', None, {'key-1': 'dep-1'}) association_manager = manager_factory.repo_unit_association_manager() association_manager.associate_unit_by_id(self.repo_id, 'type-1', unit_id_1) association_manager.associate_unit_by_id(self.repo_id, 'type-1', unit_id_2) criteria = UnitAssociationCriteria(type_ids=['type-1'], unit_filters={'key-1': 'unit-id-1'}) # Test result = self.manager.resolve_dependencies_by_criteria( self.repo_id, criteria, {}) # Verify self.assertEqual(report, result) self.assertEqual( 1, mock_plugins.MOCK_IMPORTER.resolve_dependencies.call_count) args = mock_plugins.MOCK_IMPORTER.resolve_dependencies.call_args[0] self.assertEqual(1, len(args[1]))
def test_resolve_dependencies_by_criteria(self): # Setup report = 'dep report' mock_plugins.MOCK_IMPORTER.resolve_dependencies.return_value = report unit_id_1 = manager_factory.content_manager().add_content_unit('type-1', None, {'key-1': 'unit-id-1'}) unit_id_2 = manager_factory.content_manager().add_content_unit('type-1', None, {'key-1': 'dep-1'}) association_manager = manager_factory.repo_unit_association_manager() association_manager.associate_unit_by_id(self.repo_id, 'type-1', unit_id_1) association_manager.associate_unit_by_id(self.repo_id, 'type-1', unit_id_2) criteria = UnitAssociationCriteria(type_ids=['type-1'], unit_filters={'key-1': 'unit-id-1'}) # Test result = self.manager.resolve_dependencies_by_criteria(self.repo_id, criteria, {}) # Verify self.assertEqual(report, result) self.assertEqual(1, mock_plugins.MOCK_IMPORTER.resolve_dependencies.call_count) args = mock_plugins.MOCK_IMPORTER.resolve_dependencies.call_args[0] self.assertEqual(1, len(args[1]))
def _add_unit(self, unit, pulp_unit): """ Add a unit. If it already exists, update it. This deals with a race condition where a unit might try to be updated, but does not exist. Before this method can complete, another workflow might add that same unit, causing the DuplicateKeyError below. This can happen if two syncs are running concurrently of repositories that have overlapping content. :param unit: the unit to be updated :type unit: pulp.plugins.model.Unit :param pulp_unit: the unit to be updated, as a dict :type pulp_unit: dict :return: id of the updated unit :rtype: basestring """ content_manager = manager_factory.content_manager() try: unit_id = content_manager.add_content_unit(unit.type_id, None, pulp_unit) self._added_count += 1 return unit_id except DuplicateKeyError: logger.debug( _('cannot add unit; already exists. updating instead.')) return self._update_unit(unit, pulp_unit)
def test_syntactic_sugar_methods(self): """ Tests the syntactic sugar methods for retrieving specific managers. """ # Setup factory.initialize() # Test self.assertTrue(isinstance(factory.authentication_manager(), AuthenticationManager)) self.assertTrue(isinstance(factory.cert_generation_manager(), CertGenerationManager)) self.assertTrue(isinstance(factory.certificate_manager(), CertificateManager)) self.assertTrue(isinstance(factory.password_manager(), PasswordManager)) self.assertTrue(isinstance(factory.permission_manager(), PermissionManager)) self.assertTrue(isinstance(factory.permission_query_manager(), PermissionQueryManager)) self.assertTrue(isinstance(factory.role_manager(), RoleManager)) self.assertTrue(isinstance(factory.role_query_manager(), RoleQueryManager)) self.assertTrue(isinstance(factory.user_manager(), UserManager)) self.assertTrue(isinstance(factory.user_query_manager(), UserQueryManager)) self.assertTrue(isinstance(factory.repo_manager(), RepoManager)) self.assertTrue(isinstance(factory.repo_unit_association_manager(), RepoUnitAssociationManager)) self.assertTrue(isinstance(factory.repo_publish_manager(), RepoPublishManager)) self.assertTrue(isinstance(factory.repo_query_manager(), RepoQueryManager)) self.assertTrue(isinstance(factory.repo_sync_manager(), RepoSyncManager)) self.assertTrue(isinstance(factory.content_manager(), ContentManager)) self.assertTrue(isinstance(factory.content_query_manager(), ContentQueryManager)) self.assertTrue(isinstance(factory.content_upload_manager(), ContentUploadManager)) self.assertTrue(isinstance(factory.consumer_manager(), ConsumerManager)) self.assertTrue(isinstance(factory.topic_publish_manager(), TopicPublishManager))
def _update_unit(self, unit, pulp_unit): """ Update a unit. If it is not found, add it. :param unit: the unit to be updated :type unit: pulp.plugins.model.Unit :param pulp_unit: the unit to be updated, as a dict :type pulp_unit: dict :return: id of the updated unit :rtype: basestring """ content_query_manager = manager_factory.content_query_manager() content_manager = manager_factory.content_manager() try: existing_unit = content_query_manager.get_content_unit_by_keys_dict( unit.type_id, unit.unit_key) unit_id = existing_unit['_id'] content_manager.update_content_unit(unit.type_id, unit_id, pulp_unit) self._updated_count += 1 return unit_id except MissingResource: logger.debug( _('cannot update unit; does not exist. adding instead.')) return self._add_unit(unit, pulp_unit)
def add_unit(id, repo_id, type_id): metadata = {'id' : id, 'repo_id' : repo_id,} unit_id = factory.content_manager().add_content_unit( type_id, None, metadata) return unit_id
def link_unit(self, from_unit, to_unit, bidirectional=False): """ Creates a reference between two content units. The semantics of what this relationship means depends on the types of content units being used; this call simply ensures that Pulp will save and make available the indication that a reference exists from one unit to another. By default, the reference will only exist on the from_unit side. If the bidirectional flag is set to true, a second reference will be created on the to_unit to refer back to the from_unit. Units passed to this call must have their id fields set by the Pulp server. @param from_unit: owner of the reference @type from_unit: L{Unit} @param to_unit: will be referenced by the from_unit @type to_unit: L{Unit} """ content_manager = manager_factory.content_manager() try: content_manager.link_referenced_content_units(from_unit.type_id, from_unit.id, to_unit.type_id, [to_unit.id]) if bidirectional: content_manager.link_referenced_content_units(to_unit.type_id, to_unit.id, from_unit.type_id, [from_unit.id]) except Exception, e: _logger.exception(_('Child link from parent [%(parent)s] to child [%(child)s] failed' % {'parent': str(from_unit), 'child': str(to_unit)})) raise ImporterConduitException(e), None, sys.exc_info()[2]
def _add_unit(self, unit, pulp_unit): """ Add a unit. If it already exists, update it. This deals with a race condition where a unit might try to be updated, but does not exist. Before this method can complete, another workflow might add that same unit, causing the DuplicateKeyError below. This can happen if two syncs are running concurrently of repositories that have overlapping content. :param unit: the unit to be updated :type unit: pulp.plugins.model.Unit :param pulp_unit: the unit to be updated, as a dict :type pulp_unit: dict :return: id of the updated unit :rtype: basestring """ content_manager = manager_factory.content_manager() try: unit_id = content_manager.add_content_unit(unit.type_id, None, pulp_unit) self._added_count += 1 return unit_id except DuplicateKeyError: _logger.debug(_('cannot add unit; already exists. updating instead.')) return self._update_unit(unit, pulp_unit)
def test_syntactic_sugar_methods(self): """ Tests the syntactic sugar methods for retrieving specific managers. """ # Setup factory.initialize() # Test self.assertTrue(isinstance(factory.authentication_manager(), AuthenticationManager)) self.assertTrue(isinstance(factory.cert_generation_manager(), CertGenerationManager)) self.assertTrue(isinstance(factory.certificate_manager(), CertificateManager)) self.assertTrue(isinstance(factory.password_manager(), PasswordManager)) self.assertTrue(isinstance(factory.permission_manager(), PermissionManager)) self.assertTrue(isinstance(factory.permission_query_manager(), PermissionQueryManager)) self.assertTrue(isinstance(factory.role_manager(), RoleManager)) self.assertTrue(isinstance(factory.role_query_manager(), RoleQueryManager)) self.assertTrue(isinstance(factory.user_manager(), UserManager)) self.assertTrue(isinstance(factory.user_query_manager(), UserQueryManager)) self.assertTrue(isinstance(factory.repo_manager(), RepoManager)) self.assertTrue(isinstance(factory.repo_unit_association_manager(), RepoUnitAssociationManager)) self.assertTrue(isinstance(factory.repo_publish_manager(), RepoPublishManager)) self.assertTrue(isinstance(factory.repo_query_manager(), RepoQueryManager)) self.assertTrue(isinstance(factory.repo_sync_manager(), RepoSyncManager)) self.assertTrue(isinstance(factory.content_manager(), ContentManager)) self.assertTrue(isinstance(factory.content_query_manager(), ContentQueryManager)) self.assertTrue(isinstance(factory.content_upload_manager(), ContentUploadManager)) self.assertTrue(isinstance(factory.consumer_manager(), ConsumerManager)) self.assertTrue(isinstance(factory.topic_publish_manager(), TopicPublishManager))
def add_units(self, begin, end): units = [] storage_dir = os.path.join( pulp_conf.config.get('server', 'storage_dir'), 'content') if not os.path.exists(storage_dir): os.makedirs(storage_dir) for n in range(begin, end): unit_id = self.UNIT_ID % n unit = dict(self.UNIT_KEY) unit.update(self.UNIT_METADATA) unit['N'] = n # add unit file storage_path = os.path.join(storage_dir, '.'.join( (unit_id, self.UNIT_TYPE_ID))) if n % 2 == 0: # even numbered has file associated unit['_storage_path'] = storage_path if n == 0: # 1st one is a directory of files os.makedirs(storage_path) dist_path = os.path.join(os.path.dirname(__file__), 'data/distribution.tar') tb = tarfile.open(dist_path) tb.extractall(path=storage_path) tb.close() else: with open(storage_path, 'w+') as fp: fp.write(unit_id) # add unit manager = managers.content_manager() manager.add_content_unit(self.UNIT_TYPE_ID, unit_id, unit) manager = managers.repo_unit_association_manager() # associate unit manager.associate_unit_by_id(self.REPO_ID, self.UNIT_TYPE_ID, unit_id) units.append(unit) return units
def link_unit(self, from_unit, to_unit, bidirectional=False): """ Creates a reference between two content units. The semantics of what this relationship means depends on the types of content units being used; this call simply ensures that Pulp will save and make available the indication that a reference exists from one unit to another. By default, the reference will only exist on the from_unit side. If the bidirectional flag is set to true, a second reference will be created on the to_unit to refer back to the from_unit. Units passed to this call must have their id fields set by the Pulp server. @param from_unit: owner of the reference @type from_unit: L{Unit} @param to_unit: will be referenced by the from_unit @type to_unit: L{Unit} """ content_manager = manager_factory.content_manager() try: content_manager.link_referenced_content_units(from_unit.type_id, from_unit.id, to_unit.type_id, [to_unit.id]) if bidirectional: content_manager.link_referenced_content_units(to_unit.type_id, to_unit.id, from_unit.type_id, [from_unit.id]) except Exception, e: _LOG.exception(_('Child link from parent [%(parent)s] to child [%(child)s] failed' % {'parent': str(from_unit), 'child': str(to_unit)})) raise ImporterConduitException(e), None, sys.exc_info()[2]
def add_unit(id, repo_id, type_id): metadata = { 'id': id, 'repo_id': repo_id, } unit_id = factory.content_manager().add_content_unit( type_id, None, metadata) return unit_id
def populate_units(self, key, typedef): for i in range(1, 10): unit_id = 'unit-%s' % self.UNIT_ID md = {key: str(i)} manager = factory.content_manager() manager.add_content_unit(typedef.id, unit_id, md) manager = factory.repo_unit_association_manager() manager.associate_unit_by_id(self.REPO_ID, typedef.id, unit_id, OWNER_TYPE_IMPORTER, 'test-importer') self.UNIT_ID += 1
def populate_units(self, key, typedef, additional_key=None): for i in range(1, 10): unit_id = 'unit-%s' % self.UNIT_ID md = {key: str(i)} if additional_key: md[additional_key] = str(i) manager = factory.content_manager() manager.add_content_unit(typedef.id, unit_id, md) manager = factory.repo_unit_association_manager() manager.associate_unit_by_id(self.REPO_ID, typedef.id, unit_id) self.UNIT_ID += 1
def gen_content_unit(content_type_id, content_root, name=None): name = name or "".join(random.sample(string.ascii_letters, 5)) path = os.path.join(content_root, name) file = open(path, mode="w") file.write("") file.close() unit = {"name": name, "_content_type_id": content_type_id, "_storage_path": path} content_manager = manager_factory.content_manager() unit_id = content_manager.add_content_unit(content_type_id, None, unit) unit["_id"] = unit_id return unit
def gen_content_unit_with_directory(content_type_id, content_root, name=None): name = name or ''.join(random.sample(string.ascii_letters, 5)) path = os.path.join(content_root, name) os.mkdir(path) unit = {'name': name, '_content_type_id': content_type_id, '_storage_path': path} content_manager = manager_factory.content_manager() unit_id = content_manager.add_content_unit(content_type_id, None, unit) unit['_id'] = unit_id return unit
def gen_content_unit_with_directory(content_type_id, content_root, name=None): name = name or ''.join(random.sample(string.ascii_letters, 5)) path = os.path.join(content_root, name) os.mkdir(path) unit = {'name': name, '_content_type_id': content_type_id, '_storage_path': path} content_manager = manager_factory.content_manager() unit_id = content_manager.add_content_unit(content_type_id, None, unit) unit['_id'] = unit_id return unit
def gen_content_unit(content_type_id, content_root, name=None): name = name or ''.join(random.sample(string.ascii_letters, 5)) path = os.path.join(content_root, name) file = open(path, mode='w') file.write('') file.close() unit = {'name': name, '_content_type_id': content_type_id, '_storage_path': path} content_manager = manager_factory.content_manager() unit_id = content_manager.add_content_unit(content_type_id, None, unit) unit['_id'] = unit_id return unit
def gen_content_unit(content_type_id, content_root, name=None): name = name or ''.join(random.sample(string.ascii_letters, 5)) path = os.path.join(content_root, name) file = open(path, mode='w') file.write('') file.close() unit = {'name': name, '_content_type_id': content_type_id, '_storage_path': path} content_manager = manager_factory.content_manager() unit_id = content_manager.add_content_unit(content_type_id, None, unit) unit['_id'] = unit_id return unit
def populate_units(self, key, typedef): for i in range(1,10): unit_id = 'unit-%s' % self.UNIT_ID md = {key:str(i)} manager = factory.content_manager() manager.add_content_unit(typedef.id, unit_id, md) manager = factory.repo_unit_association_manager() manager.associate_unit_by_id( self.REPO_ID, typedef.id, unit_id, OWNER_TYPE_IMPORTER, 'test-importer') self.UNIT_ID += 1
def populate_units(self, key, typedef, additional_key=None): for i in range(1, 10): unit_id = 'unit-%s' % self.UNIT_ID md = {key: str(i)} if additional_key: md[additional_key] = str(i) manager = factory.content_manager() manager.add_content_unit(typedef.id, unit_id, md) manager = factory.repo_unit_association_manager() manager.associate_unit_by_id( self.REPO_ID, typedef.id, unit_id) self.UNIT_ID += 1
def test_syntactic_sugar_methods(self): """ Tests the syntactic sugar methods for retrieving specific managers. """ # Test self.assertTrue(isinstance(factory.repo_manager(), RepoManager)) self.assertTrue(isinstance(factory.repo_unit_association_manager(), RepoUnitAssociationManager)) self.assertTrue(isinstance(factory.repo_publish_manager(), RepoPublishManager)) self.assertTrue(isinstance(factory.repo_query_manager(), RepoQueryManager)) self.assertTrue(isinstance(factory.repo_sync_manager(), RepoSyncManager)) self.assertTrue(isinstance(factory.content_manager(), ContentManager)) self.assertTrue(isinstance(factory.content_query_manager(), ContentQueryManager)) self.assertTrue(isinstance(factory.content_upload_manager(), ContentUploadManager)) self.assertTrue(isinstance(factory.consumer_manager(), ConsumerManager))
def populate(self): # make content/ dir. os.makedirs(os.path.join(self.parentfs, 'content')) pulp_conf.set('server', 'storage_dir', self.parentfs) # create repo manager = managers.repo_manager() manager.create_repo(self.REPO_ID) # add units units = [] for n in range(0, self.NUM_UNITS): unit_id = self.UNIT_ID % n unit = dict(self.UNIT_METADATA) unit['N'] = n # add unit file storage_dir = pulp_conf.get('server', 'storage_dir') storage_path = \ os.path.join(storage_dir, 'content', '.'.join((unit_id, self.UNIT_TYPE_ID))) unit['_storage_path'] = storage_path fp = open(storage_path, 'w+') fp.write(unit_id) fp.close() # add unit manager = managers.content_manager() manager.add_content_unit( self.UNIT_TYPE_ID, unit_id, unit) manager = managers.repo_unit_association_manager() # associate unit manager.associate_unit_by_id( self.REPO_ID, self.UNIT_TYPE_ID, unit_id, RepoContentUnit.OWNER_TYPE_IMPORTER, constants.HTTP_IMPORTER) units.append(unit) # CA self.units = units path = os.path.join(self.parentfs, 'ca.crt') fp = open(path, 'w+') fp.write(self.CA_CERT) fp.close() # client cert path = os.path.join(self.parentfs, 'local.crt') fp = open(path, 'w+') fp.write(self.CLIENT_CERT) fp.close()
def save_unit(self, unit): """ Performs two distinct steps on the Pulp server: - Creates or updates Pulp's knowledge of the content unit. - Associates the unit to the repository being synchronized. This call is idempotent. If the unit already exists or the association already exists, this call will have no effect. A reference to the provided unit is returned from this call. This call will populate the unit's id field with the UUID for the unit. @param unit: unit object returned from the init_unit call @type unit: L{Unit} @return: object reference to the provided unit, its state updated from the call @rtype: L{Unit} """ try: content_query_manager = manager_factory.content_query_manager() content_manager = manager_factory.content_manager() association_manager = manager_factory.repo_unit_association_manager( ) # Save or update the unit pulp_unit = common_utils.to_pulp_unit(unit) try: existing_unit = content_query_manager.get_content_unit_by_keys_dict( unit.type_id, unit.unit_key) unit.id = existing_unit['_id'] content_manager.update_content_unit(unit.type_id, unit.id, pulp_unit) self._updated_count += 1 except MissingResource: unit.id = content_manager.add_content_unit( unit.type_id, None, pulp_unit) self._added_count += 1 # Associate it with the repo association_manager.associate_unit_by_id( self.repo_id, unit.type_id, unit.id, self.association_owner_type, self.association_owner_id) return unit except Exception, e: _LOG.exception( _('Content unit association failed [%s]' % str(unit))) raise ImporterConduitException(e), None, sys.exc_info()[2]
def add_units(num_units=10): units = [] n = 0 for type_id in ALL_TYPES: for x in range(0, num_units): unit_id = create_unit_id(type_id, n) unit = dict(UNIT_METADATA) unit['N'] = n unit['_storage_path'] = create_storage_path(unit_id) manager = managers.content_manager() manager.add_content_unit(type_id, unit_id, unit) manager = managers.repo_unit_association_manager() # associate unit manager.associate_unit_by_id(REPO_ID, type_id, unit_id) units.append(unit) n += 1 return units
def add_units(num_units=10): units = [] n = 0 for type_id in ALL_TYPES: for x in range(0, num_units): unit_id = create_unit_id(type_id, n) unit = dict(UNIT_METADATA) unit['N'] = n unit['_storage_path'] = create_storage_path(unit_id) manager = managers.content_manager() manager.add_content_unit(type_id, unit_id, unit) manager = managers.repo_unit_association_manager() # associate unit with mock.patch('pulp.server.managers.repo.unit_association.repo_controller'): manager.associate_unit_by_id(REPO_ID, type_id, unit_id) units.append(unit) n += 1 return units
def PUT(self, type_id, unit_id): """ Set the pulp_user_metadata field on the existing unit. :param type_id: The Unit's type id. :type type_id: basestring :param unit_id: The id of the unit that you wish to set the pulp_user_metadata field on :type unit_id: basestring """ cqm = factory.content_query_manager() try: cqm.get_content_unit_by_id(type_id, unit_id) except MissingResource: return self.not_found(_('No content unit resource: %(r)s') % {'r': unit_id}) cm = factory.content_manager() delta = {constants.PULP_USER_METADATA_FIELDNAME: self.params()} cm.update_content_unit(type_id, unit_id, delta) return self.ok(None)
def save_unit(self, unit): """ Performs two distinct steps on the Pulp server: - Creates or updates Pulp's knowledge of the content unit. - Associates the unit to the repository being synchronized. If a unit with the provided unit key already exists, it is updated with the attributes on the passed-in unit. A reference to the provided unit is returned from this call. This call will populate the unit's id field with the UUID for the unit. @param unit: unit object returned from the init_unit call @type unit: L{Unit} @return: object reference to the provided unit, its state updated from the call @rtype: L{Unit} """ try: content_query_manager = manager_factory.content_query_manager() content_manager = manager_factory.content_manager() association_manager = manager_factory.repo_unit_association_manager() # Save or update the unit pulp_unit = common_utils.to_pulp_unit(unit) try: existing_unit = content_query_manager.get_content_unit_by_keys_dict(unit.type_id, unit.unit_key) unit.id = existing_unit['_id'] content_manager.update_content_unit(unit.type_id, unit.id, pulp_unit) self._updated_count += 1 except MissingResource: unit.id = content_manager.add_content_unit(unit.type_id, None, pulp_unit) self._added_count += 1 # Associate it with the repo association_manager.associate_unit_by_id(self.repo_id, unit.type_id, unit.id, self.association_owner_type, self.association_owner_id) return unit except Exception, e: _LOG.exception(_('Content unit association failed [%s]' % str(unit))) raise ImporterConduitException(e), None, sys.exc_info()[2]
def add_units(self, begin, end): units = [] storage_dir = os.path.join(pulp_conf.get('server', 'storage_dir'), 'content') if not os.path.exists(storage_dir): os.makedirs(storage_dir) for n in range(begin, end): unit_id = self.UNIT_ID % n unit = dict(self.UNIT_KEY) unit.update(self.UNIT_METADATA) unit['N'] = n # add unit file storage_path = os.path.join(storage_dir, '.'.join((unit_id, self.UNIT_TYPE_ID))) if n % 2 == 0: # even numbered has file associated unit['_storage_path'] = storage_path if n == 0: # 1st one is a directory of files os.makedirs(storage_path) dist_path = os.path.join(os.path.dirname(__file__), 'data/distribution.tar') tb = tarfile.open(dist_path) tb.extractall(path=storage_path) tb.close() else: with open(storage_path, 'w+') as fp: fp.write(unit_id) # add unit manager = managers.content_manager() manager.add_content_unit( self.UNIT_TYPE_ID, unit_id, unit) manager = managers.repo_unit_association_manager() # associate unit manager.associate_unit_by_id( self.REPO_ID, self.UNIT_TYPE_ID, unit_id, RepoContentUnit.OWNER_TYPE_IMPORTER, constants.HTTP_IMPORTER) units.append(unit) return units
def add_units(num_units=10): units = [] n = 0 for type_id in ALL_TYPES: for x in range(0, num_units): unit_id = create_unit_id(type_id, n) unit = dict(UNIT_METADATA) unit['N'] = n unit['_storage_path'] = create_storage_path(unit_id) manager = managers.content_manager() manager.add_content_unit(type_id, unit_id, unit) manager = managers.repo_unit_association_manager() # associate unit manager.associate_unit_by_id( REPO_ID, type_id, unit_id, RepoContentUnit.OWNER_TYPE_IMPORTER, constants.HTTP_IMPORTER) units.append(unit) n += 1 return units
def _update_unit(self, unit, pulp_unit): """ Update a unit. If it is not found, add it. :param unit: the unit to be updated :type unit: pulp.plugins.model.Unit :param pulp_unit: the unit to be updated, as a dict :type pulp_unit: dict :return: id of the updated unit :rtype: basestring """ content_query_manager = manager_factory.content_query_manager() content_manager = manager_factory.content_manager() try: existing_unit = content_query_manager.get_content_unit_by_keys_dict(unit.type_id, unit.unit_key) unit_id = existing_unit['_id'] content_manager.update_content_unit(unit.type_id, unit_id, pulp_unit) self._updated_count += 1 return unit_id except MissingResource: logger.debug(_('cannot update unit; does not exist. adding instead.')) return self._add_unit(unit, pulp_unit)
def put(self, request, type_id, unit_id): """ Set the pulp_user_metadata field on a content unit. :param type_id: The Unit's type id. :type type_id: basestring :param unit_id: The id of the unit that you wish to set the pulp_user_metadata field on :type unit_id: basestring :return: response containing pulp user metadata_field :rtype: django.http.HttpResponse or HttpResponseNotFound """ params = request.body_as_json cqm = factory.content_query_manager() try: cqm.get_content_unit_by_id(type_id, unit_id) except MissingResource: msg = _("No content unit resource: %(r)s") % {"r": unit_id} return generate_json_response(msg, HttpResponseNotFound) cm = factory.content_manager() delta = {constants.PULP_USER_METADATA_FIELDNAME: params} cm.update_content_unit(type_id, unit_id, delta) return generate_json_response(None)
def put(self, request, type_id, unit_id): """ Set the pulp_user_metadata field on a content unit. :param type_id: The Unit's type id. :type type_id: basestring :param unit_id: The id of the unit that you wish to set the pulp_user_metadata field on :type unit_id: basestring :return: response containing pulp user metadata_field :rtype: django.http.HttpResponse or HttpResponseNotFound """ params = request.body_as_json cqm = factory.content_query_manager() try: cqm.get_content_unit_by_id(type_id, unit_id) except MissingResource: msg = _('No content unit resource: %(r)s') % {'r': unit_id} return generate_json_response(msg, HttpResponseNotFound) cm = factory.content_manager() delta = {constants.PULP_USER_METADATA_FIELDNAME: params} cm.update_content_unit(type_id, unit_id, delta) return generate_json_response(None)
import logging import pymongo.errors from pulp.server.managers import factory from pulp.server.managers.repo.cud import RepoContentUnit from pulp.server.managers.repo.unit_association_query import UnitAssociationCriteria from pulp_rpm.common import ids _log = logging.getLogger('pulp') # Initialize plugin loader api and other managers factory.initialize() ass_query_mgr = factory.repo_unit_association_query_manager() ass_mgr = factory.repo_unit_association_manager() content_mgr = factory.content_manager() repo_mgr = factory.repo_manager() def _get_repos(): """ Lookups all the yum based repos in pulp. @return a list of repoids """ repos = factory.repo_query_manager().find_with_importer_type( "yum_importer") if not repos: _log.debug("No repos found to perform db migrate") return [] repo_ids = [repo['id'] for repo in repos] return repo_ids
from pulp.plugins.types import database as types_db from pulp.server.managers import factory from pulp.server.managers.repo.cud import RepoContentUnit from pulp.server.managers.repo.unit_association_query import UnitAssociationCriteria import pymongo.errors from pulp_rpm.common import ids _log = logging.getLogger('pulp') # Initialize plugin loader api and other managers factory.initialize() ass_query_mgr = factory.repo_unit_association_query_manager() ass_mgr = factory.repo_unit_association_manager() content_mgr = factory.content_manager() repo_mgr = factory.repo_manager() def _get_repos(): """ Lookups all the yum based repos in pulp. @return a list of repoids """ repos = factory.repo_query_manager().find_with_importer_type("yum_importer") if not repos: _log.debug("No repos found to perform db migrate") return [] repo_ids = [repo['id'] for repo in repos] return repo_ids def _fix_pkg_group_category_repoid(repoid, typeid):