Properly configure changed optional dependencies (#1144)

* add logic to accept optional dependency list and determine dependencies based on new service configurations

* add debugs

* properly extend iterator
This commit is contained in:
Lucy C
2022-01-28 12:55:27 -07:00
committed by GitHub
parent 6dea3c723e
commit b13911ec12
2 changed files with 36 additions and 4 deletions

View File

@@ -1,10 +1,12 @@
use std::borrow::Cow;
use std::collections::BTreeMap;
use std::collections::{BTreeMap, BTreeSet, HashMap};
use std::path::Path;
use beau_collector::BeauCollector;
use embassy::config::action::SetResult;
use embassy::config::{spec, Config};
use embassy::s9pk::manifest::PackageId;
use embassy::status::health_check::HealthCheckId;
use linear_map::LinearMap;
pub mod rules;
@@ -12,15 +14,39 @@ pub mod rules;
use anyhow::anyhow;
pub use rules::{ConfigRuleEntry, ConfigRuleEntryWithSuggestions};
use self::rules::ConfigRule;
pub type DepInfo = HashMap<PackageId, DepRuleInfo>;
#[derive(Clone, Debug, serde::Deserialize)]
pub struct DepRuleInfo {
condition: ConfigRule,
health_checks: BTreeSet<HealthCheckId>,
}
pub fn validate_configuration(
name: &str,
config: Config,
rules_path: &Path,
config_path: &Path,
maybe_deps_path: Option<&str>,
) -> Result<SetResult, anyhow::Error> {
let rules: Vec<ConfigRuleEntry> = serde_yaml::from_reader(std::fs::File::open(rules_path)?)?;
let mut cfgs = LinearMap::new();
cfgs.insert(name, Cow::Borrowed(&config));
let mut depends_on = BTreeMap::new();
if let Some(deps_path) = maybe_deps_path.map(Path::new) {
if deps_path.exists() {
let deps: DepInfo = serde_yaml::from_reader(std::fs::File::open(deps_path)?)?;
// check if new config is set to depend on any optional dependencies
depends_on.extend(
deps.into_iter()
.filter(|(_, data)| (data.condition.compiled)(&config, &cfgs))
.map(|(pkg_id, data)| (pkg_id, data.health_checks)),
);
};
}
// check that all configuration rules
let rule_check = rules
.into_iter()
.map(|r| r.check(&config, &cfgs))
@@ -35,7 +61,7 @@ pub fn validate_configuration(
std::fs::rename(config_path.with_extension("tmp"), config_path)?;
// return set result
Ok(SetResult {
depends_on: BTreeMap::new(),
depends_on,
// sending sigterm so service is restarted - in 0.3.x services, this is whatever signal is needed to send to the process to pick up the configuration
signal: Some(nix::sys::signal::SIGTERM),
})