lifecycle: rebuild reconciles bind flag idempotently and restarts
This commit is contained in:
parent
377eb994a1
commit
764d6497dd
2 changed files with 24 additions and 12 deletions
|
|
@ -37,23 +37,30 @@ pub async fn spawn(name: &str, agent_flake: &str, agent_dir: &Path) -> Result<()
|
||||||
validate(name)?;
|
validate(name)?;
|
||||||
let container = container_name(name);
|
let container = container_name(name);
|
||||||
run(&["create", &container, "--flake", agent_flake]).await?;
|
run(&["create", &container, "--flake", agent_flake]).await?;
|
||||||
append_bind_flag(&container, agent_dir)?;
|
set_bind_flag(&container, agent_dir)?;
|
||||||
run(&["start", &container]).await
|
run(&["start", &container]).await
|
||||||
}
|
}
|
||||||
|
|
||||||
/// `nixos-container` doesn't expose `--bind` on the CLI, but its start script
|
/// `nixos-container` doesn't expose `--bind` on the CLI, but its start script
|
||||||
/// expands `$EXTRA_NSPAWN_FLAGS` (from `/etc/nixos-containers/<name>.conf`)
|
/// expands `$EXTRA_NSPAWN_FLAGS` (from `/etc/nixos-containers/<name>.conf`)
|
||||||
/// unquoted into the `systemd-nspawn` invocation. Append a `--bind` flag there.
|
/// unquoted into the `systemd-nspawn` invocation. Idempotently replace the
|
||||||
fn append_bind_flag(container: &str, agent_dir: &Path) -> Result<()> {
|
/// `EXTRA_NSPAWN_FLAGS` line with the bind we want.
|
||||||
|
fn set_bind_flag(container: &str, agent_dir: &Path) -> Result<()> {
|
||||||
let path = format!("/etc/nixos-containers/{container}.conf");
|
let path = format!("/etc/nixos-containers/{container}.conf");
|
||||||
let line = format!(
|
let original = std::fs::read_to_string(&path).with_context(|| format!("read {path}"))?;
|
||||||
"\nEXTRA_NSPAWN_FLAGS=\"--bind={}:{CONTAINER_RUNTIME_MOUNT}\"\n",
|
let mut lines: Vec<String> = original
|
||||||
|
.lines()
|
||||||
|
.filter(|line| !line.trim_start().starts_with("EXTRA_NSPAWN_FLAGS="))
|
||||||
|
.map(str::to_owned)
|
||||||
|
.collect();
|
||||||
|
lines.push(format!(
|
||||||
|
"EXTRA_NSPAWN_FLAGS=\"--bind={}:{CONTAINER_RUNTIME_MOUNT}\"",
|
||||||
agent_dir.display()
|
agent_dir.display()
|
||||||
);
|
));
|
||||||
let mut content = std::fs::read_to_string(&path).with_context(|| format!("read {path}"))?;
|
let mut content = lines.join("\n");
|
||||||
content.push_str(&line);
|
content.push('\n');
|
||||||
std::fs::write(&path, content).with_context(|| format!("write {path}"))?;
|
std::fs::write(&path, content).with_context(|| format!("write {path}"))?;
|
||||||
tracing::info!(%path, "appended EXTRA_NSPAWN_FLAGS for bind mount");
|
tracing::info!(%path, "set EXTRA_NSPAWN_FLAGS for bind mount");
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -63,10 +70,14 @@ pub async fn kill(name: &str) -> Result<()> {
|
||||||
run(&["stop", &container]).await
|
run(&["stop", &container]).await
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn rebuild(name: &str, agent_flake: &str) -> Result<()> {
|
pub async fn rebuild(name: &str, agent_flake: &str, agent_dir: &Path) -> Result<()> {
|
||||||
validate(name)?;
|
validate(name)?;
|
||||||
let container = container_name(name);
|
let container = container_name(name);
|
||||||
run(&["update", &container, "--flake", agent_flake]).await
|
set_bind_flag(&container, agent_dir)?;
|
||||||
|
run(&["update", &container, "--flake", agent_flake]).await?;
|
||||||
|
// Restart so any nspawn-level changes (bind mounts, networking, etc.) apply.
|
||||||
|
run(&["stop", &container]).await?;
|
||||||
|
run(&["start", &container]).await
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn list() -> Result<Vec<String>> {
|
pub async fn list() -> Result<Vec<String>> {
|
||||||
|
|
|
||||||
|
|
@ -77,7 +77,8 @@ async fn dispatch(req: &HostRequest, agent_flake: &str, coord: &Coordinator) ->
|
||||||
}
|
}
|
||||||
HostRequest::Rebuild { name } => {
|
HostRequest::Rebuild { name } => {
|
||||||
tracing::info!(%name, "rebuild");
|
tracing::info!(%name, "rebuild");
|
||||||
lifecycle::rebuild(name, agent_flake).await?;
|
let agent_dir = Coordinator::agent_dir(name);
|
||||||
|
lifecycle::rebuild(name, agent_flake, &agent_dir).await?;
|
||||||
HostResponse::success()
|
HostResponse::success()
|
||||||
}
|
}
|
||||||
HostRequest::List => HostResponse::list(lifecycle::list().await?),
|
HostRequest::List => HostResponse::list(lifecycle::list().await?),
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue