Skip to content

Commit

Permalink
Traitlets unions of callables or values
Browse files Browse the repository at this point in the history
  • Loading branch information
costrouc committed Aug 17, 2022
1 parent 6482fc4 commit 1d1bac6
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 12 deletions.
14 changes: 11 additions & 3 deletions conda-store-server/conda_store_server/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
Bool,
validate,
TraitError,
Union,
)
from traitlets.config import LoggingConfigurable
from sqlalchemy.pool import NullPool
Expand Down Expand Up @@ -277,12 +278,19 @@ def _default_celery_results_backend(self):
config=True,
)

default_docker_base_image = Unicode(
"registry-1.docker.io/library/debian:sid-slim",
help="default base image used for the Dockerized environments. Make sure to have a proper glibc within image.",
default_docker_base_image = Union(
[Unicode(), Callable()],
help="default base image used for the Dockerized environments. Make sure to have a proper glibc within image (highly discourage alpine/musl based images). Can also be callable function which takes the `orm.Build` object as input which has access to all attributes about the build such as install packages, requested packages, name, namespace, etc",
config=True,
)

@default("default_docker_base_image")
def _default_docker_base_image(self):
def _docker_base_image(build: orm.Build):
return "registry-1.docker.io/library/debian:sid-slim"

return _docker_base_image

validate_specification = Callable(
conda_store_validate_specification,
help="callable function taking conda_store and specification as input arguments to apply for validating and modifying a given specification. If there are validation issues with the environment ValueError with message should be raised. If changed you may need to call the default function to preseve many of the trait effects e.g. `c.CondaStore.default_channels` etc",
Expand Down
2 changes: 1 addition & 1 deletion conda-store-server/conda_store_server/build.py
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,7 @@ def build_conda_docker(conda_store, build):
precs = precs_from_environment_prefix(conda_prefix, download_dir, user_conda)
records = fetch_precs(download_dir, precs)
base_image = conda_store.container_registry.pull_image(
conda_store.default_docker_base_image
utils.callable_or_value(conda_store.default_docker_base_image, build)
)
image = build_docker_environment_image(
base_image=base_image,
Expand Down
7 changes: 2 additions & 5 deletions conda-store-server/conda_store_server/server/auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
from sqlalchemy import or_, and_
import yarl

from conda_store_server import schema, orm
from conda_store_server import schema, orm, utils
from conda_store_server.server import dependencies


Expand Down Expand Up @@ -574,10 +574,7 @@ def _oauth_callback_url(request: Request):
return _oauth_callback_url

def get_oauth_callback_url(self, request: Request):
if callable(self.oauth_callback_url):
return self.oauth_callback_url(request)
else:
return self.oauth_callback_url
return utils.callable_or_value(self.oauth_callback_url, request)

login_html = Unicode(
"""
Expand Down
6 changes: 6 additions & 0 deletions conda-store-server/conda_store_server/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,3 +72,9 @@ def sort_key(v):
def datastructure_hash(v):
json_blob = json.dumps(recursive_sort(v))
return hashlib.sha256(json_blob.encode("utf-8")).hexdigest()


def callable_or_value(v, *args, **kwargs):
if callable(v):
return v(*args, **kwargs)
return v
10 changes: 7 additions & 3 deletions docs/administration.md
Original file line number Diff line number Diff line change
Expand Up @@ -216,9 +216,13 @@ assign to all files and directories in a given built environment. This
setting is useful if you want to protect environments from
modification from certain users and groups.

`CondaStore.default_docker_base_image` is the base image to use for
docker builds of Conda environments. This package at a minimum should
have the [following packages
`CondaStore.default_docker_base_image` default base image used for the
Dockerized environments. Make sure to have a proper glibc within image
(highly discourage alpine/musl based images). Can also be callable
function which takes the `orm.Build` object as input which has access
to all attributes about the build such as installed packages, requested
packages, name, namespace, etc. This package at a minimum should have
the [following packages
installed](https://docs.anaconda.com/anaconda/install/linux/). Often
times for non-graphic and non-gpu environments glibc is enough. Hence
the default docker image `library/debian:sid-slim`.
Expand Down

0 comments on commit 1d1bac6

Please sign in to comment.