Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
@audit()
def add_consumer(self, groupid, consumerid):
"""
Adds the passed in consumer to this group
"""
consumergroup = self.consumergroup(groupid)
if (consumergroup is None):
raise PulpException("No Consumer Group with id: %s found" % groupid)
consumer = self.consumerApi.consumer(consumerid)
if consumer is None:
raise PulpException("No Consumer with id: %s found" % consumerid)
conflicting_keyvalues = self.find_conflicting_keyvalues(groupid, consumerid)
if len(conflicting_keyvalues.keys()) > 0:
raise PulpException('Consumer [%s] cannot be added to consumergroup [%s] because of the following '
'conflicting key-value pairs. You need to delete these key-values from the consumer '
'in order to add it to this consumergroup: %s', consumerid, groupid, conflicting_keyvalues)
self._add_consumer(consumergroup, consumer)
self.collection.save(consumergroup, safe=True)
@audit()
def uninstallpackages(self, id, names=()):
"""
Uninstall packages on the consumers in a consnumer group.
@param id: A consumer group id.
@type id: str
@param names: The package names to uninstall.
@type names: [str,..]
"""
consumergroup = self.consumergroup(id)
if consumergroup is None:
raise PulpException("No Consumer Group with id: %s found" % id)
job = Job()
for consumerid in consumergroup['consumerids']:
consumer = self.consumerApi.consumer(consumerid)
if consumer is None:
log.error('consumer [%s], not-found', consumerid)
continue
task = AsyncTask(self.__uninstallpackages, [consumerid, names])
job.add(task)
return job
@audit()
def create(self, id, type, description=None, package_list=[]):
"""
Create a new Filter object and return it
"""
id = encode_unicode(id)
filter = self.filter(id)
if filter is not None:
raise PulpException("A Filter with id %s already exists" % id)
f = model.Filter(id, type, description, package_list)
self.collection.insert(f, safe=True)
f = self.filter(f["id"])
return f
@audit(params=['id'])
def profile_update(self, id, package_profile):
"""
Update the consumer information such as package profile
"""
consumer = self.consumer(id)
if consumer is None:
raise PulpException('Consumer [%s] not found', id)
consumer["package_profile"] = package_profile
self.collection.save(consumer, safe=True)
log.info('Successfully updated package profile for consumer %s' % id)
@audit()
def update(self, id, delta):
"""
Updates a consumer group object.
@param id: The repo ID.
@type id: str
@param delta: A dict containing update keywords.
@type delta: dict
@return: The updated object
@rtype: dict
"""
delta.pop('id', None)
group = self.consumergroup(id)
if not group:
raise PulpException('Group [%s] does not exist', id)
for key, value in delta.items():
# simple changes
if key in ('description',):
group[key] = value
continue
# unsupported
raise Exception, \
'update keyword "%s", not-supported' % key
self.collection.save(group, safe=True)
@param delta: mapping of properties and values to change
@type delta: dict
@raises PulpException: if any of the new values are invalid
'''
log.info('Updating CDS [%s]' % hostname)
log.info(delta)
# Validate ----------
if 'sync_schedule' in delta and delta['sync_schedule'] is not None: # will be None if removing the schedule
try:
dateutils.parse_iso8601_interval(delta['sync_schedule'])
except:
log.exception('Could not update CDS [%s] because the sync schedule was invalid [%s]' % (hostname, delta['sync_schedule']))
raise PulpException('Invalid sync schedule format [%s]' % delta['sync_schedule']), None, sys.exc_info()[2]
if 'cluster_id' in delta and delta['cluster_id'] is not None: # will be None if removing the cluster
if CLUSTER_ID_PATTERN.match(delta['cluster_id']) is None:
log.info('Could not update CDS [%s] because the cluster ID was invalid [%s]' % (hostname, delta['cluster_id']))
raise PulpException('Cluster ID must match the standard ID restrictions')
# Update ----------
cds = self.collection.find_one({'hostname': hostname})
# If we ever get enough values to warrant a loop, we can add it. For now, it's
# just simpler to handle one at a time.
if 'name' in delta:
cds['name'] = delta['name']
if 'description' in delta:
cds['description'] = delta['description']
# There is NO WARRANTY for this software, express or implied,
# including the implied warranties of MERCHANTABILITY,
# NON-INFRINGEMENT, or FITNESS FOR A PARTICULAR PURPOSE. You should
# have received a copy of GPLv2 along with this software; if not, see
# http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt.
"""
Mongo utility module to help pulp web services deal with the mongo db.
"""
import re
from pulp.server.exceptions import PulpException
class MalformedFilters(PulpException):
pass
def filters_to_re_spec(filters):
"""
@type filters: dict of str: list
@param filters: http request query parameters
@return: dict of field: regex of possible str values
"""
if not filters:
return None
return dict((k, re.compile('(%s)' % '|'.join(v))) for k,v in filters.items())
def filters_to_set_spec(filters, intersect=(), union=()):
"""
if key not in key_value_pairs.keys():
conflicting_consumers = self.find_consumers_with_conflicting_keyvalues(id, key, value)
if len(conflicting_consumers) == 0:
key_value_pairs[key] = value
else:
if force == 'false':
raise PulpException('Given key [%s] has different value for consumers %s '
'belonging to this group. You can use --force to '
'delete consumer\'s original value.', key, conflicting_consumers)
else:
for consumerid in conflicting_consumers:
self.consumerApi.delete_key_value_pair(consumerid, key)
key_value_pairs[key] = value
else:
raise PulpException('Given key [%s] already exists', key)
consumergroup['key_value_pairs'] = key_value_pairs
self.collection.save(consumergroup, safe=True)
'''
repo = self._get_existing_repo(id)
repo_path = os.path.join(
pulp.server.util.top_repos_location(), repo['relative_path'])
repo_repomd_path = "%s/%s" % (repo_path, "repodata/repomd.xml")
file_path = pulp.server.util.get_repomd_filetype_path(repo_repomd_path, filetype)
if not file_path:
msg = "metadata file of type [%s] cannot be found in repository [%s]" % (filetype, id)
log.info(msg)
raise PulpException(msg)
try:
pulp.server.util.modify_repo(os.path.dirname(repo_repomd_path), filetype, remove=True)
except Exception, e:
msg = "Error [%s] removing the metadata file for type [%s]" % (str(e), filetype)
log.info(msg)
raise PulpException(msg)
def __uninstallpackagegroups(self, consumerid, grpids):
"""
Task callback to uninstall package groups.
@param consumerid: The consumer ID.
@type consumerid: str
@param grpids: A list of package group ids.
@type grpids: list
@return: Whatever the agent returns.
"""
consumer = self.consumerApi.consumer(consumerid)
if consumer is None:
raise PulpException('Consumer [%s] not found', consumerid)
secret = PulpAgent.getsecret(consumer)
agent = AsyncAgent(consumerid, secret)
task = AsyncTask.current()
tm = (10, 600) # start in 10 seconds, finish in 10 minutes
pkgrps = agent.PackageGroups(task, timeout=tm)
return pkgrps.uninstall(grpids)