adding installer
This commit is contained in:
@@ -50,7 +50,7 @@ pub fn load_settings(path: &str) -> bool {
|
|||||||
Err(_) => return false, // invalid port number
|
Err(_) => return false, // invalid port number
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
_ => continue, //return false, // unknown key
|
_ => continue,//ignore unknown key
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
7
installer/protector_installer/Cargo.lock
generated
Normal file
7
installer/protector_installer/Cargo.lock
generated
Normal file
@@ -0,0 +1,7 @@
|
|||||||
|
# This file is automatically @generated by Cargo.
|
||||||
|
# It is not intended for manual editing.
|
||||||
|
version = 4
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "protector_installer"
|
||||||
|
version = "0.1.0"
|
||||||
6
installer/protector_installer/Cargo.toml
Normal file
6
installer/protector_installer/Cargo.toml
Normal file
@@ -0,0 +1,6 @@
|
|||||||
|
[package]
|
||||||
|
name = "protector_installer"
|
||||||
|
version = "0.1.0"
|
||||||
|
edition = "2024"
|
||||||
|
|
||||||
|
[dependencies]
|
||||||
96
installer/protector_installer/src/main.rs
Normal file
96
installer/protector_installer/src/main.rs
Normal file
@@ -0,0 +1,96 @@
|
|||||||
|
use std::fs::{self, File};
|
||||||
|
use std::io::Write;
|
||||||
|
use std::path::Path;
|
||||||
|
use std::process::Command;
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
// Predefined values
|
||||||
|
let app_path = "/etc/protector/app/protector";
|
||||||
|
let service_name = "protector";
|
||||||
|
let working_dir = "/etc/protector";
|
||||||
|
let log_file = "/var/log/protector.log";
|
||||||
|
|
||||||
|
// Step 1: Create folder structure
|
||||||
|
let folders = ["/etc/protector/app", "/etc/protector/conf"];
|
||||||
|
for folder in folders.iter() {
|
||||||
|
if !Path::new(folder).exists() {
|
||||||
|
match fs::create_dir_all(folder) {
|
||||||
|
Ok(_) => println!("Created folder: {}", folder),
|
||||||
|
Err(e) => eprintln!("Failed to create folder {}: {}", folder, e),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Step 2: Create empty log file if it doesn't exist
|
||||||
|
if !Path::new(log_file).exists() {
|
||||||
|
match File::create(log_file) {
|
||||||
|
Ok(_) => println!("Created log file: {}", log_file),
|
||||||
|
Err(e) => eprintln!("Failed to create log file {}: {}", log_file, e),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Step 3: Create systemd service content
|
||||||
|
let service_content = format!(
|
||||||
|
r#"[Unit]
|
||||||
|
Description={0} system-wide service
|
||||||
|
After=network.target
|
||||||
|
|
||||||
|
[Service]
|
||||||
|
Type=simple
|
||||||
|
ExecStart={1}
|
||||||
|
Restart=on-failure
|
||||||
|
WorkingDirectory={2}
|
||||||
|
StandardOutput=append:{3}
|
||||||
|
StandardError=append:{3}
|
||||||
|
|
||||||
|
[Install]
|
||||||
|
WantedBy=multi-user.target
|
||||||
|
"#,
|
||||||
|
service_name, app_path, working_dir, log_file
|
||||||
|
);
|
||||||
|
|
||||||
|
let service_path = format!("/etc/systemd/system/{}.service", service_name);
|
||||||
|
|
||||||
|
// Step 4: Write the service file
|
||||||
|
if Path::new("/etc/systemd/system/").exists() {
|
||||||
|
match File::create(&service_path) {
|
||||||
|
Ok(mut file) => {
|
||||||
|
file.write_all(service_content.as_bytes())
|
||||||
|
.expect("Failed to write service file");
|
||||||
|
println!("System-wide service file created at {}", service_path);
|
||||||
|
|
||||||
|
// Step 5: Reload systemd, enable, and start the service
|
||||||
|
let reload = Command::new("systemctl")
|
||||||
|
.arg("daemon-reload")
|
||||||
|
.status()
|
||||||
|
.expect("Failed to reload systemd");
|
||||||
|
if reload.success() {
|
||||||
|
println!("systemd reloaded successfully.");
|
||||||
|
}
|
||||||
|
|
||||||
|
let enable = Command::new("systemctl")
|
||||||
|
.arg("enable")
|
||||||
|
.arg(service_name)
|
||||||
|
.status()
|
||||||
|
.expect("Failed to enable service");
|
||||||
|
if enable.success() {
|
||||||
|
println!("Service {} enabled to start on boot.", service_name);
|
||||||
|
}
|
||||||
|
|
||||||
|
let start = Command::new("systemctl")
|
||||||
|
.arg("start")
|
||||||
|
.arg(service_name)
|
||||||
|
.status()
|
||||||
|
.expect("Failed to start service");
|
||||||
|
if start.success() {
|
||||||
|
println!("Service {} started successfully.", service_name);
|
||||||
|
} else {
|
||||||
|
eprintln!("Failed to start service {}.", service_name);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Err(e) => eprintln!("Failed to create service file: {}", e),
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
println!("Directory /etc/systemd/system/ does not exist. Cannot install");
|
||||||
|
}
|
||||||
|
}
|
||||||
1
installer/protector_installer/target/.rustc_info.json
Normal file
1
installer/protector_installer/target/.rustc_info.json
Normal file
@@ -0,0 +1 @@
|
|||||||
|
{"rustc_fingerprint":15569509355061161103,"outputs":{"17747080675513052775":{"success":true,"status":"","code":0,"stdout":"rustc 1.89.0 (29483883e 2025-08-04)\nbinary: rustc\ncommit-hash: 29483883eed69d5fb4db01964cdf2af4d86e9cb2\ncommit-date: 2025-08-04\nhost: x86_64-unknown-linux-gnu\nrelease: 1.89.0\nLLVM version: 20.1.7\n","stderr":""},"7971740275564407648":{"success":true,"status":"","code":0,"stdout":"___\nlib___.rlib\nlib___.so\nlib___.so\nlib___.a\nlib___.so\n/home/janis/.rustup/toolchains/stable-x86_64-unknown-linux-gnu\noff\npacked\nunpacked\n___\ndebug_assertions\npanic=\"unwind\"\nproc_macro\ntarget_abi=\"\"\ntarget_arch=\"x86_64\"\ntarget_endian=\"little\"\ntarget_env=\"gnu\"\ntarget_family=\"unix\"\ntarget_feature=\"fxsr\"\ntarget_feature=\"sse\"\ntarget_feature=\"sse2\"\ntarget_has_atomic=\"16\"\ntarget_has_atomic=\"32\"\ntarget_has_atomic=\"64\"\ntarget_has_atomic=\"8\"\ntarget_has_atomic=\"ptr\"\ntarget_os=\"linux\"\ntarget_pointer_width=\"64\"\ntarget_vendor=\"unknown\"\nunix\n","stderr":""}},"successes":{}}
|
||||||
3
installer/protector_installer/target/CACHEDIR.TAG
Normal file
3
installer/protector_installer/target/CACHEDIR.TAG
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
Signature: 8a477f597d28d172789f06886806bc55
|
||||||
|
# This file is a cache directory tag created by cargo.
|
||||||
|
# For information about cache directory tags see https://bford.info/cachedir/
|
||||||
Reference in New Issue
Block a user