refactor: annotation with __future__.annotations
Adds `from __future__ import annotations` to the top of every module,
right below the module's docstring. Replaces any usages of t.List,
t.Dict, t.Set, t.Tuple, and t.Type with their built-in equivalents:
list, dict, set, tuple, and type. Ensures that make test still passes
under Python 3.7, 3.8 and 3.9.
2023-01-17 18:57:23 +00:00
|
|
|
from __future__ import annotations
|
2023-01-06 18:02:17 +00:00
|
|
|
|
2023-04-27 18:25:20 +00:00
|
|
|
import os
|
2022-02-07 17:11:43 +00:00
|
|
|
import typing as t
|
2021-02-25 08:09:14 +00:00
|
|
|
|
2019-01-22 20:25:04 +00:00
|
|
|
import click
|
|
|
|
|
2023-05-04 12:06:28 +00:00
|
|
|
from tutor import bindmount
|
2022-02-07 17:11:43 +00:00
|
|
|
from tutor import config as tutor_config
|
|
|
|
from tutor import env as tutor_env
|
2023-05-04 12:06:28 +00:00
|
|
|
from tutor import exceptions, fmt, hooks, images, utils
|
2022-02-07 17:11:43 +00:00
|
|
|
from tutor.commands.context import Context
|
2023-05-04 12:06:28 +00:00
|
|
|
from tutor.commands.params import ConfigLoaderParam
|
2023-01-06 18:02:17 +00:00
|
|
|
from tutor.core.hooks import Filter
|
2022-02-07 17:11:43 +00:00
|
|
|
from tutor.types import Config
|
2019-01-22 20:25:04 +00:00
|
|
|
|
2023-05-02 08:03:26 +00:00
|
|
|
BASE_IMAGE_NAMES = [
|
|
|
|
("openedx", "DOCKER_IMAGE_OPENEDX"),
|
|
|
|
("permissions", "DOCKER_IMAGE_PERMISSIONS"),
|
2020-09-17 10:53:14 +00:00
|
|
|
]
|
2019-07-03 14:09:33 +00:00
|
|
|
|
2019-04-23 07:57:55 +00:00
|
|
|
|
2022-02-07 17:11:43 +00:00
|
|
|
@hooks.Filters.IMAGES_BUILD.add()
|
|
|
|
def _add_core_images_to_build(
|
2023-05-02 08:49:19 +00:00
|
|
|
build_images: list[tuple[str, t.Union[str, tuple[str, ...]], str, tuple[str, ...]]],
|
2022-02-07 17:11:43 +00:00
|
|
|
config: Config,
|
2023-05-02 08:49:19 +00:00
|
|
|
) -> list[tuple[str, t.Union[str, tuple[str, ...]], str, tuple[str, ...]]]:
|
2022-02-07 17:11:43 +00:00
|
|
|
"""
|
|
|
|
Add base images to the list of Docker images to build on `tutor build all`.
|
|
|
|
"""
|
2023-05-02 08:03:26 +00:00
|
|
|
for image, tag in BASE_IMAGE_NAMES:
|
|
|
|
build_images.append(
|
2023-05-02 08:49:19 +00:00
|
|
|
(
|
|
|
|
image,
|
|
|
|
os.path.join("build", image),
|
|
|
|
tutor_config.get_typed(config, tag, str),
|
|
|
|
(),
|
|
|
|
)
|
2023-05-02 08:03:26 +00:00
|
|
|
)
|
2023-04-28 19:14:22 +00:00
|
|
|
|
|
|
|
# Build openedx-dev image
|
|
|
|
build_images.append(
|
|
|
|
(
|
|
|
|
"openedx-dev",
|
2023-05-02 08:49:19 +00:00
|
|
|
os.path.join("build", "openedx"),
|
2023-05-02 08:03:26 +00:00
|
|
|
tutor_config.get_typed(config, "DOCKER_IMAGE_OPENEDX_DEV", str),
|
2023-04-28 19:14:22 +00:00
|
|
|
(
|
|
|
|
"--target=development",
|
|
|
|
f"--build-arg=APP_USER_ID={utils.get_user_id() or 1000}",
|
|
|
|
),
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
2022-02-07 17:11:43 +00:00
|
|
|
return build_images
|
|
|
|
|
|
|
|
|
|
|
|
@hooks.Filters.IMAGES_PULL.add()
|
|
|
|
def _add_images_to_pull(
|
refactor: annotation with __future__.annotations
Adds `from __future__ import annotations` to the top of every module,
right below the module's docstring. Replaces any usages of t.List,
t.Dict, t.Set, t.Tuple, and t.Type with their built-in equivalents:
list, dict, set, tuple, and type. Ensures that make test still passes
under Python 3.7, 3.8 and 3.9.
2023-01-17 18:57:23 +00:00
|
|
|
remote_images: list[tuple[str, str]], config: Config
|
|
|
|
) -> list[tuple[str, str]]:
|
2022-02-07 17:11:43 +00:00
|
|
|
"""
|
|
|
|
Add base and vendor images to the list of Docker images to pull on `tutor pull all`.
|
|
|
|
"""
|
2023-05-02 08:03:26 +00:00
|
|
|
vendor_images = [
|
|
|
|
("caddy", "DOCKER_IMAGE_CADDY"),
|
|
|
|
("elasticsearch", "DOCKER_IMAGE_ELASTICSEARCH"),
|
|
|
|
("mongodb", "DOCKER_IMAGE_MONGODB"),
|
|
|
|
("mysql", "DOCKER_IMAGE_MYSQL"),
|
|
|
|
("redis", "DOCKER_IMAGE_REDIS"),
|
|
|
|
("smtp", "DOCKER_IMAGE_SMTP"),
|
|
|
|
]
|
|
|
|
for image, tag_name in vendor_images:
|
2022-02-07 17:11:43 +00:00
|
|
|
if config.get(f"RUN_{image.upper()}", True):
|
2023-05-02 08:03:26 +00:00
|
|
|
remote_images.append((image, tutor_config.get_typed(config, tag_name, str)))
|
|
|
|
for image, tag in BASE_IMAGE_NAMES:
|
|
|
|
remote_images.append((image, tutor_config.get_typed(config, tag, str)))
|
2022-02-07 17:11:43 +00:00
|
|
|
return remote_images
|
|
|
|
|
|
|
|
|
2022-04-15 15:48:13 +00:00
|
|
|
@hooks.Filters.IMAGES_PUSH.add()
|
2022-02-07 17:11:43 +00:00
|
|
|
def _add_core_images_to_push(
|
refactor: annotation with __future__.annotations
Adds `from __future__ import annotations` to the top of every module,
right below the module's docstring. Replaces any usages of t.List,
t.Dict, t.Set, t.Tuple, and t.Type with their built-in equivalents:
list, dict, set, tuple, and type. Ensures that make test still passes
under Python 3.7, 3.8 and 3.9.
2023-01-17 18:57:23 +00:00
|
|
|
remote_images: list[tuple[str, str]], config: Config
|
|
|
|
) -> list[tuple[str, str]]:
|
2022-02-07 17:11:43 +00:00
|
|
|
"""
|
|
|
|
Add base images to the list of Docker images to push on `tutor push all`.
|
|
|
|
"""
|
2023-05-02 08:03:26 +00:00
|
|
|
for image, tag in BASE_IMAGE_NAMES:
|
|
|
|
remote_images.append((image, tutor_config.get_typed(config, tag, str)))
|
2022-02-07 17:11:43 +00:00
|
|
|
return remote_images
|
|
|
|
|
|
|
|
|
2023-05-04 12:06:28 +00:00
|
|
|
class ImageNameParam(ConfigLoaderParam):
|
2023-05-04 09:39:02 +00:00
|
|
|
"""
|
|
|
|
Convenient auto-completion of image names.
|
|
|
|
"""
|
|
|
|
|
|
|
|
def shell_complete(
|
|
|
|
self, ctx: click.Context, param: click.Parameter, incomplete: str
|
|
|
|
) -> list[click.shell_completion.CompletionItem]:
|
|
|
|
results = []
|
2023-05-04 12:06:28 +00:00
|
|
|
for name in self.iter_image_names():
|
2023-05-04 09:39:02 +00:00
|
|
|
if name.startswith(incomplete):
|
|
|
|
results.append(click.shell_completion.CompletionItem(name))
|
|
|
|
return results
|
|
|
|
|
2023-05-04 12:06:28 +00:00
|
|
|
def iter_image_names(self) -> t.Iterable["str"]:
|
2023-05-04 09:39:02 +00:00
|
|
|
raise NotImplementedError
|
|
|
|
|
|
|
|
|
|
|
|
class BuildImageNameParam(ImageNameParam):
|
2023-05-04 12:06:28 +00:00
|
|
|
def iter_image_names(self) -> t.Iterable["str"]:
|
|
|
|
for name, _path, _tag, _args in hooks.Filters.IMAGES_BUILD.iterate(self.config):
|
2023-05-04 09:39:02 +00:00
|
|
|
yield name
|
|
|
|
|
|
|
|
|
|
|
|
class PullImageNameParam(ImageNameParam):
|
2023-05-04 12:06:28 +00:00
|
|
|
def iter_image_names(self) -> t.Iterable["str"]:
|
|
|
|
for name, _tag in hooks.Filters.IMAGES_PULL.iterate(self.config):
|
2023-05-04 09:39:02 +00:00
|
|
|
yield name
|
|
|
|
|
|
|
|
|
|
|
|
class PushImageNameParam(ImageNameParam):
|
2023-05-04 12:06:28 +00:00
|
|
|
def iter_image_names(self) -> t.Iterable["str"]:
|
|
|
|
for name, _tag in hooks.Filters.IMAGES_PUSH.iterate(self.config):
|
2023-05-04 09:39:02 +00:00
|
|
|
yield name
|
|
|
|
|
|
|
|
|
2019-05-29 08:30:30 +00:00
|
|
|
@click.group(name="images", short_help="Manage docker images")
|
2021-02-25 08:09:14 +00:00
|
|
|
def images_command() -> None:
|
2019-01-22 20:25:04 +00:00
|
|
|
pass
|
|
|
|
|
2019-04-23 07:57:55 +00:00
|
|
|
|
2023-04-27 14:55:42 +00:00
|
|
|
@click.command()
|
2023-05-04 09:39:02 +00:00
|
|
|
@click.argument(
|
|
|
|
"image_names",
|
|
|
|
metavar="image",
|
|
|
|
nargs=-1,
|
|
|
|
type=BuildImageNameParam(),
|
|
|
|
)
|
2019-05-22 17:17:54 +00:00
|
|
|
@click.option(
|
|
|
|
"--no-cache", is_flag=True, help="Do not use cache when building the image"
|
|
|
|
)
|
2023-04-27 14:55:42 +00:00
|
|
|
@click.option(
|
|
|
|
"--no-registry-cache",
|
|
|
|
is_flag=True,
|
|
|
|
help="Do not use registry cache when building the image",
|
|
|
|
)
|
|
|
|
@click.option(
|
|
|
|
"--cache-to-registry",
|
|
|
|
is_flag=True,
|
|
|
|
help="Push the build cache to the remote registry. You should only enable this option if you have push rights to the remote registry.",
|
|
|
|
)
|
2023-04-03 18:44:15 +00:00
|
|
|
@click.option(
|
|
|
|
"--output",
|
|
|
|
"docker_output",
|
|
|
|
# Export image to docker. This is necessary to make the image available to docker-compose.
|
|
|
|
# The `--load` option is a shorthand for `--output=type=docker`.
|
|
|
|
default="type=docker",
|
2023-10-27 08:54:23 +00:00
|
|
|
help="Same as `docker build --output=...`.",
|
2023-04-03 18:44:15 +00:00
|
|
|
)
|
2019-01-22 20:25:04 +00:00
|
|
|
@click.option(
|
2019-05-05 09:45:24 +00:00
|
|
|
"-a",
|
|
|
|
"--build-arg",
|
2020-09-04 10:35:44 +00:00
|
|
|
"build_args",
|
2019-05-05 09:45:24 +00:00
|
|
|
multiple=True,
|
|
|
|
help="Set build-time docker ARGS in the form 'myarg=value'. This option may be specified multiple times.",
|
2019-01-22 20:25:04 +00:00
|
|
|
)
|
2020-09-04 10:35:44 +00:00
|
|
|
@click.option(
|
|
|
|
"--add-host",
|
|
|
|
"add_hosts",
|
|
|
|
multiple=True,
|
|
|
|
help="Set a custom host-to-IP mapping (host:ip).",
|
|
|
|
)
|
2021-01-19 07:48:21 +00:00
|
|
|
@click.option(
|
|
|
|
"--target",
|
|
|
|
help="Set the target build stage to build.",
|
|
|
|
)
|
2021-09-27 08:35:48 +00:00
|
|
|
@click.option(
|
|
|
|
"-d",
|
|
|
|
"--docker-arg",
|
|
|
|
"docker_args",
|
|
|
|
multiple=True,
|
|
|
|
help="Set extra options for docker build command.",
|
|
|
|
)
|
2019-12-12 16:05:56 +00:00
|
|
|
@click.pass_obj
|
2021-02-25 08:09:14 +00:00
|
|
|
def build(
|
|
|
|
context: Context,
|
refactor: annotation with __future__.annotations
Adds `from __future__ import annotations` to the top of every module,
right below the module's docstring. Replaces any usages of t.List,
t.Dict, t.Set, t.Tuple, and t.Type with their built-in equivalents:
list, dict, set, tuple, and type. Ensures that make test still passes
under Python 3.7, 3.8 and 3.9.
2023-01-17 18:57:23 +00:00
|
|
|
image_names: list[str],
|
2021-02-25 08:09:14 +00:00
|
|
|
no_cache: bool,
|
2023-04-27 14:55:42 +00:00
|
|
|
no_registry_cache: bool,
|
|
|
|
cache_to_registry: bool,
|
2023-04-03 18:44:15 +00:00
|
|
|
docker_output: str,
|
refactor: annotation with __future__.annotations
Adds `from __future__ import annotations` to the top of every module,
right below the module's docstring. Replaces any usages of t.List,
t.Dict, t.Set, t.Tuple, and t.Type with their built-in equivalents:
list, dict, set, tuple, and type. Ensures that make test still passes
under Python 3.7, 3.8 and 3.9.
2023-01-17 18:57:23 +00:00
|
|
|
build_args: list[str],
|
|
|
|
add_hosts: list[str],
|
2021-02-25 08:09:14 +00:00
|
|
|
target: str,
|
refactor: annotation with __future__.annotations
Adds `from __future__ import annotations` to the top of every module,
right below the module's docstring. Replaces any usages of t.List,
t.Dict, t.Set, t.Tuple, and t.Type with their built-in equivalents:
list, dict, set, tuple, and type. Ensures that make test still passes
under Python 3.7, 3.8 and 3.9.
2023-01-17 18:57:23 +00:00
|
|
|
docker_args: list[str],
|
2021-02-25 08:09:14 +00:00
|
|
|
) -> None:
|
2023-04-27 14:55:42 +00:00
|
|
|
"""
|
|
|
|
Build docker images
|
|
|
|
|
|
|
|
Build the docker images necessary for an Open edX platform. By default, the remote
|
|
|
|
registry cache will be used for better performance.
|
|
|
|
"""
|
2019-12-12 16:05:56 +00:00
|
|
|
config = tutor_config.load(context.root)
|
2020-09-04 10:35:44 +00:00
|
|
|
command_args = []
|
|
|
|
if no_cache:
|
|
|
|
command_args.append("--no-cache")
|
|
|
|
for build_arg in build_args:
|
|
|
|
command_args += ["--build-arg", build_arg]
|
|
|
|
for add_host in add_hosts:
|
|
|
|
command_args += ["--add-host", add_host]
|
2021-01-19 07:48:21 +00:00
|
|
|
if target:
|
|
|
|
command_args += ["--target", target]
|
2023-10-27 08:54:23 +00:00
|
|
|
if docker_output:
|
2023-04-03 18:44:15 +00:00
|
|
|
command_args.append(f"--output={docker_output}")
|
2021-09-27 08:35:48 +00:00
|
|
|
if docker_args:
|
|
|
|
command_args += docker_args
|
2023-04-27 18:25:20 +00:00
|
|
|
# Build context mounts
|
|
|
|
build_contexts = get_image_build_contexts(config)
|
|
|
|
|
2020-10-01 22:25:03 +00:00
|
|
|
for image in image_names:
|
2023-04-27 18:25:20 +00:00
|
|
|
for name, path, tag, custom_args in find_images_to_build(config, image):
|
2023-04-27 14:55:42 +00:00
|
|
|
image_build_args = [*command_args, *custom_args]
|
2023-04-27 18:25:20 +00:00
|
|
|
|
|
|
|
# Registry cache
|
2023-10-27 08:54:23 +00:00
|
|
|
if not no_registry_cache:
|
|
|
|
image_build_args.append(f"--cache-from=type=registry,ref={tag}-cache")
|
|
|
|
if cache_to_registry:
|
|
|
|
image_build_args.append(
|
|
|
|
f"--cache-to=type=registry,mode=max,ref={tag}-cache"
|
|
|
|
)
|
2023-04-27 18:25:20 +00:00
|
|
|
|
|
|
|
# Build contexts
|
|
|
|
for host_path, stage_name in build_contexts.get(name, []):
|
2023-10-27 08:54:23 +00:00
|
|
|
fmt.echo_info(
|
|
|
|
f"Adding {host_path} to the build context '{stage_name}' of image '{image}'"
|
|
|
|
)
|
|
|
|
image_build_args.append(f"--build-context={stage_name}={host_path}")
|
2023-04-27 18:25:20 +00:00
|
|
|
|
|
|
|
# Build
|
2022-02-07 17:11:43 +00:00
|
|
|
images.build(
|
2023-05-02 08:49:19 +00:00
|
|
|
tutor_env.pathjoin(context.root, path),
|
2022-02-07 17:11:43 +00:00
|
|
|
tag,
|
2023-04-27 14:55:42 +00:00
|
|
|
*image_build_args,
|
2022-02-07 17:11:43 +00:00
|
|
|
)
|
2019-10-22 14:13:50 +00:00
|
|
|
|
2019-07-02 20:16:44 +00:00
|
|
|
|
2023-04-27 18:25:20 +00:00
|
|
|
def get_image_build_contexts(config: Config) -> dict[str, list[tuple[str, str]]]:
|
|
|
|
"""
|
|
|
|
Return all build contexts for all images.
|
|
|
|
|
|
|
|
A build context is to bind-mount a host directory at build-time. This is useful, for
|
|
|
|
instance to build a Docker image with a local git checkout of a remote repo.
|
|
|
|
|
|
|
|
Users configure bind-mounts with the `MOUNTS` config setting. Plugins can then
|
2023-05-04 09:39:02 +00:00
|
|
|
automatically add build contexts based on these values.
|
2023-04-27 18:25:20 +00:00
|
|
|
"""
|
|
|
|
build_contexts: dict[str, list[tuple[str, str]]] = {}
|
2023-05-04 12:06:28 +00:00
|
|
|
for user_mount in bindmount.get_mounts(config):
|
2023-04-27 18:25:20 +00:00
|
|
|
for image_name, stage_name in hooks.Filters.IMAGES_BUILD_MOUNTS.iterate(
|
|
|
|
user_mount
|
|
|
|
):
|
|
|
|
if image_name not in build_contexts:
|
|
|
|
build_contexts[image_name] = []
|
|
|
|
build_contexts[image_name].append((user_mount, stage_name))
|
|
|
|
return build_contexts
|
|
|
|
|
|
|
|
|
|
|
|
@hooks.Filters.IMAGES_BUILD_MOUNTS.add()
|
|
|
|
def _mount_edx_platform(
|
|
|
|
volumes: list[tuple[str, str]], path: str
|
|
|
|
) -> list[tuple[str, str]]:
|
|
|
|
"""
|
|
|
|
Automatically add an edx-platform repo from the host to the build context whenever
|
|
|
|
it is added to the `MOUNTS` setting.
|
|
|
|
"""
|
|
|
|
if os.path.basename(path) == "edx-platform":
|
|
|
|
volumes.append(("openedx", "edx-platform"))
|
2023-04-28 19:14:22 +00:00
|
|
|
volumes.append(("openedx-dev", "edx-platform"))
|
2023-04-27 18:25:20 +00:00
|
|
|
return volumes
|
|
|
|
|
|
|
|
|
2019-03-18 21:39:35 +00:00
|
|
|
@click.command(short_help="Pull images from the Docker registry")
|
2023-05-04 09:39:02 +00:00
|
|
|
@click.argument("image_names", metavar="image", type=PullImageNameParam(), nargs=-1)
|
2019-12-12 16:05:56 +00:00
|
|
|
@click.pass_obj
|
refactor: annotation with __future__.annotations
Adds `from __future__ import annotations` to the top of every module,
right below the module's docstring. Replaces any usages of t.List,
t.Dict, t.Set, t.Tuple, and t.Type with their built-in equivalents:
list, dict, set, tuple, and type. Ensures that make test still passes
under Python 3.7, 3.8 and 3.9.
2023-01-17 18:57:23 +00:00
|
|
|
def pull(context: Context, image_names: list[str]) -> None:
|
2021-11-23 08:25:09 +00:00
|
|
|
config = tutor_config.load_full(context.root)
|
2020-10-01 22:25:03 +00:00
|
|
|
for image in image_names:
|
2022-02-07 17:11:43 +00:00
|
|
|
for tag in find_remote_image_tags(config, hooks.Filters.IMAGES_PULL, image):
|
|
|
|
images.pull(tag)
|
2019-10-22 14:13:50 +00:00
|
|
|
|
|
|
|
|
2019-03-18 20:53:18 +00:00
|
|
|
@click.command(short_help="Push images to the Docker registry")
|
2023-05-04 09:39:02 +00:00
|
|
|
@click.argument("image_names", metavar="image", type=PushImageNameParam(), nargs=-1)
|
2019-12-12 16:05:56 +00:00
|
|
|
@click.pass_obj
|
refactor: annotation with __future__.annotations
Adds `from __future__ import annotations` to the top of every module,
right below the module's docstring. Replaces any usages of t.List,
t.Dict, t.Set, t.Tuple, and t.Type with their built-in equivalents:
list, dict, set, tuple, and type. Ensures that make test still passes
under Python 3.7, 3.8 and 3.9.
2023-01-17 18:57:23 +00:00
|
|
|
def push(context: Context, image_names: list[str]) -> None:
|
2021-11-23 08:25:09 +00:00
|
|
|
config = tutor_config.load_full(context.root)
|
2020-10-01 22:25:03 +00:00
|
|
|
for image in image_names:
|
2022-02-07 17:11:43 +00:00
|
|
|
for tag in find_remote_image_tags(config, hooks.Filters.IMAGES_PUSH, image):
|
|
|
|
images.push(tag)
|
2020-10-01 22:25:03 +00:00
|
|
|
|
|
|
|
|
|
|
|
@click.command(short_help="Print tag associated to a Docker image")
|
2023-05-04 09:39:02 +00:00
|
|
|
@click.argument("image_names", metavar="image", type=BuildImageNameParam(), nargs=-1)
|
2020-10-01 22:25:03 +00:00
|
|
|
@click.pass_obj
|
refactor: annotation with __future__.annotations
Adds `from __future__ import annotations` to the top of every module,
right below the module's docstring. Replaces any usages of t.List,
t.Dict, t.Set, t.Tuple, and t.Type with their built-in equivalents:
list, dict, set, tuple, and type. Ensures that make test still passes
under Python 3.7, 3.8 and 3.9.
2023-01-17 18:57:23 +00:00
|
|
|
def printtag(context: Context, image_names: list[str]) -> None:
|
2021-11-23 08:25:09 +00:00
|
|
|
config = tutor_config.load_full(context.root)
|
2020-10-01 22:25:03 +00:00
|
|
|
for image in image_names:
|
2022-02-07 17:11:43 +00:00
|
|
|
for _name, _path, tag, _args in find_images_to_build(config, image):
|
2020-10-01 22:25:03 +00:00
|
|
|
print(tag)
|
2019-10-24 19:34:14 +00:00
|
|
|
|
|
|
|
|
2022-02-07 17:11:43 +00:00
|
|
|
def find_images_to_build(
|
|
|
|
config: Config, image: str
|
2023-05-02 08:49:19 +00:00
|
|
|
) -> t.Iterator[tuple[str, str, str, tuple[str, ...]]]:
|
2022-02-07 17:11:43 +00:00
|
|
|
"""
|
|
|
|
Iterate over all images to build.
|
2020-10-01 22:14:06 +00:00
|
|
|
|
2022-02-07 17:11:43 +00:00
|
|
|
If no corresponding image is found, raise exception.
|
2020-10-01 22:14:06 +00:00
|
|
|
|
2022-02-07 17:11:43 +00:00
|
|
|
Yield: (name, path, tag, build args)
|
|
|
|
"""
|
|
|
|
found = False
|
feat: strongly typed hooks
Now that the mypy bugs have been resolved, we are able to define more precisely
and cleanly the types of Actions and Filters.
Moreover, can now strongly type named actions and hooks (in consts.py). With
such a strong typing, we get early alerts of hooks called with incorrect
arguments, which is nothing short of awesome :)
This change breaks the hooks API by removing the `context=...` argument. The
reason for that is that we cannot insert arbitrary arguments between `P.args,
P.kwargs`: https://peps.python.org/pep-0612/#the-components-of-a-paramspec
> A function declared as def inner(a: A, b: B, *args: P.args, **kwargs:
> P.kwargs) -> R has type Callable[Concatenate[A, B, P], R]. Placing
> keyword-only parameters between the *args and **kwargs is forbidden.
Getting the documentation to build in nitpicky mode is quite difficult... We
need to add `nitpick_ignore` to the docs conf.py, otherwise sphinx complains
about many missing class references. This, despite upgrading almost all doc
requirements (except docutils).
2022-10-06 10:05:01 +00:00
|
|
|
for name, path, tag, args in hooks.Filters.IMAGES_BUILD.iterate(config):
|
2023-05-02 08:49:19 +00:00
|
|
|
relative_path = path if isinstance(path, str) else os.path.join(*path)
|
2022-04-15 08:51:19 +00:00
|
|
|
if image in [name, "all"]:
|
2022-02-07 17:11:43 +00:00
|
|
|
found = True
|
|
|
|
tag = tutor_env.render_str(config, tag)
|
2023-05-02 08:49:19 +00:00
|
|
|
yield (name, relative_path, tag, args)
|
2021-11-25 08:53:49 +00:00
|
|
|
|
2022-02-07 17:11:43 +00:00
|
|
|
if not found:
|
2021-11-25 08:53:49 +00:00
|
|
|
raise ImageNotFoundError(image)
|
|
|
|
|
2020-10-01 22:14:06 +00:00
|
|
|
|
2022-02-07 17:11:43 +00:00
|
|
|
def find_remote_image_tags(
|
feat: strongly typed hooks
Now that the mypy bugs have been resolved, we are able to define more precisely
and cleanly the types of Actions and Filters.
Moreover, can now strongly type named actions and hooks (in consts.py). With
such a strong typing, we get early alerts of hooks called with incorrect
arguments, which is nothing short of awesome :)
This change breaks the hooks API by removing the `context=...` argument. The
reason for that is that we cannot insert arbitrary arguments between `P.args,
P.kwargs`: https://peps.python.org/pep-0612/#the-components-of-a-paramspec
> A function declared as def inner(a: A, b: B, *args: P.args, **kwargs:
> P.kwargs) -> R has type Callable[Concatenate[A, B, P], R]. Placing
> keyword-only parameters between the *args and **kwargs is forbidden.
Getting the documentation to build in nitpicky mode is quite difficult... We
need to add `nitpick_ignore` to the docs conf.py, otherwise sphinx complains
about many missing class references. This, despite upgrading almost all doc
requirements (except docutils).
2022-10-06 10:05:01 +00:00
|
|
|
config: Config,
|
2023-01-06 18:02:17 +00:00
|
|
|
filtre: Filter[list[tuple[str, str]], [Config]],
|
feat: strongly typed hooks
Now that the mypy bugs have been resolved, we are able to define more precisely
and cleanly the types of Actions and Filters.
Moreover, can now strongly type named actions and hooks (in consts.py). With
such a strong typing, we get early alerts of hooks called with incorrect
arguments, which is nothing short of awesome :)
This change breaks the hooks API by removing the `context=...` argument. The
reason for that is that we cannot insert arbitrary arguments between `P.args,
P.kwargs`: https://peps.python.org/pep-0612/#the-components-of-a-paramspec
> A function declared as def inner(a: A, b: B, *args: P.args, **kwargs:
> P.kwargs) -> R has type Callable[Concatenate[A, B, P], R]. Placing
> keyword-only parameters between the *args and **kwargs is forbidden.
Getting the documentation to build in nitpicky mode is quite difficult... We
need to add `nitpick_ignore` to the docs conf.py, otherwise sphinx complains
about many missing class references. This, despite upgrading almost all doc
requirements (except docutils).
2022-10-06 10:05:01 +00:00
|
|
|
image: str,
|
2022-02-07 17:11:43 +00:00
|
|
|
) -> t.Iterator[str]:
|
|
|
|
"""
|
|
|
|
Iterate over all images to push or pull.
|
2020-10-01 22:14:06 +00:00
|
|
|
|
2022-02-07 17:11:43 +00:00
|
|
|
If no corresponding image is found, raise exception.
|
2021-11-25 08:53:49 +00:00
|
|
|
|
2022-02-07 17:11:43 +00:00
|
|
|
Yield: tag
|
|
|
|
"""
|
feat: strongly typed hooks
Now that the mypy bugs have been resolved, we are able to define more precisely
and cleanly the types of Actions and Filters.
Moreover, can now strongly type named actions and hooks (in consts.py). With
such a strong typing, we get early alerts of hooks called with incorrect
arguments, which is nothing short of awesome :)
This change breaks the hooks API by removing the `context=...` argument. The
reason for that is that we cannot insert arbitrary arguments between `P.args,
P.kwargs`: https://peps.python.org/pep-0612/#the-components-of-a-paramspec
> A function declared as def inner(a: A, b: B, *args: P.args, **kwargs:
> P.kwargs) -> R has type Callable[Concatenate[A, B, P], R]. Placing
> keyword-only parameters between the *args and **kwargs is forbidden.
Getting the documentation to build in nitpicky mode is quite difficult... We
need to add `nitpick_ignore` to the docs conf.py, otherwise sphinx complains
about many missing class references. This, despite upgrading almost all doc
requirements (except docutils).
2022-10-06 10:05:01 +00:00
|
|
|
all_remote_images = filtre.iterate(config)
|
2022-02-07 17:11:43 +00:00
|
|
|
found = False
|
|
|
|
for name, tag in all_remote_images:
|
2022-04-15 08:51:19 +00:00
|
|
|
if image in [name, "all"]:
|
2022-02-07 17:11:43 +00:00
|
|
|
found = True
|
|
|
|
yield tutor_env.render_str(config, tag)
|
|
|
|
if not found:
|
2021-11-25 08:53:49 +00:00
|
|
|
raise ImageNotFoundError(image)
|
|
|
|
|
2019-04-23 07:57:55 +00:00
|
|
|
|
2021-11-25 08:53:49 +00:00
|
|
|
class ImageNotFoundError(exceptions.TutorError):
|
|
|
|
def __init__(self, image_name: str):
|
2022-02-07 17:11:43 +00:00
|
|
|
super().__init__(f"Image '{image_name}' could not be found")
|
2021-11-25 08:53:49 +00:00
|
|
|
|
|
|
|
|
2019-04-23 07:57:55 +00:00
|
|
|
images_command.add_command(build)
|
|
|
|
images_command.add_command(pull)
|
|
|
|
images_command.add_command(push)
|
2020-10-01 22:25:03 +00:00
|
|
|
images_command.add_command(printtag)
|