From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from firstgate.proxmox.com (firstgate.proxmox.com [212.224.123.68]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits)) (No client certificate requested) by lists.proxmox.com (Postfix) with ESMTPS id 936786B927 for ; Tue, 21 Sep 2021 09:34:04 +0200 (CEST) Received: from firstgate.proxmox.com (localhost [127.0.0.1]) by firstgate.proxmox.com (Proxmox) with ESMTP id 87DBEA78A for ; Tue, 21 Sep 2021 09:34:04 +0200 (CEST) Received: from proxmox-new.maurer-it.com (proxmox-new.maurer-it.com [94.136.29.106]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by firstgate.proxmox.com (Proxmox) with ESMTPS id C9770A781 for ; Tue, 21 Sep 2021 09:34:03 +0200 (CEST) Received: from proxmox-new.maurer-it.com (localhost.localdomain [127.0.0.1]) by proxmox-new.maurer-it.com (Proxmox) with ESMTP id 380A7449A1; Tue, 21 Sep 2021 09:33:57 +0200 (CEST) From: Dietmar Maurer To: pbs-devel@lists.proxmox.com Date: Tue, 21 Sep 2021 09:33:50 +0200 Message-Id: <20210921073351.3860286-1-dietmar@proxmox.com> X-Mailer: git-send-email 2.30.2 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-SPAM-LEVEL: Spam detection results: 0 AWL 0.591 Adjusted score from AWL reputation of From: address BAYES_00 -1.9 Bayes spam probability is 0 to 1% KAM_DMARC_STATUS 0.01 Test Rule for DKIM or SPF Failure with Strict Alignment SPF_HELO_NONE 0.001 SPF: HELO does not publish an SPF Record SPF_PASS -0.001 SPF: sender matches SPF record URIBL_BLOCKED 0.001 ADMINISTRATOR NOTICE: The query to URIBL was blocked. See http://wiki.apache.org/spamassassin/DnsBlocklists#dnsbl-block for more information. [unit.rs] Subject: [pbs-devel] [PATCH proxmox-backup 1/2] pbs-systemd: do not depend on pbs-tools X-BeenThere: pbs-devel@lists.proxmox.com X-Mailman-Version: 2.1.29 Precedence: list List-Id: Proxmox Backup Server development discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Tue, 21 Sep 2021 07:34:04 -0000 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> = Result<(I, O), nom::Err>; + +fn parse_error<'a>(i: &'a str, context: &'static str) -> nom::Err> { + 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 + 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