]> git.proxmox.com Git - proxmox-backup.git/blame - src/tools/systemd/config.rs
update to proxmox-sys 0.2 crate
[proxmox-backup.git] / src / tools / systemd / config.rs
CommitLineData
f486e9e5
DM
1use anyhow::Error;
2use lazy_static::lazy_static;
3
4use super::types::*;
5
6ef1b649
WB
6use proxmox_schema::*;
7use proxmox_section_config::{SectionConfig, SectionConfigData, SectionConfigPlugin};
f486e9e5 8
25877d05 9use proxmox_sys::{fs::replace_file, fs::CreateOptions};
f486e9e5
DM
10
11
12lazy_static! {
00491c02
DM
13 pub static ref SERVICE_CONFIG: SectionConfig = init_service();
14 pub static ref TIMER_CONFIG: SectionConfig = init_timer();
b9cf6ee7 15 pub static ref MOUNT_CONFIG: SectionConfig = init_mount();
f486e9e5
DM
16}
17
00491c02 18fn init_service() -> SectionConfig {
f486e9e5
DM
19
20 let mut config = SectionConfig::with_systemd_syntax(&SYSTEMD_SECTION_NAME_SCHEMA);
21
22 match SystemdUnitSection::API_SCHEMA {
23 Schema::Object(ref obj_schema) => {
16c75c58 24 let plugin = SectionConfigPlugin::new("Unit".to_string(), None, obj_schema);
f486e9e5
DM
25 config.register_plugin(plugin);
26 }
27 _ => unreachable!(),
28 };
29 match SystemdInstallSection::API_SCHEMA {
30 Schema::Object(ref obj_schema) => {
16c75c58 31 let plugin = SectionConfigPlugin::new("Install".to_string(), None, obj_schema);
f486e9e5
DM
32 config.register_plugin(plugin);
33 }
34 _ => unreachable!(),
35 };
36 match SystemdServiceSection::API_SCHEMA {
37 Schema::Object(ref obj_schema) => {
16c75c58 38 let plugin = SectionConfigPlugin::new("Service".to_string(), None, obj_schema);
f486e9e5
DM
39 config.register_plugin(plugin);
40 }
41 _ => unreachable!(),
42 };
00491c02
DM
43
44 config
45}
46
47fn init_timer() -> SectionConfig {
48
49 let mut config = SectionConfig::with_systemd_syntax(&SYSTEMD_SECTION_NAME_SCHEMA);
50
51 match SystemdUnitSection::API_SCHEMA {
52 Schema::Object(ref obj_schema) => {
16c75c58 53 let plugin = SectionConfigPlugin::new("Unit".to_string(), None, obj_schema);
00491c02
DM
54 config.register_plugin(plugin);
55 }
56 _ => unreachable!(),
57 };
58 match SystemdInstallSection::API_SCHEMA {
59 Schema::Object(ref obj_schema) => {
16c75c58 60 let plugin = SectionConfigPlugin::new("Install".to_string(), None, obj_schema);
00491c02
DM
61 config.register_plugin(plugin);
62 }
63 _ => unreachable!(),
64 };
f486e9e5
DM
65 match SystemdTimerSection::API_SCHEMA {
66 Schema::Object(ref obj_schema) => {
16c75c58 67 let plugin = SectionConfigPlugin::new("Timer".to_string(), None, obj_schema);
f486e9e5
DM
68 config.register_plugin(plugin);
69 }
70 _ => unreachable!(),
71 };
72
73 config
74}
75
b9cf6ee7
DM
76fn init_mount() -> SectionConfig {
77
78 let mut config = SectionConfig::with_systemd_syntax(&SYSTEMD_SECTION_NAME_SCHEMA);
79
80 match SystemdUnitSection::API_SCHEMA {
81 Schema::Object(ref obj_schema) => {
82 let plugin = SectionConfigPlugin::new("Unit".to_string(), None, obj_schema);
83 config.register_plugin(plugin);
84 }
85 _ => unreachable!(),
86 };
87 match SystemdInstallSection::API_SCHEMA {
88 Schema::Object(ref obj_schema) => {
89 let plugin = SectionConfigPlugin::new("Install".to_string(), None, obj_schema);
90 config.register_plugin(plugin);
91 }
92 _ => unreachable!(),
93 };
94 match SystemdMountSection::API_SCHEMA {
95 Schema::Object(ref obj_schema) => {
96 let plugin = SectionConfigPlugin::new("Mount".to_string(), None, obj_schema);
97 config.register_plugin(plugin);
98 }
99 _ => unreachable!(),
100 };
101
102 config
103}
104
00491c02 105fn parse_systemd_config(config: &SectionConfig, filename: &str) -> Result<SectionConfigData, Error> {
f486e9e5 106
25877d05 107 let raw = proxmox_sys::fs::file_get_contents(filename)?;
f486e9e5
DM
108 let input = String::from_utf8(raw)?;
109
00491c02 110 let data = config.parse(filename, &input)?;
f486e9e5
DM
111
112 Ok(data)
113}
114
00491c02
DM
115pub fn parse_systemd_service(filename: &str) -> Result<SectionConfigData, Error> {
116 parse_systemd_config(&SERVICE_CONFIG, filename)
117}
118
119pub fn parse_systemd_timer(filename: &str) -> Result<SectionConfigData, Error> {
120 parse_systemd_config(&TIMER_CONFIG, filename)
121}
f486e9e5 122
d4f2397d
DM
123pub fn parse_systemd_mount(filename: &str) -> Result<SectionConfigData, Error> {
124 parse_systemd_config(&MOUNT_CONFIG, filename)
125}
126
00491c02
DM
127fn save_systemd_config(config: &SectionConfig, filename: &str, data: &SectionConfigData) -> Result<(), Error> {
128 let raw = config.write(filename, &data)?;
f486e9e5
DM
129
130 let mode = nix::sys::stat::Mode::from_bits_truncate(0o0644);
131 // set the correct owner/group/permissions while saving file, owner(rw) = root
132 let options = CreateOptions::new()
133 .perm(mode)
134 .owner(nix::unistd::ROOT);
135
e0a19d33 136 replace_file(filename, raw.as_bytes(), options, true)?;
f486e9e5
DM
137
138 Ok(())
139}
00491c02
DM
140
141pub fn save_systemd_service(filename: &str, data: &SectionConfigData) -> Result<(), Error> {
142 save_systemd_config(&SERVICE_CONFIG, filename, data)
143}
144
145pub fn save_systemd_timer(filename: &str, data: &SectionConfigData) -> Result<(), Error> {
146 save_systemd_config(&TIMER_CONFIG, filename, data)
147}
b9cf6ee7
DM
148
149pub fn save_systemd_mount(filename: &str, data: &SectionConfigData) -> Result<(), Error> {
150 save_systemd_config(&MOUNT_CONFIG, filename, data)
151}