Skip to content

Commit c3fc0dd

Browse files
kasimekaytsssun
authored andcommitted
host-containers: add support for custom entrypoint commands
Add command field to host container configuration and corresponding template support to customize container entrypoints. Implements CTR_COMMAND environment variable in service files that is passed to host-ctr. Signed-off-by: Yutong Sun <[email protected]>
1 parent bd65882 commit c3fc0dd

File tree

5 files changed

+40
-7
lines changed

5 files changed

+40
-7
lines changed

packages/os/host-containers-toml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[required-extensions]
22
host-containers = "v1"
3-
std = { version = "v1", helpers = ["if_not_null"]}
3+
std = { version = "v1", helpers = ["if_not_null", "toml_encode"]}
44
+++
55
{{#if_not_null settings.host-containers}}
66
{{#each settings.host-containers}}
@@ -17,5 +17,8 @@ superpowered = {{this.superpowered}}
1717
{{#if_not_null this.user-data}}
1818
user-data = "{{{this.user-data}}}"
1919
{{/if_not_null}}
20+
{{#if_not_null this.command}}
21+
command = {{ toml_encode this.command }}
22+
{{/if_not_null}}
2023
{{/each}}
2124
{{/if_not_null}}

packages/os/[email protected]

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ ExecStart=/usr/bin/host-ctr run \
1212
--container-id='%i' \
1313
--source='${CTR_SOURCE}' \
1414
--superpowered='${CTR_SUPERPOWERED}' \
15+
--command='${CTR_COMMAND}' \
1516
--registry-config=/etc/host-containers/host-ctr.toml
1617
Restart=always
1718
RestartSec=45

sources/api/host-containers/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ simplelog.workspace = true
1818
snafu.workspace = true
1919
toml.workspace = true
2020
bottlerocket-modeled-types.workspace = true
21+
serde_json.workspace = true
2122

2223
[dev-dependencies]
2324
tempfile.workspace = true

sources/api/host-containers/src/config.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,6 @@ pub(crate) struct HostContainer {
1515
pub(crate) enabled: Option<bool>,
1616
pub(crate) superpowered: Option<bool>,
1717
pub(crate) user_data: Option<ValidBase64>,
18+
#[serde(default)]
19+
pub(crate) command: Vec<String>,
1820
}

sources/api/host-containers/src/main.rs

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,16 @@ mod error {
113113
name: String,
114114
source: std::io::Error,
115115
},
116+
117+
#[snafu(display(
118+
"Failed to serialize container entrypoint command {:?}: {}",
119+
command,
120+
source
121+
))]
122+
SerializeContainerCommand {
123+
command: Vec<String>,
124+
source: serde_json::Error,
125+
},
116126
}
117127
}
118128

@@ -233,10 +243,17 @@ where
233243
}
234244

235245
/// Write out the EnvironmentFile that systemd uses to fill in arguments to host-ctr
236-
fn write_env_file<S1, S2>(name: S1, source: S2, enabled: bool, superpowered: bool) -> Result<()>
246+
fn write_env_file<S1, S2, S3>(
247+
name: S1,
248+
source: S2,
249+
enabled: bool,
250+
superpowered: bool,
251+
command: S3,
252+
) -> Result<()>
237253
where
238254
S1: AsRef<str>,
239255
S2: AsRef<str>,
256+
S3: AsRef<str>,
240257
{
241258
let name = name.as_ref();
242259
let filename = format!("{name}.env");
@@ -247,6 +264,8 @@ where
247264
.context(error::EnvFileBuildFailedSnafu { name })?;
248265
writeln!(output, "CTR_SOURCE={}", source.as_ref())
249266
.context(error::EnvFileBuildFailedSnafu { name })?;
267+
writeln!(output, "CTR_COMMAND={}", command.as_ref())
268+
.context(error::EnvFileBuildFailedSnafu { name })?;
250269

251270
writeln!(
252271
output,
@@ -336,10 +355,15 @@ where
336355
})?;
337356
let enabled = image_details.enabled.unwrap_or(false);
338357
let superpowered = image_details.superpowered.unwrap_or(false);
358+
let command = serde_json::to_string(&image_details.command).context(
359+
error::SerializeContainerCommandSnafu {
360+
command: image_details.command.clone(),
361+
},
362+
)?;
339363

340364
info!(
341-
"Host container '{}' is enabled: {}, superpowered: {}, with source: {}",
342-
name, enabled, superpowered, source
365+
"Host container '{}' is enabled: {}, superpowered: {}, with source: {}, entrypoint command: {}",
366+
name, enabled, superpowered, source, command
343367
);
344368

345369
// Create the directory regardless if user data was provided for the container
@@ -360,7 +384,7 @@ where
360384

361385
// Write the environment file needed for the systemd service to have details about this
362386
// specific host container
363-
write_env_file(name, source, enabled, superpowered)?;
387+
write_env_file(name, source, enabled, superpowered, command)?;
364388

365389
// Now start/stop the container according to the 'enabled' setting
366390
let unit_name = format!("host-containers@{name}.service");
@@ -376,13 +400,13 @@ where
376400
// We want to ensure the host container is running with its most recent configuration.
377401
if host_containerd_unit.is_active()? {
378402
debug!("Cleaning up host container: '{}'", unit_name);
379-
command(
403+
crate::command(
380404
constants::HOST_CTR_BIN,
381405
["clean-up", "--container-id", name],
382406
)?;
383407
}
384408

385-
let systemd_target = command(constants::SYSTEMCTL_BIN, ["get-default"])?;
409+
let systemd_target = crate::command(constants::SYSTEMCTL_BIN, ["get-default"])?;
386410

387411
// What happens next depends on whether the system has finished booting, and whether the
388412
// host container is enabled.
@@ -501,6 +525,7 @@ mod test {
501525
enabled = true
502526
superpowered = true
503527
user-data = "Zm9vCg=="
528+
command = ["sh", "-c", "echo hello"]
504529
"#;
505530

506531
let temp_dir = tempfile::TempDir::new().unwrap();
@@ -517,6 +542,7 @@ mod test {
517542
enabled: Some(true),
518543
superpowered: Some(true),
519544
user_data: Some(ValidBase64::try_from("Zm9vCg==").unwrap()),
545+
command: ["sh", "-c", "echo hello"].map(String::from).into(),
520546
},
521547
);
522548

0 commit comments

Comments
 (0)