public inbox for pbs-devel@lists.proxmox.com
 help / color / mirror / Atom feed
From: Dietmar Maurer <dietmar@proxmox.com>
To: pbs-devel@lists.proxmox.com
Subject: [pbs-devel] [PATCH proxmox-backup 1/2] pbs-systemd: do not depend on pbs-tools
Date: Tue, 21 Sep 2021 09:33:50 +0200	[thread overview]
Message-ID: <20210921073351.3860286-1-dietmar@proxmox.com> (raw)

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





             reply	other threads:[~2021-09-21  7:34 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-09-21  7:33 Dietmar Maurer [this message]
2021-09-21  7:33 ` [pbs-devel] [PATCH proxmox-backup 2/2] rename pbs-systemd to proxmox-systemd Dietmar Maurer
2021-09-21  8:07   ` [pbs-devel] applied: " Thomas Lamprecht
2021-09-21  8:07 ` [pbs-devel] applied: [PATCH proxmox-backup 1/2] pbs-systemd: do not depend on pbs-tools Thomas Lamprecht

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20210921073351.3860286-1-dietmar@proxmox.com \
    --to=dietmar@proxmox.com \
    --cc=pbs-devel@lists.proxmox.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
Service provided by Proxmox Server Solutions GmbH | Privacy | Legal