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
|
|
|
|
2022-10-19 15:46:31 +00:00
|
|
|
class BaseTaskRunner:
|
2021-09-27 09:48:17 +00:00
|
|
|
"""
|
2022-10-19 15:46:31 +00:00
|
|
|
A task runner is responsible for running bash commands in the right context.
|
2022-10-18 14:57:07 +00:00
|
|
|
|
2022-10-19 15:46:31 +00:00
|
|
|
Commands may be loaded from string or template files. The `run_task` method must be
|
2022-10-18 14:57:07 +00:00
|
|
|
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
|
|
|
|
|
2022-10-19 15:46:31 +00:00
|
|
|
def run_task_from_template(self, service: str, *path: str) -> None:
|
2019-06-05 17:57:30 +00:00
|
|
|
command = self.render(*path)
|
2022-10-19 15:46:31 +00:00
|
|
|
self.run_task(service, command)
|
2019-05-21 09:56:49 +00:00
|
|
|
|
2022-10-19 15:46:31 +00:00
|
|
|
def run_task_from_str(self, service: str, command: str) -> None:
|
2022-10-18 14:57:07 +00:00
|
|
|
rendered = env.render_str(self.config, command).strip()
|
2022-10-19 15:46:31 +00:00
|
|
|
self.run_task(service, rendered)
|
2022-10-18 14:57:07 +00:00
|
|
|
|
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
|
|
|
|
2022-10-19 15:46:31 +00:00
|
|
|
def run_task(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
|
|
|
|
|
|
|
|
|
2022-10-19 15:46:31 +00:00
|
|
|
class BaseComposeTaskRunner(BaseTaskRunner):
|
2021-09-27 09:48:17 +00:00
|
|
|
def docker_compose(self, *command: str) -> int:
|
|
|
|
raise NotImplementedError
|