feat: add actual alloy component
Signed-off-by: kjuulh <contact@kjuulh.io>
This commit is contained in:
144
alloy/src/lib.rs
144
alloy/src/lib.rs
@@ -1,8 +1,15 @@
|
||||
use bindings::exports::component::churn_tasks::task::Guest;
|
||||
use std::{io::Write, path::PathBuf};
|
||||
|
||||
use bindings::{
|
||||
component::churn_tasks::process::Process, exports::component::churn_tasks::task::Guest,
|
||||
};
|
||||
|
||||
#[allow(warnings)]
|
||||
mod bindings;
|
||||
|
||||
const ALLOY_CONFIG_PATH: &str = "/etc/alloy/config.alloy";
|
||||
const ALLOY_CONFIG_FILE: &str = include_str!("../files/config.alloy");
|
||||
|
||||
struct Component;
|
||||
|
||||
impl Guest for Component {
|
||||
@@ -11,12 +18,143 @@ impl Guest for Component {
|
||||
}
|
||||
|
||||
fn should_run() -> bool {
|
||||
true
|
||||
let output = Process::new().run_process(
|
||||
&["systemctl", "is-enabled", "alloy.service"]
|
||||
.into_iter()
|
||||
.map(|i| i.into())
|
||||
.collect::<Vec<String>>(),
|
||||
);
|
||||
|
||||
if !output.contains("enabled") {
|
||||
return true;
|
||||
}
|
||||
|
||||
let output = Process::new().run_process(
|
||||
&["systemctl", "is-active", "alloy.service"]
|
||||
.into_iter()
|
||||
.map(|i| i.into())
|
||||
.collect::<Vec<String>>(),
|
||||
);
|
||||
|
||||
if output.contains("inactive") {
|
||||
return true;
|
||||
}
|
||||
|
||||
match std::fs::read_to_string(ALLOY_CONFIG_PATH) {
|
||||
Ok(content) => return content != ALLOY_CONFIG_FILE,
|
||||
Err(e) => {
|
||||
if e.kind() == std::io::ErrorKind::NotFound {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
false
|
||||
}
|
||||
|
||||
fn execute() {
|
||||
println!("I was run");
|
||||
println!("running alloy installation");
|
||||
|
||||
let output = Process::new().run_process(
|
||||
&["systemctl", "is-enabled", "alloy.service"]
|
||||
.into_iter()
|
||||
.map(|i| i.into())
|
||||
.collect::<Vec<String>>(),
|
||||
);
|
||||
if !output.contains("enabled") {
|
||||
install_alloy().expect("to be able to install alloy");
|
||||
}
|
||||
|
||||
let restart = match std::fs::read_to_string(ALLOY_CONFIG_PATH) {
|
||||
Ok(content) => {
|
||||
if content != ALLOY_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())
|
||||
.expect("to be able to write file");
|
||||
file.flush().expect("to be able to flush file");
|
||||
true
|
||||
} else {
|
||||
false
|
||||
}
|
||||
}
|
||||
Err(e) => {
|
||||
if e.kind() == std::io::ErrorKind::NotFound {
|
||||
if let Some(parent) = PathBuf::from(ALLOY_CONFIG_PATH).parent() {
|
||||
std::fs::create_dir_all(parent)
|
||||
.expect("to be able to create dir for alloy");
|
||||
}
|
||||
|
||||
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())
|
||||
.expect("to be able to write file");
|
||||
file.flush().expect("to be able to flush file");
|
||||
|
||||
false
|
||||
} else {
|
||||
false
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
if restart {
|
||||
restart_alloy().expect("to be able to restart alloy");
|
||||
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");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn install_alloy() -> Result<(), String> {
|
||||
println!("=== installing alloy ===");
|
||||
run_command(["apt", "install", "gpg"])?;
|
||||
run_command(["mkdir", "-p", "/etc/apt/keyrings/"])?;
|
||||
run_command(["bash", "-c", "wget -q -O - https://apt.grafana.com/gpg.key | gpg --dearmor | sudo tee /etc/apt/keyrings/grafana.gpg > /dev/null && echo \"deb [signed-by=/etc/apt/keyrings/grafana.gpg] https://apt.grafana.com stable main\" | sudo tee /etc/apt/sources.list.d/grafana.list"])?;
|
||||
run_command(["apt-get", "update"])?;
|
||||
run_command(["apt-get", "install", "alloy"])?;
|
||||
println!("=== finished installing alloy ===");
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn restart_alloy() -> Result<(), String> {
|
||||
println!("=== restarting alloy ===");
|
||||
run_command(["systemctl", "restart", "alloy"])?;
|
||||
println!("=== finished restarting alloy ===");
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn run_and_activate_alloy() -> Result<(), String> {
|
||||
println!("=== starting alloy ===");
|
||||
run_command(["systemctl", "start", "alloy"])?;
|
||||
println!("=== finished starting alloy ===");
|
||||
|
||||
println!("=== enabling alloy ===");
|
||||
run_command(["systemctl", "enable", "alloy"])?;
|
||||
println!("=== finished enabling alloy ===");
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn run_command(input: impl IntoIterator<Item = impl Into<String>>) -> Result<(), String> {
|
||||
let args = input.into_iter().map(|i| i.into()).collect::<Vec<String>>();
|
||||
|
||||
println!("running {}", args.join(" "));
|
||||
let output = Process::new().run_process(&args);
|
||||
println!("output: {}", output);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
bindings::export!(Component with_types_in bindings);
|
||||
|
Reference in New Issue
Block a user