[pbs-devel] [PATCH proxmox-backup 1/2] pbs-systemd: do not depend on pbs-tools

Dietmar Maurer dietmar at proxmox.com
Tue Sep 21 09:33:50 CEST 2021


Instead, copy a few line of nom helper code, and implement
a simple run_command helper.
---
 pbs-systemd/Cargo.toml        |  2 +-
 pbs-systemd/src/parse_time.rs | 43 ++++++++++++++++++++++++++-----
 pbs-systemd/src/unit.rs       | 48 +++++++++++++++++++++++++++++------
 3 files changed, 77 insertions(+), 16 deletions(-)

diff --git a/pbs-systemd/Cargo.toml b/pbs-systemd/Cargo.toml
index b4575f0a..830ab816 100644
--- a/pbs-systemd/Cargo.toml
+++ b/pbs-systemd/Cargo.toml
@@ -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" }
diff --git a/pbs-systemd/src/parse_time.rs b/pbs-systemd/src/parse_time.rs
index 05ac5672..ba9449b1 100644
--- a/pbs-systemd/src/parse_time.rs
+++ b/pbs-systemd/src/parse_time.rs
@@ -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();
diff --git a/pbs-systemd/src/unit.rs b/pbs-systemd/src/unit.rs
index 811493fe..af3db1a6 100644
--- a/pbs-systemd/src/unit.rs
+++ b/pbs-systemd/src/unit.rs
@@ -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(())
 }
-- 
2.30.2






More information about the pbs-devel mailing list