aboutsummaryrefslogtreecommitdiffstats
path: root/pkgs/populate/src/shell.rs
diff options
context:
space:
mode:
authortv <tv@krebsco.de>2026-11-14 04:18:14 +0000
committertv <tv@krebsco.de>2026-11-14 06:24:36 +0100
commit7198af72e3490015cb229f75abaf0e1b9a3108f0 (patch)
tree5aa2696a0a885c8b33f084f8f1ed7f25136d3d72 /pkgs/populate/src/shell.rs
parent36181e19ccef8d86d53dc5d504eb382c7d9b25fe (diff)
populate: populate shell, running shell code on a target
populate shell [OPTION]... TARGET SCRIPT runs SCRIPT with /bin/sh on TARGET, reached like populate reaches it, deciding whether it's local when it runs, with local or remote as $1, so SCRIPT can tell. It takes populate's target options but --path, and --tty, giving SCRIPT a terminal on a remote target, and --local-only, failing before connecting unless the target is local. It replaces itself with the command running SCRIPT, so the exit status, the terminal, and signals are SCRIPT's. krops can run nixos-rebuild with it, which needs whether the target is local, unless it decides that during evaluation, which pure evaluation can't. The command line's parsing is shared between populate and populate shell.
Diffstat (limited to 'pkgs/populate/src/shell.rs')
-rw-r--r--pkgs/populate/src/shell.rs72
1 files changed, 72 insertions, 0 deletions
diff --git a/pkgs/populate/src/shell.rs b/pkgs/populate/src/shell.rs
new file mode 100644
index 0000000..befd4aa
--- /dev/null
+++ b/pkgs/populate/src/shell.rs
@@ -0,0 +1,72 @@
+//! populate shell: runs shell code on a target, reached like populate
+//! reaches it.
+
+use crate::cli::ShellOptions;
+use crate::run::{self, Origin};
+use std::os::unix::process::CommandExt;
+use std::process::{Command, ExitCode};
+
+/// Runs the script as the options say, replacing populate, so the script's
+/// exit status, terminal, and signals are populate's. Returns only if
+/// that fails.
+pub fn shell(options: ShellOptions) -> ExitCode {
+ let origin = Origin::from_env();
+ let target = options
+ .target
+ .complete(&origin, options.sudo, options.ssh_args);
+ let local = options.local.unwrap_or_else(|| origin.is_local(&target));
+ if options.local_only && !local {
+ let user = target
+ .user
+ .as_deref()
+ .map(|user| format!("{user}@"))
+ .unwrap_or_default();
+ eprintln!(
+ "populate: shell: {user}{} is not the machine populate runs on",
+ target.host
+ );
+ return ExitCode::FAILURE;
+ }
+ let mut words: Vec<String> = Vec::new();
+ if target.sudo {
+ words.push("sudo".to_string());
+ }
+ words.extend([
+ "/bin/sh".to_string(),
+ "-c".to_string(),
+ options.script,
+ "sh".to_string(),
+ if local { "local" } else { "remote" }.to_string(),
+ ]);
+ let mut command = if local {
+ let mut command = Command::new(&words[0]);
+ command.args(&words[1..]);
+ command
+ } else {
+ let mut command = Command::new("ssh");
+ if let Some(user) = &target.user {
+ command.args(["-l", user]);
+ }
+ if let Some(port) = &target.port {
+ command.args(["-p", port]);
+ }
+ command
+ .arg(if options.tty { "-t" } else { "-T" })
+ .args(&target.extra_options)
+ .arg(&target.host)
+ .arg(
+ words
+ .iter()
+ .map(|word| run::quote(word))
+ .collect::<Vec<_>>()
+ .join(" "),
+ );
+ command
+ };
+ let error = command.exec();
+ eprintln!(
+ "populate: shell: can't run {}: {error}",
+ command.get_program().to_string_lossy()
+ );
+ ExitCode::from(127)
+}