Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Separate conda-store app instance from config #1023

Merged
merged 12 commits into from
Jan 9, 2025
12 changes: 6 additions & 6 deletions conda-store-server/conda_store_server/_internal/orm.py
Original file line number Diff line number Diff line change
Expand Up @@ -304,10 +304,10 @@ def build_path(self, conda_store):
else:
namespace = ""

store_directory = os.path.abspath(conda_store.store_directory)
store_directory = os.path.abspath(conda_store.config.store_directory)
res = (
pathlib.Path(
conda_store.build_directory.format(
conda_store.config.build_directory.format(
store_directory=store_directory,
namespace=namespace,
)
Expand All @@ -320,7 +320,7 @@ def build_path(self, conda_store):
raise BuildPathError("build_path too long: must be <= 255 characters")
# Note: cannot use the '/' operator to prepend the extended-length
# prefix
if sys.platform == "win32" and conda_store.win_extended_length_prefix:
if sys.platform == "win32" and conda_store.config.win_extended_length_prefix:
return pathlib.Path(f"\\\\?\\{res}")
else:
return res
Expand All @@ -342,17 +342,17 @@ def environment_path(self, conda_store):
if BuildKey.current_version() >= 3:
return None

store_directory = os.path.abspath(conda_store.store_directory)
store_directory = os.path.abspath(conda_store.config.store_directory)
namespace = self.environment.namespace.name
name = self.specification.name
res = pathlib.Path(
conda_store.environment_directory.format(
conda_store.config.environment_directory.format(
store_directory=store_directory, namespace=namespace, name=name
)
)
# Note: cannot use the '/' operator to prepend the extended-length
# prefix
if sys.platform == "win32" and conda_store.win_extended_length_prefix:
if sys.platform == "win32" and conda_store.config.win_extended_length_prefix:
return pathlib.Path(f"\\\\?\\{res}")
else:
return res
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ def _conda_command(self, conda_store) -> str:
return settings.conda_command

def _conda_flags(self, conda_store) -> str:
return conda_store.conda_flags
return conda_store.config.conda_flags

@utils.run_in_tempdir
def lock_environment(
Expand Down
18 changes: 10 additions & 8 deletions conda-store-server/conda_store_server/_internal/server/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@
from conda_store_server import __version__, storage
from conda_store_server._internal import dbutil, orm
from conda_store_server._internal.server import views
from conda_store_server.app import CondaStore
from conda_store_server.conda_store import CondaStore
from conda_store_server.conda_store_config import CondaStore as CondaStoreConfig
from conda_store_server.server import auth


Expand Down Expand Up @@ -193,21 +194,22 @@ def initialize(self, *args, **kwargs):
super().initialize(*args, **kwargs)
self.load_config_file(self.config_file)

self.conda_store = CondaStore(parent=self, log=self.log)
self.conda_store_config = CondaStoreConfig(parent=self, log=self.log)
self.conda_store = CondaStore(config=self.conda_store_config)

self.conda_store.ensure_directories()
self.log.info(
f"Running conda-store with store directory: {self.conda_store.store_directory}"
f"Running conda-store with store directory: {self.conda_store.config.store_directory}"
)

self.authentication = self.authentication_class(
parent=self,
parent=self.conda_store_config,
log=self.log,
authentication_db=self.conda_store.session_factory,
)

# ensure checks on redis_url
self.conda_store.redis_url
self.conda_store.config.redis_url

def init_fastapi_app(self):
def trim_slash(url):
Expand Down Expand Up @@ -363,7 +365,7 @@ def _check_worker(self, delay=5):
# Creates a new DB connection since this will be run in a separate
# thread and connections cannot be shared between threads
session_factory = orm.new_session_factory(
url=self.conda_store.database_url,
url=self.conda_store.config.database_url,
poolclass=QueuePool,
)

Expand All @@ -388,8 +390,8 @@ def _check_worker(self, delay=5):

def start(self):
"""Start the CondaStoreServer application, and run a FastAPI-based webserver."""
if self.conda_store.upgrade_db:
dbutil.upgrade(self.conda_store.database_url)
if self.conda_store.config.upgrade_db:
dbutil.upgrade(self.conda_store.config.database_url)

with self.conda_store.session_factory() as db:
self.conda_store.ensure_settings(db)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,12 @@
from fastapi import APIRouter, Body, Depends, HTTPException, Query, Request
from fastapi.responses import JSONResponse, PlainTextResponse, RedirectResponse

from conda_store_server import __version__, api, app
from conda_store_server import __version__, api
from conda_store_server._internal import orm, schema
from conda_store_server._internal.environment import filter_environments
from conda_store_server._internal.schema import AuthenticationToken, Permissions
from conda_store_server._internal.server import dependencies
from conda_store_server.conda_store import CondaStore
from conda_store_server.exception import CondaStoreError
from conda_store_server.server.auth import Authentication

Expand Down Expand Up @@ -171,7 +172,7 @@ async def api_get_permissions(
"primary_namespace": (
entity.primary_namespace
if authenticated
else conda_store.default_namespace
else conda_store.config.default_namespace
),
"entity_permissions": entity_binding_permissions,
"entity_roles": entity_binding_roles,
Expand Down Expand Up @@ -227,7 +228,7 @@ async def api_post_token(
entity = schema.AuthenticationToken(
exp=datetime.datetime.now(tz=datetime.timezone.utc)
+ datetime.timedelta(days=1),
primary_namespace=conda_store.default_namespace,
primary_namespace=conda_store.config.default_namespace,
role_bindings={},
)

Expand Down Expand Up @@ -633,7 +634,7 @@ async def api_delete_namespace(
)
async def api_list_environments(
auth: Authentication = Depends(dependencies.get_auth),
conda_store: app.CondaStore = Depends(dependencies.get_conda_store),
conda_store: CondaStore = Depends(dependencies.get_conda_store),
entity: AuthenticationToken = Depends(dependencies.get_entity),
paginated_args: PaginatedArgs = Depends(get_paginated_args),
artifact: Optional[schema.BuildArtifactType] = None,
Expand Down Expand Up @@ -877,7 +878,7 @@ async def api_post_specification(
permissions = {Permissions.ENVIRONMENT_CREATE}

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

namespace_name = namespace or default_namespace
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ async def ui_create_get_environment(
)

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

def sort_namespace(n):
Expand Down
9 changes: 5 additions & 4 deletions conda-store-server/conda_store_server/_internal/worker/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
from traitlets.config import Application, catch_config_error

from conda_store_server import __version__
from conda_store_server.app import CondaStore
from conda_store_server.conda_store import CondaStore
from conda_store_server.conda_store_config import CondaStore as CondaStoreConfig


class CondaStoreWorker(Application):
Expand Down Expand Up @@ -67,10 +68,10 @@ def initialize(self, *args, **kwargs):
super().initialize(*args, **kwargs)
self.load_config_file(self.config_file)

self.conda_store = CondaStore(parent=self, log=self.log)

self.conda_store_config = CondaStoreConfig(parent=self, log=self.log)
self.conda_store = CondaStore(config=self.conda_store_config)
# ensure checks on redis_url
self.conda_store.redis_url
self.conda_store.config.redis_url

def logger_to_celery_logging_level(self, logging_level):
# celery supports the log levels DEBUG | INFO | WARNING | ERROR | CRITICAL | FATAL
Expand Down
14 changes: 7 additions & 7 deletions conda-store-server/conda_store_server/_internal/worker/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ def task_watch_paths(self):
settings = conda_store.get_settings(db)

conda_store.configuration(db).update_storage_metrics(
db, conda_store.store_directory
db, conda_store.config.store_directory
)

environment_paths = environment.discover_environments(self.worker.watch_paths)
Expand All @@ -106,7 +106,7 @@ def task_update_storage_metrics(self):
conda_store = self.worker.conda_store
with conda_store.session_factory() as db:
conda_store.configuration(db).update_storage_metrics(
db, conda_store.store_directory
db, conda_store.config.store_directory
)


Expand Down Expand Up @@ -174,7 +174,7 @@ def task_update_conda_channel(self, channel_name):

is_locked = False

if conda_store.redis_url is not None:
if conda_store.config.redis_url is not None:
lock = conda_store.redis.lock(task_key, timeout=60 * 15) # timeout 15min
else:
lockfile_path = os.path.join(f"/tmp/task_lock_{task_key}")
Expand All @@ -195,7 +195,7 @@ def task_update_conda_channel(self, channel_name):
)

except TimeoutError:
if conda_store.redis_url is None:
if conda_store.config.redis_url is None:
conda_store.log.warning(
f"Timeout when acquiring lock with key {task_key} - We assume the task is already being run"
)
Expand Down Expand Up @@ -269,9 +269,9 @@ def delete_build_artifact(db: Session, conda_store, build_artifact):
# ignore key
conda_prefix = build_artifact.build.build_path(conda_store)
# be REALLY sure this is a directory within store directory
if str(conda_prefix).startswith(conda_store.store_directory) and os.path.isdir(
conda_prefix
):
if str(conda_prefix).startswith(
conda_store.config.store_directory
) and os.path.isdir(conda_prefix):
shutil.rmtree(conda_prefix)
db.delete(build_artifact)
else:
Expand Down
Loading
Loading