Skip to main content

aither_sandbox/spawn/
launch.rs

1//! What the container and SSH backends run with, as the embedder prepared it.
2//!
3//! The backends know nothing of how a tool set is chosen, pinned or
4//! installed: the embedder hands over the directory its programs are in, the
5//! shell to run scripts in and the environment its programs need.
6
7use std::borrow::Cow;
8
9use askama::Template;
10
11use crate::ContainerPath;
12
13#[derive(Template)]
14#[template(path = "tool_path.sh", escape = "none")]
15struct PathTemplate<'a> {
16    bin_dir: Cow<'a, str>,
17    exports: Vec<(&'a str, Cow<'a, str>)>,
18    script: &'a str,
19}
20
21#[derive(Template)]
22#[template(path = "managed_shell.sh", escape = "none")]
23struct ShellTemplate<'a> {
24    shell: Cow<'a, str>,
25    arguments: String,
26}
27
28fn with_path(
29    bin_dir: &str,
30    exports: &[(String, String)],
31    script: &str,
32) -> Result<String, askama::Error> {
33    PathTemplate {
34        bin_dir: shell_words::quote(bin_dir),
35        exports: exports
36            .iter()
37            .map(|(name, value)| (name.as_str(), shell_words::quote(value)))
38            .collect(),
39        script,
40    }
41    .render()
42}
43
44/// The arguments with which a managed bash runs `script`: no startup file is
45/// read, so a script behaves the same on every host.
46#[must_use]
47pub fn shell_arguments(script: &str) -> Vec<String> {
48    ["--noprofile", "--norc", "-c", script]
49        .map(Into::into)
50        .into()
51}
52
53/// The shell prepared on the host, for
54/// [`Shell::builder_on_host`](crate::Shell::builder_on_host): the program a
55/// script runs under, as a path on this machine.
56#[derive(Debug, Clone, PartialEq, Eq)]
57pub struct HostLaunch {
58    shell: std::sync::Arc<str>,
59}
60
61impl HostLaunch {
62    /// Scripts run under the bash at `shell`.
63    #[must_use]
64    pub fn new(shell: impl Into<std::sync::Arc<str>>) -> Self {
65        Self {
66            shell: shell.into(),
67        }
68    }
69
70    /// The bash that runs scripts.
71    #[must_use]
72    pub const fn shell(&self) -> &std::sync::Arc<str> {
73        &self.shell
74    }
75}
76
77/// The programs prepared inside a container, for
78/// [`Backend::Container`](crate::Backend::Container).
79#[derive(Debug, Clone, PartialEq, Eq)]
80pub struct ContainerLaunch {
81    bin_dir: ContainerPath,
82    shell: ContainerPath,
83}
84
85impl ContainerLaunch {
86    /// Programs in `bin_dir` inside the container, run by the bash at `shell`.
87    #[must_use]
88    pub const fn new(bin_dir: ContainerPath, shell: ContainerPath) -> Self {
89        Self { bin_dir, shell }
90    }
91
92    /// The directory the programs are in.
93    #[must_use]
94    pub const fn bin_dir(&self) -> &ContainerPath {
95        &self.bin_dir
96    }
97
98    /// `script` with the programs' directory first on its `PATH`.
99    ///
100    /// # Errors
101    /// Fails when the template cannot render.
102    pub fn wrap_script(&self, script: &str) -> Result<String, askama::Error> {
103        with_path(self.bin_dir.as_str(), &[], script)
104    }
105
106    /// A command line running `script` in the container's bash.
107    ///
108    /// # Errors
109    /// Fails when the template cannot render.
110    pub fn shell_command(&self, script: &str) -> Result<String, askama::Error> {
111        ShellTemplate {
112            shell: shell_words::quote(self.shell.as_str()),
113            arguments: shell_words::join(shell_arguments(script)),
114        }
115        .render()
116    }
117}
118
119/// The programs prepared on a server, for
120/// [`Backend::Ssh`](crate::Backend::Ssh). Paths are on the server and text,
121/// because a script names them.
122#[derive(Debug, Clone, PartialEq, Eq)]
123pub struct RemoteLaunch {
124    bin_dir: String,
125    shell: String,
126    exports: Vec<(String, String)>,
127    writable_dir: Option<String>,
128}
129
130impl RemoteLaunch {
131    /// Programs in `bin_dir` on the server, run by the bash at `shell`.
132    #[must_use]
133    pub fn new(bin_dir: impl Into<String>, shell: impl Into<String>) -> Self {
134        Self {
135            bin_dir: bin_dir.into(),
136            shell: shell.into(),
137            exports: Vec::new(),
138            writable_dir: None,
139        }
140    }
141
142    /// Variables a script starts with, in order.
143    #[must_use]
144    pub fn with_exports(mut self, exports: impl IntoIterator<Item = (String, String)>) -> Self {
145        self.exports.extend(exports);
146        self
147    }
148
149    /// A directory on the server the sandboxed programs may read, write and
150    /// run in, apart from the programs' own.
151    #[must_use]
152    pub fn with_writable_dir(mut self, dir: impl Into<String>) -> Self {
153        self.writable_dir = Some(dir.into());
154        self
155    }
156
157    /// The directory the programs are in.
158    #[must_use]
159    pub fn bin_dir(&self) -> &str {
160        &self.bin_dir
161    }
162
163    /// The bash that runs scripts.
164    #[must_use]
165    pub fn shell(&self) -> &str {
166        &self.shell
167    }
168
169    /// The directory the sandboxed programs may write, when there is one.
170    #[must_use]
171    pub fn writable_dir(&self) -> Option<&str> {
172        self.writable_dir.as_deref()
173    }
174
175    /// `script` with the programs' directory first on its `PATH`, after the
176    /// variables.
177    ///
178    /// # Errors
179    /// Fails when the template cannot render.
180    pub fn wrap_script(&self, script: &str) -> Result<String, askama::Error> {
181        with_path(&self.bin_dir, &self.exports, script)
182    }
183}