api2/node/services: turn service api calls into workers
to be in line with pve/pmg and be able to show the progress in the gui Signed-off-by: Dominik Csapak <d.csapak@proxmox.com>
This commit is contained in:
parent
934deeff2d
commit
a1e9c05738
@ -4,12 +4,13 @@ use anyhow::{bail, Error};
|
|||||||
use serde_json::{json, Value};
|
use serde_json::{json, Value};
|
||||||
|
|
||||||
use proxmox::{sortable, identity, list_subdirs_api_method};
|
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::router::SubdirMap;
|
||||||
use proxmox::api::schema::*;
|
use proxmox::api::schema::*;
|
||||||
|
|
||||||
use crate::api2::types::*;
|
use crate::api2::types::*;
|
||||||
use crate::config::acl::{PRIV_SYS_AUDIT, PRIV_SYS_MODIFY};
|
use crate::config::acl::{PRIV_SYS_AUDIT, PRIV_SYS_MODIFY};
|
||||||
|
use crate::server::WorkerTask;
|
||||||
|
|
||||||
static SERVICE_NAME_LIST: [&str; 7] = [
|
static SERVICE_NAME_LIST: [&str; 7] = [
|
||||||
"proxmox-backup",
|
"proxmox-backup",
|
||||||
@ -181,31 +182,43 @@ fn get_service_state(
|
|||||||
Ok(json_service_state(&service, status))
|
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, userid: Userid) -> Result<Value, Error> {
|
||||||
|
|
||||||
// fixme: run background worker (fork_worker) ???
|
let workerid = format!("srv{}", &cmd);
|
||||||
|
|
||||||
let cmd = match cmd {
|
let cmd = match cmd {
|
||||||
"start"|"stop"|"restart"=> cmd,
|
"start"|"stop"|"restart"=> cmd.to_string(),
|
||||||
"reload" => "try-reload-or-restart", // some services do not implement reload
|
"reload" => "try-reload-or-restart".to_string(), // some services do not implement reload
|
||||||
_ => bail!("unknown service command '{}'", cmd),
|
_ => bail!("unknown service command '{}'", cmd),
|
||||||
};
|
};
|
||||||
|
let service = service.to_string();
|
||||||
|
|
||||||
|
let upid = WorkerTask::new_thread(
|
||||||
|
&workerid,
|
||||||
|
Some(service.clone()),
|
||||||
|
userid,
|
||||||
|
false,
|
||||||
|
move |_worker| {
|
||||||
|
|
||||||
if service == "proxmox-backup" && cmd == "stop" {
|
if service == "proxmox-backup" && cmd == "stop" {
|
||||||
bail!("invalid service cmd '{} {}' cannot stop essential service!", service, cmd);
|
bail!("invalid service cmd '{} {}' cannot stop essential service!", service, cmd);
|
||||||
}
|
}
|
||||||
|
|
||||||
let real_service_name = real_service_name(service);
|
let real_service_name = real_service_name(&service);
|
||||||
|
|
||||||
let status = Command::new("systemctl")
|
let status = Command::new("systemctl")
|
||||||
.args(&[cmd, real_service_name])
|
.args(&[&cmd, real_service_name])
|
||||||
.status()?;
|
.status()?;
|
||||||
|
|
||||||
if !status.success() {
|
if !status.success() {
|
||||||
bail!("systemctl {} failed with {}", cmd, status);
|
bail!("systemctl {} failed with {}", cmd, status);
|
||||||
}
|
}
|
||||||
|
|
||||||
Ok(Value::Null)
|
Ok(())
|
||||||
|
}
|
||||||
|
)?;
|
||||||
|
|
||||||
|
Ok(upid.into())
|
||||||
}
|
}
|
||||||
|
|
||||||
#[api(
|
#[api(
|
||||||
@ -228,11 +241,14 @@ fn run_service_command(service: &str, cmd: &str) -> Result<Value, Error> {
|
|||||||
fn start_service(
|
fn start_service(
|
||||||
service: String,
|
service: String,
|
||||||
_param: Value,
|
_param: Value,
|
||||||
|
rpcenv: &mut dyn RpcEnvironment,
|
||||||
) -> Result<Value, Error> {
|
) -> Result<Value, Error> {
|
||||||
|
|
||||||
|
let userid: Userid = rpcenv.get_user().unwrap().parse()?;
|
||||||
|
|
||||||
log::info!("starting service {}", service);
|
log::info!("starting service {}", service);
|
||||||
|
|
||||||
run_service_command(&service, "start")
|
run_service_command(&service, "start", userid)
|
||||||
}
|
}
|
||||||
|
|
||||||
#[api(
|
#[api(
|
||||||
@ -255,11 +271,14 @@ fn start_service(
|
|||||||
fn stop_service(
|
fn stop_service(
|
||||||
service: String,
|
service: String,
|
||||||
_param: Value,
|
_param: Value,
|
||||||
|
rpcenv: &mut dyn RpcEnvironment,
|
||||||
) -> Result<Value, Error> {
|
) -> Result<Value, Error> {
|
||||||
|
|
||||||
|
let userid: Userid = rpcenv.get_user().unwrap().parse()?;
|
||||||
|
|
||||||
log::info!("stopping service {}", service);
|
log::info!("stopping service {}", service);
|
||||||
|
|
||||||
run_service_command(&service, "stop")
|
run_service_command(&service, "stop", userid)
|
||||||
}
|
}
|
||||||
|
|
||||||
#[api(
|
#[api(
|
||||||
@ -282,15 +301,18 @@ fn stop_service(
|
|||||||
fn restart_service(
|
fn restart_service(
|
||||||
service: String,
|
service: String,
|
||||||
_param: Value,
|
_param: Value,
|
||||||
|
rpcenv: &mut dyn RpcEnvironment,
|
||||||
) -> Result<Value, Error> {
|
) -> Result<Value, Error> {
|
||||||
|
|
||||||
|
let userid: Userid = rpcenv.get_user().unwrap().parse()?;
|
||||||
|
|
||||||
log::info!("re-starting service {}", service);
|
log::info!("re-starting service {}", service);
|
||||||
|
|
||||||
if &service == "proxmox-backup-proxy" {
|
if &service == "proxmox-backup-proxy" {
|
||||||
// special case, avoid aborting running tasks
|
// special case, avoid aborting running tasks
|
||||||
run_service_command(&service, "reload")
|
run_service_command(&service, "reload", userid)
|
||||||
} else {
|
} else {
|
||||||
run_service_command(&service, "restart")
|
run_service_command(&service, "restart", userid)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -314,11 +336,14 @@ fn restart_service(
|
|||||||
fn reload_service(
|
fn reload_service(
|
||||||
service: String,
|
service: String,
|
||||||
_param: Value,
|
_param: Value,
|
||||||
|
rpcenv: &mut dyn RpcEnvironment,
|
||||||
) -> Result<Value, Error> {
|
) -> Result<Value, Error> {
|
||||||
|
|
||||||
|
let userid: Userid = rpcenv.get_user().unwrap().parse()?;
|
||||||
|
|
||||||
log::info!("reloading service {}", service);
|
log::info!("reloading service {}", service);
|
||||||
|
|
||||||
run_service_command(&service, "reload")
|
run_service_command(&service, "reload", userid)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user