src/tools/systemd/parser.rs: use different setups for service and timer files, code cleanup
This commit is contained in:
		@ -3,7 +3,6 @@ use regex::Regex;
 | 
			
		||||
use lazy_static::lazy_static;
 | 
			
		||||
 | 
			
		||||
use proxmox::api::section_config::SectionConfigData;
 | 
			
		||||
use proxmox::tools::{fs::replace_file, fs::CreateOptions};
 | 
			
		||||
 | 
			
		||||
use crate::PROXMOX_SAFE_ID_REGEX_STR;
 | 
			
		||||
use crate::tools::systemd::parser::*;
 | 
			
		||||
@ -59,7 +58,7 @@ pub fn list_jobs() -> Result<Vec<JobListEntry>, Error> {
 | 
			
		||||
        };
 | 
			
		||||
 | 
			
		||||
        // 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]) {
 | 
			
		||||
            ("gc", store) => {
 | 
			
		||||
@ -152,22 +151,10 @@ pub fn create_garbage_collection_job(
 | 
			
		||||
 | 
			
		||||
    let basename = format!("{}/pbs-gc-{}", SYSTEMD_CONFIG_DIR, store);
 | 
			
		||||
    let timer_fn = format!("{}.timer", basename);
 | 
			
		||||
    let timer_config = CONFIG.write(&timer_fn, &timer_config)?;
 | 
			
		||||
 | 
			
		||||
    let service_fn = format!("{}.service", basename);
 | 
			
		||||
    let service_config = CONFIG.write(&service_fn, &service_config)?;
 | 
			
		||||
 | 
			
		||||
    let backup_user = crate::backup::backup_user()?;
 | 
			
		||||
    let mode = nix::sys::stat::Mode::from_bits_truncate(0o0750);
 | 
			
		||||
    // 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)?;
 | 
			
		||||
    save_systemd_service(&service_fn, &service_config)?;
 | 
			
		||||
    save_systemd_timer(&timer_fn, &timer_config)?;
 | 
			
		||||
 | 
			
		||||
    Ok(())
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@ -16,10 +16,11 @@ use proxmox::tools::{fs::replace_file, fs::CreateOptions};
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
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);
 | 
			
		||||
 | 
			
		||||
@ -44,6 +45,28 @@ fn init() -> SectionConfig {
 | 
			
		||||
        }
 | 
			
		||||
        _ => 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 {
 | 
			
		||||
        Schema::Object(ref obj_schema) =>  {
 | 
			
		||||
            let plugin = SectionConfigPlugin::new("Timer".to_string(), obj_schema);
 | 
			
		||||
@ -55,19 +78,26 @@ fn init() -> SectionConfig {
 | 
			
		||||
    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 input = String::from_utf8(raw)?;
 | 
			
		||||
 | 
			
		||||
    let data = CONFIG.parse(filename, &input)?;
 | 
			
		||||
    let data = config.parse(filename, &input)?;
 | 
			
		||||
 | 
			
		||||
    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> {
 | 
			
		||||
    let raw = CONFIG.write(filename, &config)?;
 | 
			
		||||
pub fn parse_systemd_timer(filename: &str) -> Result<SectionConfigData, Error> {
 | 
			
		||||
    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);
 | 
			
		||||
    // 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(())
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
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)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
		Reference in New Issue
	
	Block a user