]> git.proxmox.com Git - proxmox-backup.git/commitdiff
pbs-systemd: do not depend on pbs-tools
authorDietmar Maurer <dietmar@proxmox.com>
Tue, 21 Sep 2021 07:33:50 +0000 (09:33 +0200)
committerThomas Lamprecht <t.lamprecht@proxmox.com>
Tue, 21 Sep 2021 08:06:27 +0000 (10:06 +0200)
Instead, copy a few line of nom helper code, and implement
a simple run_command helper.

pbs-systemd/Cargo.toml
pbs-systemd/src/parse_time.rs
pbs-systemd/src/unit.rs

index b4575f0ac7ed87366eda12625b6ec8f4d19f3100..830ab8163f3bdc82f04f1adb197df4fea57cec7c 100644 (file)
@@ -13,4 +13,4 @@ nom = "5.1"
 
 proxmox = { version = "0.13.3", default-features = false }
 
-pbs-tools = { path = "../pbs-tools" }
+#pbs-tools = { path = "../pbs-tools" }
index 05ac56726de2c2b40affadce5309e1f1c14370c0..ba9449b1e2dee53f75488bcea106391728946705 100644 (file)
@@ -1,23 +1,52 @@
 use std::collections::HashMap;
 
-use anyhow::{Error};
+use anyhow::{bail, Error};
 use lazy_static::lazy_static;
 
 use super::time::*;
 
-use pbs_tools::nom::{
-    parse_complete_line, parse_u64, parse_error, IResult,
-};
-
 use nom::{
-    error::{context},
+    error::{context, ParseError, VerboseError},
     bytes::complete::{tag, take_while1},
-    combinator::{map_res, opt, recognize},
+    combinator::{map_res, all_consuming, opt, recognize},
     sequence::{pair, preceded, tuple},
     character::complete::{alpha1, space0, digit1},
     multi::separated_nonempty_list,
 };
 
+type IResult<I, O, E = VerboseError<I>> = Result<(I, O), nom::Err<E>>;
+
+fn parse_error<'a>(i: &'a str, context: &'static str) -> nom::Err<VerboseError<&'a str>> {
+    let err = VerboseError { errors: Vec::new() };
+    let err = VerboseError::add_context(i, context, err);
+    nom::Err::Error(err)
+}
+
+// Parse a 64 bit unsigned integer
+fn parse_u64(i: &str) -> IResult<&str, u64> {
+    map_res(recognize(digit1), str::parse)(i)
+}
+
+// Parse complete input, generate simple error message (use this for sinple line input).
+fn parse_complete_line<'a, F, O>(what: &str, i: &'a str, parser: F) -> Result<O, Error>
+    where F: Fn(&'a str) -> IResult<&'a str, O>,
+{
+    match all_consuming(parser)(i) {
+        Err(nom::Err::Error(VerboseError { errors })) |
+        Err(nom::Err::Failure(VerboseError { errors })) => {
+            if errors.is_empty() {
+                bail!("unable to parse {}", what);
+            } else {
+                bail!("unable to parse {} at '{}' - {:?}", what, errors[0].0, errors[0].1);
+            }
+        }
+        Err(err) => {
+            bail!("unable to parse {} - {}", what, err);
+        }
+        Ok((_, data)) => Ok(data),
+    }
+}
+
 lazy_static! {
     pub static ref TIME_SPAN_UNITS: HashMap<&'static str, f64> = {
         let mut map = HashMap::new();
index 811493fef18d51322797e0f513b092ee830ffcdc..af3db1a6f7c04755b8d9286500b2f7e9c596f2e4 100644 (file)
@@ -1,6 +1,38 @@
-use anyhow::{bail, Error};
+use std::process::Command;
+
+use anyhow::{bail, format_err, Error};
+
+fn run_command(mut command: Command) -> Result<(), Error> {
+    let output = command
+        .output()
+        .map_err(|err| format_err!("failed to execute {:?} - {}", command, err))?;
+
+    proxmox::try_block!({
+        if !output.status.success() {
+            match output.status.code() {
+                Some(code) => {
+                    if code != 0 {
+                        let msg = String::from_utf8(output.stderr)
+                            .map(|m| {
+                                if m.is_empty() {
+                                    String::from("no error message")
+                                } else {
+                                    m
+                                }
+                            })
+                            .unwrap_or_else(|_| String::from("non utf8 error message (suppressed)"));
+
+                        bail!("status code: {} - {}", code, msg);
+                    }
+                }
+                None => bail!("terminated by signal"),
+            }
+        }
+        Ok(())
+    }).map_err(|err| format_err!("command {:?} failed - {}", command, err))?;
 
-use pbs_tools::run_command;
+    Ok(())
+}
 
 /// Escape strings for usage in systemd unit names
 pub fn escape_unit(mut unit: &str, is_path: bool) -> String {
@@ -88,7 +120,7 @@ pub fn reload_daemon() -> Result<(), Error> {
     let mut command = std::process::Command::new("systemctl");
     command.arg("daemon-reload");
 
-    run_command(command, None)?;
+    run_command(command)?;
 
     Ok(())
 }
@@ -98,7 +130,7 @@ pub fn disable_unit(unit: &str) -> Result<(), Error> {
     command.arg("disable");
     command.arg(unit);
 
-    run_command(command, None)?;
+    run_command(command)?;
 
     Ok(())
 }
@@ -108,7 +140,7 @@ pub fn enable_unit(unit: &str) -> Result<(), Error> {
     command.arg("enable");
     command.arg(unit);
 
-    run_command(command, None)?;
+    run_command(command)?;
 
     Ok(())
 }
@@ -118,7 +150,7 @@ pub fn start_unit(unit: &str) -> Result<(), Error> {
     command.arg("start");
     command.arg(unit);
 
-    run_command(command, None)?;
+    run_command(command)?;
 
     Ok(())
 }
@@ -128,7 +160,7 @@ pub fn stop_unit(unit: &str) -> Result<(), Error> {
     command.arg("stop");
     command.arg(unit);
 
-    run_command(command, None)?;
+    run_command(command)?;
 
     Ok(())
 }
@@ -138,7 +170,7 @@ pub fn reload_unit(unit: &str) -> Result<(), Error> {
     command.arg("try-reload-or-restart");
     command.arg(unit);
 
-    run_command(command, None)?;
+    run_command(command)?;
 
     Ok(())
 }