2019-01-26 16:54:18 +00:00
|
|
|
use failure::*;
|
|
|
|
|
|
|
|
use crate::tools;
|
2019-02-17 09:16:33 +00:00
|
|
|
use crate::api_schema::*;
|
2019-02-17 08:59:20 +00:00
|
|
|
use crate::api_schema::router::*;
|
2019-01-26 16:54:18 +00:00
|
|
|
use serde_json::{json, Value};
|
|
|
|
|
|
|
|
use std::process::{Command, Stdio};
|
|
|
|
|
2019-05-09 05:44:09 +00:00
|
|
|
use crate::api2::types::*;
|
|
|
|
|
2019-04-15 07:48:02 +00:00
|
|
|
static SERVICE_NAME_LIST: [&str; 7] = [
|
2019-01-26 16:54:18 +00:00
|
|
|
"proxmox-backup",
|
2019-04-15 07:48:02 +00:00
|
|
|
"proxmox-backup-proxy",
|
2019-01-26 16:54:18 +00:00
|
|
|
"sshd",
|
|
|
|
"syslog",
|
|
|
|
"cron",
|
|
|
|
"postfix",
|
|
|
|
"systemd-timesyncd",
|
|
|
|
];
|
|
|
|
|
2019-01-27 11:40:31 +00:00
|
|
|
fn real_service_name(service: &str) -> &str {
|
2019-01-26 16:54:18 +00:00
|
|
|
|
|
|
|
// since postfix package 3.1.0-3.1 the postfix unit is only here
|
|
|
|
// to manage subinstances, of which the default is called "-".
|
|
|
|
// This is where we look for the daemon status
|
|
|
|
|
2019-01-27 11:40:31 +00:00
|
|
|
if service == "postfix" {
|
|
|
|
"postfix@-"
|
|
|
|
} else {
|
|
|
|
service
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn get_full_service_state(service: &str) -> Result<Value, Error> {
|
|
|
|
|
|
|
|
let real_service_name = real_service_name(service);
|
2019-01-26 16:54:18 +00:00
|
|
|
|
|
|
|
let mut child = Command::new("/bin/systemctl")
|
|
|
|
.args(&["show", real_service_name])
|
|
|
|
.stdout(Stdio::piped())
|
|
|
|
.spawn()?;
|
|
|
|
|
|
|
|
use std::io::{BufRead,BufReader};
|
|
|
|
|
|
|
|
let mut result = json!({});
|
|
|
|
|
|
|
|
if let Some(ref mut stdout) = child.stdout {
|
|
|
|
for line in BufReader::new(stdout).lines() {
|
|
|
|
match line {
|
|
|
|
Ok(line) => {
|
|
|
|
let mut iter = line.splitn(2, '=');
|
|
|
|
let key = iter.next();
|
|
|
|
let value = iter.next();
|
|
|
|
if let (Some(key), Some(value)) = (key, value) {
|
|
|
|
result[key] = Value::from(value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Err(err) => {
|
|
|
|
log::error!("reading service config failed: {}", err);
|
|
|
|
let _ = child.kill();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let status = child.wait().unwrap();
|
|
|
|
if !status.success() {
|
|
|
|
bail!("systemctl show failed with {}", status);
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(result)
|
|
|
|
}
|
|
|
|
|
2019-01-27 11:40:31 +00:00
|
|
|
fn json_service_state(service: &str, status: Value) -> Value {
|
|
|
|
|
|
|
|
if let Some(desc) = status["Description"].as_str() {
|
|
|
|
let name = status["Name"].as_str().unwrap_or(service);
|
|
|
|
let state = status["SubState"].as_str().unwrap_or("unknown");
|
|
|
|
return json!({
|
|
|
|
"service": service,
|
|
|
|
"name": name,
|
|
|
|
"desc": desc,
|
|
|
|
"state": state,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
Value::Null
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-01-26 16:54:18 +00:00
|
|
|
fn list_services(
|
2019-01-30 17:25:37 +00:00
|
|
|
_param: Value,
|
2019-01-26 16:54:18 +00:00
|
|
|
_info: &ApiMethod,
|
2019-06-07 11:10:56 +00:00
|
|
|
_rpcenv: &mut dyn RpcEnvironment,
|
2019-01-26 16:54:18 +00:00
|
|
|
) -> Result<Value, Error> {
|
|
|
|
|
|
|
|
let mut list = vec![];
|
|
|
|
|
|
|
|
for service in &SERVICE_NAME_LIST {
|
|
|
|
match get_full_service_state(service) {
|
|
|
|
Ok(status) => {
|
2019-01-27 11:40:31 +00:00
|
|
|
let state = json_service_state(service, status);
|
|
|
|
if state != Value::Null {
|
|
|
|
list.push(state);
|
2019-01-26 16:54:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
Err(err) => log::error!("{}", err),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(Value::from(list))
|
|
|
|
}
|
|
|
|
|
2019-01-27 11:40:31 +00:00
|
|
|
fn get_service_state(
|
|
|
|
param: Value,
|
|
|
|
_info: &ApiMethod,
|
2019-06-07 11:10:56 +00:00
|
|
|
_rpcenv: &mut dyn RpcEnvironment,
|
2019-01-27 11:40:31 +00:00
|
|
|
) -> Result<Value, Error> {
|
|
|
|
|
|
|
|
let service = tools::required_string_param(¶m, "service")?;
|
|
|
|
|
|
|
|
if !SERVICE_NAME_LIST.contains(&service) {
|
|
|
|
bail!("unknown service name '{}'", service);
|
|
|
|
}
|
|
|
|
|
|
|
|
let status = get_full_service_state(service)?;
|
|
|
|
|
|
|
|
Ok(json_service_state(service, status))
|
|
|
|
}
|
|
|
|
|
|
|
|
fn run_service_command(service: &str, cmd: &str) -> Result<Value, Error> {
|
|
|
|
|
|
|
|
// fixme: run background worker (fork_worker) ???
|
|
|
|
|
|
|
|
match cmd {
|
|
|
|
"start"|"stop"|"restart"|"reload" => {},
|
|
|
|
_ => bail!("unknown service command '{}'", cmd),
|
|
|
|
}
|
|
|
|
|
2019-10-26 09:36:01 +00:00
|
|
|
if service == "proxmox-backup" && cmd != "restart" {
|
|
|
|
bail!("invalid service cmd '{} {}'", service, cmd);
|
2019-01-27 11:40:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
let real_service_name = real_service_name(service);
|
|
|
|
|
|
|
|
let status = Command::new("/bin/systemctl")
|
|
|
|
.args(&[cmd, real_service_name])
|
|
|
|
.status()?;
|
|
|
|
|
|
|
|
if !status.success() {
|
|
|
|
bail!("systemctl {} failed with {}", cmd, status);
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(Value::Null)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn start_service(
|
|
|
|
param: Value,
|
|
|
|
_info: &ApiMethod,
|
2019-06-07 11:10:56 +00:00
|
|
|
_rpcenv: &mut dyn RpcEnvironment,
|
2019-01-27 11:40:31 +00:00
|
|
|
) -> Result<Value, Error> {
|
|
|
|
|
|
|
|
let service = tools::required_string_param(¶m, "service")?;
|
|
|
|
|
|
|
|
log::info!("starting service {}", service);
|
|
|
|
|
|
|
|
run_service_command(service, "start")
|
|
|
|
}
|
|
|
|
|
|
|
|
fn stop_service(
|
|
|
|
param: Value,
|
|
|
|
_info: &ApiMethod,
|
2019-06-07 11:10:56 +00:00
|
|
|
_rpcenv: &mut dyn RpcEnvironment,
|
2019-01-27 11:40:31 +00:00
|
|
|
) -> Result<Value, Error> {
|
|
|
|
|
|
|
|
let service = tools::required_string_param(¶m, "service")?;
|
|
|
|
|
|
|
|
log::info!("stoping service {}", service);
|
|
|
|
|
|
|
|
run_service_command(service, "stop")
|
|
|
|
}
|
|
|
|
|
|
|
|
fn restart_service(
|
|
|
|
param: Value,
|
|
|
|
_info: &ApiMethod,
|
2019-06-07 11:10:56 +00:00
|
|
|
_rpcenv: &mut dyn RpcEnvironment,
|
2019-01-27 11:40:31 +00:00
|
|
|
) -> Result<Value, Error> {
|
|
|
|
|
|
|
|
let service = tools::required_string_param(¶m, "service")?;
|
|
|
|
|
|
|
|
log::info!("re-starting service {}", service);
|
|
|
|
|
2019-04-15 07:48:02 +00:00
|
|
|
if service == "proxmox-backup-proxy" {
|
|
|
|
// special case, avoid aborting running tasks
|
|
|
|
run_service_command(service, "reload")
|
|
|
|
} else {
|
|
|
|
run_service_command(service, "restart")
|
|
|
|
}
|
2019-01-27 11:40:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn reload_service(
|
|
|
|
param: Value,
|
|
|
|
_info: &ApiMethod,
|
2019-06-07 11:10:56 +00:00
|
|
|
_rpcenv: &mut dyn RpcEnvironment,
|
2019-01-27 11:40:31 +00:00
|
|
|
) -> Result<Value, Error> {
|
|
|
|
|
|
|
|
let service = tools::required_string_param(¶m, "service")?;
|
|
|
|
|
|
|
|
log::info!("reloading service {}", service);
|
|
|
|
|
|
|
|
run_service_command(service, "reload")
|
|
|
|
}
|
|
|
|
|
2019-11-21 08:36:41 +00:00
|
|
|
|
|
|
|
const SERVICE_ID_SCHEMA: Schema = StringSchema::new("Service ID.")
|
|
|
|
.max_length(256)
|
|
|
|
.schema();
|
|
|
|
|
|
|
|
const SERVICE_SUBDIRS: SubdirMap = &[
|
|
|
|
(
|
|
|
|
"reload", &Router::new()
|
|
|
|
.post(
|
|
|
|
&ApiMethod::new(
|
|
|
|
&ApiHandler::Sync(&reload_service),
|
|
|
|
&ObjectSchema::new(
|
|
|
|
"Reload service.",
|
|
|
|
&[
|
|
|
|
("node", false, &NODE_SCHEMA),
|
|
|
|
("service", false, &SERVICE_ID_SCHEMA),
|
|
|
|
],
|
|
|
|
)
|
|
|
|
).protected(true)
|
|
|
|
)
|
|
|
|
),
|
|
|
|
(
|
|
|
|
"restart", &Router::new()
|
|
|
|
.post(
|
|
|
|
&ApiMethod::new(
|
|
|
|
&ApiHandler::Sync(&restart_service),
|
|
|
|
&ObjectSchema::new(
|
|
|
|
"Restart service.",
|
|
|
|
&[
|
|
|
|
("node", false, &NODE_SCHEMA),
|
|
|
|
("service", false, &SERVICE_ID_SCHEMA),
|
|
|
|
],
|
|
|
|
)
|
|
|
|
).protected(true)
|
|
|
|
)
|
|
|
|
),
|
|
|
|
(
|
|
|
|
"start", &Router::new()
|
|
|
|
.post(
|
|
|
|
&ApiMethod::new(
|
|
|
|
&ApiHandler::Sync(&start_service),
|
|
|
|
&ObjectSchema::new(
|
|
|
|
"Start service.",
|
|
|
|
&[
|
|
|
|
("node", false, &NODE_SCHEMA),
|
|
|
|
("service", false, &SERVICE_ID_SCHEMA),
|
|
|
|
],
|
|
|
|
)
|
|
|
|
).protected(true)
|
|
|
|
)
|
|
|
|
),
|
|
|
|
(
|
|
|
|
"state", &Router::new()
|
|
|
|
.get(
|
|
|
|
&ApiMethod::new(
|
|
|
|
&ApiHandler::Sync(&get_service_state),
|
|
|
|
&ObjectSchema::new(
|
|
|
|
"Read service properties.",
|
|
|
|
&[
|
|
|
|
("node", false, &NODE_SCHEMA),
|
|
|
|
("service", false, &SERVICE_ID_SCHEMA),
|
|
|
|
],
|
|
|
|
)
|
2019-01-26 16:54:18 +00:00
|
|
|
)
|
|
|
|
)
|
2019-11-21 08:36:41 +00:00
|
|
|
),
|
|
|
|
(
|
|
|
|
"stop", &Router::new()
|
|
|
|
.post(
|
|
|
|
&ApiMethod::new(
|
|
|
|
&ApiHandler::Sync(&stop_service),
|
|
|
|
&ObjectSchema::new(
|
|
|
|
"Stop service.",
|
|
|
|
&[
|
|
|
|
("node", false, &NODE_SCHEMA),
|
|
|
|
("service", false, &SERVICE_ID_SCHEMA),
|
|
|
|
],
|
|
|
|
)
|
|
|
|
).protected(true)
|
|
|
|
)
|
|
|
|
),
|
|
|
|
];
|
|
|
|
|
|
|
|
const SERVICE_ROUTER: Router = Router::new()
|
|
|
|
.get(&list_subdirs_api_method!(SERVICE_SUBDIRS))
|
|
|
|
.subdirs(SERVICE_SUBDIRS);
|
|
|
|
|
|
|
|
pub const ROUTER: Router = Router::new()
|
|
|
|
.get(
|
|
|
|
&ApiMethod::new(
|
|
|
|
&ApiHandler::Sync(&list_services),
|
|
|
|
&ObjectSchema::new(
|
|
|
|
"Service list.",
|
|
|
|
&[ ("node", false, &NODE_SCHEMA) ],
|
|
|
|
)
|
|
|
|
).returns(
|
|
|
|
&ArraySchema::new(
|
|
|
|
"Returns a list of systemd services.",
|
|
|
|
&ObjectSchema::new(
|
|
|
|
"Service details.",
|
|
|
|
&[
|
|
|
|
("service", false, &SERVICE_ID_SCHEMA),
|
|
|
|
("name", false, &StringSchema::new("systemd service name.").schema()),
|
|
|
|
("desc", false, &StringSchema::new("systemd service description.").schema()),
|
|
|
|
("state", false, &StringSchema::new("systemd service 'SubState'.").schema()),
|
|
|
|
],
|
|
|
|
).schema()
|
|
|
|
).schema()
|
2019-01-27 11:40:31 +00:00
|
|
|
)
|
2019-11-21 08:36:41 +00:00
|
|
|
)
|
|
|
|
.match_all("service", &SERVICE_ROUTER);
|
|
|
|
|