2021-01-24 23:08:48 +01:00
|
|
|
use std::path::PathBuf;
|
|
|
|
use std::process::Stdio;
|
2020-12-18 10:27:44 +01:00
|
|
|
|
2020-12-29 20:31:19 +01:00
|
|
|
use clap::{App, Arg, ArgMatches};
|
2021-01-24 23:08:48 +01:00
|
|
|
use futures::future::join3;
|
|
|
|
use tokio::io::{AsyncRead, AsyncBufReadExt, BufReader};
|
|
|
|
use tokio::process::Command;
|
2020-12-16 05:21:26 +01:00
|
|
|
|
2021-11-21 08:34:52 +01:00
|
|
|
use super::nix::{Flake, Hive, HivePath, NixResult};
|
|
|
|
use super::nix::deployment::TargetNodeMap;
|
|
|
|
use super::job::JobHandle;
|
2020-12-18 10:27:44 +01:00
|
|
|
|
2021-11-21 08:34:52 +01:00
|
|
|
/// Non-interactive execution of an arbitrary command.
|
2021-01-24 23:08:48 +01:00
|
|
|
pub struct CommandExecution {
|
|
|
|
command: Command,
|
2021-11-21 08:34:52 +01:00
|
|
|
job: Option<JobHandle>,
|
2021-12-08 08:13:31 +01:00
|
|
|
hide_stdout: bool,
|
2021-01-24 23:08:48 +01:00
|
|
|
stdout: Option<String>,
|
|
|
|
stderr: Option<String>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl CommandExecution {
|
2021-02-10 04:28:45 +01:00
|
|
|
pub fn new(command: Command) -> Self {
|
2021-01-24 23:08:48 +01:00
|
|
|
Self {
|
|
|
|
command,
|
2021-11-21 08:34:52 +01:00
|
|
|
job: None,
|
2021-12-08 08:13:31 +01:00
|
|
|
hide_stdout: false,
|
2021-01-24 23:08:48 +01:00
|
|
|
stdout: None,
|
|
|
|
stderr: None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-21 08:34:52 +01:00
|
|
|
/// Sets the job associated with this execution.
|
|
|
|
pub fn set_job(&mut self, job: Option<JobHandle>) {
|
|
|
|
self.job = job;
|
2021-01-24 23:08:48 +01:00
|
|
|
}
|
|
|
|
|
2021-12-08 08:13:31 +01:00
|
|
|
/// Sets whether to hide stdout.
|
|
|
|
pub fn set_hide_stdout(&mut self, hide_stdout: bool) {
|
|
|
|
self.hide_stdout = hide_stdout;
|
|
|
|
}
|
|
|
|
|
2021-11-21 08:34:52 +01:00
|
|
|
/// Returns logs from the last invocation.
|
2021-01-24 23:08:48 +01:00
|
|
|
pub fn get_logs(&self) -> (Option<&String>, Option<&String>) {
|
|
|
|
(self.stdout.as_ref(), self.stderr.as_ref())
|
|
|
|
}
|
|
|
|
|
2021-11-21 08:34:52 +01:00
|
|
|
/// Runs the command.
|
2021-01-24 23:08:48 +01:00
|
|
|
pub async fn run(&mut self) -> NixResult<()> {
|
|
|
|
self.command.stdin(Stdio::null());
|
|
|
|
self.command.stdout(Stdio::piped());
|
|
|
|
self.command.stderr(Stdio::piped());
|
|
|
|
|
|
|
|
self.stdout = Some(String::new());
|
|
|
|
self.stderr = Some(String::new());
|
|
|
|
|
|
|
|
let mut child = self.command.spawn()?;
|
|
|
|
|
|
|
|
let stdout = BufReader::new(child.stdout.take().unwrap());
|
|
|
|
let stderr = BufReader::new(child.stderr.take().unwrap());
|
|
|
|
|
2021-12-08 08:13:31 +01:00
|
|
|
let stdout_job = if self.hide_stdout { None } else { self.job.clone() };
|
|
|
|
|
2021-01-24 23:08:48 +01:00
|
|
|
let futures = join3(
|
2021-12-08 08:13:31 +01:00
|
|
|
capture_stream(stdout, stdout_job, false),
|
2021-11-21 08:34:52 +01:00
|
|
|
capture_stream(stderr, self.job.clone(), true),
|
2021-01-24 23:08:48 +01:00
|
|
|
child.wait(),
|
|
|
|
);
|
|
|
|
|
2021-12-05 10:14:12 +01:00
|
|
|
let (stdout, stderr, wait) = futures.await;
|
|
|
|
self.stdout = Some(stdout?);
|
|
|
|
self.stderr = Some(stderr?);
|
2021-01-24 23:08:48 +01:00
|
|
|
|
|
|
|
let exit = wait?;
|
|
|
|
|
|
|
|
if exit.success() {
|
|
|
|
Ok(())
|
|
|
|
} else {
|
2021-04-29 00:09:40 +02:00
|
|
|
Err(exit.into())
|
2021-01-24 23:08:48 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-03 19:37:03 +01:00
|
|
|
pub async fn hive_from_args(args: &ArgMatches) -> NixResult<Hive> {
|
2020-12-29 20:31:19 +01:00
|
|
|
let path = match args.occurrences_of("config") {
|
|
|
|
0 => {
|
|
|
|
// traverse upwards until we find hive.nix
|
|
|
|
let mut cur = std::env::current_dir()?;
|
2021-06-29 10:02:43 +02:00
|
|
|
let mut file_path = None;
|
2020-12-29 20:31:19 +01:00
|
|
|
|
|
|
|
loop {
|
2021-10-28 23:09:35 +02:00
|
|
|
let flake = cur.join("flake.nix");
|
|
|
|
if flake.is_file() {
|
|
|
|
file_path = Some(flake);
|
|
|
|
break;
|
|
|
|
}
|
2020-12-29 20:31:19 +01:00
|
|
|
|
2021-10-28 23:09:35 +02:00
|
|
|
let legacy = cur.join("hive.nix");
|
|
|
|
if legacy.is_file() {
|
|
|
|
file_path = Some(legacy);
|
2020-12-29 20:31:19 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
match cur.parent() {
|
|
|
|
Some(parent) => {
|
|
|
|
cur = parent.to_owned();
|
|
|
|
}
|
|
|
|
None => {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-29 10:02:43 +02:00
|
|
|
if file_path.is_none() {
|
|
|
|
log::error!("Could not find `hive.nix` or `flake.nix` in {:?} or any parent directory", std::env::current_dir()?);
|
2020-12-29 20:31:19 +01:00
|
|
|
}
|
|
|
|
|
2021-06-29 10:02:43 +02:00
|
|
|
file_path.unwrap()
|
2020-12-29 20:31:19 +01:00
|
|
|
}
|
|
|
|
_ => {
|
|
|
|
let path = args.value_of("config").expect("The config arg should exist").to_owned();
|
2021-06-29 10:02:43 +02:00
|
|
|
let fpath = canonicalize_cli_path(&path);
|
|
|
|
|
2021-11-23 22:33:23 +01:00
|
|
|
if !fpath.exists() && path.contains(':') {
|
2021-06-29 10:02:43 +02:00
|
|
|
// Treat as flake URI
|
2021-10-26 08:38:10 +02:00
|
|
|
let flake = Flake::from_uri(path).await?;
|
|
|
|
let hive_path = HivePath::Flake(flake);
|
2021-06-29 10:02:43 +02:00
|
|
|
let mut hive = Hive::new(hive_path)?;
|
|
|
|
|
|
|
|
if args.is_present("show-trace") {
|
|
|
|
hive.set_show_trace(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
return Ok(hive);
|
|
|
|
}
|
|
|
|
|
|
|
|
fpath
|
2020-12-29 20:31:19 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2021-10-26 08:38:10 +02:00
|
|
|
let hive_path = HivePath::from_path(path).await?;
|
2021-12-04 10:03:26 +01:00
|
|
|
match &hive_path {
|
|
|
|
HivePath::Legacy(p) => {
|
|
|
|
log::info!("Using configuration: {}", p.to_string_lossy());
|
|
|
|
}
|
|
|
|
HivePath::Flake(flake) => {
|
|
|
|
log::info!("Using flake: {}", flake.uri());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-29 10:02:43 +02:00
|
|
|
let mut hive = Hive::new(hive_path)?;
|
2020-12-29 20:31:19 +01:00
|
|
|
|
|
|
|
if args.is_present("show-trace") {
|
2021-06-29 10:02:43 +02:00
|
|
|
hive.set_show_trace(true);
|
2020-12-29 20:31:19 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
Ok(hive)
|
|
|
|
}
|
|
|
|
|
2022-01-03 19:37:03 +01:00
|
|
|
pub fn register_selector_args<'a>(command: App<'a>) -> App<'a> {
|
2020-12-29 06:35:43 +01:00
|
|
|
command
|
2022-01-03 19:37:03 +01:00
|
|
|
.arg(Arg::new("on")
|
2020-12-16 05:21:26 +01:00
|
|
|
.long("on")
|
2021-01-02 05:45:41 +01:00
|
|
|
.value_name("NODES")
|
|
|
|
.help("Node selector")
|
|
|
|
.long_help(r#"Select a list of nodes to deploy to.
|
|
|
|
|
|
|
|
The list is comma-separated and globs are supported. To match tags, prepend the filter by @. Valid examples:
|
2020-12-16 05:21:26 +01:00
|
|
|
|
|
|
|
- host1,host2,host3
|
|
|
|
- edge-*
|
2020-12-18 10:27:44 +01:00
|
|
|
- edge-*,core-*
|
|
|
|
- @a-tag,@tags-can-have-*"#)
|
2020-12-16 05:21:26 +01:00
|
|
|
.takes_value(true))
|
|
|
|
}
|
2020-12-29 20:31:19 +01:00
|
|
|
|
2021-06-29 10:02:43 +02:00
|
|
|
fn canonicalize_cli_path(path: &str) -> PathBuf {
|
2021-11-23 22:33:23 +01:00
|
|
|
if !path.starts_with('/') {
|
2020-12-29 20:31:19 +01:00
|
|
|
format!("./{}", path).into()
|
|
|
|
} else {
|
|
|
|
path.into()
|
|
|
|
}
|
|
|
|
}
|
2021-02-09 23:57:11 +01:00
|
|
|
|
2021-12-05 10:14:12 +01:00
|
|
|
pub async fn capture_stream<R>(mut stream: BufReader<R>, job: Option<JobHandle>, stderr: bool) -> NixResult<String>
|
|
|
|
where R: AsyncRead + Unpin
|
|
|
|
{
|
2021-02-09 23:57:11 +01:00
|
|
|
let mut log = String::new();
|
|
|
|
|
|
|
|
loop {
|
|
|
|
let mut line = String::new();
|
2021-12-05 10:14:12 +01:00
|
|
|
let len = stream.read_line(&mut line).await?;
|
2021-02-09 23:57:11 +01:00
|
|
|
|
|
|
|
if len == 0 {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
let trimmed = line.trim_end();
|
2021-11-21 08:34:52 +01:00
|
|
|
|
|
|
|
if let Some(job) = &job {
|
|
|
|
if stderr {
|
2021-12-05 10:14:12 +01:00
|
|
|
job.stderr(trimmed.to_string())?;
|
2021-11-21 08:34:52 +01:00
|
|
|
} else {
|
2021-12-05 10:14:12 +01:00
|
|
|
job.stdout(trimmed.to_string())?;
|
2021-11-21 08:34:52 +01:00
|
|
|
}
|
|
|
|
}
|
2021-02-09 23:57:11 +01:00
|
|
|
|
|
|
|
log += trimmed;
|
|
|
|
log += "\n";
|
|
|
|
}
|
|
|
|
|
2021-12-05 10:14:12 +01:00
|
|
|
Ok(log)
|
2021-02-09 23:57:11 +01:00
|
|
|
}
|
2021-11-21 08:34:52 +01:00
|
|
|
|
|
|
|
pub fn get_label_width(targets: &TargetNodeMap) -> Option<usize> {
|
|
|
|
targets.keys().map(|n| n.len()).max()
|
|
|
|
}
|