Skip to content
This repository has been archived by the owner on Dec 7, 2022. It is now read-only.

Convert the distributor manager to mongoengine friendly controller. #2233

Merged
merged 1 commit into from Dec 16, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 5 additions & 5 deletions nodes/parent/pulp_node/distributors/http/distributor.py
Expand Up @@ -6,7 +6,6 @@

from pulp.plugins.distributor import Distributor
from pulp.server.db import model
from pulp.server.managers import factory
from pulp.server.config import config as pulp_conf
from pulp.server.compat import json

Expand Down Expand Up @@ -251,9 +250,10 @@ def _add_distributors(self, repo_id, payload):
:type payload: dict
"""
distributors = []
manager = factory.repo_distributor_manager()
for dist in manager.get_distributors(repo_id):
if dist['distributor_type_id'] in constants.ALL_DISTRIBUTORS:
for dist in model.Distributor.objects(repo_id=repo_id):
if dist.distributor_type_id in constants.ALL_DISTRIBUTORS:
continue
distributors.append(dist)
serialized = model.Distributor.serializer(dist).data
serialized.pop('_href')
distributors.append(serialized)
payload['distributors'] = distributors
2 changes: 1 addition & 1 deletion nodes/test/nodes_tests/test_conduit.py
Expand Up @@ -91,7 +91,7 @@ def setUp(self):

def tearDown(self):
super(QueryTests, self).tearDown()
model.Repository.drop_collection()
model.Repository.objects.delete()
RepoContentUnit.get_collection().remove()
unit_db.clean()

Expand Down
35 changes: 17 additions & 18 deletions nodes/test/nodes_tests/test_plugins.py
Expand Up @@ -29,7 +29,6 @@
from pulp.server import config as pulp_conf
from pulp.server.db import connection
from pulp.server.db import model
from pulp.server.db.model.repository import RepoDistributor
from pulp.server.db.model.repository import RepoContentUnit
from pulp.server.db.model.consumer import Consumer, Bind
from pulp.server.db.model.content import ContentType
Expand Down Expand Up @@ -169,8 +168,8 @@ def setUp(self):
self.alias = (self.parentfs, self.parentfs)
Consumer.get_collection().remove()
Bind.get_collection().remove()
RepoDistributor.get_collection().remove()
model.Importer.drop_collection()
model.Distributor.objects.delete()
model.Importer.objects.delete()
RepoContentUnit.get_collection().remove()
unit_db.clean()
self.define_plugins()
Expand All @@ -190,9 +189,9 @@ def tearDown(self):
shutil.rmtree(self.childfs)
Consumer.get_collection().remove()
Bind.get_collection().remove()
model.Repository.drop_collection()
RepoDistributor.get_collection().remove()
model.Importer.drop_collection()
model.Repository.objects.delete()
model.Distributor.objects.delete()
model.Importer.objects.delete()
RepoContentUnit.get_collection().remove()
unit_db.clean()

Expand Down Expand Up @@ -658,8 +657,8 @@ def test_import(self, *mocks):
cfg = self.dist_conf()
conduit = RepoPublishConduit(self.REPO_ID, constants.HTTP_DISTRIBUTOR)
dist.publish_repo(repo, conduit, cfg)
model.Repository.drop_collection()
RepoDistributor.get_collection().remove()
model.Repository.objects.delete()
model.Distributor.objects.delete()
RepoContentUnit.get_collection().remove()
unit_db.clean()
self.define_plugins()
Expand Down Expand Up @@ -700,8 +699,8 @@ def test_import_cached_manifest_matched(self, mock_fetch, *unused):
configuration = self.dist_conf()
conduit = RepoPublishConduit(self.REPO_ID, constants.HTTP_DISTRIBUTOR)
dist.publish_repo(repo, conduit, configuration)
model.Repository.drop_collection()
RepoDistributor.get_collection().remove()
model.Repository.objects.delete()
model.Distributor.objects.delete()
RepoContentUnit.get_collection().remove()
unit_db.clean()
self.define_plugins()
Expand Down Expand Up @@ -743,8 +742,8 @@ def test_import_cached_manifest_missing_units(self, *unused):
configuration = self.dist_conf()
conduit = RepoPublishConduit(self.REPO_ID, constants.HTTP_DISTRIBUTOR)
dist.publish_repo(repo, conduit, configuration)
model.Repository.drop_collection()
RepoDistributor.get_collection().remove()
model.Repository.objects.delete()
model.Distributor.objects.delete()
RepoContentUnit.get_collection().remove()
unit_db.clean()
self.define_plugins()
Expand Down Expand Up @@ -784,8 +783,8 @@ def test_import_cached_manifest_units_invalid(self, *unused):
configuration = self.dist_conf()
conduit = RepoPublishConduit(self.REPO_ID, constants.HTTP_DISTRIBUTOR)
dist.publish_repo(repo, conduit, configuration)
model.Repository.drop_collection()
RepoDistributor.get_collection().remove()
model.Repository.objects.delete()
model.Distributor.objects.delete()
RepoContentUnit.get_collection().remove()
unit_db.clean()
self.define_plugins()
Expand Down Expand Up @@ -829,8 +828,8 @@ def test_import_unit_files_already_exist(self, *mocks):
cfg = self.dist_conf()
conduit = RepoPublishConduit(self.REPO_ID, constants.HTTP_DISTRIBUTOR)
dist.publish_repo(repo, conduit, cfg)
model.Repository.drop_collection()
RepoDistributor.get_collection().remove()
model.Repository.objects.delete()
model.Distributor.objects.delete()
RepoContentUnit.get_collection().remove()
unit_db.clean()
self.define_plugins()
Expand Down Expand Up @@ -873,8 +872,8 @@ def test_import_unit_files_already_exist_size_mismatch(self, *mocks):
cfg = self.dist_conf()
conduit = RepoPublishConduit(self.REPO_ID, constants.HTTP_DISTRIBUTOR)
dist.publish_repo(repo, conduit, cfg)
model.Repository.drop_collection()
RepoDistributor.get_collection().remove()
model.Repository.objects.delete()
model.Distributor.objects.delete()
RepoContentUnit.get_collection().remove()
unit_db.clean()
self.define_plugins()
Expand Down
37 changes: 15 additions & 22 deletions server/pulp/plugins/conduits/mixins.py
Expand Up @@ -305,40 +305,33 @@ def __init__(self, repo_id, distributor_id):

def get_scratchpad(self):
"""
Returns the value set in the scratchpad for this repository. If no
value has been set, None is returned.

@return: value saved for the repository and this distributor
@rtype: <serializable>
Returns the scratchpad for this Distributor or None if it has not been set.

@raises DistributorConduitException: wraps any exception that may occur
in the Pulp server
:return: value saved for the repository and this distributor
:rtype: dict or None
:raises DistributorConduitException: wraps any exception that may occur in the Pulp server
"""
try:
distributor_manager = manager_factory.repo_distributor_manager()
value = distributor_manager.get_distributor_scratchpad(self.repo_id,
self.distributor_id)
return value
qs = model.Distributor.objects.get_or_404
distributor = qs(repo_id=self.repo_id, distributor_id=self.distributor_id)
return distributor.scratchpad
except Exception, e:
_logger.exception('Error getting scratchpad for repository [%s]' % self.repo_id)
raise DistributorConduitException(e), None, sys.exc_info()[2]

def set_scratchpad(self, value):
"""
Saves the given value to the scratchpad for this repository. It can later
be retrieved in subsequent syncs through get_scratchpad. The type for
the given value is anything that can be stored in the database (string,
list, dict, etc.).
Sets the scratchpad of this distributor to the given value.

@param value: will overwrite the existing scratchpad
@type value: <serializable>

@raises DistributorConduitException: wraps any exception that may occur
in the Pulp server
:param value: will overwrite the existing scratchpad
:type value: dict
:raises DistributorConduitException: wraps any exception that may occur in the Pulp server
"""
try:
distributor_manager = manager_factory.repo_distributor_manager()
distributor_manager.set_distributor_scratchpad(self.repo_id, self.distributor_id, value)
qs = model.Distributor.objects.get_or_404
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's worth noting that the except statement below will catch the MissingResource exception and turn it into whatever a DistributorConduitException is. :( You've preserved previous behavior, which is totally fine, but I thought I'd at least point it out that the result of calling get_or_404 is being diminished.

distributor = qs(repo_id=self.repo_id, distributor_id=self.distributor_id)
distributor.scratchpad = value
distributor.save()
except Exception, e:
_logger.exception('Error setting scratchpad for repository [%s]' % self.repo_id)
raise DistributorConduitException(e), None, sys.exc_info()[2]
Expand Down
35 changes: 17 additions & 18 deletions server/pulp/plugins/conduits/repo_config.py
Expand Up @@ -5,7 +5,9 @@

import os

from pulp.server.db.model.repository import RepoDistributor
from mongoengine import Q

from pulp.server.db import model


class RepoConfigConduit(object):
Expand All @@ -14,19 +16,16 @@ def __init__(self, distributor_type):

def get_repo_distributors_by_relative_url(self, rel_url, repo_id=None):
"""
Get the config repo_id and config objects matching a given relative URL. This is agnostic
to preceding slashes.
Retrieve a dict containing the repo_id, distributor_id and config for all distributors that
conflict with the given relative URL. This is agnostic to preceding slashes.

:param rel_url: a relative URL for a distributor config
:type rel_url: str

:type rel_url: basestring
:param repo_id: the id of a repo to skip, If not specified all repositories will be
included in the search
:type repo_id: str

:return: a cursor to iterate over the list of repository configurations whose
configuration conflicts with rel_url
:rtype: pymongo.cursor.Cursor
:type repo_id: basestring
:return: info about each distributor whose configuration conflicts with rel_url
:rtype: list of dicts
"""

# build a list of all the sub urls that could conflict with the provided URL.
Expand All @@ -44,14 +43,14 @@ def get_repo_distributors_by_relative_url(self, rel_url, repo_id=None):
# Search for all the sub urls as well as any url that would fall within the specified url.
# The regex here basically matches the a url if it starts with (optional preceding slash)
# the working url. Anything can follow as long as it is separated by a slash.
spec = {'$or': [{'config.relative_url': {'$regex': '^/?' + working_url + '(/.*|/?\z)'}},
{'config.relative_url': {'$in': matching_url_list}},
{'$and': [{'config.relative_url': {'$exists': False}},
{'repo_id': repo_id_url}]}
]}
rel_url_match = Q(config__relative_url={'$regex': '^/?' + working_url + '(/.*|/?\z)'})
rel_url_in_list = Q(config__relative_url__in=matching_url_list)
rel_url_is_repo_id = Q(config__relative_url__exists=False) & Q(repo_id=repo_id_url)

spec = rel_url_is_repo_id | rel_url_in_list | rel_url_match

if repo_id is not None:
spec = {'$and': [{'repo_id': {'$ne': repo_id}}, spec]}
spec = Q(repo_id__ne=repo_id) & spec

projection = {'repo_id': 1, 'config': 1}
return RepoDistributor.get_collection().find(spec, projection)
dists = model.Distributor.objects(spec).only('repo_id', 'config')
return [{'repo_id': dist.repo_id, '_id': dist.id, 'config': dist.config} for dist in dists]
12 changes: 4 additions & 8 deletions server/pulp/plugins/conduits/repo_publish.py
Expand Up @@ -6,13 +6,11 @@
import logging
import sys

from pulp.server import exceptions as pulp_exceptions
from pulp.common import dateutils
from pulp.plugins.conduits.mixins import (
DistributorConduitException, RepoScratchPadMixin, RepoScratchpadReadMixin,
DistributorScratchPadMixin, RepoGroupDistributorScratchPadMixin, StatusMixin,
SingleRepoUnitsMixin, MultipleRepoUnitsMixin, PublishReportMixin)
from pulp.server.db.model.repository import RepoDistributor
from pulp.server.db import model
from pulp.server.managers import factory as manager_factory


Expand Down Expand Up @@ -64,11 +62,9 @@ def last_publish(self):
:raises DistributorConduitException: if any errors occur
"""
try:
collection = RepoDistributor.get_collection()
distributor = collection.find_one({'repo_id': self.repo_id, 'id': self.distributor_id})
if distributor is None:
raise pulp_exceptions.MissingResource(self.repo_id)
return dateutils.ensure_tz(distributor['last_publish'])
dist = model.Distributor.objects.only('last_publish').get_or_404(
repo_id=self.repo_id, distributor_id=self.distributor_id)
return dist.last_publish
except Exception, e:
_logger.exception('Error getting last publish time for repo [%s]' % self.repo_id)
raise DistributorConduitException(e), None, sys.exc_info()[2]
Expand Down