kanit

Toy init system
Log | Files | Refs | README | LICENSE

builder.rs (4011B)


      1 use async_process::{Child, Command};
      2 
      3 use kanit_common::error::{Context, ErrorKind, Result};
      4 
      5 use crate::{RestartPolicy, Supervisor};
      6 
      7 pub struct SupervisorBuilder(Supervisor);
      8 
      9 #[allow(dead_code)]
     10 impl SupervisorBuilder {
     11     pub fn new<S: ToString, I: IntoIterator<Item = S>>(cmd: S, args: I) -> Self {
     12         Self(Supervisor {
     13             exec: cmd.to_string(),
     14             args: args.into_iter().map(|s| s.to_string()).collect(),
     15             restart_delay: None,
     16             restart_attempts: None,
     17             restart_policy: None,
     18             cgroup: None,
     19             pwd: None,
     20             root: None,
     21             env: vec![],
     22             group: None,
     23             user: None,
     24             stdout: None,
     25             stderr: None,
     26         })
     27     }
     28 
     29     pub fn from_supervisor(supervisor: Supervisor) -> Self {
     30         Self(supervisor)
     31     }
     32 
     33     pub fn build(self) -> Supervisor {
     34         self.0
     35     }
     36 
     37     pub fn spawn(self) -> Result<Child> {
     38         let mut args = vec![];
     39 
     40         if let Some(delay) = self.0.restart_delay {
     41             args.push("-d".to_string());
     42             args.push(delay.to_string());
     43         }
     44 
     45         if let Some(attempts) = self.0.restart_attempts {
     46             args.push("-a".to_string());
     47             args.push(attempts.to_string());
     48         }
     49 
     50         if let Some(policy) = self.0.restart_policy {
     51             args.push("-P".to_string());
     52             args.push(policy.to_string());
     53         }
     54 
     55         if let Some(pwd) = self.0.pwd {
     56             args.push("-p".to_string());
     57             args.push(pwd);
     58         }
     59 
     60         if let Some(root) = self.0.root {
     61             args.push("-r".to_string());
     62             args.push(root);
     63         }
     64 
     65         for pair in self.0.env {
     66             args.push("-e".to_string());
     67             args.push(pair);
     68         }
     69 
     70         if let Some(group) = self.0.group {
     71             args.push("-g".to_string());
     72             args.push(group);
     73         }
     74 
     75         if let Some(user) = self.0.user {
     76             args.push("-u".to_string());
     77             args.push(user);
     78         }
     79 
     80         if let Some(stdout) = self.0.stdout {
     81             args.push("--stdout".to_string());
     82             args.push(stdout);
     83         }
     84 
     85         if let Some(stderr) = self.0.stderr {
     86             args.push("--stderr".to_string());
     87             args.push(stderr);
     88         }
     89 
     90         if let Some(cgroup) = self.0.cgroup {
     91             args.push("--cgroup".to_string());
     92             args.push(cgroup);
     93         }
     94 
     95         args.push("--".to_string());
     96 
     97         args.push(self.0.exec);
     98 
     99         args.extend_from_slice(&self.0.args);
    100 
    101         Command::new("kanit-supervisor")
    102             .args(args)
    103             .spawn()
    104             .context_kind("failed to spawn supervisor", ErrorKind::Recoverable)
    105     }
    106 
    107     pub fn restart_delay(mut self, delay: u64) -> Self {
    108         self.0.restart_delay = Some(delay);
    109         self
    110     }
    111 
    112     pub fn restart_attempts(mut self, attempts: u64) -> Self {
    113         self.0.restart_attempts = Some(attempts);
    114         self
    115     }
    116 
    117     pub fn restart_policy(mut self, policy: RestartPolicy) -> Self {
    118         self.0.restart_policy = Some(policy);
    119         self
    120     }
    121 
    122     pub fn cgroup(mut self, cgroup: String) -> Self {
    123         self.0.cgroup = Some(cgroup);
    124         self
    125     }
    126 
    127     pub fn pwd(mut self, pwd: String) -> Self {
    128         self.0.pwd = Some(pwd);
    129         self
    130     }
    131 
    132     pub fn root(mut self, root: String) -> Self {
    133         self.0.root = Some(root);
    134         self
    135     }
    136 
    137     pub fn env(mut self, key: String, value: String) -> Self {
    138         self.0.env.push(format!("{key}={value}"));
    139         self
    140     }
    141 
    142     pub fn group(mut self, group: String) -> Self {
    143         self.0.group = Some(group);
    144         self
    145     }
    146 
    147     pub fn user(mut self, user: String) -> Self {
    148         self.0.user = Some(user);
    149         self
    150     }
    151 
    152     pub fn stdout(mut self, stdout: String) -> Self {
    153         self.0.stdout = Some(stdout);
    154         self
    155     }
    156 
    157     pub fn stderr(mut self, stderr: String) -> Self {
    158         self.0.stderr = Some(stderr);
    159         self
    160     }
    161 }