Пример #1
0
def task_delete_namespace(self, namespace_id):
    conda_store = self.worker.conda_store
    namespace = api.get_namespace(conda_store.db, id=namespace_id)

    for environment_orm in namespace.environments:
        for build in environment_orm.builds:
            conda_store.log.info(f"deleting artifacts for build={build.id}")
            for build_artifact in api.list_build_artifacts(
                    conda_store.db,
                    build_id=build.id,
            ).all():
                delete_build_artifact(conda_store, build_artifact)
        conda_store.db.delete(environment_orm)
    conda_store.db.delete(namespace)
    conda_store.db.commit()
Пример #2
0
def api_delete_namespace(
    namespace: str,
    request: Request,
    conda_store=Depends(dependencies.get_conda_store),
    auth=Depends(dependencies.get_auth),
):
    auth.authorize_request(
        request, namespace, {Permissions.NAMESPACE_DELETE}, require=True
    )

    namespace_orm = api.get_namespace(conda_store.db, namespace)
    if namespace_orm is None:
        raise HTTPException(status_code=404, detail="namespace does not exist")

    conda_store.delete_namespace(namespace)
    return {"status": "ok"}
Пример #3
0
def api_get_namespace(
    namespace: str,
    request: Request,
    conda_store=Depends(dependencies.get_conda_store),
    auth=Depends(dependencies.get_auth),
):
    auth.authorize_request(
        request, namespace, {Permissions.NAMESPACE_READ}, require=True
    )

    namespace = api.get_namespace(conda_store.db, namespace, show_soft_deleted=False)
    if namespace is None:
        raise HTTPException(status_code=404, detail="namespace does not exist")

    return {
        "status": "ok",
        "data": schema.Namespace.from_orm(namespace).dict(),
    }
Пример #4
0
def api_create_namespace(
    namespace: str,
    request: Request,
    conda_store=Depends(dependencies.get_conda_store),
    auth=Depends(dependencies.get_auth),
):
    auth.authorize_request(
        request, namespace, {Permissions.NAMESPACE_CREATE}, require=True
    )

    namespace_orm = api.get_namespace(conda_store.db, namespace)
    if namespace_orm:
        raise HTTPException(status_code=409, detail="namespace already exists")

    try:
        api.create_namespace(conda_store.db, namespace)
    except ValueError as e:
        raise HTTPException(status_code=400, detail=str(e.args[0]))
    conda_store.db.commit()
    return {"status": "ok"}
Пример #5
0
    def delete_namespace(self, namespace):
        namespace = api.get_namespace(self.db, name=namespace)
        if namespace is None:
            raise utils.CondaStoreError(
                f"namespace={namespace} does not exist")

        utcnow = datetime.datetime.utcnow()
        namespace.deleted_on = utcnow
        for environment_orm in namespace.environments:
            environment_orm.deleted_on = utcnow
            for build in environment_orm.builds:
                build.deleted_on = utcnow
        self.db.commit()

        self.celery_app

        # must import tasks after a celery app has been initialized
        from conda_store_server.worker import tasks

        tasks.task_delete_namespace.si(namespace.id).apply_async()
Пример #6
0
def api_post_specification(
    request: Request,
    conda_store=Depends(dependencies.get_conda_store),
    auth=Depends(dependencies.get_auth),
    entity=Depends(dependencies.get_entity),
    specification: str = Body(""),
    namespace: Optional[str] = Body(None),
):
    permissions = {Permissions.ENVIRONMENT_CREATE}

    default_namespace = (
        entity.primary_namespace if entity else conda_store.default_namespace
    )

    namespace_name = namespace or default_namespace
    namespace = api.get_namespace(conda_store.db, namespace_name)
    if namespace is None:
        permissions.add(Permissions.NAMESPACE_CREATE)

    try:
        specification = yaml.safe_load(specification)
        specification = schema.CondaSpecification.parse_obj(specification)
    except yaml.error.YAMLError:
        raise HTTPException(status_code=400, detail="Unable to parse. Invalid YAML")
    except pydantic.ValidationError as e:
        raise HTTPException(status_code=400, detail=str(e))

    auth.authorize_request(
        request,
        f"{namespace_name}/{specification.name}",
        permissions,
        require=True,
    )

    try:
        build_id = api.post_specification(conda_store, specification, namespace_name)
    except ValueError as e:
        raise HTTPException(status_code=400, detail=str(e.args[0]))

    return {"status": "ok", "data": {"build_id": build_id}}
Пример #7
0
    def register_environment(self,
                             specification: dict,
                             namespace: str = None,
                             force_build=False):
        """Register a given specification to conda store with given namespace/name.

        If force_build is True a build will be triggered even if
        specification already exists.

        """
        namespace = namespace or self.default_namespace

        # Create Namespace if namespace if it does not exist
        namespace_model = api.get_namespace(self.db, name=namespace)
        if namespace_model is None:
            namespace = api.create_namespace(self.db, name=namespace)
            self.db.commit()
        else:
            namespace = namespace_model

        specification_model = self.validate_specification(
            conda_store=self,
            namespace=namespace.name,
            specification=schema.CondaSpecification.parse_obj(specification),
        )
        specification_sha256 = utils.datastructure_hash(
            specification_model.dict())

        specification = api.get_specification(self.db,
                                              sha256=specification_sha256)
        if specification is None:
            self.log.info(
                f"specification name={specification_model.name} sha256={specification_sha256} registered"
            )
            specification = orm.Specification(specification_model.dict())
            self.db.add(specification)
            self.db.commit()
        else:
            self.log.debug(
                f"specification name={specification_model.name} sha256={specification_sha256} already registered"
            )
            if not force_build:
                return

        # Create Environment if specification of given namespace/name
        # does not exist yet
        environment = api.get_environment(self.db,
                                          namespace_id=namespace.id,
                                          name=specification.name)
        environment_was_empty = environment is None
        if environment_was_empty:
            environment = orm.Environment(
                name=specification.name,
                namespace_id=namespace.id,
            )
            self.db.add(environment)
            self.db.commit()

        build = self.create_build(environment.id, specification.sha256)

        if environment_was_empty:
            environment.current_build = build
            self.db.commit()

        return build.id
Пример #8
0
 def ensure_namespace(self):
     """Ensure that conda-store default namespaces exists"""
     namespace = api.get_namespace(self.db, name=self.default_namespace)
     if namespace is None:
         api.create_namespace(self.db, name=self.default_namespace)