feat: add get variable

Signed-off-by: kjuulh <contact@kjuulh.io>
This commit is contained in:
2025-01-04 01:10:33 +01:00
parent 36281f1d54
commit c647adff54
7 changed files with 591 additions and 111 deletions

30
alloy/Cargo.lock generated
View File

@@ -7,6 +7,7 @@ name = "alloy"
version = "0.1.0"
dependencies = [
"dirs",
"minijinja",
"wit-bindgen-rt",
]
@@ -70,6 +71,15 @@ dependencies = [
"libc",
]
[[package]]
name = "minijinja"
version = "2.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2c37e1b517d1dcd0e51dc36c4567b9d5a29262b3ec8da6cb5d35e27a8fb529b5"
dependencies = [
"serde",
]
[[package]]
name = "option-ext"
version = "0.2.0"
@@ -105,6 +115,26 @@ dependencies = [
"thiserror",
]
[[package]]
name = "serde"
version = "1.0.217"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "02fc4265df13d6fa1d00ecff087228cc0a2b5f3c0e87e258d8b94a156e984c70"
dependencies = [
"serde_derive",
]
[[package]]
name = "serde_derive"
version = "1.0.217"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5a9bf7cf98d04a2b28aead066b7496853d4779c9cc183c440dbac457641e19a0"
dependencies = [
"proc-macro2",
"quote",
"syn",
]
[[package]]
name = "syn"
version = "2.0.90"

View File

@@ -5,6 +5,7 @@ edition = "2021"
[dependencies]
dirs = "5.0.1"
minijinja = "2.5.0"
wit-bindgen-rt = { version = "0.36.0", features = ["bitflags"] }
[lib]

View File

@@ -30,7 +30,7 @@ prometheus.scrape "default" {
prometheus.remote_write "default" {
external_labels = {
"node" = "new-node",
"node" = "{{ node_name }}",
}
endpoint {
@@ -75,7 +75,7 @@ loki.process "filter_logs" {
loki.write "grafana_loki" {
external_labels = {
"node" = "new-node",
"node" = "{{ node_name }}",
}
endpoint {

View File

@@ -127,6 +127,40 @@ pub mod component {
}
}
}
impl Process {
#[allow(unused_unsafe, clippy::all)]
pub fn get_variable(&self, key: &str) -> _rt::String {
unsafe {
#[repr(align(4))]
struct RetArea([::core::mem::MaybeUninit<u8>; 8]);
let mut ret_area = RetArea(
[::core::mem::MaybeUninit::uninit(); 8],
);
let vec0 = key;
let ptr0 = vec0.as_ptr().cast::<u8>();
let len0 = vec0.len();
let ptr1 = ret_area.0.as_mut_ptr().cast::<u8>();
#[cfg(target_arch = "wasm32")]
#[link(
wasm_import_module = "component:churn-tasks/process@0.1.0"
)]
extern "C" {
#[link_name = "[method]process.get-variable"]
fn wit_import(_: i32, _: *mut u8, _: usize, _: *mut u8);
}
#[cfg(not(target_arch = "wasm32"))]
fn wit_import(_: i32, _: *mut u8, _: usize, _: *mut u8) {
unreachable!()
}
wit_import((self).handle() as i32, ptr0.cast_mut(), len0, ptr1);
let l2 = *ptr1.add(0).cast::<*mut u8>();
let l3 = *ptr1.add(4).cast::<usize>();
let len4 = l3;
let bytes4 = _rt::Vec::from_raw_parts(l2.cast(), len4, len4);
_rt::string_lift(bytes4)
}
}
}
}
}
}
@@ -344,16 +378,17 @@ pub(crate) use __export_alloy_impl as export;
#[cfg(target_arch = "wasm32")]
#[link_section = "component-type:wit-bindgen:0.35.0:component:alloy:alloy:encoded world"]
#[doc(hidden)]
pub static __WIT_BINDGEN_COMPONENT_TYPE: [u8; 390] = *b"\
\0asm\x0d\0\x01\0\0\x19\x16wit-component-encoding\x04\0\x07\x8a\x02\x01A\x02\x01\
A\x04\x01B\x08\x04\0\x07process\x03\x01\x01i\0\x01@\0\0\x01\x04\0\x14[constructo\
pub static __WIT_BINDGEN_COMPONENT_TYPE: [u8; 439] = *b"\
\0asm\x0d\0\x01\0\0\x19\x16wit-component-encoding\x04\0\x07\xbb\x02\x01A\x02\x01\
A\x04\x01B\x0a\x04\0\x07process\x03\x01\x01i\0\x01@\0\0\x01\x04\0\x14[constructo\
r]process\x01\x02\x01h\0\x01ps\x01@\x02\x04self\x03\x06inputs\x04\0s\x04\0\x1b[m\
ethod]process.run-process\x01\x05\x03\0#component:churn-tasks/process@0.1.0\x05\0\
\x01B\x06\x01@\0\0s\x04\0\x02id\x01\0\x01@\0\0\x7f\x04\0\x0ashould-run\x01\x01\x01\
@\0\x01\0\x04\0\x07execute\x01\x02\x04\0\x20component:churn-tasks/task@0.1.0\x05\
\x01\x04\0\x15component:alloy/alloy\x04\0\x0b\x0b\x01\0\x05alloy\x03\0\0\0G\x09p\
roducers\x01\x0cprocessed-by\x02\x0dwit-component\x070.220.0\x10wit-bindgen-rust\
\x060.35.0";
ethod]process.run-process\x01\x05\x01@\x02\x04self\x03\x03keys\0s\x04\0\x1c[meth\
od]process.get-variable\x01\x06\x03\0#component:churn-tasks/process@0.1.0\x05\0\x01\
B\x06\x01@\0\0s\x04\0\x02id\x01\0\x01@\0\0\x7f\x04\0\x0ashould-run\x01\x01\x01@\0\
\x01\0\x04\0\x07execute\x01\x02\x04\0\x20component:churn-tasks/task@0.1.0\x05\x01\
\x04\0\x15component:alloy/alloy\x04\0\x0b\x0b\x01\0\x05alloy\x03\0\0\0G\x09produ\
cers\x01\x0cprocessed-by\x02\x0dwit-component\x070.220.0\x10wit-bindgen-rust\x06\
0.35.0";
#[inline(never)]
#[doc(hidden)]
pub fn __link_custom_section_describing_imports() {

View File

@@ -3,6 +3,7 @@ use std::{io::Write, path::PathBuf};
use bindings::{
component::churn_tasks::process::Process, exports::component::churn_tasks::task::Guest,
};
use minijinja::{context, Environment};
#[allow(warnings)]
mod bindings;
@@ -55,6 +56,9 @@ impl Guest for Component {
fn execute() {
println!("running alloy installation");
let process = Process::new();
let node_name = process.get_variable("node_name");
let output = Process::new().run_process(
&["systemctl", "is-enabled", "alloy.service"]
.into_iter()
@@ -67,6 +71,11 @@ impl Guest for Component {
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 {
let mut file = std::fs::File::create(ALLOY_CONFIG_PATH)
.expect("to be able to create file");
@@ -122,7 +131,13 @@ fn install_alloy() -> Result<(), String> {
run_command(["bash", "-c", "wget -q -O - https://apt.grafana.com/gpg.key | gpg --dearmor | sudo tee /etc/apt/keyrings/grafana.gpg > /dev/null"])?;
run_command(["bash", "-c", "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"])?;
run_command([
"apt-get",
"install",
"-o Dpkg::Options::=\"--force-confdef\"",
"-o Dpkg::Options::=\"--force-confold\"",
"alloy",
])?;
println!("=== finished installing alloy ===");
Ok(())