feat: make sure to check the right file contents

Signed-off-by: kjuulh <contact@kjuulh.io>
This commit is contained in:
Kasper Juul Hermansen 2025-01-04 01:52:22 +01:00
parent c647adff54
commit 451270f64b

View File

@ -27,6 +27,7 @@ impl Guest for Component {
);
if !output.contains("enabled") {
println!("alloy is not enabled");
return true;
}
@ -38,11 +39,21 @@ impl Guest for Component {
);
if output.contains("inactive") {
println!("alloy is inactive");
return true;
}
match std::fs::read_to_string(ALLOY_CONFIG_PATH) {
Ok(content) => return content != ALLOY_CONFIG_FILE,
Ok(content) => {
let process = Process::new();
let node_name = process.get_variable("node_name");
let config_file = generate_alloy_file(node_name);
let is_different = content != config_file;
if is_different {
println!("config file was different");
return true;
}
}
Err(e) => {
if e.kind() == std::io::ErrorKind::NotFound {
return true;
@ -69,17 +80,13 @@ impl Guest for Component {
install_alloy().expect("to be able to install alloy");
}
let config_file = generate_alloy_file(node_name);
let restart = match std::fs::read_to_string(ALLOY_CONFIG_PATH) {
Ok(content) => {
let mut env = Environment::new();
env.add_template("alloy.config", &content).unwrap();
let tmpl = env.get_template("alloy.config").unwrap();
let content = tmpl.render(context! {node_name => node_name}).unwrap();
if content != ALLOY_CONFIG_FILE {
if content != config_file {
let mut file = std::fs::File::create(ALLOY_CONFIG_PATH)
.expect("to be able to create file");
file.write_all(ALLOY_CONFIG_FILE.as_bytes())
file.write_all(config_file.as_bytes())
.expect("to be able to write file");
file.flush().expect("to be able to flush file");
true
@ -96,7 +103,7 @@ impl Guest for Component {
let mut file = std::fs::File::create(ALLOY_CONFIG_PATH)
.expect("to be able to create file");
file.write_all(ALLOY_CONFIG_FILE.as_bytes())
file.write_all(config_file.as_bytes())
.expect("to be able to write file");
file.flush().expect("to be able to flush file");
@ -112,18 +119,17 @@ impl Guest for Component {
return;
}
let output = Process::new().run_process(
&["systemctl", "is-active", "alloy.service"]
.into_iter()
.map(|i| i.into())
.collect::<Vec<String>>(),
);
if output.contains("inactive") {
run_and_activate_alloy().expect("to be able to active alloy");
}
run_and_activate_alloy().expect("to be able to active alloy");
}
}
fn generate_alloy_file(node_name: String) -> String {
let mut env = Environment::new();
env.add_template("alloy.config", ALLOY_CONFIG_FILE).unwrap();
let tmpl = env.get_template("alloy.config").unwrap();
tmpl.render(context! {node_name => node_name}).unwrap()
}
fn install_alloy() -> Result<(), String> {
println!("=== installing alloy ===");
run_command(["apt", "install", "-y", "gpg"])?;