]> git.proxmox.com Git - proxmox-backup.git/blobdiff - src/api2/node/services.rs
move acl to pbs_config workspaces, pbs_api_types cleanups
[proxmox-backup.git] / src / api2 / node / services.rs
index 923252cd5303b0218b4161d5315192e824bfc776..25edd1b6eda6c9880832f1c5da588ec07ce839bb 100644 (file)
@@ -4,12 +4,12 @@ use anyhow::{bail, Error};
 use serde_json::{json, Value};
 
 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::*;
 
-use crate::api2::types::*;
-use crate::config::acl::{PRIV_SYS_AUDIT, PRIV_SYS_MODIFY};
+use pbs_api_types::{Authid, NODE_SCHEMA, SERVICE_ID_SCHEMA, PRIV_SYS_AUDIT, PRIV_SYS_MODIFY};
+
+use crate::server::WorkerTask;
 
 static SERVICE_NAME_LIST: [&str; 7] = [
     "proxmox-backup",
@@ -21,7 +21,7 @@ static SERVICE_NAME_LIST: [&str; 7] = [
     "systemd-timesyncd",
 ];
 
-fn real_service_name(service: &str) -> &str {
+pub fn real_service_name(service: &str) -> &str {
 
     // since postfix package 3.1.0-3.1 the postfix unit is only here
     // to manage subinstances, of which the default is called "-".
@@ -181,31 +181,43 @@ fn get_service_state(
     Ok(json_service_state(&service, status))
 }
 
-fn run_service_command(service: &str, cmd: &str) -> Result<Value, Error> {
+fn run_service_command(service: &str, cmd: &str, auth_id: Authid) -> Result<Value, Error> {
 
-    // fixme: run background worker (fork_worker) ???
+    let workerid = format!("srv{}", &cmd);
 
     let cmd = match cmd {
-        "start"|"stop"|"restart"=> cmd,
-        "reload" => "try-reload-or-restart", // some services do not implement reload
+        "start"|"stop"|"restart"=> cmd.to_string(),
+        "reload" => "try-reload-or-restart".to_string(), // some services do not implement reload
         _ => bail!("unknown service command '{}'", cmd),
     };
+    let service = service.to_string();
 
-    if service == "proxmox-backup" && cmd == "stop" {
-        bail!("invalid service cmd '{} {}' cannot stop essential service!", service, cmd);
-    }
+    let upid = WorkerTask::new_thread(
+        &workerid,
+        Some(service.clone()),
+        auth_id,
+        false,
+        move |_worker| {
 
-    let real_service_name = real_service_name(service);
+            if service == "proxmox-backup" && cmd == "stop" {
+                bail!("invalid service cmd '{} {}' cannot stop essential service!", service, cmd);
+            }
 
-    let status = Command::new("systemctl")
-        .args(&[cmd, real_service_name])
-        .status()?;
+            let real_service_name = real_service_name(&service);
 
-    if !status.success() {
-        bail!("systemctl {} failed with {}", cmd, status);
-    }
+            let status = Command::new("systemctl")
+                .args(&[&cmd, real_service_name])
+                .status()?;
+
+            if !status.success() {
+                bail!("systemctl {} failed with {}", cmd, status);
+            }
 
-    Ok(Value::Null)
+            Ok(())
+        }
+    )?;
+
+    Ok(upid.into())
 }
 
 #[api(
@@ -228,11 +240,14 @@ fn run_service_command(service: &str, cmd: &str) -> Result<Value, Error> {
 fn start_service(
     service: String,
     _param: Value,
+    rpcenv: &mut dyn RpcEnvironment,
 ) -> Result<Value, Error> {
 
+    let auth_id: Authid = rpcenv.get_auth_id().unwrap().parse()?;
+
     log::info!("starting service {}", service);
 
-    run_service_command(&service, "start")
+    run_service_command(&service, "start", auth_id)
 }
 
 #[api(
@@ -255,11 +270,14 @@ fn start_service(
 fn stop_service(
     service: String,
     _param: Value,
+    rpcenv: &mut dyn RpcEnvironment,
  ) -> Result<Value, Error> {
 
+    let auth_id: Authid = rpcenv.get_auth_id().unwrap().parse()?;
+
     log::info!("stopping service {}", service);
 
-    run_service_command(&service, "stop")
+    run_service_command(&service, "stop", auth_id)
 }
 
 #[api(
@@ -282,15 +300,18 @@ fn stop_service(
 fn restart_service(
     service: String,
     _param: Value,
+    rpcenv: &mut dyn RpcEnvironment,
 ) -> Result<Value, Error> {
 
+    let auth_id: Authid = rpcenv.get_auth_id().unwrap().parse()?;
+
     log::info!("re-starting service {}", service);
 
     if &service == "proxmox-backup-proxy" {
         // special case, avoid aborting running tasks
-        run_service_command(&service, "reload")
+        run_service_command(&service, "reload", auth_id)
     } else {
-        run_service_command(&service, "restart")
+        run_service_command(&service, "restart", auth_id)
     }
 }
 
@@ -314,18 +335,16 @@ fn restart_service(
 fn reload_service(
     service: String,
     _param: Value,
+    rpcenv: &mut dyn RpcEnvironment,
 ) -> Result<Value, Error> {
 
+    let auth_id: Authid = rpcenv.get_auth_id().unwrap().parse()?;
+
     log::info!("reloading service {}", service);
 
-    run_service_command(&service, "reload")
+    run_service_command(&service, "reload", auth_id)
 }
 
-
-const SERVICE_ID_SCHEMA: Schema = StringSchema::new("Service ID.")
-    .max_length(256)
-    .schema();
-
 #[sortable]
 const SERVICE_SUBDIRS: SubdirMap = &sorted!([
     (