2022-10-18 14:57:07 +00:00
|
|
|
from tutor import env
|
|
|
|
from tutor.types import Config
|
2020-03-13 11:09:48 +00:00
|
|
|
|
2019-05-05 09:45:24 +00:00
|
|
|
|
2021-03-13 18:46:44 +00:00
|
|
|
class BaseJobRunner:
|
2021-09-27 09:48:17 +00:00
|
|
|
"""
|
2022-10-18 14:57:07 +00:00
|
|
|
A job runner is responsible for running bash commands in the right context.
|
|
|
|
|
|
|
|
Commands may be loaded from string or template files. The `run_job` method must be
|
|
|
|
implemented by child classes.
|
2021-09-27 09:48:17 +00:00
|
|
|
"""
|
|
|
|
|
2021-04-06 10:09:00 +00:00
|
|
|
def __init__(self, root: str, config: Config):
|
2019-05-21 09:56:49 +00:00
|
|
|
self.root = root
|
|
|
|
self.config = config
|
|
|
|
|
2021-02-25 08:09:14 +00:00
|
|
|
def run_job_from_template(self, service: str, *path: str) -> None:
|
2019-06-05 17:57:30 +00:00
|
|
|
command = self.render(*path)
|
Improve job running in local and k8s
Running jobs was previously done with "exec". This was because it
allowed us to avoid copying too much container specification information
from the docker-compose/deployments files to the jobs files. However,
this was limiting:
- In order to run a job, the corresponding container had to be running.
This was particularly painful in Kubernetes, where containers are
crashing as long as migrations are not correctly run.
- Containers in which we need to run jobs needed to be present in the
docker-compose/deployments files. This is unnecessary, for example when
mysql is disabled, or in the case of the certbot container.
Now, we create dedicated jobs files, both for local and k8s deployment.
This introduces a little redundancy, but not too much. Note that
dependent containers are not listed in the docker-compose.jobs.yml file,
so an actual platform is still supposed to be running when we launch the
jobs.
This also introduces a subtle change: now, jobs go through the container
entrypoint prior to running. This is probably a good thing, as it will
avoid forgetting about incorrect environment variables.
In k8s, we find ourselves interacting way too much with the kubectl
utility. Parsing output from the CLI is a pain. So we need to switch to
the native kubernetes client library.
2020-03-25 17:47:36 +00:00
|
|
|
self.run_job(service, command)
|
2019-05-21 09:56:49 +00:00
|
|
|
|
2022-10-18 14:57:07 +00:00
|
|
|
def run_job_from_str(self, service: str, command: str) -> None:
|
|
|
|
rendered = env.render_str(self.config, command).strip()
|
|
|
|
self.run_job(service, rendered)
|
|
|
|
|
2021-02-25 08:09:14 +00:00
|
|
|
def render(self, *path: str) -> str:
|
|
|
|
rendered = env.render_file(self.config, *path).strip()
|
|
|
|
if isinstance(rendered, bytes):
|
|
|
|
raise TypeError("Cannot load job from binary file")
|
|
|
|
return rendered
|
2019-06-05 17:57:30 +00:00
|
|
|
|
2021-02-25 08:09:14 +00:00
|
|
|
def run_job(self, service: str, command: str) -> int:
|
2021-09-27 09:48:17 +00:00
|
|
|
"""
|
|
|
|
Given a (potentially large) string command, run it with the
|
|
|
|
corresponding service. Implementations will differ depending on the
|
|
|
|
deployment strategy.
|
|
|
|
"""
|
2019-05-21 09:56:49 +00:00
|
|
|
raise NotImplementedError
|
|
|
|
|
|
|
|
|
2021-09-27 09:48:17 +00:00
|
|
|
class BaseComposeJobRunner(BaseJobRunner):
|
|
|
|
def docker_compose(self, *command: str) -> int:
|
|
|
|
raise NotImplementedError
|