src/tools/systemd/parser.rs: use different setups for service and timer files, code cleanup

This commit is contained in:
Dietmar Maurer 2020-05-14 13:55:13 +02:00
parent 2ebdbac1c4
commit 00491c0230
2 changed files with 47 additions and 22 deletions

View File

@ -3,7 +3,6 @@ use regex::Regex;
use lazy_static::lazy_static; use lazy_static::lazy_static;
use proxmox::api::section_config::SectionConfigData; use proxmox::api::section_config::SectionConfigData;
use proxmox::tools::{fs::replace_file, fs::CreateOptions};
use crate::PROXMOX_SAFE_ID_REGEX_STR; use crate::PROXMOX_SAFE_ID_REGEX_STR;
use crate::tools::systemd::parser::*; use crate::tools::systemd::parser::*;
@ -59,7 +58,7 @@ pub fn list_jobs() -> Result<Vec<JobListEntry>, Error> {
}; };
// fixme: read config data ? // fixme: read config data ?
//let config = parse_systemd_config(&format!("{}/{}", SYSTEMD_CONFIG_DIR, name))?; //let config = parse_systemd_timer(&format!("{}/{}", SYSTEMD_CONFIG_DIR, name))?;
match (&caps[1], &caps[2]) { match (&caps[1], &caps[2]) {
("gc", store) => { ("gc", store) => {
@ -152,22 +151,10 @@ pub fn create_garbage_collection_job(
let basename = format!("{}/pbs-gc-{}", SYSTEMD_CONFIG_DIR, store); let basename = format!("{}/pbs-gc-{}", SYSTEMD_CONFIG_DIR, store);
let timer_fn = format!("{}.timer", basename); let timer_fn = format!("{}.timer", basename);
let timer_config = CONFIG.write(&timer_fn, &timer_config)?;
let service_fn = format!("{}.service", basename); let service_fn = format!("{}.service", basename);
let service_config = CONFIG.write(&service_fn, &service_config)?;
let backup_user = crate::backup::backup_user()?; save_systemd_service(&service_fn, &service_config)?;
let mode = nix::sys::stat::Mode::from_bits_truncate(0o0750); save_systemd_timer(&timer_fn, &timer_config)?;
// set the correct owner/group/permissions while saving file
// owner(rw) = root, group(r)= backup
let options = CreateOptions::new()
.perm(mode)
.owner(nix::unistd::ROOT)
.group(backup_user.gid);
replace_file(service_fn, service_config.as_bytes(), options.clone())?;
replace_file(timer_fn, timer_config.as_bytes(), options)?;
Ok(()) Ok(())
} }

View File

@ -16,10 +16,11 @@ use proxmox::tools::{fs::replace_file, fs::CreateOptions};
lazy_static! { lazy_static! {
pub static ref CONFIG: SectionConfig = init(); pub static ref SERVICE_CONFIG: SectionConfig = init_service();
pub static ref TIMER_CONFIG: SectionConfig = init_timer();
} }
fn init() -> SectionConfig { fn init_service() -> SectionConfig {
let mut config = SectionConfig::with_systemd_syntax(&SYSTEMD_SECTION_NAME_SCHEMA); let mut config = SectionConfig::with_systemd_syntax(&SYSTEMD_SECTION_NAME_SCHEMA);
@ -44,6 +45,28 @@ fn init() -> SectionConfig {
} }
_ => unreachable!(), _ => unreachable!(),
}; };
config
}
fn init_timer() -> SectionConfig {
let mut config = SectionConfig::with_systemd_syntax(&SYSTEMD_SECTION_NAME_SCHEMA);
match SystemdUnitSection::API_SCHEMA {
Schema::Object(ref obj_schema) => {
let plugin = SectionConfigPlugin::new("Unit".to_string(), obj_schema);
config.register_plugin(plugin);
}
_ => unreachable!(),
};
match SystemdInstallSection::API_SCHEMA {
Schema::Object(ref obj_schema) => {
let plugin = SectionConfigPlugin::new("Install".to_string(), obj_schema);
config.register_plugin(plugin);
}
_ => unreachable!(),
};
match SystemdTimerSection::API_SCHEMA { match SystemdTimerSection::API_SCHEMA {
Schema::Object(ref obj_schema) => { Schema::Object(ref obj_schema) => {
let plugin = SectionConfigPlugin::new("Timer".to_string(), obj_schema); let plugin = SectionConfigPlugin::new("Timer".to_string(), obj_schema);
@ -55,19 +78,26 @@ fn init() -> SectionConfig {
config config
} }
pub fn parse_systemd_config(filename: &str) -> Result<SectionConfigData, Error> { fn parse_systemd_config(config: &SectionConfig, filename: &str) -> Result<SectionConfigData, Error> {
let raw = proxmox::tools::fs::file_get_contents(filename)?; let raw = proxmox::tools::fs::file_get_contents(filename)?;
let input = String::from_utf8(raw)?; let input = String::from_utf8(raw)?;
let data = CONFIG.parse(filename, &input)?; let data = config.parse(filename, &input)?;
Ok(data) Ok(data)
} }
pub fn parse_systemd_service(filename: &str) -> Result<SectionConfigData, Error> {
parse_systemd_config(&SERVICE_CONFIG, filename)
}
pub fn save_systemd_config(filename: &str, config: &SectionConfigData) -> Result<(), Error> { pub fn parse_systemd_timer(filename: &str) -> Result<SectionConfigData, Error> {
let raw = CONFIG.write(filename, &config)?; parse_systemd_config(&TIMER_CONFIG, filename)
}
fn save_systemd_config(config: &SectionConfig, filename: &str, data: &SectionConfigData) -> Result<(), Error> {
let raw = config.write(filename, &data)?;
let mode = nix::sys::stat::Mode::from_bits_truncate(0o0644); let mode = nix::sys::stat::Mode::from_bits_truncate(0o0644);
// set the correct owner/group/permissions while saving file, owner(rw) = root // set the correct owner/group/permissions while saving file, owner(rw) = root
@ -79,3 +109,11 @@ pub fn save_systemd_config(filename: &str, config: &SectionConfigData) -> Result
Ok(()) Ok(())
} }
pub fn save_systemd_service(filename: &str, data: &SectionConfigData) -> Result<(), Error> {
save_systemd_config(&SERVICE_CONFIG, filename, data)
}
pub fn save_systemd_timer(filename: &str, data: &SectionConfigData) -> Result<(), Error> {
save_systemd_config(&TIMER_CONFIG, filename, data)
}