2020-06-13 23:02:16 +00:00
|
|
|
use std::fs;
|
|
|
|
use serde::Deserialize;
|
|
|
|
use toml;
|
2020-06-14 22:53:39 +00:00
|
|
|
use tokio;
|
|
|
|
|
|
|
|
use crate::processes;
|
2020-06-13 23:02:16 +00:00
|
|
|
|
|
|
|
#[derive(Deserialize, Debug)]
|
|
|
|
pub struct Service {
|
|
|
|
pub name: String,
|
|
|
|
pub start: Start,
|
|
|
|
pub stop: Option<Stop>,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Deserialize, Debug)]
|
|
|
|
pub struct Start {
|
|
|
|
pub command: String,
|
|
|
|
pub args: Vec<String>
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Deserialize, Debug)]
|
|
|
|
pub struct Stop {
|
|
|
|
pub command: String,
|
|
|
|
pub args: Vec<String>
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn get_services() -> Vec<Service> {
|
|
|
|
let mut services = Vec::new();
|
|
|
|
let paths = fs::read_dir("/etc/mini/").unwrap();
|
|
|
|
|
|
|
|
for path in paths {
|
|
|
|
// TODO: check ext
|
|
|
|
let svc_file_contents = fs::read_to_string(path.unwrap().path()).unwrap();
|
|
|
|
let svc : Service = toml::from_str(&svc_file_contents).unwrap();
|
|
|
|
services.push(svc);
|
|
|
|
}
|
|
|
|
|
|
|
|
services
|
|
|
|
}
|
2020-06-14 22:53:39 +00:00
|
|
|
|
|
|
|
pub async fn launch_services() {
|
|
|
|
let services = get_services();
|
|
|
|
|
|
|
|
for s in services {
|
|
|
|
tokio::spawn(async move {
|
|
|
|
println!("starting {}", s.name);
|
|
|
|
let args: Vec<&str> = s.start.args.iter().map(|e| e.as_str()).collect();
|
|
|
|
processes::run_wait(&s.start.command, &args[..]).await;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn stop_services() {
|
|
|
|
let services = get_services();
|
|
|
|
|
|
|
|
for s in services {
|
|
|
|
println!("stopping {}", s.name);
|
|
|
|
if let Some(stop) = s.stop {
|
|
|
|
let args: Vec<&str> = stop.args.iter().map(|e| e.as_str()).collect();
|
|
|
|
processes::sync_run_wait(&stop.command, &args[..]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|