Compare commits

...

3 Commits

Author SHA1 Message Date
11e2a14cc4 wip: early integration 2023-11-25 15:46:49 +11:00
f40b8677ab wip: parsing cron 2023-11-23 17:23:53 +11:00
4049e4a953 wip: parsing crontab 2023-11-23 12:48:47 +11:00
10 changed files with 332 additions and 28 deletions

46
Cargo.lock generated
View File

@ -17,6 +17,15 @@ version = "1.0.2"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f26201604c87b1e01bd3d98f8d5d9a8fcbb815e8cedb41ffccbeb4bf593a35fe" 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]] [[package]]
name = "autocfg" name = "autocfg"
version = "1.1.0" version = "1.1.0"
@ -95,6 +104,12 @@ version = "0.3.3"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d77f7ec81a6d05a3abb01ab6eb7590f6083d08449fe5a1c8b1e620283546ccb7" checksum = "d77f7ec81a6d05a3abb01ab6eb7590f6083d08449fe5a1c8b1e620283546ccb7"
[[package]]
name = "lazy_static"
version = "1.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646"
[[package]] [[package]]
name = "libc" name = "libc"
version = "0.2.150" version = "0.2.150"
@ -223,6 +238,35 @@ dependencies = [
"bitflags 1.3.2", "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]] [[package]]
name = "rustc-demangle" name = "rustc-demangle"
version = "0.1.23" version = "0.1.23"
@ -396,7 +440,9 @@ checksum = "ed94fce61571a4006852b7389a063ab983c02eb1bb37b47f8272ce92d06d9538"
name = "wingmate-rs" name = "wingmate-rs"
version = "0.1.0" version = "0.1.0"
dependencies = [ dependencies = [
"lazy_static",
"nix", "nix",
"regex",
"tokio", "tokio",
"tokio-util", "tokio-util",
] ]

View File

@ -6,6 +6,8 @@ edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies] [dependencies]
lazy_static = "1.4.0"
nix = {version = "0.27.1", features = ["process", "signal", "fs"]} nix = {version = "0.27.1", features = ["process", "signal", "fs"]}
regex = "1.10.2"
tokio = { version = "1.34.0", features = ["full"] } tokio = { version = "1.34.0", features = ["full"] }
tokio-util = "0.7.10" tokio-util = "0.7.10"

View File

@ -1,7 +1,7 @@
FROM ubuntu:22.04 FROM ubuntu:22.04
ADD target/debug/init /usr/local/bin/init ADD target/debug/init /usr/local/bin/init
ADD docker/ / ADD docker/etc/ /etc/
RUN chmod ugo+x /etc/wingmate/services/one && chmod ugo+x /etc/wingmate/services/two.sh && \ RUN chmod ugo+x /etc/wingmate/services/one && chmod ugo+x /etc/wingmate/services/two.sh && \
chmod ugo-x /etc/wingmate/services/three.sh chmod ugo-x /etc/wingmate/services/three.sh

View File

@ -0,0 +1,3 @@
17 * * * * sleep 1
*/12 * * * * sleep 1
12,17,27 * * * * sleep 1

View File

@ -4,5 +4,10 @@ use wingmate_rs::init;
#[tokio::main] #[tokio::main]
async fn main() -> Result<(), Box<dyn error::Error>> { async fn main() -> Result<(), Box<dyn error::Error>> {
init::start().await if let Err(e) = init::start().await {
eprintln!("{}", e);
return Err(e);
}
Ok(())
} }

View File

@ -2,10 +2,28 @@ mod daemon;
mod config; mod config;
pub(crate) mod error; pub(crate) mod error;
use std::env;
use std::error as std_err; use std::error as std_err;
pub async fn start() -> Result<(), Box<dyn std_err::Error>> { pub async fn start() -> Result<(), Box<dyn std_err::Error>> {
let _config = config::Config::find(vec![String::from("/etc/wingmate")])?; let mut vec_search: Vec<String> = Vec::new();
dbg!(_config);
daemon::start().await 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
} }

View File

@ -1,8 +1,12 @@
use std::fs; use std::fs;
use std::env;
use std::path::PathBuf; use std::path::PathBuf;
use std::io::{BufReader, BufRead};
use std::error as std_error; use std::error as std_error;
use crate::init::error as wingmate_error; use crate::init::error as wingmate_error;
use nix::unistd::{access, AccessFlags}; use nix::unistd::{access, AccessFlags};
use lazy_static::lazy_static;
use regex::Regex;
#[derive(Debug)] #[derive(Debug)]
pub enum Command { pub enum Command {
@ -10,9 +14,29 @@ pub enum Command {
Direct(String) 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)] #[derive(Debug)]
pub struct Config { pub struct Config {
pub services: Vec<Command>, pub services: Vec<Command>,
pub cron: Vec<Crontab>,
shell_path: Option<String>,
} }
impl Config { impl Config {
@ -22,7 +46,8 @@ impl Config {
} }
let mut svc_commands: Vec<Command> = Vec::new(); let mut svc_commands: Vec<Command> = Vec::new();
for p in search_path { let mut cron : Vec<Crontab> = Vec::new();
'search: for p in search_path {
let mut buf = PathBuf::new(); let mut buf = PathBuf::new();
buf.push(p); buf.push(p);
if let Ok(m) = fs::metadata(buf.as_path()) { if let Ok(m) = fs::metadata(buf.as_path()) {
@ -34,22 +59,20 @@ impl Config {
let ep = dirent.path(); let ep = dirent.path();
if let Ok(_) = access(ep.as_path(), AccessFlags::X_OK) { if let Ok(_) = access(ep.as_path(), AccessFlags::X_OK) {
// execute directly // execute directly
svc_commands.push(Command::Direct(String::from(ep.as_path().to_string_lossy()))); svc_commands.push(Command::Direct(String::from(ep.to_string_lossy())));
} else { } else {
// call with shell // call with shell
svc_commands.push(Command::ShellPrefixed(String::from(ep.as_path().to_string_lossy()))); svc_commands.push(Command::ShellPrefixed(String::from(ep.to_string_lossy())));
} }
} }
} }
} }
let cron = buf.join("cron"); cron = Self::read_crontab(&mut buf)?;
if let Ok(cron_iter) = fs::read_dir(cron.as_path()) {
for entry in cron_iter { //TODO: need to include cron in the condition
if let Ok(_dirent) = entry { if !svc_commands.is_empty() || !cron.is_empty() {
// read the cron file break 'search;
}
}
} }
} else { } else {
// reserve for future use; when we have a centralized config file // reserve for future use; when we have a centralized config file
@ -57,11 +80,166 @@ impl Config {
} }
} }
if svc_commands.is_empty() { if svc_commands.is_empty() && cron.is_empty() {
return Err(wingmate_error::NoServiceOrCronFoundError.into()); return Err(wingmate_error::NoServiceOrCronFoundError.into());
} }
let config = Config { services: svc_commands }; let mut config = Config {
services: svc_commands,
cron,
shell_path: None,
};
config.find_shell()?;
Ok(config) 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: &regex::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))
}
}
} }

View File

@ -7,8 +7,9 @@ use std::error;
use tokio::task::JoinSet; use tokio::task::JoinSet;
use tokio_util::sync::CancellationToken; use tokio_util::sync::CancellationToken;
use std::sync::{Arc, Mutex}; use std::sync::{Arc, Mutex};
use crate::init::config;
pub async fn start() -> Result<(), Box<dyn error::Error>> { pub async fn start(cfg: config::Config) -> Result<(), Box<dyn error::Error>> {
let sync_flag = Arc::new(Mutex::new(false)); let sync_flag = Arc::new(Mutex::new(false));
let sig_sync_flag = sync_flag.clone(); let sig_sync_flag = sync_flag.clone();
@ -24,7 +25,7 @@ pub async fn start() -> Result<(), Box<dyn error::Error>> {
}); });
//TODO: start the process starter //TODO: start the process starter
starter::start_process(&mut set, starter_cancel); starter::start_services(&mut set, &cfg, starter_cancel)?;
//TODO: spawn_blocking for waiter //TODO: spawn_blocking for waiter
set.spawn_blocking(move || { set.spawn_blocking(move || {

View File

@ -1,5 +1,5 @@
use tokio::task::JoinSet; use tokio::task::JoinSet;
use tokio::process::Command; use tokio::process::{Command, Child};
use tokio_util::sync::CancellationToken; use tokio_util::sync::CancellationToken;
use tokio::select; use tokio::select;
use tokio::io::Result as tokio_result; use tokio::io::Result as tokio_result;
@ -10,17 +10,33 @@ use std::error;
use nix::sys::signal::{kill, Signal}; use nix::sys::signal::{kill, Signal};
use nix::errno::Errno; use nix::errno::Errno;
use nix::unistd::Pid; use nix::unistd::Pid;
use crate::init::config;
use crate::init::error::NoShellAvailableError;
pub fn start_process(ts: &mut JoinSet<Result<(), Box<dyn error::Error + Send + Sync>>>, cancel: CancellationToken) { pub fn start_services(ts: &mut JoinSet<Result<(), Box<dyn error::Error + Send + Sync>>>, cfg: &config::Config, cancel: CancellationToken)
for _j in 0..5 { -> 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())?;
// }
let cancel = cancel.clone(); let cancel = cancel.clone();
ts.spawn(async move { ts.spawn(async move {
'autorestart: loop { 'autorestart: loop {
let mut child = Command::new("sleep").arg("1") let mut child: Child;
.spawn() let shell = shell.clone();
.expect("failed to spawn"); 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");
}
}
select! { select! {
_ = cancel.cancelled() => { _ = cancel.cancelled() => {
@ -64,6 +80,8 @@ pub fn start_process(ts: &mut JoinSet<Result<(), Box<dyn error::Error + Send + S
} }
println!("starter: spawning completed"); println!("starter: spawning completed");
Ok(())
} }
fn result_match(result: tokio_result<ExitStatus>) -> Result<(), Box<dyn error::Error + Send + Sync>> { fn result_match(result: tokio_result<ExitStatus>) -> Result<(), Box<dyn error::Error + Send + Sync>> {

View File

@ -23,3 +23,36 @@ impl fmt::Display for NoServiceOrCronFoundError {
} }
impl error::Error 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 {}