Compare commits
No commits in common. "11e2a14cc4e9f29cfcd5298e149ebe1fb9c32df1" and "6e85e129932f8ef7fb92af33facede6fbff7b378" have entirely different histories.
11e2a14cc4
...
6e85e12993
46
Cargo.lock
generated
46
Cargo.lock
generated
@ -17,15 +17,6 @@ version = "1.0.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "f26201604c87b1e01bd3d98f8d5d9a8fcbb815e8cedb41ffccbeb4bf593a35fe"
|
||||
|
||||
[[package]]
|
||||
name = "aho-corasick"
|
||||
version = "1.1.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "b2969dcb958b36655471fc61f7e416fa76033bdd4bfed0678d8fee1e2d07a1f0"
|
||||
dependencies = [
|
||||
"memchr",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "autocfg"
|
||||
version = "1.1.0"
|
||||
@ -104,12 +95,6 @@ version = "0.3.3"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "d77f7ec81a6d05a3abb01ab6eb7590f6083d08449fe5a1c8b1e620283546ccb7"
|
||||
|
||||
[[package]]
|
||||
name = "lazy_static"
|
||||
version = "1.4.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646"
|
||||
|
||||
[[package]]
|
||||
name = "libc"
|
||||
version = "0.2.150"
|
||||
@ -238,35 +223,6 @@ dependencies = [
|
||||
"bitflags 1.3.2",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "regex"
|
||||
version = "1.10.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "380b951a9c5e80ddfd6136919eef32310721aa4aacd4889a8d39124b026ab343"
|
||||
dependencies = [
|
||||
"aho-corasick",
|
||||
"memchr",
|
||||
"regex-automata",
|
||||
"regex-syntax",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "regex-automata"
|
||||
version = "0.4.3"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "5f804c7828047e88b2d32e2d7fe5a105da8ee3264f01902f796c8e067dc2483f"
|
||||
dependencies = [
|
||||
"aho-corasick",
|
||||
"memchr",
|
||||
"regex-syntax",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "regex-syntax"
|
||||
version = "0.8.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "c08c74e62047bb2de4ff487b251e4a92e24f48745648451635cec7d591162d9f"
|
||||
|
||||
[[package]]
|
||||
name = "rustc-demangle"
|
||||
version = "0.1.23"
|
||||
@ -440,9 +396,7 @@ checksum = "ed94fce61571a4006852b7389a063ab983c02eb1bb37b47f8272ce92d06d9538"
|
||||
name = "wingmate-rs"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"lazy_static",
|
||||
"nix",
|
||||
"regex",
|
||||
"tokio",
|
||||
"tokio-util",
|
||||
]
|
||||
|
||||
@ -6,8 +6,6 @@ edition = "2021"
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
||||
lazy_static = "1.4.0"
|
||||
nix = {version = "0.27.1", features = ["process", "signal", "fs"]}
|
||||
regex = "1.10.2"
|
||||
tokio = { version = "1.34.0", features = ["full"] }
|
||||
tokio-util = "0.7.10"
|
||||
|
||||
@ -1,7 +1,7 @@
|
||||
FROM ubuntu:22.04
|
||||
|
||||
ADD target/debug/init /usr/local/bin/init
|
||||
ADD docker/etc/ /etc/
|
||||
ADD docker/ /
|
||||
|
||||
RUN chmod ugo+x /etc/wingmate/services/one && chmod ugo+x /etc/wingmate/services/two.sh && \
|
||||
chmod ugo-x /etc/wingmate/services/three.sh
|
||||
|
||||
@ -1,3 +0,0 @@
|
||||
17 * * * * sleep 1
|
||||
*/12 * * * * sleep 1
|
||||
12,17,27 * * * * sleep 1
|
||||
@ -4,10 +4,5 @@ use wingmate_rs::init;
|
||||
|
||||
#[tokio::main]
|
||||
async fn main() -> Result<(), Box<dyn error::Error>> {
|
||||
if let Err(e) = init::start().await {
|
||||
eprintln!("{}", e);
|
||||
return Err(e);
|
||||
}
|
||||
|
||||
Ok(())
|
||||
init::start().await
|
||||
}
|
||||
24
src/init.rs
24
src/init.rs
@ -2,28 +2,10 @@ mod daemon;
|
||||
mod config;
|
||||
pub(crate) mod error;
|
||||
|
||||
use std::env;
|
||||
use std::error as std_err;
|
||||
|
||||
pub async fn start() -> Result<(), Box<dyn std_err::Error>> {
|
||||
let mut vec_search: Vec<String> = Vec::new();
|
||||
|
||||
match env::var("WINGMATE_CONFIG_PATH") {
|
||||
Ok(paths) => {
|
||||
for p in paths.split(':') {
|
||||
vec_search.push(String::from(p));
|
||||
}
|
||||
},
|
||||
Err(e) => {
|
||||
if let env::VarError::NotUnicode(_) = e {
|
||||
return Err(e.into());
|
||||
} else {
|
||||
vec_search.push(String::from("/etc/wingmate"));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
let config = config::Config::find(vec_search)?;
|
||||
dbg!(&config);
|
||||
daemon::start(config).await
|
||||
let _config = config::Config::find(vec![String::from("/etc/wingmate")])?;
|
||||
dbg!(_config);
|
||||
daemon::start().await
|
||||
}
|
||||
@ -1,12 +1,8 @@
|
||||
use std::fs;
|
||||
use std::env;
|
||||
use std::path::PathBuf;
|
||||
use std::io::{BufReader, BufRead};
|
||||
use std::error as std_error;
|
||||
use crate::init::error as wingmate_error;
|
||||
use nix::unistd::{access, AccessFlags};
|
||||
use lazy_static::lazy_static;
|
||||
use regex::Regex;
|
||||
|
||||
#[derive(Debug)]
|
||||
pub enum Command {
|
||||
@ -14,29 +10,9 @@ pub enum Command {
|
||||
Direct(String)
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub enum CronTimeFieldSpec {
|
||||
Any,
|
||||
Exact(u8),
|
||||
MultiOccurrence(Vec<u8>),
|
||||
Every(u8)
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct Crontab {
|
||||
pub minute: CronTimeFieldSpec,
|
||||
pub hour: CronTimeFieldSpec,
|
||||
pub day_of_month: CronTimeFieldSpec,
|
||||
pub month: CronTimeFieldSpec,
|
||||
pub day_of_week: CronTimeFieldSpec,
|
||||
pub command: String,
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct Config {
|
||||
pub services: Vec<Command>,
|
||||
pub cron: Vec<Crontab>,
|
||||
shell_path: Option<String>,
|
||||
}
|
||||
|
||||
impl Config {
|
||||
@ -46,8 +22,7 @@ impl Config {
|
||||
}
|
||||
|
||||
let mut svc_commands: Vec<Command> = Vec::new();
|
||||
let mut cron : Vec<Crontab> = Vec::new();
|
||||
'search: for p in search_path {
|
||||
for p in search_path {
|
||||
let mut buf = PathBuf::new();
|
||||
buf.push(p);
|
||||
if let Ok(m) = fs::metadata(buf.as_path()) {
|
||||
@ -59,20 +34,22 @@ impl Config {
|
||||
let ep = dirent.path();
|
||||
if let Ok(_) = access(ep.as_path(), AccessFlags::X_OK) {
|
||||
// execute directly
|
||||
svc_commands.push(Command::Direct(String::from(ep.to_string_lossy())));
|
||||
svc_commands.push(Command::Direct(String::from(ep.as_path().to_string_lossy())));
|
||||
} else {
|
||||
// call with shell
|
||||
svc_commands.push(Command::ShellPrefixed(String::from(ep.to_string_lossy())));
|
||||
svc_commands.push(Command::ShellPrefixed(String::from(ep.as_path().to_string_lossy())));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
cron = Self::read_crontab(&mut buf)?;
|
||||
|
||||
//TODO: need to include cron in the condition
|
||||
if !svc_commands.is_empty() || !cron.is_empty() {
|
||||
break 'search;
|
||||
let cron = buf.join("cron");
|
||||
if let Ok(cron_iter) = fs::read_dir(cron.as_path()) {
|
||||
for entry in cron_iter {
|
||||
if let Ok(_dirent) = entry {
|
||||
// read the cron file
|
||||
}
|
||||
}
|
||||
}
|
||||
} else {
|
||||
// reserve for future use; when we have a centralized config file
|
||||
@ -80,166 +57,11 @@ impl Config {
|
||||
}
|
||||
}
|
||||
|
||||
if svc_commands.is_empty() && cron.is_empty() {
|
||||
if svc_commands.is_empty() {
|
||||
return Err(wingmate_error::NoServiceOrCronFoundError.into());
|
||||
}
|
||||
|
||||
let mut config = Config {
|
||||
services: svc_commands,
|
||||
cron,
|
||||
shell_path: None,
|
||||
};
|
||||
config.find_shell()?;
|
||||
|
||||
let config = Config { services: svc_commands };
|
||||
Ok(config)
|
||||
}
|
||||
|
||||
fn read_crontab(path: &mut PathBuf) -> Result<Vec<Crontab>, Box<dyn std_error::Error>> {
|
||||
lazy_static! {
|
||||
static ref CRON_REGEX: Regex = Regex::new(
|
||||
r"^\s*(?P<minute>\S+)\s+(?P<hour>\S+)\s+(?P<dom>\S+)\s+(?P<month>\S+)\s+(?P<dow>\S+)\s+(?P<command>\S.*\S)\s*$"
|
||||
).unwrap();
|
||||
}
|
||||
|
||||
let cron_path = path.join("crontab");
|
||||
let mut ret_vec: Vec<Crontab> = Vec::new();
|
||||
|
||||
if let Ok(f) = fs::File::open(cron_path.as_path()) {
|
||||
for line in BufReader::new(f).lines() {
|
||||
if let Ok(l) = line {
|
||||
let cap = CRON_REGEX.captures(&l).ok_or::<Box<dyn std_error::Error>>(wingmate_error::CronSyntaxError(String::from(&l)).into())?;
|
||||
|
||||
let mut match_str = cap.name("minute").ok_or::<Box<dyn std_error::Error>>(
|
||||
wingmate_error::CronSyntaxError(format!("cannot capture minute in \"{}\"", &l)).into()
|
||||
)?;
|
||||
let minute = Self::to_cron_time_field_spec(&match_str).map_err(|e| {
|
||||
Box::new(wingmate_error::CronSyntaxError(format!("failed to parse minute \"{}\" in \"{}\": {}", &match_str.as_str(), &l, e)))
|
||||
})?;
|
||||
|
||||
match_str = cap.name("hour").ok_or::<Box<dyn std_error::Error>>(
|
||||
wingmate_error::CronSyntaxError(format!("cannot capture hour in \"{}\"", &l)).into()
|
||||
)?;
|
||||
let hour = Self::to_cron_time_field_spec(&match_str).map_err(|e| {
|
||||
Box::new(wingmate_error::CronSyntaxError(format!("failed to parse hour \"{}\" in \"{}\": {}", &match_str.as_str(), &l, e)))
|
||||
})?;
|
||||
|
||||
match_str = cap.name("dom").ok_or::<Box<dyn std_error::Error>>(
|
||||
wingmate_error::CronSyntaxError(format!("cannot capture day of month in \"{}\"", &l)).into()
|
||||
)?;
|
||||
let dom = Self::to_cron_time_field_spec(&match_str).map_err(|e| {
|
||||
Box::new(wingmate_error::CronSyntaxError(format!("failed to parse day of month \"{}\" in \"{}\": {}", &match_str.as_str(), &l, e)))
|
||||
})?;
|
||||
|
||||
match_str = cap.name("month").ok_or::<Box<dyn std_error::Error>>(
|
||||
wingmate_error::CronSyntaxError(format!("cannot capture month in \"{}\"", &l)).into()
|
||||
)?;
|
||||
let month = Self::to_cron_time_field_spec(&match_str).map_err(|e| {
|
||||
Box::new(wingmate_error::CronSyntaxError(format!("failed to parse month \"{}\" in \"{}\": {}", &match_str.as_str(), &l, e)))
|
||||
})?;
|
||||
|
||||
match_str = cap.name("dow").ok_or::<Box<dyn std_error::Error>>(
|
||||
wingmate_error::CronSyntaxError(format!("cannot capture day of week in \"{}\"", &l)).into()
|
||||
)?;
|
||||
let dow = Self::to_cron_time_field_spec(&match_str).map_err(|e| {
|
||||
Box::new(wingmate_error::CronSyntaxError(format!("failed to parse day of week \"{}\" in \"{}\": {}", &match_str.as_str(), &l, e)))
|
||||
})?;
|
||||
|
||||
match_str = cap.name("command").ok_or::<Box<dyn std_error::Error>>(
|
||||
wingmate_error::CronSyntaxError(format!("cannot capture command in \"{}\"", &l)).into()
|
||||
)?;
|
||||
|
||||
ret_vec.push(Crontab {
|
||||
minute,
|
||||
hour,
|
||||
day_of_month: dom,
|
||||
month,
|
||||
day_of_week: dow,
|
||||
command: String::from(match_str.as_str())
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Ok(ret_vec)
|
||||
}
|
||||
|
||||
fn to_cron_time_field_spec(match_str: ®ex::Match) -> Result<CronTimeFieldSpec, Box<dyn std_error::Error>> {
|
||||
let field = match_str.as_str();
|
||||
|
||||
if field == "*" {
|
||||
return Ok(CronTimeFieldSpec::Any);
|
||||
} else if field.starts_with("*/") {
|
||||
let every = field[2..].parse::<u8>()?;
|
||||
return Ok(CronTimeFieldSpec::Every(every));
|
||||
} else if field.contains(",") {
|
||||
let multi: Vec<&str> = field.split(",").collect();
|
||||
let mut multi_occurrence: Vec<u8> = Vec::new();
|
||||
|
||||
for m in multi {
|
||||
let ur = m.parse::<u8>()?;
|
||||
multi_occurrence.push(ur);
|
||||
}
|
||||
|
||||
return Ok(CronTimeFieldSpec::MultiOccurrence(multi_occurrence));
|
||||
} else {
|
||||
let n = field.parse::<u8>()?;
|
||||
return Ok(CronTimeFieldSpec::Exact(n));
|
||||
}
|
||||
}
|
||||
|
||||
fn find_shell(&mut self) -> Result<(), Box<dyn std_error::Error>> {
|
||||
|
||||
let shell: String;
|
||||
match env::var("WINGMATE_SHELL") {
|
||||
Ok(sh) => {
|
||||
shell = sh;
|
||||
},
|
||||
Err(e) => {
|
||||
match e {
|
||||
env::VarError::NotPresent => {
|
||||
shell = String::from("sh");
|
||||
},
|
||||
env::VarError::NotUnicode(_) => {
|
||||
return Err(e.into());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
let env_path = env::var("PATH")?;
|
||||
let vec_path: Vec<&str> = env_path.split(':').collect();
|
||||
|
||||
for p in vec_path {
|
||||
let mut search_path = PathBuf::new();
|
||||
search_path.push(p);
|
||||
|
||||
let shell_path = search_path.join(&shell);
|
||||
if let Ok(_) = fs::metadata(shell_path.as_path()) {
|
||||
self.shell_path = Some(String::from(shell_path.to_string_lossy()));
|
||||
return Ok(());
|
||||
}
|
||||
}
|
||||
|
||||
Err(wingmate_error::ShellNotFoundError(shell).into())
|
||||
}
|
||||
|
||||
pub fn get_service_iter(&self) -> std::slice::Iter<Command> {
|
||||
self.services.iter()
|
||||
}
|
||||
|
||||
pub fn get_shell(&self) -> Option<String> {
|
||||
if let Some(shell) = &self.shell_path {
|
||||
return Some(shell.clone());
|
||||
}
|
||||
None
|
||||
}
|
||||
}
|
||||
|
||||
impl Clone for Command {
|
||||
fn clone(&self) -> Self {
|
||||
match self {
|
||||
Command::Direct(d) => Command::Direct(String::from(d)),
|
||||
Command::ShellPrefixed(s) => Command::ShellPrefixed(String::from(s))
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -7,9 +7,8 @@ use std::error;
|
||||
use tokio::task::JoinSet;
|
||||
use tokio_util::sync::CancellationToken;
|
||||
use std::sync::{Arc, Mutex};
|
||||
use crate::init::config;
|
||||
|
||||
pub async fn start(cfg: config::Config) -> Result<(), Box<dyn error::Error>> {
|
||||
pub async fn start() -> Result<(), Box<dyn error::Error>> {
|
||||
let sync_flag = Arc::new(Mutex::new(false));
|
||||
let sig_sync_flag = sync_flag.clone();
|
||||
|
||||
@ -25,7 +24,7 @@ pub async fn start(cfg: config::Config) -> Result<(), Box<dyn error::Error>> {
|
||||
});
|
||||
|
||||
//TODO: start the process starter
|
||||
starter::start_services(&mut set, &cfg, starter_cancel)?;
|
||||
starter::start_process(&mut set, starter_cancel);
|
||||
|
||||
//TODO: spawn_blocking for waiter
|
||||
set.spawn_blocking(move || {
|
||||
|
||||
@ -1,5 +1,5 @@
|
||||
use tokio::task::JoinSet;
|
||||
use tokio::process::{Command, Child};
|
||||
use tokio::process::Command;
|
||||
use tokio_util::sync::CancellationToken;
|
||||
use tokio::select;
|
||||
use tokio::io::Result as tokio_result;
|
||||
@ -10,33 +10,17 @@ use std::error;
|
||||
use nix::sys::signal::{kill, Signal};
|
||||
use nix::errno::Errno;
|
||||
use nix::unistd::Pid;
|
||||
use crate::init::config;
|
||||
use crate::init::error::NoShellAvailableError;
|
||||
|
||||
|
||||
pub fn start_services(ts: &mut JoinSet<Result<(), Box<dyn error::Error + Send + Sync>>>, cfg: &config::Config, cancel: CancellationToken)
|
||||
-> Result<(), Box<dyn error::Error>> {
|
||||
|
||||
for svc_ in cfg.get_service_iter() {
|
||||
let shell: String = cfg.get_shell().ok_or::<Box<dyn error::Error>>(NoShellAvailableError.into())?;
|
||||
let svc = svc_.clone();
|
||||
// if let config::Command::ShellPrefixed(_) = svc {
|
||||
// shell = cfg.get_shell().ok_or::<Box<dyn error::Error>>(NoShellAvailableError.into())?;
|
||||
// }
|
||||
pub fn start_process(ts: &mut JoinSet<Result<(), Box<dyn error::Error + Send + Sync>>>, cancel: CancellationToken) {
|
||||
for _j in 0..5 {
|
||||
let cancel = cancel.clone();
|
||||
ts.spawn(async move {
|
||||
'autorestart: loop {
|
||||
let mut child: Child;
|
||||
let shell = shell.clone();
|
||||
let svc = svc.clone();
|
||||
match svc {
|
||||
config::Command::Direct(c) => {
|
||||
child = Command::new(c).spawn().expect("change me");
|
||||
},
|
||||
config::Command::ShellPrefixed(s) => {
|
||||
child = Command::new(shell).arg(s).spawn().expect("change me");
|
||||
}
|
||||
}
|
||||
let mut child = Command::new("sleep").arg("1")
|
||||
.spawn()
|
||||
.expect("failed to spawn");
|
||||
|
||||
|
||||
select! {
|
||||
_ = cancel.cancelled() => {
|
||||
@ -80,8 +64,6 @@ pub fn start_services(ts: &mut JoinSet<Result<(), Box<dyn error::Error + Send +
|
||||
|
||||
}
|
||||
println!("starter: spawning completed");
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn result_match(result: tokio_result<ExitStatus>) -> Result<(), Box<dyn error::Error + Send + Sync>> {
|
||||
|
||||
@ -23,36 +23,3 @@ impl fmt::Display for NoServiceOrCronFoundError {
|
||||
}
|
||||
|
||||
impl error::Error for NoServiceOrCronFoundError {}
|
||||
|
||||
#[derive(Debug,Clone)]
|
||||
pub struct CronSyntaxError(pub String);
|
||||
|
||||
impl fmt::Display for CronSyntaxError {
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
write!(f, "cron syntax error at: {}", self.0)
|
||||
}
|
||||
}
|
||||
|
||||
impl error::Error for CronSyntaxError {}
|
||||
|
||||
#[derive(Debug,Clone)]
|
||||
pub struct ShellNotFoundError(pub String);
|
||||
|
||||
impl fmt::Display for ShellNotFoundError {
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
write!(f, "shell not found: {}", self.0)
|
||||
}
|
||||
}
|
||||
|
||||
impl error::Error for ShellNotFoundError {}
|
||||
|
||||
#[derive(Debug,Clone)]
|
||||
pub struct NoShellAvailableError;
|
||||
|
||||
impl fmt::Display for NoShellAvailableError {
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
write!(f, "no shell available")
|
||||
}
|
||||
}
|
||||
|
||||
impl error::Error for NoShellAvailableError {}
|
||||
Loading…
x
Reference in New Issue
Block a user