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
|
|
|
|
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
|
|
|
|
|
2022-02-07 17:11:43 +00:00
|
|
|
from tutor import config as tutor_config
|
|
|
|
from tutor import env as tutor_env
|
|
|
|
from tutor import exceptions, hooks, images
|
|
|
|
from tutor.commands.context import Context
|
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
|
|
|
|
2021-10-25 17:32:13 +00:00
|
|
|
BASE_IMAGE_NAMES = ["openedx", "permissions"]
|
2020-09-17 10:53:14 +00:00
|
|
|
VENDOR_IMAGES = [
|
|
|
|
"caddy",
|
|
|
|
"elasticsearch",
|
|
|
|
"mongodb",
|
|
|
|
"mysql",
|
|
|
|
"redis",
|
|
|
|
"smtp",
|
|
|
|
]
|
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(
|
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_images: list[tuple[str, tuple[str, ...], str, tuple[str, ...]]],
|
2022-02-07 17:11:43 +00:00
|
|
|
config: Config,
|
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
|
|
|
) -> list[tuple[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`.
|
|
|
|
"""
|
|
|
|
for image in BASE_IMAGE_NAMES:
|
|
|
|
tag = images.get_tag(config, image)
|
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
|
|
|
build_images.append((image, ("build", image), tag, ()))
|
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`.
|
|
|
|
"""
|
|
|
|
for image in VENDOR_IMAGES:
|
|
|
|
if config.get(f"RUN_{image.upper()}", True):
|
|
|
|
remote_images.append((image, images.get_tag(config, image)))
|
|
|
|
for image in BASE_IMAGE_NAMES:
|
|
|
|
remote_images.append((image, images.get_tag(config, image)))
|
|
|
|
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`.
|
|
|
|
"""
|
|
|
|
for image in BASE_IMAGE_NAMES:
|
|
|
|
remote_images.append((image, images.get_tag(config, image)))
|
|
|
|
return remote_images
|
|
|
|
|
|
|
|
|
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
|
|
|
|
2019-01-22 20:25:04 +00:00
|
|
|
@click.command(
|
|
|
|
short_help="Build docker images",
|
2019-05-05 09:45:24 +00:00
|
|
|
help="Build the docker images necessary for an Open edX platform.",
|
2019-02-13 19:18:47 +00:00
|
|
|
)
|
2020-10-01 22:25:03 +00:00
|
|
|
@click.argument("image_names", metavar="image", nargs=-1)
|
2019-05-22 17:17:54 +00:00
|
|
|
@click.option(
|
|
|
|
"--no-cache", is_flag=True, help="Do not use cache when building the image"
|
|
|
|
)
|
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,
|
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:
|
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]
|
2021-09-27 08:35:48 +00:00
|
|
|
if docker_args:
|
|
|
|
command_args += docker_args
|
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, custom_args in find_images_to_build(config, image):
|
|
|
|
images.build(
|
|
|
|
tutor_env.pathjoin(context.root, *path),
|
|
|
|
tag,
|
|
|
|
*command_args,
|
|
|
|
*custom_args,
|
|
|
|
)
|
2019-10-22 14:13:50 +00:00
|
|
|
|
2019-07-02 20:16:44 +00:00
|
|
|
|
2019-03-18 21:39:35 +00:00
|
|
|
@click.command(short_help="Pull images from the Docker registry")
|
2020-10-01 22:25:03 +00:00
|
|
|
@click.argument("image_names", metavar="image", 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")
|
2020-10-01 22:25:03 +00:00
|
|
|
@click.argument("image_names", metavar="image", 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")
|
|
|
|
@click.argument("image_names", metavar="image", nargs=-1)
|
|
|
|
@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
|
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
|
|
|
) -> t.Iterator[tuple[str, tuple[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):
|
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)
|
|
|
|
yield (name, 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)
|