difftreelog
feat ability to select specialisation to activate
in: trunk
6 files changed
Cargo.lockdiffbeforeafterboth--- a/Cargo.lock
+++ b/Cargo.lock
@@ -784,7 +784,7 @@
"itertools",
"nix-eval",
"nixlike",
- "once_cell",
+ "nom",
"openssh",
"owo-colors",
"peg",
cmds/fleet/Cargo.tomldiffbeforeafterboth--- a/cmds/fleet/Cargo.toml
+++ b/cmds/fleet/Cargo.toml
@@ -19,7 +19,6 @@
serde_json.workspace = true
tempfile.workspace = true
time = { version = "0.3", features = ["serde"] }
-once_cell = "1.19"
hostname = "0.4.0"
age-core = "0.10"
peg = "0.8"
@@ -45,6 +44,7 @@
human-repr = { version = "1.1", optional = true }
indicatif = { version = "0.17", optional = true }
nix-eval.workspace = true
+nom = "7.1.3"
[features]
# Not quite stable
cmds/fleet/src/cmds/build_systems.rsdiffbeforeafterboth126 action: DeployAction,126 action: DeployAction,127 host: &ConfigHost,127 host: &ConfigHost,128 built: PathBuf,128 built: PathBuf,129 specialisation: Option<String>,129 disable_rollback: bool,130 disable_rollback: bool,130) -> Result<()> {131) -> Result<()> {131 let mut failed = false;132 let mut failed = false;190 if action.should_activate() && !failed {191 if action.should_activate() && !failed {191 let _span = info_span!("activating").entered();192 let _span = info_span!("activating").entered();192 info!("executing activation script");193 info!("executing activation script");193 let mut switch_script = built.clone();194 let specialised = if let Some(specialisation) = specialisation {194 switch_script.push("bin");195 let mut specialised = built.join("specialisation");196 specialised.push(specialisation);197 specialised198 } else {199 built.clone()200 };195 switch_script.push("switch-to-configuration");201 let switch_script = specialised.join("bin/switch-to-configuration");196 let mut cmd = host.cmd(switch_script).in_current_span().await?;202 let mut cmd = host.cmd(switch_script).in_current_span().await?;197 cmd.arg(action.name().expect("upload.should_activate == false"));203 cmd.arg(action.name().expect("upload.should_activate == false"));198 if let Err(e) = cmd.sudo().run().in_current_span().await {204 if let Err(e) = cmd.sudo().run().in_current_span().await {255 .system261 .system256 .build[{ build_attr }]262 .build[{ build_attr }]257 );263 );258 let outputs = drv.build().await.map_err(|e| {264 let outputs = drv.build().await.inspect_err(|_| {259 if build_attr == "sdImage" {265 if build_attr == "sdImage" {260 info!("sd-image build failed");266 info!("sd-image build failed");261 info!("Make sure you have imported modulesPath/installer/sd-card/sd-image-<arch>[-installer].nix (For installer, you may want to check config)");267 info!("Make sure you have imported modulesPath/installer/sd-card/sd-image-<arch>[-installer].nix (For installer, you may want to check config)");262 }268 }263 e264 })?;269 })?;265 let out_output = outputs270 let out_output = outputs266 .get("out")271 .get("out")275 let set = LocalSet::new();280 let set = LocalSet::new();276 let build_attr = self.build_attr.clone();281 let build_attr = self.build_attr.clone();277 for host in hosts.into_iter() {282 for host in hosts.into_iter() {278 if config.should_skip(&host.name) {283 if config.should_skip(&host).await? {279 continue;284 continue;280 }285 }281 let config = config.clone();286 let config = config.clone();324 let hosts = config.list_hosts().await?;329 let hosts = config.list_hosts().await?;325 let set = LocalSet::new();330 let set = LocalSet::new();326 for host in hosts.into_iter() {331 for host in hosts.into_iter() {327 if config.should_skip(&host.name) {332 if config.should_skip(&host).await? {328 continue;333 continue;329 }334 }330 let config = config.clone();335 let config = config.clone();383 deploy_task(self.action, &host, built, self.disable_rollback).await388 self.action,389 &host,390 built,391 if let Ok(v) = config.action_attr(&host, "specialisation").await {392 v393 } else {394 error!("unreachable? failed to get specialization");395 return;396 },397 self.disable_rollback,398 )399 .await384 {400 {cmds/fleet/src/cmds/secrets/mod.rsdiffbeforeafterboth--- a/cmds/fleet/src/cmds/secrets/mod.rs
+++ b/cmds/fleet/src/cmds/secrets/mod.rs
@@ -436,7 +436,7 @@
match self {
Secret::ForceKeys => {
for host in config.list_hosts().await? {
- if config.should_skip(&host.name) {
+ if config.should_skip(&host).await? {
continue;
}
config.key(&host.name).await?;
@@ -639,7 +639,7 @@
}
}
for host in config.list_hosts().await? {
- if config.should_skip(&host.name) {
+ if config.should_skip(&host).await? {
continue;
}
cmds/fleet/src/host.rsdiffbeforeafterboth--- a/cmds/fleet/src/host.rs
+++ b/cmds/fleet/src/host.rs
@@ -1,4 +1,6 @@
use std::{
+ cell::OnceCell,
+ collections::BTreeMap,
env::current_dir,
ffi::{OsStr, OsString},
fmt::Display,
@@ -10,9 +12,16 @@
};
use anyhow::{anyhow, bail, ensure, Context, Result};
-use clap::{ArgGroup, Parser};
+use clap::Parser;
use fleet_shared::SecretData;
use nix_eval::{nix_go, nix_go_json, NixSessionPool, Value};
+use nom::{
+ bytes::complete::take_while1,
+ character::complete::char,
+ combinator::{map, opt},
+ multi::separated_list1,
+ sequence::{preceded, separated_pair},
+};
use openssh::SessionBuilder;
use serde::de::DeserializeOwned;
use tempfile::NamedTempFile;
@@ -53,10 +62,26 @@
pub name: String,
pub local: bool,
pub session: OnceLock<Arc<openssh::Session>>,
+ groups: OnceCell<Vec<String>>,
pub nixos_config: Option<Value>,
}
impl ConfigHost {
+ pub async fn tags(&self) -> Result<Vec<String>> {
+ if let Some(v) = self.groups.get() {
+ return Ok(v.clone());
+ }
+ // TOCTOU is possible here in case if config is changed, but this case is not handled anywhere anyway,
+ // assuming getting tags always returns the same value.
+ let Some(nixos_config) = &self.nixos_config else {
+ return Ok(vec![]);
+ };
+ let tags: Vec<String> = nix_go_json!(nixos_config.tags);
+
+ let _ = self.groups.set(tags.clone());
+
+ Ok(tags)
+ }
async fn open_session(&self) -> Result<Arc<openssh::Session>> {
assert!(!self.local, "do not open ssh connection to local session");
// FIXME: TOCTOU
@@ -217,15 +242,71 @@
}
impl Config {
- pub fn should_skip(&self, host: &str) -> bool {
- if !self.opts.skip.is_empty() {
- self.opts.skip.iter().any(|h| h as &str == host)
- } else if !self.opts.only.is_empty() {
- !self.opts.only.iter().any(|h| h as &str == host)
- } else {
- false
+ pub async fn should_skip(&self, host: &ConfigHost) -> Result<bool> {
+ if !self.opts.skip.is_empty() && self.opts.skip.iter().any(|h| h as &str == host.name) {
+ return Ok(true);
+ }
+ if self.opts.only.is_empty() {
+ return Ok(false);
+ }
+ let mut have_group_matches = false;
+ for item in self.opts.only.iter() {
+ match item {
+ HostItem::Host { name, .. } if *name == host.name => {
+ return Ok(false);
+ }
+ HostItem::Tag { .. } => {
+ have_group_matches = true;
+ }
+ _ => {}
+ }
}
+ if have_group_matches {
+ let host_tags = host.tags().await?;
+ for item in self.opts.only.iter() {
+ match item {
+ HostItem::Tag { name, .. } if host_tags.contains(name) => {
+ return Ok(false);
+ }
+ _ => {}
+ }
+ }
+ }
+ Ok(true)
}
+ pub async fn action_attr(&self, host: &ConfigHost, attr: &str) -> Result<Option<String>> {
+ if self.opts.only.is_empty() {
+ return Ok(None);
+ }
+ let mut have_group_matches = false;
+ for item in self.opts.only.iter() {
+ match item {
+ HostItem::Host { name, attrs }
+ if *name == host.name && attrs.contains_key(attr) =>
+ {
+ return Ok(attrs.get(attr).cloned());
+ }
+ HostItem::Tag { attrs, .. } if attrs.contains_key(attr) => {
+ have_group_matches = true;
+ }
+ _ => {}
+ }
+ }
+ if have_group_matches {
+ let host_tags = host.tags().await?;
+ for item in self.opts.only.iter() {
+ match item {
+ HostItem::Tag { name, attrs }
+ if host_tags.contains(name) && attrs.contains_key(attr) =>
+ {
+ return Ok(attrs.get(attr).cloned());
+ }
+ _ => {}
+ }
+ }
+ }
+ Ok(None)
+ }
pub fn is_local(&self, host: &str) -> bool {
self.opts.localhost.as_ref().map(|s| s as &str) == Some(host)
}
@@ -237,6 +318,11 @@
local: true,
session: OnceLock::new(),
nixos_config: None,
+ groups: {
+ let cell = OnceCell::new();
+ let _ = cell.set(vec![]);
+ cell
+ },
}
}
@@ -249,6 +335,7 @@
local: self.is_local(name),
session: OnceLock::new(),
nixos_config: Some(nixos_config),
+ groups: OnceCell::new(),
})
}
pub async fn list_hosts(&self) -> Result<Vec<ConfigHost>> {
@@ -356,15 +443,59 @@
}
}
+#[derive(Clone)]
+enum HostItem {
+ Host {
+ name: String,
+ attrs: BTreeMap<String, String>,
+ },
+ Tag {
+ name: String,
+ attrs: BTreeMap<String, String>,
+ },
+}
+fn host_item_parser(input: &str) -> Result<HostItem, String> {
+ fn err_to_string(err: nom::Err<nom::error::Error<&str>>) -> String {
+ err.to_string()
+ }
+
+ let (input, is_tag) = map(opt(char('@')), |c| c.is_some())(input).map_err(err_to_string)?;
+ let (input, name) = map(
+ take_while1(|v| v != ',' && v != '?' && v != '@'),
+ str::to_owned,
+ )(input)
+ .map_err(err_to_string)?;
+
+ let kw_item = separated_pair(
+ map(take_while1(|v| v != '&' && v != '='), str::to_owned),
+ char('='),
+ map(take_while1(|v| v != '&'), str::to_owned),
+ );
+ let kw = map(separated_list1(char('&'), kw_item), |vec| {
+ vec.into_iter().collect::<BTreeMap<_, _>>()
+ });
+ let mut opt_kw = map(opt(preceded(char('?'), kw)), Option::unwrap_or_default);
+
+ let (input, attrs) = opt_kw(input).map_err(err_to_string)?;
+
+ if !input.is_empty() {
+ return Err(format!("unexpected trailing input: {input:?}"));
+ }
+ Ok(if is_tag {
+ HostItem::Tag { name, attrs }
+ } else {
+ HostItem::Host { name, attrs }
+ })
+}
+
#[derive(Parser, Clone)]
-#[clap(group = ArgGroup::new("target_hosts"))]
pub struct FleetOpts {
/// All hosts except those would be skipped
- #[clap(long, number_of_values = 1, group = "target_hosts")]
- only: Vec<String>,
+ #[clap(long, number_of_values = 1, value_parser = host_item_parser)]
+ only: Vec<HostItem>,
/// Hosts to skip
- #[clap(long, number_of_values = 1, group = "target_hosts")]
+ #[clap(long, number_of_values = 1)]
skip: Vec<String>,
/// Host, which should be threaten as current machine
flake.lockdiffbeforeafterboth--- a/flake.lock
+++ b/flake.lock
@@ -7,11 +7,11 @@
]
},
"locked": {
- "lastModified": 1720226507,
- "narHash": "sha256-yHVvNsgrpyNTXZBEokL8uyB2J6gB1wEx0KOJzoeZi1A=",
+ "lastModified": 1721699339,
+ "narHash": "sha256-UqtSwU13vpzzM6w8tGghEbA7ObM3NCDzSpz19QQo9XE=",
"owner": "ipetkov",
"repo": "crane",
- "rev": "0aed560c5c0a61c9385bddff471a13036203e11c",
+ "rev": "0081e9c447f3b70822c142908f08ceeb436982b8",
"type": "github"
},
"original": {
@@ -40,11 +40,11 @@
},
"nixpkgs": {
"locked": {
- "lastModified": 1720525988,
- "narHash": "sha256-6Vvrwl2rKrRt5gAYTFlM/pihCwHw8SY2o81TBm7KhIQ=",
+ "lastModified": 1721814637,
+ "narHash": "sha256-L3QkCvxeByJfW45wLkdZ9pL5h9PezOwwfx7G2sRfjiU=",
"owner": "nixos",
"repo": "nixpkgs",
- "rev": "a630e7a8476e51b116f1ca7444dbad20701823d7",
+ "rev": "e0c444a0b8413a31df199052f5714d409dc4c1d0",
"type": "github"
},
"original": {
@@ -68,11 +68,11 @@
},
"nixpkgs-stable-for-tests": {
"locked": {
- "lastModified": 1720386169,
- "narHash": "sha256-NGKVY4PjzwAa4upkGtAMz1npHGoRzWotlSnVlqI40mo=",
+ "lastModified": 1721548954,
+ "narHash": "sha256-7cCC8+Tdq1+3OPyc3+gVo9dzUNkNIQfwSDJ2HSi2u3o=",
"owner": "nixos",
"repo": "nixpkgs",
- "rev": "194846768975b7ad2c4988bdb82572c00222c0d7",
+ "rev": "63d37ccd2d178d54e7fb691d7ec76000740ea24a",
"type": "github"
},
"original": {
@@ -98,11 +98,11 @@
]
},
"locked": {
- "lastModified": 1720491570,
- "narHash": "sha256-PHS2BcQ9kxBpu9GKlDg3uAlrX/ahQOoAiVmwGl6BjD4=",
+ "lastModified": 1721810656,
+ "narHash": "sha256-33UCMmgPL+sz06+iupNkl99hcBABP56ENcxSoKqr0TY=",
"owner": "oxalica",
"repo": "rust-overlay",
- "rev": "b970af40fdc4bd80fd764796c5f97c15e2b564eb",
+ "rev": "a6afdaab4a47d6ecf647a74968e92a51c4a18e5a",
"type": "github"
},
"original": {