rust fmt for pbs src

Signed-off-by: Thomas Lamprecht <t.lamprecht@proxmox.com>
This commit is contained in:
Thomas Lamprecht
2022-04-14 14:03:46 +02:00
parent ee0ea73500
commit 9531d2c570
30 changed files with 644 additions and 572 deletions

View File

@ -8,7 +8,6 @@ use proxmox_section_config::{SectionConfig, SectionConfigData, SectionConfigPlug
use proxmox_sys::{fs::replace_file, fs::CreateOptions};
lazy_static! {
pub static ref SERVICE_CONFIG: SectionConfig = init_service();
pub static ref TIMER_CONFIG: SectionConfig = init_timer();
@ -16,25 +15,24 @@ lazy_static! {
}
fn init_service() -> SectionConfig {
let mut config = SectionConfig::with_systemd_syntax(&SYSTEMD_SECTION_NAME_SCHEMA);
match SystemdUnitSection::API_SCHEMA {
Schema::Object(ref obj_schema) => {
Schema::Object(ref obj_schema) => {
let plugin = SectionConfigPlugin::new("Unit".to_string(), None, obj_schema);
config.register_plugin(plugin);
}
_ => unreachable!(),
};
match SystemdInstallSection::API_SCHEMA {
Schema::Object(ref obj_schema) => {
Schema::Object(ref obj_schema) => {
let plugin = SectionConfigPlugin::new("Install".to_string(), None, obj_schema);
config.register_plugin(plugin);
}
_ => unreachable!(),
};
match SystemdServiceSection::API_SCHEMA {
Schema::Object(ref obj_schema) => {
Schema::Object(ref obj_schema) => {
let plugin = SectionConfigPlugin::new("Service".to_string(), None, obj_schema);
config.register_plugin(plugin);
}
@ -45,25 +43,24 @@ fn init_service() -> SectionConfig {
}
fn init_timer() -> SectionConfig {
let mut config = SectionConfig::with_systemd_syntax(&SYSTEMD_SECTION_NAME_SCHEMA);
match SystemdUnitSection::API_SCHEMA {
Schema::Object(ref obj_schema) => {
Schema::Object(ref obj_schema) => {
let plugin = SectionConfigPlugin::new("Unit".to_string(), None, obj_schema);
config.register_plugin(plugin);
}
_ => unreachable!(),
};
match SystemdInstallSection::API_SCHEMA {
Schema::Object(ref obj_schema) => {
Schema::Object(ref obj_schema) => {
let plugin = SectionConfigPlugin::new("Install".to_string(), None, obj_schema);
config.register_plugin(plugin);
}
_ => unreachable!(),
};
match SystemdTimerSection::API_SCHEMA {
Schema::Object(ref obj_schema) => {
Schema::Object(ref obj_schema) => {
let plugin = SectionConfigPlugin::new("Timer".to_string(), None, obj_schema);
config.register_plugin(plugin);
}
@ -74,25 +71,24 @@ fn init_timer() -> SectionConfig {
}
fn init_mount() -> SectionConfig {
let mut config = SectionConfig::with_systemd_syntax(&SYSTEMD_SECTION_NAME_SCHEMA);
match SystemdUnitSection::API_SCHEMA {
Schema::Object(ref obj_schema) => {
Schema::Object(ref obj_schema) => {
let plugin = SectionConfigPlugin::new("Unit".to_string(), None, obj_schema);
config.register_plugin(plugin);
}
_ => unreachable!(),
};
match SystemdInstallSection::API_SCHEMA {
Schema::Object(ref obj_schema) => {
Schema::Object(ref obj_schema) => {
let plugin = SectionConfigPlugin::new("Install".to_string(), None, obj_schema);
config.register_plugin(plugin);
}
_ => unreachable!(),
};
match SystemdMountSection::API_SCHEMA {
Schema::Object(ref obj_schema) => {
Schema::Object(ref obj_schema) => {
let plugin = SectionConfigPlugin::new("Mount".to_string(), None, obj_schema);
config.register_plugin(plugin);
}
@ -102,8 +98,10 @@ fn init_mount() -> SectionConfig {
config
}
fn parse_systemd_config(config: &SectionConfig, filename: &str) -> Result<SectionConfigData, Error> {
fn parse_systemd_config(
config: &SectionConfig,
filename: &str,
) -> Result<SectionConfigData, Error> {
let raw = proxmox_sys::fs::file_get_contents(filename)?;
let input = String::from_utf8(raw)?;
@ -124,14 +122,16 @@ pub fn parse_systemd_mount(filename: &str) -> Result<SectionConfigData, Error> {
parse_systemd_config(&MOUNT_CONFIG, filename)
}
fn save_systemd_config(config: &SectionConfig, filename: &str, data: &SectionConfigData) -> Result<(), Error> {
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
let options = CreateOptions::new()
.perm(mode)
.owner(nix::unistd::ROOT);
let options = CreateOptions::new().perm(mode).owner(nix::unistd::ROOT);
replace_file(filename, raw.as_bytes(), options, true)?;

View File

@ -1,34 +1,30 @@
use serde::{Serialize, Deserialize};
use serde::{Deserialize, Serialize};
use proxmox_schema::*;
use pbs_api_types::SINGLE_LINE_COMMENT_FORMAT;
use proxmox_schema::*;
pub const SYSTEMD_SECTION_NAME_SCHEMA: Schema = StringSchema::new(
"Section name")
pub const SYSTEMD_SECTION_NAME_SCHEMA: Schema = StringSchema::new("Section name")
.format(&ApiStringFormat::Enum(&[
EnumEntry::new("Unit", "Unit"),
EnumEntry::new("Timer", "Timer"),
EnumEntry::new("Install", "Install"),
EnumEntry::new("Mount", "Mount"),
EnumEntry::new("Service", "Service")]))
EnumEntry::new("Service", "Service"),
]))
.schema();
pub const SYSTEMD_STRING_SCHEMA: Schema =
StringSchema::new("Systemd configuration value.")
pub const SYSTEMD_STRING_SCHEMA: Schema = StringSchema::new("Systemd configuration value.")
.format(&SINGLE_LINE_COMMENT_FORMAT)
.schema();
pub const SYSTEMD_STRING_ARRAY_SCHEMA: Schema = ArraySchema::new(
"Array of Strings", &SYSTEMD_STRING_SCHEMA)
.schema();
pub const SYSTEMD_STRING_ARRAY_SCHEMA: Schema =
ArraySchema::new("Array of Strings", &SYSTEMD_STRING_SCHEMA).schema();
pub const SYSTEMD_TIMESPAN_ARRAY_SCHEMA: Schema = ArraySchema::new(
"Array of time spans", &SYSTEMD_TIMESPAN_SCHEMA)
.schema();
pub const SYSTEMD_TIMESPAN_ARRAY_SCHEMA: Schema =
ArraySchema::new("Array of time spans", &SYSTEMD_TIMESPAN_SCHEMA).schema();
pub const SYSTEMD_CALENDAR_EVENT_ARRAY_SCHEMA: Schema = ArraySchema::new(
"Array of calendar events", &SYSTEMD_CALENDAR_EVENT_SCHEMA)
.schema();
pub const SYSTEMD_CALENDAR_EVENT_ARRAY_SCHEMA: Schema =
ArraySchema::new("Array of calendar events", &SYSTEMD_CALENDAR_EVENT_SCHEMA).schema();
#[api(
properties: {
@ -70,44 +66,44 @@ pub const SYSTEMD_CALENDAR_EVENT_ARRAY_SCHEMA: Schema = ArraySchema::new(
#[allow(non_snake_case)]
/// Systemd Timer Section
pub struct SystemdTimerSection {
#[serde(skip_serializing_if="Option::is_none")]
#[serde(skip_serializing_if = "Option::is_none")]
pub OnCalendar: Option<Vec<String>>,
/// If true, the time when the service unit was last triggered is stored on disk.
#[serde(skip_serializing_if="Option::is_none")]
#[serde(skip_serializing_if = "Option::is_none")]
pub Persistent: Option<bool>,
#[serde(skip_serializing_if="Option::is_none")]
#[serde(skip_serializing_if = "Option::is_none")]
pub OnActiveSec: Option<Vec<String>>,
#[serde(skip_serializing_if="Option::is_none")]
#[serde(skip_serializing_if = "Option::is_none")]
pub OnBootSec: Option<Vec<String>>,
#[serde(skip_serializing_if="Option::is_none")]
#[serde(skip_serializing_if = "Option::is_none")]
pub OnStartupSec: Option<Vec<String>>,
#[serde(skip_serializing_if="Option::is_none")]
#[serde(skip_serializing_if = "Option::is_none")]
pub OnUnitActiveSec: Option<Vec<String>>,
#[serde(skip_serializing_if="Option::is_none")]
#[serde(skip_serializing_if = "Option::is_none")]
pub OnUnitInactiveSec: Option<Vec<String>>,
#[serde(skip_serializing_if="Option::is_none")]
#[serde(skip_serializing_if = "Option::is_none")]
pub RandomizedDelaySec: Option<String>,
#[serde(skip_serializing_if="Option::is_none")]
#[serde(skip_serializing_if = "Option::is_none")]
pub AccuracySec: Option<String>,
/// Trigger when system clock jumps.
#[serde(skip_serializing_if="Option::is_none")]
#[serde(skip_serializing_if = "Option::is_none")]
pub OnClockChange: Option<bool>,
/// Trigger when time zone changes.
#[serde(skip_serializing_if="Option::is_none")]
#[serde(skip_serializing_if = "Option::is_none")]
pub OnTimezomeChange: Option<bool>,
/// The unit to activate when this timer elapses.
#[serde(skip_serializing_if="Option::is_none")]
#[serde(skip_serializing_if = "Option::is_none")]
pub Unit: Option<String>,
/// If true, an elapsing timer will cause the system to resume from suspend.
#[serde(skip_serializing_if="Option::is_none")]
#[serde(skip_serializing_if = "Option::is_none")]
pub WakeSystem: Option<bool>,
/// If true, an elapsed timer will stay loaded, and its state remains queryable.
#[serde(skip_serializing_if="Option::is_none")]
#[serde(skip_serializing_if = "Option::is_none")]
pub RemainAfterElapse: Option<bool>,
}
@ -128,9 +124,9 @@ pub struct SystemdTimerSection {
/// Systemd Service Section
pub struct SystemdServiceSection {
/// The process start-up type for this service unit.
#[serde(skip_serializing_if="Option::is_none")]
#[serde(skip_serializing_if = "Option::is_none")]
pub Type: Option<ServiceStartup>,
#[serde(skip_serializing_if="Option::is_none")]
#[serde(skip_serializing_if = "Option::is_none")]
pub ExecStart: Option<Vec<String>>,
}
@ -142,7 +138,7 @@ pub struct SystemdUnitSection {
/// A human readable name for the unit.
pub Description: String,
/// Check whether the system has AC power.
#[serde(skip_serializing_if="Option::is_none")]
#[serde(skip_serializing_if = "Option::is_none")]
pub ConditionACPower: Option<bool>,
}
@ -173,16 +169,16 @@ pub struct SystemdUnitSection {
#[allow(non_snake_case)]
/// Systemd Install Section
pub struct SystemdInstallSection {
#[serde(skip_serializing_if="Option::is_none")]
#[serde(skip_serializing_if = "Option::is_none")]
pub Alias: Option<Vec<String>>,
#[serde(skip_serializing_if="Option::is_none")]
#[serde(skip_serializing_if = "Option::is_none")]
pub Also: Option<Vec<String>>,
/// DefaultInstance for template unit.
#[serde(skip_serializing_if="Option::is_none")]
#[serde(skip_serializing_if = "Option::is_none")]
pub DefaultInstance: Option<String>,
#[serde(skip_serializing_if="Option::is_none")]
#[serde(skip_serializing_if = "Option::is_none")]
pub WantedBy: Option<Vec<String>>,
#[serde(skip_serializing_if="Option::is_none")]
#[serde(skip_serializing_if = "Option::is_none")]
pub RequiredBy: Option<Vec<String>>,
}
@ -203,27 +199,27 @@ pub struct SystemdMountSection {
/// absolute path of a file or directory for the mount point
pub Where: String,
/// Takes a string for the file system type. See mount(8) for details.
#[serde(skip_serializing_if="Option::is_none")]
#[serde(skip_serializing_if = "Option::is_none")]
pub Type: Option<String>,
/// Mount options to use when mounting. This takes a comma-separated list of options.
#[serde(skip_serializing_if="Option::is_none")]
#[serde(skip_serializing_if = "Option::is_none")]
pub Options: Option<String>,
/// If true, parsing of the options specified in Options= is relaxed, and unknown mount options are tolerated.
#[serde(skip_serializing_if="Option::is_none")]
#[serde(skip_serializing_if = "Option::is_none")]
pub SloppyOptions: Option<bool>,
/// Use lazy unmount
#[serde(skip_serializing_if="Option::is_none")]
#[serde(skip_serializing_if = "Option::is_none")]
pub LazyUnmount: Option<bool>,
/// Use forces unmount
#[serde(skip_serializing_if="Option::is_none")]
#[serde(skip_serializing_if = "Option::is_none")]
pub ForceUnmount: Option<bool>,
/// Directories of mount points (and any parent directories) are
/// automatically created if needed. Takes an access mode in octal
/// notation. Defaults to 0755.
#[serde(skip_serializing_if="Option::is_none")]
#[serde(skip_serializing_if = "Option::is_none")]
pub DirectoryMode: Option<String>,
/// Configures the time to wait for the mount command to finish.
#[serde(skip_serializing_if="Option::is_none")]
#[serde(skip_serializing_if = "Option::is_none")]
pub TimeoutSec: Option<String>,
}
@ -246,12 +242,12 @@ pub enum ServiceStartup {
Notify,
}
pub const SYSTEMD_TIMESPAN_SCHEMA: Schema = StringSchema::new(
"systemd time span")
pub const SYSTEMD_TIMESPAN_SCHEMA: Schema = StringSchema::new("systemd time span")
.format(&ApiStringFormat::VerifyFn(proxmox_time::verify_time_span))
.schema();
pub const SYSTEMD_CALENDAR_EVENT_SCHEMA: Schema = StringSchema::new(
"systemd calendar event")
.format(&ApiStringFormat::VerifyFn(proxmox_time::verify_calendar_event))
pub const SYSTEMD_CALENDAR_EVENT_SCHEMA: Schema = StringSchema::new("systemd calendar event")
.format(&ApiStringFormat::VerifyFn(
proxmox_time::verify_calendar_event,
))
.schema();

View File

@ -20,7 +20,9 @@ fn run_command(mut command: Command) -> Result<(), Error> {
m
}
})
.unwrap_or_else(|_| String::from("non utf8 error message (suppressed)"));
.unwrap_or_else(|_| {
String::from("non utf8 error message (suppressed)")
});
bail!("status code: {} - {}", code, msg);
}
@ -29,7 +31,8 @@ fn run_command(mut command: Command) -> Result<(), Error> {
}
}
Ok(())
}).map_err(|err| format_err!("command {:?} failed - {}", command, err))?;
})
.map_err(|err| format_err!("command {:?} failed - {}", command, err))?;
Ok(())
}
@ -96,7 +99,6 @@ pub fn reload_unit(unit: &str) -> Result<(), Error> {
#[test]
fn test_escape_unit() -> Result<(), Error> {
fn test_escape(i: &str, expected: &str, is_path: bool) {
use proxmox_sys::systemd::{escape_unit, unescape_unit};
let escaped = escape_unit(i, is_path);