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) server-digest SHA256) (No client certificate requested) by lists.proxmox.com (Postfix) with ESMTPS id A161760C53 for ; Fri, 14 Aug 2020 13:00:39 +0200 (CEST) Received: from firstgate.proxmox.com (localhost [127.0.0.1]) by firstgate.proxmox.com (Proxmox) with ESMTP id 9589420C99 for ; Fri, 14 Aug 2020 13:00:39 +0200 (CEST) Received: from proxmox-new.maurer-it.com (proxmox-new.maurer-it.com [212.186.127.180]) (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 9A1F920C8C for ; Fri, 14 Aug 2020 13:00:37 +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 64DB3445F9 for ; Fri, 14 Aug 2020 13:00:37 +0200 (CEST) Date: Fri, 14 Aug 2020 13:00:29 +0200 From: Fabian =?iso-8859-1?q?Gr=FCnbichler?= To: Proxmox Backup Server development discussion References: <20200814100308.10005-1-d.csapak@proxmox.com> In-Reply-To: <20200814100308.10005-1-d.csapak@proxmox.com> MIME-Version: 1.0 User-Agent: astroid/0.15.0 (https://github.com/astroidmail/astroid) Message-Id: <1597402815.qrcl9zmds4.astroid@nora.none> Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable X-SPAM-LEVEL: Spam detection results: 0 AWL 0.033 Adjusted score from AWL reputation of From: address KAM_DMARC_STATUS 0.01 Test Rule for DKIM or SPF Failure with Strict Alignment RCVD_IN_DNSWL_MED -2.3 Sender listed at https://www.dnswl.org/, medium trust 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. [proxmox.com, services.rs] Subject: [pbs-devel] applied: [PATCH proxmox-backup v2] api2/node/services: turn service api calls into workers 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: Fri, 14 Aug 2020 11:00:39 -0000 On August 14, 2020 12:03 pm, Dominik Csapak wrote: > to be in line with pve/pmg and be able to show the progress in the gui >=20 > Signed-off-by: Dominik Csapak > --- > changes from v1: > * use Userid struct instead of strings > * rebase on master > src/api2/node/services.rs | 67 +++++++++++++++++++++++++++------------ > 1 file changed, 46 insertions(+), 21 deletions(-) >=20 > diff --git a/src/api2/node/services.rs b/src/api2/node/services.rs > index 923252cd..849bead7 100644 > --- a/src/api2/node/services.rs > +++ b/src/api2/node/services.rs > @@ -4,12 +4,13 @@ use anyhow::{bail, Error}; > use serde_json::{json, Value}; > =20 > use proxmox::{sortable, identity, list_subdirs_api_method}; > -use proxmox::api::{api, Router, Permission}; > +use proxmox::api::{api, Router, Permission, RpcEnvironment}; > use proxmox::api::router::SubdirMap; > use proxmox::api::schema::*; > =20 > use crate::api2::types::*; > use crate::config::acl::{PRIV_SYS_AUDIT, PRIV_SYS_MODIFY}; > +use crate::server::WorkerTask; > =20 > static SERVICE_NAME_LIST: [&str; 7] =3D [ > "proxmox-backup", > @@ -181,31 +182,43 @@ fn get_service_state( > Ok(json_service_state(&service, status)) > } > =20 > -fn run_service_command(service: &str, cmd: &str) -> Result= { > +fn run_service_command(service: &str, cmd: &str, userid: Userid) -> Resu= lt { > =20 > - // fixme: run background worker (fork_worker) ??? > + let workerid =3D format!("srv{}", &cmd); > =20 > let cmd =3D match cmd { > - "start"|"stop"|"restart"=3D> cmd, > - "reload" =3D> "try-reload-or-restart", // some services do not i= mplement reload > + "start"|"stop"|"restart"=3D> cmd.to_string(), > + "reload" =3D> "try-reload-or-restart".to_string(), // some servi= ces do not implement reload > _ =3D> bail!("unknown service command '{}'", cmd), > }; > + let service =3D service.to_string(); > =20 > - if service =3D=3D "proxmox-backup" && cmd =3D=3D "stop" { > - bail!("invalid service cmd '{} {}' cannot stop essential service= !", service, cmd); > - } > + let upid =3D WorkerTask::new_thread( > + &workerid, > + Some(service.clone()), > + userid, > + false, > + move |_worker| { > =20 > - let real_service_name =3D real_service_name(service); > + if service =3D=3D "proxmox-backup" && cmd =3D=3D "stop" { > + bail!("invalid service cmd '{} {}' cannot stop essential= service!", service, cmd); > + } > =20 > - let status =3D Command::new("systemctl") > - .args(&[cmd, real_service_name]) > - .status()?; > + let real_service_name =3D real_service_name(&service); > =20 > - if !status.success() { > - bail!("systemctl {} failed with {}", cmd, status); > - } > + let status =3D Command::new("systemctl") > + .args(&[&cmd, real_service_name]) > + .status()?; > + > + if !status.success() { > + bail!("systemctl {} failed with {}", cmd, status); > + } > =20 > - Ok(Value::Null) > + Ok(()) > + } > + )?; > + > + Ok(upid.into()) > } > =20 > #[api( > @@ -228,11 +241,14 @@ fn run_service_command(service: &str, cmd: &str) ->= Result { > fn start_service( > service: String, > _param: Value, > + rpcenv: &mut dyn RpcEnvironment, > ) -> Result { > =20 > + let userid: Userid =3D rpcenv.get_user().unwrap().parse()?; > + > log::info!("starting service {}", service); > =20 > - run_service_command(&service, "start") > + run_service_command(&service, "start", userid) > } > =20 > #[api( > @@ -255,11 +271,14 @@ fn start_service( > fn stop_service( > service: String, > _param: Value, > + rpcenv: &mut dyn RpcEnvironment, > ) -> Result { > =20 > + let userid: Userid =3D rpcenv.get_user().unwrap().parse()?; > + > log::info!("stopping service {}", service); > =20 > - run_service_command(&service, "stop") > + run_service_command(&service, "stop", userid) > } > =20 > #[api( > @@ -282,15 +301,18 @@ fn stop_service( > fn restart_service( > service: String, > _param: Value, > + rpcenv: &mut dyn RpcEnvironment, > ) -> Result { > =20 > + let userid: Userid =3D rpcenv.get_user().unwrap().parse()?; > + > log::info!("re-starting service {}", service); > =20 > if &service =3D=3D "proxmox-backup-proxy" { > // special case, avoid aborting running tasks > - run_service_command(&service, "reload") > + run_service_command(&service, "reload", userid) > } else { > - run_service_command(&service, "restart") > + run_service_command(&service, "restart", userid) > } > } > =20 > @@ -314,11 +336,14 @@ fn restart_service( > fn reload_service( > service: String, > _param: Value, > + rpcenv: &mut dyn RpcEnvironment, > ) -> Result { > =20 > + let userid: Userid =3D rpcenv.get_user().unwrap().parse()?; > + > log::info!("reloading service {}", service); > =20 > - run_service_command(&service, "reload") > + run_service_command(&service, "reload", userid) > } > =20 > =20 > --=20 > 2.20.1 >=20 >=20 >=20 > _______________________________________________ > pbs-devel mailing list > pbs-devel@lists.proxmox.com > https://lists.proxmox.com/cgi-bin/mailman/listinfo/pbs-devel >=20 >=20 >=20 =