2021-02-08 18:38:14 -08:00
|
|
|
use std::collections::HashMap;
|
2021-11-20 23:34:52 -08:00
|
|
|
use std::hash::Hash;
|
|
|
|
use std::ops::Deref;
|
2021-02-10 18:17:49 -08:00
|
|
|
use std::path::Path;
|
2020-12-15 20:21:26 -08:00
|
|
|
|
2022-01-08 01:20:36 -08:00
|
|
|
use serde::de;
|
2021-11-20 23:34:52 -08:00
|
|
|
use serde::{Deserialize, Deserializer, Serialize};
|
2021-10-23 13:22:35 +02:00
|
|
|
use users::get_current_username;
|
2022-01-08 01:20:36 -08:00
|
|
|
use validator::{Validate, ValidationError as ValidationErrorType};
|
|
|
|
|
|
|
|
use crate::error::{ColmenaResult, ColmenaError};
|
2021-01-24 14:08:48 -08:00
|
|
|
|
2020-12-19 15:07:29 -08:00
|
|
|
pub mod host;
|
2021-01-13 12:20:27 -08:00
|
|
|
pub use host::{Host, CopyDirection, CopyOptions};
|
2021-02-08 18:38:14 -08:00
|
|
|
use host::Ssh;
|
2020-12-15 20:21:26 -08:00
|
|
|
|
2021-01-24 14:08:48 -08:00
|
|
|
pub mod hive;
|
2021-06-29 01:02:43 -07:00
|
|
|
pub use hive::{Hive, HivePath};
|
2021-01-24 14:08:48 -08:00
|
|
|
|
|
|
|
pub mod store;
|
2022-01-01 16:41:35 -08:00
|
|
|
pub use store::{StorePath, StoreDerivation, BuildResult};
|
2021-01-24 14:08:48 -08:00
|
|
|
|
2021-02-08 18:38:14 -08:00
|
|
|
pub mod key;
|
|
|
|
pub use key::Key;
|
|
|
|
|
2021-01-24 14:08:48 -08:00
|
|
|
pub mod profile;
|
2021-12-07 23:13:31 -08:00
|
|
|
pub use profile::{Profile, ProfileDerivation};
|
2021-01-24 14:08:48 -08:00
|
|
|
|
|
|
|
pub mod deployment;
|
2021-11-20 23:34:52 -08:00
|
|
|
pub use deployment::Goal;
|
2020-12-15 20:21:26 -08:00
|
|
|
|
2021-06-29 01:02:43 -07:00
|
|
|
pub mod info;
|
|
|
|
pub use info::NixCheck;
|
|
|
|
|
2021-10-25 23:38:10 -07:00
|
|
|
pub mod flake;
|
|
|
|
pub use flake::Flake;
|
|
|
|
|
2021-11-20 23:34:52 -08:00
|
|
|
pub mod node_filter;
|
|
|
|
pub use node_filter::NodeFilter;
|
|
|
|
|
2022-01-08 01:20:36 -08:00
|
|
|
/// Path to the main system profile.
|
2021-11-23 13:33:23 -08:00
|
|
|
pub const SYSTEM_PROFILE: &str = "/nix/var/nix/profiles/system";
|
2020-12-19 16:28:34 -08:00
|
|
|
|
2022-01-08 01:20:36 -08:00
|
|
|
/// Path to the system profile that's currently active.
|
|
|
|
pub const CURRENT_PROFILE: &str = "/run/current-system";
|
|
|
|
|
2021-11-20 23:34:52 -08:00
|
|
|
/// A node's attribute name.
|
|
|
|
#[derive(Serialize, Deserialize, Clone, Debug, Hash, Eq, PartialEq)]
|
|
|
|
#[serde(transparent)]
|
|
|
|
pub struct NodeName(
|
|
|
|
#[serde(deserialize_with = "NodeName::deserialize")]
|
|
|
|
String
|
|
|
|
);
|
|
|
|
|
2021-02-08 18:38:14 -08:00
|
|
|
#[derive(Debug, Clone, Validate, Deserialize)]
|
2021-01-24 14:08:48 -08:00
|
|
|
pub struct NodeConfig {
|
2020-12-15 20:21:26 -08:00
|
|
|
#[serde(rename = "targetHost")]
|
2020-12-19 15:07:29 -08:00
|
|
|
target_host: Option<String>,
|
2020-12-15 20:21:26 -08:00
|
|
|
|
|
|
|
#[serde(rename = "targetUser")]
|
2021-10-23 13:22:35 +02:00
|
|
|
target_user: Option<String>,
|
2020-12-19 15:07:29 -08:00
|
|
|
|
2021-02-09 21:02:00 -08:00
|
|
|
#[serde(rename = "targetPort")]
|
|
|
|
target_port: Option<u16>,
|
|
|
|
|
2020-12-19 15:07:29 -08:00
|
|
|
#[serde(rename = "allowLocalDeployment")]
|
|
|
|
allow_local_deployment: bool,
|
2022-01-01 16:41:35 -08:00
|
|
|
|
|
|
|
#[serde(rename = "buildOnTarget")]
|
|
|
|
build_on_target: bool,
|
|
|
|
|
2020-12-15 20:21:26 -08:00
|
|
|
tags: Vec<String>,
|
2021-02-08 18:38:14 -08:00
|
|
|
|
2021-04-08 01:14:28 -07:00
|
|
|
#[serde(rename = "replaceUnknownProfiles")]
|
|
|
|
replace_unknown_profiles: bool,
|
|
|
|
|
2021-05-24 00:15:38 -07:00
|
|
|
#[serde(rename = "privilegeEscalationCommand")]
|
|
|
|
privilege_escalation_command: Vec<String>,
|
|
|
|
|
2021-02-08 18:38:14 -08:00
|
|
|
#[validate(custom = "validate_keys")]
|
|
|
|
keys: HashMap<String, Key>,
|
2020-12-15 20:21:26 -08:00
|
|
|
}
|
|
|
|
|
2021-11-20 23:34:52 -08:00
|
|
|
impl NodeName {
|
|
|
|
/// Returns the string.
|
|
|
|
pub fn as_str(&self) -> &str {
|
|
|
|
&self.0
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Creates a NodeName from a String.
|
2022-01-08 01:20:36 -08:00
|
|
|
pub fn new(name: String) -> ColmenaResult<Self> {
|
2021-11-20 23:34:52 -08:00
|
|
|
let validated = Self::validate(name)?;
|
|
|
|
Ok(Self(validated))
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Deserializes a potentially-invalid node name.
|
|
|
|
fn deserialize<'de, D>(deserializer: D) -> Result<String, D::Error>
|
|
|
|
where D: Deserializer<'de>
|
|
|
|
{
|
|
|
|
use de::Error;
|
|
|
|
String::deserialize(deserializer)
|
|
|
|
.and_then(|s| {
|
|
|
|
Self::validate(s).map_err(|e| Error::custom(e.to_string()))
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-01-08 01:20:36 -08:00
|
|
|
fn validate(s: String) -> ColmenaResult<String> {
|
2021-11-20 23:34:52 -08:00
|
|
|
// FIXME: Elaborate
|
2021-11-23 13:33:23 -08:00
|
|
|
if s.is_empty() {
|
2022-01-08 01:20:36 -08:00
|
|
|
return Err(ColmenaError::EmptyNodeName);
|
2021-11-20 23:34:52 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
Ok(s)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Deref for NodeName {
|
|
|
|
type Target = str;
|
|
|
|
|
|
|
|
fn deref(&self) -> &str {
|
|
|
|
self.0.as_str()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-24 14:08:48 -08:00
|
|
|
impl NodeConfig {
|
2020-12-18 01:27:44 -08:00
|
|
|
pub fn tags(&self) -> &[String] { &self.tags }
|
2020-12-19 15:07:29 -08:00
|
|
|
pub fn allows_local_deployment(&self) -> bool { self.allow_local_deployment }
|
|
|
|
|
2022-01-01 16:41:35 -08:00
|
|
|
pub fn build_on_target(&self) -> bool { self.build_on_target }
|
|
|
|
pub fn set_build_on_target(&mut self, enable: bool) {
|
|
|
|
self.build_on_target = enable;
|
|
|
|
}
|
|
|
|
|
2021-02-09 21:02:00 -08:00
|
|
|
pub fn to_ssh_host(&self) -> Option<Ssh> {
|
2020-12-19 15:07:29 -08:00
|
|
|
self.target_host.as_ref().map(|target_host| {
|
2021-10-23 13:22:35 +02:00
|
|
|
let username =
|
|
|
|
match &self.target_user {
|
|
|
|
Some(uname) => uname.clone(),
|
2021-10-23 20:49:14 -07:00
|
|
|
None => get_current_username().unwrap().into_string().unwrap(),
|
2021-10-23 13:22:35 +02:00
|
|
|
};
|
2021-11-23 13:33:23 -08:00
|
|
|
let mut host = Ssh::new(username, target_host.clone());
|
2021-05-24 00:15:38 -07:00
|
|
|
host.set_privilege_escalation_command(self.privilege_escalation_command.clone());
|
2021-02-09 21:02:00 -08:00
|
|
|
|
|
|
|
if let Some(target_port) = self.target_port {
|
|
|
|
host.set_port(target_port);
|
|
|
|
}
|
|
|
|
|
2020-12-19 15:07:29 -08:00
|
|
|
host
|
|
|
|
})
|
2020-12-18 01:27:44 -08:00
|
|
|
}
|
2020-12-15 20:21:26 -08:00
|
|
|
}
|
|
|
|
|
2021-02-08 18:38:14 -08:00
|
|
|
fn validate_keys(keys: &HashMap<String, Key>) -> Result<(), ValidationErrorType> {
|
|
|
|
// Bad secret names:
|
|
|
|
// - /etc/passwd
|
|
|
|
// - ../../../../../etc/passwd
|
|
|
|
|
|
|
|
for name in keys.keys() {
|
|
|
|
let path = Path::new(name);
|
|
|
|
if path.has_root() {
|
|
|
|
return Err(ValidationErrorType::new("Secret key name cannot be absolute"));
|
|
|
|
}
|
|
|
|
|
2021-11-23 13:33:23 -08:00
|
|
|
if path.components().count() != 1 {
|
2021-02-10 18:17:49 -08:00
|
|
|
return Err(ValidationErrorType::new("Secret key name cannot contain path separators"));
|
2021-02-08 18:38:14 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|