feat: make sure to check the right file contents
Signed-off-by: kjuulh <contact@kjuulh.io>
This commit is contained in:
parent
c647adff54
commit
451270f64b
@ -27,6 +27,7 @@ impl Guest for Component {
|
|||||||
);
|
);
|
||||||
|
|
||||||
if !output.contains("enabled") {
|
if !output.contains("enabled") {
|
||||||
|
println!("alloy is not enabled");
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -38,11 +39,21 @@ impl Guest for Component {
|
|||||||
);
|
);
|
||||||
|
|
||||||
if output.contains("inactive") {
|
if output.contains("inactive") {
|
||||||
|
println!("alloy is inactive");
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
match std::fs::read_to_string(ALLOY_CONFIG_PATH) {
|
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) => {
|
Err(e) => {
|
||||||
if e.kind() == std::io::ErrorKind::NotFound {
|
if e.kind() == std::io::ErrorKind::NotFound {
|
||||||
return true;
|
return true;
|
||||||
@ -69,17 +80,13 @@ impl Guest for Component {
|
|||||||
install_alloy().expect("to be able to install alloy");
|
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) {
|
let restart = match std::fs::read_to_string(ALLOY_CONFIG_PATH) {
|
||||||
Ok(content) => {
|
Ok(content) => {
|
||||||
let mut env = Environment::new();
|
if content != config_file {
|
||||||
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 {
|
|
||||||
let mut file = std::fs::File::create(ALLOY_CONFIG_PATH)
|
let mut file = std::fs::File::create(ALLOY_CONFIG_PATH)
|
||||||
.expect("to be able to create file");
|
.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");
|
.expect("to be able to write file");
|
||||||
file.flush().expect("to be able to flush file");
|
file.flush().expect("to be able to flush file");
|
||||||
true
|
true
|
||||||
@ -96,7 +103,7 @@ impl Guest for Component {
|
|||||||
|
|
||||||
let mut file = std::fs::File::create(ALLOY_CONFIG_PATH)
|
let mut file = std::fs::File::create(ALLOY_CONFIG_PATH)
|
||||||
.expect("to be able to create file");
|
.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");
|
.expect("to be able to write file");
|
||||||
file.flush().expect("to be able to flush file");
|
file.flush().expect("to be able to flush file");
|
||||||
|
|
||||||
@ -112,18 +119,17 @@ impl Guest for Component {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
let output = Process::new().run_process(
|
run_and_activate_alloy().expect("to be able to active alloy");
|
||||||
&["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");
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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> {
|
fn install_alloy() -> Result<(), String> {
|
||||||
println!("=== installing alloy ===");
|
println!("=== installing alloy ===");
|
||||||
run_command(["apt", "install", "-y", "gpg"])?;
|
run_command(["apt", "install", "-y", "gpg"])?;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user