git.delta.rocks / jrsonnet / refs/commits / faec7071817b

difftreelog

source

crates/fleet-base/src/opts.rs7.2 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::{11	FetchSettings, FlakeLockFlags, FlakeReference, FlakeReferenceParseFlags, FlakeSettings, Value,12	gc_now, nix_go, util::assert_warn,13};14use nom::{15	Parser,16	bytes::complete::take_while1,17	character::complete::char,18	combinator::{map, opt},19	multi::separated_list1,20	sequence::{preceded, separated_pair},21};2223use crate::{24	fleetdata::FleetData,25	host::{Config, ConfigHost, FleetConfigInternals},26	primops::{PRIMOPS_DATA, init_primops},27};2829#[derive(Clone)]30pub enum HostItem {31	Host {32		name: String,33		attrs: BTreeMap<String, String>,34	},35	Tag {36		name: String,37		attrs: BTreeMap<String, String>,38	},39}40fn host_item_parser(input: &str) -> Result<HostItem, String> {41	fn err_to_string(err: nom::Err<nom::error::Error<&str>>) -> String {42		err.to_string()43	}4445	let (input, is_tag) = map(opt(char('@')), |c| c.is_some())46		.parse_complete(input)47		.map_err(err_to_string)?;48	let (input, name) = map(49		take_while1(|v| v != ',' && v != '?' && v != '@'),50		str::to_owned,51	)52	.parse_complete(input)53	.map_err(err_to_string)?;5455	let kw_item = separated_pair(56		map(take_while1(|v| v != '&' && v != '='), str::to_owned),57		char('='),58		map(take_while1(|v| v != '&'), str::to_owned),59	);60	let kw = map(separated_list1(char('&'), kw_item), |vec| {61		vec.into_iter().collect::<BTreeMap<_, _>>()62	});63	let mut opt_kw = map(opt(preceded(char('?'), kw)), Option::unwrap_or_default);6465	let (input, attrs) = opt_kw.parse_complete(input).map_err(err_to_string)?;6667	if !input.is_empty() {68		return Err(format!("unexpected trailing input: {input:?}"));69	}70	Ok(if is_tag {71		HostItem::Tag { name, attrs }72	} else {73		HostItem::Host { name, attrs }74	})75}7677// TODO: Rename to HostSelector78#[derive(clap::Parser, Clone)]79pub struct FleetOpts {80	/// All hosts except those would be skipped81	#[clap(long, number_of_values = 1, value_parser = host_item_parser)]82	pub only: Vec<HostItem>,8384	/// Hosts to skip85	#[clap(long, number_of_values = 1)]86	pub skip: Vec<String>,8788	/// Host, which should be threaten as current machine89	// TODO: Replace with connectivity refactor90	#[clap(long, default_value_t = hostname::get().expect("unknown hostname").to_str().expect("hostname is not utf-8").to_owned())]91	pub localhost: String,9293	/// Override detected system for host, to perform builds via94	/// binfmt-declared qemu instead of trying to crosscompile95	#[clap(long, default_value = env!("NIX_SYSTEM"))]96	pub local_system: String,9798	/// By default fleet continues on single derivation build failure99	/// this flag makes command fail immediately100	///101	/// Opposite of Nix's --keep-going102	#[clap(long)]103	pub fail_fast: bool,104}105106impl FleetOpts {107	pub fn filter_skipped(108		&self,109		hosts: impl IntoIterator<Item = ConfigHost>,110	) -> Result<Vec<ConfigHost>> {111		let mut out = Vec::new();112		for host in hosts {113			if self.should_skip(&host)? {114				continue;115			}116			out.push(host);117		}118		Ok(out)119	}120	pub fn should_skip(&self, host: &ConfigHost) -> Result<bool> {121		if self.skip.iter().any(|h| h as &str == host.name) {122			return Ok(true);123		}124		if self.only.is_empty() {125			return Ok(false);126		}127		let mut have_group_matches = false;128		for item in self.only.iter() {129			match item {130				HostItem::Host { name, .. } if *name == host.name => {131					return Ok(false);132				}133				HostItem::Tag { .. } => {134					have_group_matches = true;135				}136				_ => {}137			}138		}139		if have_group_matches {140			let host_tags = host.tags()?;141			for item in self.only.iter() {142				match item {143					HostItem::Tag { name, .. } if host_tags.contains(name) => {144						return Ok(false);145					}146					_ => {}147				}148			}149		}150		Ok(true)151	}152	pub fn action_attr<T: FromStr>(&self, host: &ConfigHost, attr: &str) -> Result<Option<T>>153	where154		T::Err: Sync,155		anyhow::Error: From<T::Err>,156	{157		let str = self.action_attr_str(host, attr)?;158		Ok(str.map(|v| T::from_str(&v)).transpose()?)159	}160	pub fn action_attr_str(&self, host: &ConfigHost, attr: &str) -> Result<Option<String>> {161		if self.only.is_empty() {162			return Ok(None);163		}164		let mut have_group_matches = false;165		for item in self.only.iter() {166			match item {167				HostItem::Host { name, attrs }168					if *name == host.name && attrs.contains_key(attr) =>169				{170					return Ok(attrs.get(attr).cloned());171				}172				HostItem::Tag { attrs, .. } if attrs.contains_key(attr) => {173					have_group_matches = true;174				}175				_ => {}176			}177		}178		if have_group_matches {179			let host_tags = host.tags()?;180			for item in self.only.iter() {181				match item {182					HostItem::Tag { name, attrs }183						if host_tags.contains(name) && attrs.contains_key(attr) =>184					{185						return Ok(attrs.get(attr).cloned());186					}187					_ => {}188				}189			}190		}191		Ok(None)192	}193	pub fn is_local(&self, host: &str) -> bool {194		self.localhost == host195	}196197	// TODO: Config should be detached from opts.198	pub fn build(&self, nix_args: Vec<OsString>, assert: bool) -> Result<Config> {199		let cwd = current_dir()?;200		let mut directory = cwd.clone();201		let mut fleet_data_path = directory.join("fleet.nix");202		while !fleet_data_path.is_file() {203			// fleet.nix204			fleet_data_path.pop();205			if !directory.pop() || !fleet_data_path.pop() {206				bail!(207					"fleet.nix not found at {} or any of the parent directories",208					cwd.display()209				);210			}211			fleet_data_path.push("fleet.nix");212		}213		let bytes =214			std::fs::read_to_string(&fleet_data_path).context("reading fleet state (fleet.nix)")?;215		let data = Arc::new(Mutex::new(FleetData::from_str(&bytes)?));216217		init_primops();218219		let mut fetch_settings = FetchSettings::new();220		fetch_settings.set(c"warn-dirty", c"false");221222		let mut flake_settings = FlakeSettings::new()?;223		let mut parse = FlakeReferenceParseFlags::new(&flake_settings)?;224		// For some reason, lazy trees not being used when there is no base dir set225		parse.set_base_dir("/")?;226227		let (mut flake, _) = FlakeReference::new(228			directory229				.to_str()230				.ok_or_else(|| anyhow::anyhow!("fleet dir should have utf-8 path"))?,231			&flake_settings,232			&parse,233			&fetch_settings,234		)?;235236		let lock = FlakeLockFlags::new(&flake_settings)?;237238		let flake = flake.lock(&fetch_settings, &flake_settings, &lock)?;239240		let flake = flake.get_attrs(&mut flake_settings)?;241242		let builtins_field = Value::eval("builtins")?;243244		let fleet_root = flake.get_field("fleetConfigurations")?;245		let fleet_field = nix_go!(fleet_root.default(Obj {}));246247		let config_field = nix_go!(fleet_field.config);248249		if assert {250			assert_warn("fleet config evaluation", &config_field)251				.context("failed to verify assertions")?;252		}253254		let import = nix_go!(builtins_field.import);255		let overlays = nix_go!(config_field.nixpkgs.overlays);256		let nixpkgs = nix_go!(config_field.nixpkgs.buildUsing);257		let nixpkgs_imported = nix_go!(import(nixpkgs));258259		let default_pkgs = nix_go!(nixpkgs_imported(Obj {260			overlays,261			system: self.local_system.clone(),262		}));263264		if cfg!(debug_assertions) {265			gc_now();266		}267		let config = Config(Arc::new(FleetConfigInternals {268			directory,269			data,270			flake_outputs: flake,271			local_system: self.local_system.clone(),272			nix_args,273			config_field,274			default_pkgs,275			nixpkgs,276			localhost: self.localhost.to_owned(),277		}));278279		PRIMOPS_DATA280			.set(config.clone())281			.map_err(|_| ())282			.expect("only one fleet config may exist per process");283		Ok(config)284	}285}