From a50efdeea06990eecdaa65ce4ef02f553cea1807 Mon Sep 17 00:00:00 2001 From: Nickiel12 Date: Fri, 25 Aug 2023 20:11:27 -0700 Subject: [PATCH] Ready for nix testing --- Cargo.lock | 93 +++++++++++++++++++++- flake.nix | 27 ++++--- status_cloud/Cargo.toml | 1 + status_cloud/src/main.rs | 161 +++++++++++++++++++-------------------- 4 files changed, 187 insertions(+), 95 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index bd93052..52abec7 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -17,6 +17,21 @@ version = "1.0.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f26201604c87b1e01bd3d98f8d5d9a8fcbb815e8cedb41ffccbeb4bf593a35fe" +[[package]] +name = "android-tzdata" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e999941b234f3131b00bc13c22d06e8c5ff726d1b6318ac7eb276997bbb4fef0" + +[[package]] +name = "android_system_properties" +version = "0.1.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "819e7219dbd41043ac279b19830f2efc897156490d7fd6ea916720117ee66311" +dependencies = [ + "libc", +] + [[package]] name = "anstream" version = "0.5.0" @@ -131,6 +146,21 @@ version = "1.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" +[[package]] +name = "chrono" +version = "0.4.26" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ec837a71355b28f6556dbd569b37b3f363091c0bd4b2e735674521b4c5fd9bc5" +dependencies = [ + "android-tzdata", + "iana-time-zone", + "js-sys", + "num-traits", + "time", + "wasm-bindgen", + "winapi", +] + [[package]] name = "clap" version = "4.4.0" @@ -348,7 +378,7 @@ checksum = "be4136b2a15dd319360be1c07d9933517ccf0be8f16bf62a3bee4f0d618df427" dependencies = [ "cfg-if", "libc", - "wasi", + "wasi 0.11.0+wasi-snapshot-preview1", ] [[package]] @@ -465,6 +495,29 @@ dependencies = [ "tokio-native-tls", ] +[[package]] +name = "iana-time-zone" +version = "0.1.57" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2fad5b825842d2b38bd206f3e81d6957625fd7f0a361e345c30e01a0ae2dd613" +dependencies = [ + "android_system_properties", + "core-foundation-sys", + "iana-time-zone-haiku", + "js-sys", + "wasm-bindgen", + "windows", +] + +[[package]] +name = "iana-time-zone-haiku" +version = "0.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f31827a206f56af32e590ba56d5d2d085f558508192593743f16b2306495269f" +dependencies = [ + "cc", +] + [[package]] name = "idna" version = "0.4.0" @@ -558,7 +611,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "927a765cd3fc26206e66b296465fa9d3e5ab003e651c1b3c060e7956d96b19d2" dependencies = [ "libc", - "wasi", + "wasi 0.11.0+wasi-snapshot-preview1", "windows-sys", ] @@ -580,6 +633,15 @@ dependencies = [ "tempfile", ] +[[package]] +name = "num-traits" +version = "0.2.16" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f30b0abd723be7e2ffca1272140fac1a2f084c77ec3e123c192b66af1ee9e6c2" +dependencies = [ + "autocfg", +] + [[package]] name = "num_cpus" version = "1.16.0" @@ -890,6 +952,7 @@ dependencies = [ name = "status_cloud" version = "0.1.0" dependencies = [ + "chrono", "clap", "confy", "reqwest", @@ -947,6 +1010,17 @@ dependencies = [ "syn", ] +[[package]] +name = "time" +version = "0.1.45" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1b797afad3f312d1c66a56d11d0316f916356d11bd158fbc6ca6389ff6bf805a" +dependencies = [ + "libc", + "wasi 0.10.0+wasi-snapshot-preview1", + "winapi", +] + [[package]] name = "tinyvec" version = "1.6.0" @@ -1096,6 +1170,12 @@ dependencies = [ "try-lock", ] +[[package]] +name = "wasi" +version = "0.10.0+wasi-snapshot-preview1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1a143597ca7c7793eff794def352d41792a93c481eb1042423ff7ff72ba2c31f" + [[package]] name = "wasi" version = "0.11.0+wasi-snapshot-preview1" @@ -1200,6 +1280,15 @@ version = "0.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" +[[package]] +name = "windows" +version = "0.48.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e686886bc078bc1b0b600cac0147aadb815089b6e4da64016cbd754b6342700f" +dependencies = [ + "windows-targets", +] + [[package]] name = "windows-sys" version = "0.48.0" diff --git a/flake.nix b/flake.nix index ac77c41..4c45222 100644 --- a/flake.nix +++ b/flake.nix @@ -62,8 +62,7 @@ rust-project TODO: write shell script for automatically updating `cargoHash` pname = "status_cloud"; version = "0.1.0"; buildAndTestSubdir = "status_cloud"; - cargoHash = "sha256-GpAWrgFBzOTBCfZy/Ehy7IszuSgSOc/4N -2gbeed0jqI="; + cargoHash = "sha256-2eSCxWKf2VH9escaWUDFcozJyyo4xt9SCgBPxlwckTk="; meta = meta // { description = "Server status saved to a nextcloud note service."; }; @@ -83,11 +82,10 @@ rust-project TODO: write shell script for automatically updating `cargoHash` The status_cloud package that should be used. ''; }; - port = lib.mkOption { - type = lib.types.port; - default = 3001; + config_path = lib.mkOption { + type = lib.types.path; description = lib.mdDoc '' - The port at which to run. + The file path to the toml that contains user information secrets ''; }; }; @@ -96,16 +94,23 @@ rust-project TODO: write shell script for automatically updating `cargoHash` pkg = self.packages.${system}.status_cloud; in lib.mkIf cfg.enable { description = pkg.meta.description; - after = [ "network.target" ]; - wantedBy = [ "network.target" ]; serviceConfig = { + Type = "oneshot"; ExecStart = '' - ${cfg.package}/bin/status_cloud --port ${builtins.toString cfg.port} + ${cfg.package}/bin/status_cloud --config_file ${builtins.toString cfg.config_path} ''; - Restart = "always"; - DynamicUser = true; }; }; + + config.systemd.timers.status_cloud = let + cfg = config.services.status_cloud; + pkg = self.packages.${system}.status_cloud; + in lib.mkIf cfg.enable { + wantedBy = [ "timers.target" ]; + partOf = [ "status_cloud.service" ]; + timerConfig.OnCalendar = [ "*:0/15" ]; + }; + }; }; } diff --git a/status_cloud/Cargo.toml b/status_cloud/Cargo.toml index 480cb59..3196f60 100644 --- a/status_cloud/Cargo.toml +++ b/status_cloud/Cargo.toml @@ -6,6 +6,7 @@ edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] +chrono = "0.4.26" clap = { version = "4.4.0", features = ["derive"] } confy = "0.5.1" reqwest = { version = "0.11.18", features = ["blocking", "json"] } diff --git a/status_cloud/src/main.rs b/status_cloud/src/main.rs index a46d2a4..77c94d4 100644 --- a/status_cloud/src/main.rs +++ b/status_cloud/src/main.rs @@ -4,9 +4,87 @@ use reqwest::blocking::Client; use clap::Parser; use confy; -use std::env; use std::process::Command; + +// TODO: logging + +fn main() -> Result<(), Box> { + + let args = CliArgs::parse(); + let cfg: Config = confy::load_path(args.config_file)?; + let mut body_content: String = format!("*Last Updated:* {} \n", chrono::Local::now().format("%D - %H:%M:%S")); + + let mut drives: Vec = vec![]; + + { + let rust_drives = std::fs::read_dir("/dev/").unwrap(); + + for path in rust_drives { + match path { + Ok(ref val) => { + let tmp = val.path().to_string_lossy().to_string(); + if tmp.starts_with("/dev/sd") { + if tmp.len() == 8 { + drives.push(tmp); + } + } + }, + _ => {} + } + } + } + + + { + let mut drive_temps: Vec = vec![]; + + for drive in drives { + let output = Command::new("hddtemp").arg(drive).output()?; + + let tmp = std::str::from_utf8(&output.stdout)?.to_string(); + + if !tmp.contains("sensor") { + drive_temps.push(tmp.replace("\n", "Not running as sudo?")); + } else { + drive_temps.push(tmp[0..9].to_string() + " No Sensor"); + } + } + body_content.push_str("## Hard Drive Temps\n"); + body_content.push_str(drive_temps.join("\n").as_str()); + } + + + let update = Client::new() + .put(format!("https://files.nickiel.net/index.php/apps/notes/api/v1/notes/{}", cfg.note_id.clone())) + .header("Accept", "application/json") + .header("Content-Type", "application/json") + .basic_auth(cfg.user.clone(), Some(cfg.pswd.clone())) + .body(serde_json::to_string(&NoteUpdate {content: body_content}).unwrap()); + + //println!("{:#?}", update.send()?.json::()?); + + Ok(()) +} + + +#[derive(Serialize, Deserialize, Debug)] +struct Note { + id: usize, + etag: String, + readonly: bool, + modified: u64, + title: String, + category: String, + content: String, + favorite: bool +} + +#[derive(Serialize, Deserialize, Debug)] +struct NoteUpdate { + content: String, +} + #[derive(Serialize, Deserialize)] struct Config { user: String, @@ -31,84 +109,3 @@ struct CliArgs { #[arg(short, long)] config_file: String, } - -// TODO: logging - -fn main() -> Result<(), Box> { - - let args = CliArgs::parse(); - - let cfg: Config = confy::load_path(args.config_file)?; - - let mut drives: Vec = vec![]; - - { - let rust_drives = std::fs::read_dir("/dev/").unwrap(); - - for path in rust_drives { - match path { - Ok(ref val) => { - let tmp = val.path().to_string_lossy().to_string(); - if tmp.starts_with("/dev/sd") { - if tmp.len() == 8 { - drives.push(tmp); - } - } - }, - _ => {} - } - } - } - - let mut body_content: String = String::new(); - - { - let mut drive_temps: Vec = vec![]; - - for drive in drives { - let output = Command::new("hddtemp").arg(drive).output()?; - - let tmp = std::str::from_utf8(&output.stdout)?.to_string(); - - if !tmp.contains("sensor") { - drive_temps.push(tmp.replace("\n", "")); - } else { - drive_temps.push(tmp[0..9].to_string() + " No Sensor"); - } - } - body_content.push_str("## Hard Drive Temps\n"); - body_content.push_str(drive_temps.join("\n").as_str()); - } - - - - let update = Client::new() - .put(format!("https://files.nickiel.net/index.php/apps/notes/api/v1/notes/{}", cfg.note_id.clone())) - .header("Accept", "application/json") - .header("Content-Type", "application/json") - .basic_auth(cfg.user.clone(), Some(cfg.pswd.clone())) - .body(serde_json::to_string(&NoteUpdate {content: body_content}).unwrap()); - - println!("{:#?}", update.send()? - .json::()?); - - Ok(()) -} - - -#[derive(Serialize, Deserialize, Debug)] -struct Note { - id: usize, - etag: String, - readonly: bool, - modified: u64, - title: String, - category: String, - content: String, - favorite: bool -} - -#[derive(Serialize, Deserialize, Debug)] -struct NoteUpdate { - content: String, -}