Something went wrong. Try again.
ALPHA: wire is a tool to deploy nixos systems (contribution fork) forked from althaea.zone/wire
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452// SPDX-License-Identifier: AGPL-3.0-or-later// Copyright 2024-2025 wire Contributors
#![allow(clippy::missing_errors_doc)]use enum_dispatch::enum_dispatch;use gethostname::gethostname;use serde::{Deserialize, Serialize};use std::fmt::Display;use std::sync::Arc;use std::sync::atomic::AtomicBool;use tokio::sync::{RwLock, oneshot};use tracing::instrument;
use crate::commands::builder::CommandStringBuilder;use crate::commands::{CommandArguments, WireCommandChip, run_command};use crate::errors::NetworkError;use crate::hive::HiveLocation;use crate::hive::steps::build::Build;use crate::hive::steps::evaluate::Evaluate;use crate::hive::steps::keys::{Key, Keys, PushKeyAgent};use crate::hive::steps::ping::Ping;use crate::hive::steps::push::{PushBuildOutput, PushEvaluatedOutput};use crate::{StrictHostKeyChecking, SubCommandModifiers};
use super::HiveLibError;use super::steps::activate::SwitchToConfiguration;
#[derive( Serialize, Deserialize, Clone, Debug, Hash, Eq, PartialEq, PartialOrd, Ord, derive_more::Display,)]pub struct Name(pub Arc<str>);
#[derive(Serialize, Deserialize, Clone, Debug, Hash, Eq, PartialEq)]pub struct Target { pub hosts: Vec<Arc<str>>, pub user: Arc<str>, pub port: u32,
#[serde(skip)] current_host: usize,}
#[derive(Clone, Debug)]pub struct SharedTarget(pub Arc<RwLock<Target>>);
// Hack specifically for testing if two steps that have the same shared target// are equal#[cfg(test)]impl PartialEq for SharedTarget { fn eq(&self, other: &Self) -> bool { let self_guard = self .0 .try_read() .expect("failed to target read in test context"); let other_guard = other .0 .try_read() .expect("failed to target read in test context");
*self_guard == *other_guard }}
impl Target { #[instrument(ret(level = tracing::Level::DEBUG), skip_all)] pub fn create_ssh_opts(&self, modifiers: SubCommandModifiers) -> Result<String, HiveLibError> { self.create_ssh_args(modifiers).map(|x| x.join(" ")) }
#[instrument(ret(level = tracing::Level::DEBUG))] pub fn create_ssh_args( &self, modifiers: SubCommandModifiers, ) -> Result<Vec<String>, HiveLibError> { let mut vector = vec![ "-l".to_string(), self.user.to_string(), "-p".to_string(), self.port.to_string(), ]; let mut options = vec![ format!( "StrictHostKeyChecking={}", match modifiers.ssh_accept_host { StrictHostKeyChecking::AcceptNew => "accept-new", StrictHostKeyChecking::No => "no", } ) .to_string(), ];
options.extend(["BatchMode=yes".to_string()]);
vector.push("-o".to_string()); vector.extend(options.into_iter().intersperse("-o".to_string()));
if modifiers.ssh_verbosity > 0 { vector.push(format!("-{}", "v".repeat(modifiers.ssh_verbosity))); }
Ok(vector) }
/// Tests the connection to a node pub async fn ping(&self, modifiers: SubCommandModifiers) -> Result<(), HiveLibError> { let host = self.get_preferred_host()?;
let mut command_string = CommandStringBuilder::new("ssh"); command_string.arg(format!("{}@{host}", self.user)); command_string.arg(self.create_ssh_opts(modifiers)?); command_string.arg("exit");
let output = run_command( &CommandArguments::new(command_string, modifiers) .log_stdout() .mode(crate::commands::ChildOutputMode::Interactive), ) .await?;
output.wait_till_success().await.map_err(|source| { HiveLibError::NetworkError(NetworkError::HostUnreachable { host: host.to_string(), source, }) })?;
Ok(()) }}
#[cfg(test)]impl Default for Target { fn default() -> Self { Target { hosts: vec!["NAME".into()], user: "root".into(), port: 22, current_host: 0, } }}
impl Target { pub fn get_preferred_host(&self) -> Result<&Arc<str>, HiveLibError> { self.hosts .get(self.current_host) .ok_or(HiveLibError::NetworkError(NetworkError::HostsExhausted)) }
pub const fn host_failed(&mut self) { self.current_host += 1; }
#[cfg(test)] #[must_use] pub fn from_host(host: &str) -> Self { Target { hosts: vec![host.into()], ..Default::default() } }}
impl Display for Target { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { let hosts = itertools::Itertools::join( &mut self .hosts .iter() .map(|host| format!("{}@{host}:{}", self.user, self.port)), ", ", );
write!(f, "{hosts}") }}
#[derive(Serialize, Deserialize, Clone, Debug, Eq, PartialEq, Hash)]pub struct Node { #[serde(rename = "target")] pub target: Target,
#[serde(rename = "buildOnTarget")] pub build_remotely: bool,
#[serde(rename = "allowLocalDeployment")] pub allow_local_deployment: bool,
#[serde(default)] pub tags: im::HashSet<String>,
#[serde(rename(deserialize = "_keys", serialize = "keys"))] pub keys: Vec<Arc<Key>>,
#[serde(rename(deserialize = "_hostPlatform", serialize = "host_platform"))] pub host_platform: Arc<str>,
#[serde(rename( deserialize = "privilegeEscalationCommand", serialize = "privilege_escalation_command" ))] pub privilege_escalation_command: Arc<Vec<Arc<str>>>,}
#[cfg(test)]impl Default for Node { fn default() -> Self { Node { target: Target::default(), keys: Vec::new(), tags: im::HashSet::new(), privilege_escalation_command: vec!["sudo".into(), "--".into()].into(), allow_local_deployment: true, build_remotely: false, host_platform: "x86_64-linux".into(), } }}
impl Node { #[cfg(test)] #[must_use] pub fn from_host(host: &str) -> Self { Node { target: Target::from_host(host), ..Default::default() } }}
#[must_use]pub fn should_apply_locally(allow_local_deployment: bool, name: &str) -> bool { *name == *gethostname() && allow_local_deployment}
#[derive(derive_more::Display)]pub enum Push<'a> { Derivation(&'a Derivation), Path(&'a String),}
#[derive(Deserialize, Clone, Debug)]pub struct Derivation(String);
impl Display for Derivation { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { self.0.fmt(f).and_then(|()| write!(f, "^*")) }}
#[derive(derive_more::Display, Debug, Clone, Copy, PartialEq, Eq)]pub enum SwitchToConfigurationGoal { Switch, Boot, Test, DryActivate,}
#[derive(derive_more::Display, Debug, Clone, Copy)]pub enum ApplyGoal { SwitchToConfiguration(SwitchToConfigurationGoal), Build, Push, Keys,}
#[enum_dispatch]pub(crate) trait ExecuteStep: Send + Sync + Display + std::fmt::Debug { async fn execute(&self, ctx: &mut Context) -> Result<(), HiveLibError>;}
// may include other options such as FailAll in the future#[non_exhaustive]#[derive(Clone, Copy, Default)]pub enum HandleUnreachable { Ignore, #[default] FailNode,}
#[derive(Default)]pub struct StepState { pub evaluation: Option<Derivation>, pub evaluation_rx: Option<oneshot::Receiver<Result<Derivation, HiveLibError>>>, pub build: Option<String>, pub key_agent_directory: Option<String>,}
pub struct Context { pub hive_location: Arc<HiveLocation>, pub modifiers: SubCommandModifiers, pub state: StepState, pub should_quit: Arc<AtomicBool>, pub name: Name,}
#[enum_dispatch(ExecuteStep)]#[derive(Debug)]#[cfg_attr(test, derive(PartialEq))]pub enum Step { Ping, PushKeyAgent, Keys, Evaluate, PushEvaluatedOutput, Build, PushBuildOutput, SwitchToConfiguration,}
impl Display for Step { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { match self { Self::Ping(step) => step.fmt(f), Self::PushKeyAgent(step) => step.fmt(f), Self::Keys(step) => step.fmt(f), Self::Evaluate(step) => step.fmt(f), Self::PushEvaluatedOutput(step) => step.fmt(f), Self::Build(step) => step.fmt(f), Self::PushBuildOutput(step) => step.fmt(f), Self::SwitchToConfiguration(step) => step.fmt(f), } }}
#[cfg(test)]mod tests { use rand::distr::Alphabetic;
use super::*; use std::{assert_matches::assert_matches, env};
#[test] fn test_ssh_opts() { let target = Target::from_host("hello-world"); let subcommand_modifiers = SubCommandModifiers { non_interactive: false, ..Default::default() }; let tmp = format!( "/tmp/{}", rand::distr::SampleString::sample_string(&Alphabetic, &mut rand::rng(), 10) );
std::fs::create_dir(&tmp).unwrap();
unsafe { env::set_var("XDG_RUNTIME_DIR", &tmp) }
let args = [ "-l".to_string(), target.user.to_string(), "-p".to_string(), target.port.to_string(), "-o".to_string(), "StrictHostKeyChecking=accept-new".to_string(), "-o".to_string(), "BatchMode=yes".to_string(), ];
assert_eq!(target.create_ssh_args(subcommand_modifiers).unwrap(), args); assert_eq!( target.create_ssh_opts(subcommand_modifiers).unwrap(), args.join(" ") );
assert_eq!( target.create_ssh_args(subcommand_modifiers).unwrap(), [ "-l".to_string(), target.user.to_string(), "-p".to_string(), target.port.to_string(), "-o".to_string(), "StrictHostKeyChecking=accept-new".to_string(), "-o".to_string(), "BatchMode=yes".to_string(), ] );
assert_eq!( target.create_ssh_args(subcommand_modifiers).unwrap(), [ "-l".to_string(), target.user.to_string(), "-p".to_string(), target.port.to_string(), "-o".to_string(), "StrictHostKeyChecking=accept-new".to_string(), "-o".to_string(), "BatchMode=yes".to_string(), ] );
// forced non interactive is the same as --non-interactive assert_eq!( target.create_ssh_args(subcommand_modifiers).unwrap(), target .create_ssh_args(SubCommandModifiers { non_interactive: true, ..Default::default() }) .unwrap() ); }
#[test] fn target_fails_increments() { let mut target = Target::from_host("localhost");
assert_eq!(target.current_host, 0);
for i in 0..100 { target.host_failed(); assert_eq!(target.current_host, i + 1); } }
#[test] fn get_preferred_host_fails() { let mut target = Target { hosts: vec![ "un.reachable.1".into(), "un.reachable.2".into(), "un.reachable.3".into(), "un.reachable.4".into(), "un.reachable.5".into(), ], ..Default::default() };
assert_ne!( target.get_preferred_host().unwrap().to_string(), "un.reachable.5" );
for i in 1..=5 { assert_eq!( target.get_preferred_host().unwrap().to_string(), format!("un.reachable.{i}") ); target.host_failed(); }
for _ in 0..5 { assert_matches!( target.get_preferred_host(), Err(HiveLibError::NetworkError(NetworkError::HostsExhausted)) ); } }}