git.delta.rocks / jrsonnet / refs/commits / 94ece5cae749

difftreelog

source

crates/fleet-base/src/opts.rs6.5 KiBsourcehistory
1use std::{2	collections::BTreeMap,3	env::current_dir,4	ffi::OsString,5	str::FromStr,6	sync::{Arc, Mutex},7};89use anyhow::{Context, Result, bail};10use nix_eval::{NixSessionPool, Value, nix_go, util::assert_warn};11use nom::{12	Parser,13	bytes::complete::take_while1,14	character::complete::char,15	combinator::{map, opt},16	multi::separated_list1,17	sequence::{preceded, separated_pair},18};1920use crate::{21	fleetdata::FleetData,22	host::{Config, ConfigHost, FleetConfigInternals},23};2425#[derive(Clone)]26pub enum HostItem {27	Host {28		name: String,29		attrs: BTreeMap<String, String>,30	},31	Tag {32		name: String,33		attrs: BTreeMap<String, String>,34	},35}36fn host_item_parser(input: &str) -> Result<HostItem, String> {37	fn err_to_string(err: nom::Err<nom::error::Error<&str>>) -> String {38		err.to_string()39	}4041	let (input, is_tag) = map(opt(char('@')), |c| c.is_some())42		.parse_complete(input)43		.map_err(err_to_string)?;44	let (input, name) = map(45		take_while1(|v| v != ',' && v != '?' && v != '@'),46		str::to_owned,47	)48	.parse_complete(input)49	.map_err(err_to_string)?;5051	let kw_item = separated_pair(52		map(take_while1(|v| v != '&' && v != '='), str::to_owned),53		char('='),54		map(take_while1(|v| v != '&'), str::to_owned),55	);56	let kw = map(separated_list1(char('&'), kw_item), |vec| {57		vec.into_iter().collect::<BTreeMap<_, _>>()58	});59	let mut opt_kw = map(opt(preceded(char('?'), kw)), Option::unwrap_or_default);6061	let (input, attrs) = opt_kw.parse_complete(input).map_err(err_to_string)?;6263	if !input.is_empty() {64		return Err(format!("unexpected trailing input: {input:?}"));65	}66	Ok(if is_tag {67		HostItem::Tag { name, attrs }68	} else {69		HostItem::Host { name, attrs }70	})71}7273// TODO: Rename to HostSelector74#[derive(clap::Parser, Clone)]75pub struct FleetOpts {76	/// All hosts except those would be skipped77	#[clap(long, number_of_values = 1, value_parser = host_item_parser)]78	pub only: Vec<HostItem>,7980	/// Hosts to skip81	#[clap(long, number_of_values = 1)]82	pub skip: Vec<String>,8384	/// Host, which should be threaten as current machine85	// TODO: Replace with connectivity refactor86	#[clap(long, default_value_t = hostname::get().expect("unknown hostname").to_str().expect("hostname is not utf-8").to_owned())]87	pub localhost: String,8889	/// Override detected system for host, to perform builds via90	/// binfmt-declared qemu instead of trying to crosscompile91	#[clap(long, default_value = env!("NIX_SYSTEM"))]92	pub local_system: String,9394	/// By default fleet continues on single derivation build failure95	/// this flag makes command fail immediately96	///97	/// Opposite of Nix's --keep-going98	#[clap(long)]99	pub fail_fast: bool,100}101102impl FleetOpts {103	pub async fn filter_skipped(104		&self,105		hosts: impl IntoIterator<Item = ConfigHost>,106	) -> Result<Vec<ConfigHost>> {107		let mut out = Vec::new();108		for host in hosts {109			if self.should_skip(&host).await? {110				continue;111			}112			out.push(host);113		}114		Ok(out)115	}116	pub async fn should_skip(&self, host: &ConfigHost) -> Result<bool> {117		if self.skip.iter().any(|h| h as &str == host.name) {118			return Ok(true);119		}120		if self.only.is_empty() {121			return Ok(false);122		}123		let mut have_group_matches = false;124		for item in self.only.iter() {125			match item {126				HostItem::Host { name, .. } if *name == host.name => {127					return Ok(false);128				}129				HostItem::Tag { .. } => {130					have_group_matches = true;131				}132				_ => {}133			}134		}135		if have_group_matches {136			let host_tags = host.tags().await?;137			for item in self.only.iter() {138				match item {139					HostItem::Tag { name, .. } if host_tags.contains(name) => {140						return Ok(false);141					}142					_ => {}143				}144			}145		}146		Ok(true)147	}148	pub async fn action_attr<T: FromStr>(&self, host: &ConfigHost, attr: &str) -> Result<Option<T>>149	where150		T::Err: Sync,151		anyhow::Error: From<T::Err>,152	{153		let str = self.action_attr_str(host, attr).await?;154		Ok(str.map(|v| T::from_str(&v)).transpose()?)155	}156	pub async fn action_attr_str(&self, host: &ConfigHost, attr: &str) -> Result<Option<String>> {157		if self.only.is_empty() {158			return Ok(None);159		}160		let mut have_group_matches = false;161		for item in self.only.iter() {162			match item {163				HostItem::Host { name, attrs }164					if *name == host.name && attrs.contains_key(attr) =>165				{166					return Ok(attrs.get(attr).cloned());167				}168				HostItem::Tag { attrs, .. } if attrs.contains_key(attr) => {169					have_group_matches = true;170				}171				_ => {}172			}173		}174		if have_group_matches {175			let host_tags = host.tags().await?;176			for item in self.only.iter() {177				match item {178					HostItem::Tag { name, attrs }179						if host_tags.contains(name) && attrs.contains_key(attr) =>180					{181						return Ok(attrs.get(attr).cloned());182					}183					_ => {}184				}185			}186		}187		Ok(None)188	}189	pub fn is_local(&self, host: &str) -> bool {190		self.localhost == host191	}192193	// TODO: Config should be detached from opts.194	pub async fn build(&self, nix_args: Vec<OsString>, assert: bool) -> Result<Config> {195		let cwd = current_dir()?;196		let mut directory = cwd.clone();197		let mut fleet_data_path = directory.join("fleet.nix");198		while !fleet_data_path.is_file() {199			// fleet.nix200			fleet_data_path.pop();201			if !directory.pop() || !fleet_data_path.pop() {202				bail!(203					"fleet.nix not found at {} or any of the parent directories",204					cwd.display()205				);206			}207			fleet_data_path.push("fleet.nix");208		}209		let bytes =210			std::fs::read_to_string(&fleet_data_path).context("reading fleet state (fleet.nix)")?;211		let data: Mutex<FleetData> = nixlike::parse_str(&bytes)?;212213		let pool = NixSessionPool::new(214			directory.as_os_str().to_owned(),215			nix_args.clone(),216			self.local_system.clone(),217			self.fail_fast,218		)219		.await?;220		let nix_session = pool.get().await?;221222		let builtins_field = Value::binding(nix_session.clone(), "builtins").await?;223224		let fleet_root = Value::binding(nix_session.clone(), "fleetConfigurations").await?;225		let fleet_field = nix_go!(fleet_root.default({ data }));226227		let config_field = nix_go!(fleet_field.config);228229		if assert {230			assert_warn("fleet config evaluation", &config_field).await?;231		}232233		let import = nix_go!(builtins_field.import);234		let overlays = nix_go!(config_field.nixpkgs.overlays);235		let nixpkgs = nix_go!(config_field.nixpkgs.buildUsing);236		let nixpkgs_imported = nix_go!(nixpkgs | import);237238		let default_pkgs = nix_go!(nixpkgs_imported(Obj {239			overlays,240			system: self.local_system.clone(),241		}));242243		Ok(Config(Arc::new(FleetConfigInternals {244			nix_session,245			directory,246			data,247			local_system: self.local_system.clone(),248			nix_args,249			config_field,250			default_pkgs,251			nixpkgs,252			localhost: self.localhost.to_owned(),253		})))254	}255}