Add cron
This commit is contained in:
parent
6190b0e373
commit
7d6329a409
4 changed files with 137 additions and 9 deletions
|
|
@ -1,12 +1,24 @@
|
||||||
use std::error::Error;
|
use std::error::Error;
|
||||||
use std::ffi::OsStr;
|
use std::ffi::OsStr;
|
||||||
use std::io::Result as IoResult;
|
use std::io::{Result as IoResult, Write};
|
||||||
use std::process::Command;
|
use std::process::Command;
|
||||||
use std::process::Output;
|
use std::process::Output;
|
||||||
|
use std::process::Stdio;
|
||||||
|
|
||||||
pub trait CommandRunner {
|
pub trait CommandRunner {
|
||||||
fn run_with_args<S: AsRef<OsStr> + ?Sized>(&self, program: &str, args: &[&S])
|
fn run_with_args_and_stdin<S: AsRef<OsStr> + ?Sized>(
|
||||||
-> IoResult<Output>;
|
&self,
|
||||||
|
program: &str,
|
||||||
|
args: &[&S],
|
||||||
|
stdin: &str,
|
||||||
|
) -> IoResult<Output>;
|
||||||
|
fn run_with_args<S: AsRef<OsStr> + ?Sized>(
|
||||||
|
&self,
|
||||||
|
program: &str,
|
||||||
|
args: &[&S],
|
||||||
|
) -> IoResult<Output> {
|
||||||
|
self.run_with_args_and_stdin(program, args, "")
|
||||||
|
}
|
||||||
fn get_output<S: AsRef<OsStr> + ?Sized>(
|
fn get_output<S: AsRef<OsStr> + ?Sized>(
|
||||||
&self,
|
&self,
|
||||||
program: &str,
|
program: &str,
|
||||||
|
|
@ -47,14 +59,26 @@ pub trait CommandRunner {
|
||||||
pub struct StdCommandRunner;
|
pub struct StdCommandRunner;
|
||||||
|
|
||||||
impl CommandRunner for StdCommandRunner {
|
impl CommandRunner for StdCommandRunner {
|
||||||
fn run_with_args<S: AsRef<OsStr> + ?Sized>(
|
fn run_with_args_and_stdin<S: AsRef<OsStr> + ?Sized>(
|
||||||
&self,
|
&self,
|
||||||
program: &str,
|
program: &str,
|
||||||
args: &[&S],
|
args: &[&S],
|
||||||
|
input: &str,
|
||||||
) -> IoResult<Output> {
|
) -> IoResult<Output> {
|
||||||
// FIXME: logger
|
// FIXME: logger
|
||||||
//println!("{} {:?}", program, args);
|
//println!("{} {:?}", program, args);
|
||||||
let res = Command::new(program).args(args).output();
|
let mut child = Command::new(program)
|
||||||
|
.args(args)
|
||||||
|
.stdin(Stdio::piped())
|
||||||
|
.stdout(Stdio::piped())
|
||||||
|
.stderr(Stdio::piped())
|
||||||
|
.spawn()
|
||||||
|
.expect("Failed to spawn child process");
|
||||||
|
let stdin = child.stdin.as_mut().expect("Failed to open stdin");
|
||||||
|
stdin
|
||||||
|
.write_all(input.as_bytes())
|
||||||
|
.expect("Failed to write to stdin");
|
||||||
|
let res = child.wait_with_output();
|
||||||
println!("{:?}", res);
|
println!("{:?}", res);
|
||||||
res
|
res
|
||||||
}
|
}
|
||||||
|
|
@ -114,10 +138,11 @@ impl<'a, C> CommandRunner for SetuidCommandRunner<'a, C>
|
||||||
where
|
where
|
||||||
C: 'a + CommandRunner,
|
C: 'a + CommandRunner,
|
||||||
{
|
{
|
||||||
fn run_with_args<S: AsRef<OsStr> + ?Sized>(
|
fn run_with_args_and_stdin<S: AsRef<OsStr> + ?Sized>(
|
||||||
&self,
|
&self,
|
||||||
program: &str,
|
program: &str,
|
||||||
args: &[&S],
|
args: &[&S],
|
||||||
|
input: &str,
|
||||||
) -> IoResult<Output> {
|
) -> IoResult<Output> {
|
||||||
let uid = get_user_by_name(self.user_name)
|
let uid = get_user_by_name(self.user_name)
|
||||||
.expect("User does not exist")
|
.expect("User does not exist")
|
||||||
|
|
@ -125,7 +150,20 @@ where
|
||||||
let set_home = TempSetEnv::new("HOME", format!("/home/{}", self.user_name));
|
let set_home = TempSetEnv::new("HOME", format!("/home/{}", self.user_name));
|
||||||
let set_dbus = TempSetEnv::new("XDG_RUNTIME_DIR", format!("/run/user/{}", uid));
|
let set_dbus = TempSetEnv::new("XDG_RUNTIME_DIR", format!("/run/user/{}", uid));
|
||||||
//println!("{} {:?}", program, args);
|
//println!("{} {:?}", program, args);
|
||||||
let res = Command::new(program).uid(uid).gid(uid).args(args).output();
|
let mut child = Command::new(program)
|
||||||
|
.args(args)
|
||||||
|
.stdin(Stdio::piped())
|
||||||
|
.uid(uid)
|
||||||
|
.gid(uid)
|
||||||
|
.stdout(Stdio::piped())
|
||||||
|
.stderr(Stdio::piped())
|
||||||
|
.spawn()
|
||||||
|
.expect("Failed to spawn child process");
|
||||||
|
let stdin = child.stdin.as_mut().expect("Failed to open stdin");
|
||||||
|
stdin
|
||||||
|
.write_all(input.as_bytes())
|
||||||
|
.expect("Failed to write to stdin");
|
||||||
|
let res = child.wait_with_output();
|
||||||
println!("{:?}", res);
|
println!("{:?}", res);
|
||||||
res
|
res
|
||||||
}
|
}
|
||||||
|
|
@ -158,16 +196,19 @@ impl<'a, C> CommandRunner for SuCommandRunner<'a, C>
|
||||||
where
|
where
|
||||||
C: 'a + CommandRunner,
|
C: 'a + CommandRunner,
|
||||||
{
|
{
|
||||||
fn run_with_args<S: AsRef<OsStr> + ?Sized>(
|
fn run_with_args_and_stdin<S: AsRef<OsStr> + ?Sized>(
|
||||||
&self,
|
&self,
|
||||||
program: &str,
|
program: &str,
|
||||||
args: &[&S],
|
args: &[&S],
|
||||||
|
input: &str,
|
||||||
) -> IoResult<Output> {
|
) -> IoResult<Output> {
|
||||||
let raw_new_args = [self.user_name, "-s", "/usr/bin/env", "--", program];
|
let raw_new_args = [self.user_name, "-s", "/usr/bin/env", "--", program];
|
||||||
let mut new_args: Vec<&OsStr> = raw_new_args.iter().map(|s| s.as_ref()).collect();
|
let mut new_args: Vec<&OsStr> = raw_new_args.iter().map(|s| s.as_ref()).collect();
|
||||||
let old_args: Vec<&OsStr> = args.iter().map(|s| s.as_ref()).collect();
|
let old_args: Vec<&OsStr> = args.iter().map(|s| s.as_ref()).collect();
|
||||||
new_args.extend_from_slice(&old_args);
|
new_args.extend_from_slice(&old_args);
|
||||||
self.command_runner.run_with_args("su", &new_args)
|
self
|
||||||
|
.command_runner
|
||||||
|
.run_with_args_and_stdin("su", &new_args, input)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
77
src/symbols/cron.rs
Normal file
77
src/symbols/cron.rs
Normal file
|
|
@ -0,0 +1,77 @@
|
||||||
|
use std::error::Error;
|
||||||
|
use std::fmt;
|
||||||
|
use std::ops::Deref;
|
||||||
|
|
||||||
|
use command_runner::CommandRunner;
|
||||||
|
use resources::Resource;
|
||||||
|
use symbols::{Action, OwnedSymbolAction, Symbol, SymbolAction, SymbolRunner};
|
||||||
|
|
||||||
|
pub struct Cron<'r, C, U, R: 'r + CommandRunner>
|
||||||
|
where
|
||||||
|
C: Deref<Target = str>,
|
||||||
|
U: Deref<Target = str>,
|
||||||
|
{
|
||||||
|
user: U,
|
||||||
|
content: C,
|
||||||
|
command_runner: &'r R,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'r, U, R: 'r + CommandRunner> Cron<'r, String, U, R>
|
||||||
|
where
|
||||||
|
U: Deref<Target = str>,
|
||||||
|
{
|
||||||
|
pub fn new<C: Deref<Target = str>>(user: U, content: C, command_runner: &'r R) -> Self {
|
||||||
|
Cron {
|
||||||
|
user,
|
||||||
|
content: String::from(&*content) + "\n",
|
||||||
|
command_runner,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'r, C, U, R: 'r + CommandRunner> Symbol for Cron<'r, C, U, R>
|
||||||
|
where
|
||||||
|
C: Deref<Target = str>,
|
||||||
|
U: Deref<Target = str>,
|
||||||
|
{
|
||||||
|
fn target_reached(&self) -> Result<bool, Box<dyn Error>> {
|
||||||
|
let tab = try!(self
|
||||||
|
.command_runner
|
||||||
|
.get_output("crontab", &["-l", "-u", &self.user]));
|
||||||
|
return Ok(tab == self.content.bytes().collect::<Vec<u8>>());
|
||||||
|
}
|
||||||
|
|
||||||
|
fn execute(&self) -> Result<(), Box<dyn Error>> {
|
||||||
|
self.command_runner.run_with_args_and_stdin(
|
||||||
|
"crontab",
|
||||||
|
&["-u", &self.user, "-"],
|
||||||
|
&self.content,
|
||||||
|
)?;
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
fn get_prerequisites(&self) -> Vec<Resource> {
|
||||||
|
vec![]
|
||||||
|
}
|
||||||
|
|
||||||
|
fn as_action<'a>(&'a self, runner: &'a dyn SymbolRunner) -> Box<dyn Action + 'a> {
|
||||||
|
Box::new(SymbolAction::new(runner, self))
|
||||||
|
}
|
||||||
|
|
||||||
|
fn into_action<'a>(self: Box<Self>, runner: &'a dyn SymbolRunner) -> Box<dyn Action + 'a>
|
||||||
|
where
|
||||||
|
Self: 'a,
|
||||||
|
{
|
||||||
|
Box::new(OwnedSymbolAction::new(runner, *self))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'r, C, U, R: 'r + CommandRunner> fmt::Display for Cron<'r, C, U, R>
|
||||||
|
where
|
||||||
|
C: Deref<Target = str>,
|
||||||
|
U: Deref<Target = str>,
|
||||||
|
{
|
||||||
|
fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> {
|
||||||
|
write!(f, "Cron {}", &*self.user)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -5,6 +5,7 @@ use std::path::Path;
|
||||||
use command_runner::{CommandRunner, SetuidCommandRunner};
|
use command_runner::{CommandRunner, SetuidCommandRunner};
|
||||||
use storage::{SimpleStorage, Storage};
|
use storage::{SimpleStorage, Storage};
|
||||||
use symbols::acme::{AcmeCert, AcmeCertChain};
|
use symbols::acme::{AcmeCert, AcmeCertChain};
|
||||||
|
use symbols::cron::Cron;
|
||||||
use symbols::file::File;
|
use symbols::file::File;
|
||||||
use symbols::git::checkout::GitCheckout;
|
use symbols::git::checkout::GitCheckout;
|
||||||
use symbols::hook::Hook;
|
use symbols::hook::Hook;
|
||||||
|
|
@ -368,4 +369,12 @@ env[PATH] = /usr/local/bin:/usr/bin:/bin
|
||||||
) -> Box<dyn Action + 'a> {
|
) -> Box<dyn Action + 'a> {
|
||||||
Box::new(File::new(path, content)).into_action(self.symbol_runner)
|
Box::new(File::new(path, content)).into_action(self.symbol_runner)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn get_cron<'a, T: 'a + Deref<Target = str>, U: 'a + Deref<Target = str>>(
|
||||||
|
&'a self,
|
||||||
|
user: U,
|
||||||
|
content: T,
|
||||||
|
) -> Box<dyn Action + 'a> {
|
||||||
|
Box::new(Cron::new(user, content, self.command_runner)).into_action(self.symbol_runner)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -68,6 +68,7 @@ impl<'a, S: Symbol + 'a> Action for OwnedSymbolAction<'a, S> {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub mod acme;
|
pub mod acme;
|
||||||
|
pub mod cron;
|
||||||
pub mod dir;
|
pub mod dir;
|
||||||
pub mod factory;
|
pub mod factory;
|
||||||
pub mod file;
|
pub mod file;
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue