]> git.proxmox.com Git - proxmox-backup.git/commitdiff
src/tools/systemd/parser.rs: use different setups for service and timer files, code...
authorDietmar Maurer <dietmar@proxmox.com>
Thu, 14 May 2020 11:55:13 +0000 (13:55 +0200)
committerDietmar Maurer <dietmar@proxmox.com>
Thu, 14 May 2020 11:55:13 +0000 (13:55 +0200)
src/config/jobs.rs
src/tools/systemd/parser.rs

index 7011d3544a953b3bac5aa0d78795fd848dcd2da1..f381a1012c96207e83c6db5096dcd3bebd5bb1c5 100644 (file)
@@ -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(())
 }
index f18d63ef349a16e8dd6ba6b8bc7d2125c06b7904..cc5ad697914141a9b60c18babb6381e0da11bea9 100644 (file)
@@ -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 parse_systemd_timer(filename: &str) -> Result<SectionConfigData, Error> {
+    parse_systemd_config(&TIMER_CONFIG, filename)
+}
 
-pub fn save_systemd_config(filename: &str, config: &SectionConfigData) -> Result<(), Error> {
-    let raw = CONFIG.write(filename, &config)?;
+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)
+}