Skip to content

Commit

Permalink
Initial work on adding traitlets
Browse files Browse the repository at this point in the history
  • Loading branch information
costrouc committed Jul 14, 2021
1 parent f3d959f commit faa3e83
Show file tree
Hide file tree
Showing 16 changed files with 302 additions and 294 deletions.
10 changes: 0 additions & 10 deletions conda-store-server/conda_store_server/__main__.py

This file was deleted.

89 changes: 65 additions & 24 deletions conda-store-server/conda_store_server/app.py
Original file line number Diff line number Diff line change
@@ -1,44 +1,85 @@
import os
import pathlib
import datetime
import logging
import shutil

import yaml
from traitlets import Type, Unicode, Integer
from traitlets.config import LoggingConfigurable

from conda_store_server import orm, utils, storage, schema

logger = logging.getLogger(__name__)

class CondaStore(LoggingConfigurable):
storage_backend_class = Type(
default_value=storage.S3Storage,
klass=storage.Storage,
allow_none=False,
)

store_directory = Unicode(
'conda-store-state',
help="directory for conda-store to build environments and store state",
)

environment_directory = Unicode(
'conda-store-state/envs',
help="directory for symlinking conda environment builds",
)

database_url = Unicode(
os.environ.get(
'CONDA_STORE_DATABASE_URL',
'sqlite:///conda-store.sqlite'),
help="url for the database. e.g. 'sqlite:///conda-store.sqlite'"
)

default_uid = Integer(
os.getuid(),
help="default uid to assign to built environments",
config=True,
)

default_gid = Integer(
os.getgid(),
help="default gid to assign to built environments",
config=True,
)

default_permissions = Unicode(
'775',
help="default file permissions to assign to built environments",
config=True
)

class CondaStore:
def __init__(self, store_directory, database_url=None, storage_backend="s3"):
self.store_directory = pathlib.Path(store_directory).resolve()
if not self.store_directory.is_dir():
logger.info(f"creating directory store_directory={store_directory}")
self.store_directory.mkdir(parents=True)
@property
def session_factory(self):
if hasattr(self, '_session_factory'):
return self._session_factory

self.database_url = database_url or os.environ.get(
"CONDA_STORE_DB_URL",
f'sqlite:///{self.store_directory / "conda_store.sqlite"}',
self._session_factory = orm.new_session_factory(
url=self.database_url
)
return self._session_factory

Session = orm.new_session_factory(url=self.database_url)
self.db = Session()

if storage_backend == schema.StorageBackend.FILESYSTEM:
storage_directory = self.store_directory / "storage"
self.storage = storage.LocalStorage(storage_directory)
elif storage_backend == schema.StorageBackend.S3:
self.storage = storage.S3Storage()
@property
def db(self):
if hasattr(self, '_db'):
return self._db

self.configuration.store_directory = str(self.store_directory)
self._db = self.session_factory()
return self._db

@property
def configuration(self):
return orm.CondaStoreConfiguration.configuration(self.db)

def ensure_directories(self):
os.makedirs(self.store_directory, exist_ok=True)
os.makedirs(self.environment_directory, exist_ok=True)

def update_storage_metrics(self):
self.log.info('updating storage metrics')
configuration = self.configuration
disk_usage = shutil.disk_usage(str(self.store_directory))
configuration.disk_usage = disk_usage.used
Expand All @@ -59,7 +100,7 @@ def update_conda_channels(self, channels=None, update_interval=60 * 60):
+ datetime.timedelta(seconds=update_interval)
< datetime.datetime.now()
):
logger.info(f"packages were updated in the last seconds={update_interval}")
self.log.info(f"packages were updated in the last seconds={update_interval}")
return

for channel in channels:
Expand Down Expand Up @@ -88,18 +129,18 @@ def register_environment(self, specification, namespace="library"):
orm.Specification.sha256 == specification_sha256
)
if query.count() != 0:
logger.debug(
self.log.debug(
f"already registered specification name={specification.name} sha256={specification_sha256}"
)
return

logger.info(
self.log.info(
f"registering specification name={specification.name} sha256={specification_sha256}"
)
specification = orm.Specification(specification.dict())
self.db.add(specification)
self.db.commit()
logger.info(
self.log.info(
f"scheduling specification for build name={specification.name} sha256={specification.sha256}"
)
build = orm.Build(specification_id=specification.id)
Expand Down
12 changes: 6 additions & 6 deletions conda-store-server/conda_store_server/build.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ def package_query(package):


def start_conda_build(conda_store, paths, storage_threshold, poll_interval):
logger.info(f"polling interval set to {poll_interval} seconds")
conda_store.log.info(f"polling interval set to {poll_interval} seconds")
while True:
environments = discover_environments(paths)
for environment in environments:
Expand All @@ -136,9 +136,9 @@ def start_conda_build(conda_store, paths, storage_threshold, poll_interval):

def conda_build(conda_store):
build = claim_build(conda_store)
store_directory = pathlib.Path(conda_store.configuration.store_directory)
store_directory = pathlib.Path(conda_store.store_directory)
environment_directory = pathlib.Path(
conda_store.configuration.environment_directory
conda_store.environment_directory
)
build_path = build.build_path(store_directory)
environment_path = build.environment_path(environment_directory)
Expand Down Expand Up @@ -180,9 +180,9 @@ def conda_build(conda_store):

# modify permissions, uid, gid if they do not match
stat_info = os.stat(build_path)
permissions = conda_store.configuration.default_permissions
uid = conda_store.configuration.default_uid
gid = conda_store.configuration.default_gid
permissions = conda_store.default_permissions
uid = conda_store.default_uid
gid = conda_store.default_gid

if permissions is not None and oct(stat.S_IMODE(stat_info.st_mode))[-3:] != str(
permissions
Expand Down
168 changes: 0 additions & 168 deletions conda-store-server/conda_store_server/cli.py

This file was deleted.

6 changes: 0 additions & 6 deletions conda-store-server/conda_store_server/orm.py
Original file line number Diff line number Diff line change
Expand Up @@ -193,12 +193,6 @@ class CondaStoreConfiguration(Base):
__tablename__ = "conda_store_configuration"

id = Column(Integer, primary_key=True)
store_directory = Column(String)
environment_directory = Column(String)

default_permissions = Column(String, default=None)
default_uid = Column(String, default=None)
default_gid = Column(String, default=None)

last_package_update = Column(DateTime)

Expand Down
Loading

0 comments on commit faa3e83

Please sign in to comment.