Exemple #1
0
    def test_modified_is_older_than_created(self, session):
        binary = Binary(
            'ceph-1.0.rpm',
            self.p,
            distro='centos',
            distro_version='7',
            arch='x86_64',
            )
        session.commit()
        initial_created = binary.created.time()
        binary.ref = 'master'
        session.commit()
        binary = Binary.get(1)

        assert initial_created < binary.modified.time()
Exemple #2
0
    def index_post(self):
        contents = request.POST.get('file', False)
        if contents is False:
            error('/errors/invalid/', 'no file object found in "file" param in POST request')
        file_obj = contents.file
        filename = contents.filename
        self.binary = self.get_binary(filename)
        self.binary_name = filename
        if self.binary is not None:
            if os.path.exists(self.binary.path):
                if request.POST.get('force', False) is False:
                    error('/errors/invalid', 'resource already exists and "force" key was not used')

        full_path = self.save_file(file_obj)

        if self.binary is None:
            path = full_path
            distro = request.context['distro']
            distro_version = request.context['distro_version']
            arch = request.context['arch']
            ref = request.context['ref']
            sha1 = request.context['sha1']

            self.binary = Binary(
                self.binary_name, self.project, arch=arch,
                distro=distro, distro_version=distro_version,
                ref=ref, sha1=sha1, path=path, size=os.path.getsize(path)
            )
        else:
            self.binary.path = full_path

        # check if this binary is interesting for other configured projects,
        # and if so, then mark those other repos so that they can be re-built
        self.mark_related_repos()
        return dict()
Exemple #3
0
 def test_binary_create_repo_object(self, session):
     binary = Binary(
         'ceph-1.0.rpm',
         self.p,
         ref='firefly',
         distro='centos',
         distro_version='7',
         arch='x86_64',
         )
     session.commit()
     binary = Binary.get(1)
     assert binary.repo.ref == 'firefly'
     assert binary.repo.distro_version == '7'
Exemple #4
0
 def test_binary_uses_explicit_repo(self, session):
     repo = Repo(self.p, 'firefly', 'centos', '7')
     binary = Binary(
         'ceph-1.0.rpm',
         self.p,
         distro='centos',
         distro_version='7',
         arch='x86_64',
         repo=repo,
         )
     session.commit()
     binary = Binary.get(1)
     assert binary.repo.ref == 'firefly'
     assert binary.repo.distro_version == '7'
Exemple #5
0
 def test_binary_reuse_repo_object(self, session):
     repo = Repo(self.p, 'hammer', 'centos', '7')
     session.commit()
     binary = Binary(
         'ceph-1.0.rpm',
         self.p,
         ref='hammer',
         distro='centos',
         distro_version='7',
         arch='x86_64',
         )
     session.commit()
     binary = Binary.get(1)
     assert binary.repo.ref == 'hammer'
     assert binary.repo.distro_version == '7'
Exemple #6
0
    def test_new_binary_upload_creates_model_with_path_forced(self, session, tmpdir):
        pecan.conf.binary_root = str(tmpdir)

        session.app.post(
            '/binaries/ceph/giant/ceph/el6/x86_64/',
            upload_files=[('file', 'ceph-9.0.0-0.el6.x86_64.rpm', 'hello tharrrr')]
        )
        session.app.put(
            '/binaries/ceph/giant/ceph/el6/x86_64/ceph-9.0.0-0.el6.x86_64.rpm/',
            upload_files=[('file', 'ceph-9.0.0-0.el6.x86_64.rpm', 'hello tharrrr')]
        )
        session.app.put(
            '/binaries/ceph/giant/ceph/el6/x86_64/ceph-9.0.0-0.el6.x86_64.rpm/',
            upload_files=[('file', 'ceph-9.0.0-0.el6.x86_64.rpm', 'hello tharrrr')]
        )

        binary = Binary.get(1)
        assert binary.path.endswith('ceph/giant/ceph/el6/x86_64/ceph-9.0.0-0.el6.x86_64.rpm')
Exemple #7
0
 def get_binary(self, name):
     return Binary.filter_by(
         name=name, project=self.project, arch=self.arch,
         distro=self.distro, distro_version=self.distro_version,
         ref=self.ref, sha1=self.sha1
     ).first()
Exemple #8
0
class ArchController(object):

    def __init__(self, arch):
        self.arch = arch
        self.project = models.Project.get(request.context['project_id'])
        self.distro = request.context['distro']
        self.distro_version = request.context['distro_version']
        self.ref = request.context['ref']
        self.sha1 = request.context['sha1']
        request.context['arch'] = self.arch

    @expose(generic=True, template='json')
    def index(self):
        abort(405)

    @index.when(method='HEAD', template='json')
    def index_head(self):
        binaries = self.project.binaries.filter_by(
            distro=self.distro,
            distro_version=self.distro_version,
            ref=self.ref,
            sha1=self.sha1,
            arch=self.arch).all()

        if not binaries:
            abort(404)
        return dict()

    @index.when(method='GET', template='json')
    def index_get(self):
        binaries = self.project.binaries.filter_by(
            distro=self.distro,
            distro_version=self.distro_version,
            ref=self.ref,
            sha1=self.sha1,
            arch=self.arch).all()

        if not binaries:
            abort(404)

        resp = {}
        for b in self.project.binaries.filter_by(
                distro=self.distro,
                distro_version=self.distro_version,
                ref=self.ref,
                sha1=self.sha1,
                arch=self.arch).all():
            resp[b.name] = b
        return resp

    def get_binary(self, name):
        return Binary.filter_by(
            name=name, project=self.project, arch=self.arch,
            distro=self.distro, distro_version=self.distro_version,
            ref=self.ref, sha1=self.sha1
        ).first()

    @secure(basic_auth)
    @index.when(method='POST', template='json')
    def index_post(self):
        contents = request.POST.get('file', False)
        if contents is False:
            error('/errors/invalid/', 'no file object found in "file" param in POST request')
        file_obj = contents.file
        filename = contents.filename
        self.binary = self.get_binary(filename)
        self.binary_name = filename
        if self.binary is not None:
            if os.path.exists(self.binary.path):
                if request.POST.get('force', False) is False:
                    error('/errors/invalid', 'resource already exists and "force" key was not used')

        full_path = self.save_file(file_obj)

        if self.binary is None:
            path = full_path
            distro = request.context['distro']
            distro_version = request.context['distro_version']
            arch = request.context['arch']
            ref = request.context['ref']
            sha1 = request.context['sha1']

            self.binary = Binary(
                self.binary_name, self.project, arch=arch,
                distro=distro, distro_version=distro_version,
                ref=ref, sha1=sha1, path=path, size=os.path.getsize(path)
            )
        else:
            self.binary.path = full_path

        # check if this binary is interesting for other configured projects,
        # and if so, then mark those other repos so that they can be re-built
        self.mark_related_repos()
        return dict()

    def mark_related_repos(self):
        related_projects = util.get_related_projects(self.project.name)
        repos = []
        projects = []
        for project_name, refs in related_projects.items():
            p = models.projects.get_or_create(name=project_name)
            projects.append(p)
            repo_query = []
            if refs == ['all']:
                # we need all the repos available
                repo_query = models.Repo.filter_by(project=p).all()
            else:
                for ref in refs:
                    repo_query = models.Repo.filter_by(project=p, ref=ref).all()
            if repo_query:
                for r in repo_query:
                    repos.append(r)

        if not repos:
            # there are no repositories associated with this project, so go ahead
            # and create one so that it can be queried by the celery task later
            for project in projects:
                repo = models.Repo(
                    project,
                    self.ref,
                    self.distro,
                    self.distro_version,
                    sha1=self.sha1,
                )
                repo.needs_update = repository_is_automatic(project.name)
                repo.type = self.binary._get_repo_type()

        else:
            for repo in repos:
                repo.needs_update = repository_is_automatic(repo.project.name)
                if repo.type is None:
                    repo.type = self.binary._get_repo_type()

    def create_directory(self):
        end_part = request.url.split('binaries/')[-1].rstrip('/')
        # take out the binary name
        end_part = end_part.split(self.binary_name)[0]
        path = os.path.join(pecan.conf.binary_root, end_part.lstrip('/'))
        if not os.path.isdir(path):
            os.makedirs(path)
        return path

    def save_file(self, file_obj):
        dir_path = self.create_directory()
        if self.binary_name in os.listdir(dir_path):
            # resource exists so we will update it
            response.status = 200
        else:
            # we will create a resource
            response.status = 201

        destination = os.path.join(dir_path, self.binary_name)

        with open(destination, 'wb') as f:
            file_iterable = FileIter(file_obj)
            for chunk in file_iterable:
                f.write(chunk)

        # return the full path to the saved object:
        return destination

    @expose()
    def _lookup(self, name, *remainder):
        return BinaryController(name), remainder

    flavors = _flavors.FlavorsController()
Exemple #9
0
 def test_get_index_single_sha1(self, session):
     p = Project('ceph')
     Binary('ceph-1.0.0.rpm', p, sha1="head", ref='master', distro='centos', distro_version='el6', arch='i386')
     session.commit()
     result = session.app.get('/binaries/ceph/master/head/')
     assert result.json == {'centos': ['el6']}