Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion src/cli/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3106,7 +3106,10 @@ environments:
";
let (_parser, result) = parse_with_contract(contract, &["apply", "--environment", "test"]);
let err = result.unwrap_err();
assert!(err.contains("{profile}"), "error names the placeholder: {err}");
assert!(
err.contains("{profile}"),
"error names the placeholder: {err}"
);
assert!(err.contains("no profile"), "error states the cause: {err}");
}

Expand Down
59 changes: 52 additions & 7 deletions src/modules/services/sd_service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ pub struct SystemdServiceTask {
pub service: String,
pub enabled: Option<String>,
pub started: Option<String>,
pub reload: Option<String>,
pub restart: Option<String>,
pub with: Option<PreLogicInput>,
pub and: Option<PostLogicInput>,
Expand All @@ -40,6 +41,7 @@ struct SystemdServiceAction {
pub service: String,
pub enabled: Option<bool>,
pub started: Option<bool>,
pub reload: bool,
pub restart: bool,
}

Expand Down Expand Up @@ -100,6 +102,12 @@ impl IsTask for SystemdServiceTask {
&String::from("started"),
&self.started,
)?,
reload: handle.template.boolean_option_default_false(
request,
tm,
&String::from("reload"),
&self.reload,
)?,
restart: handle.template.boolean_option_default_false(
request,
tm,
Expand Down Expand Up @@ -142,26 +150,52 @@ impl IsAction for SystemdServiceAction {
_ => {}
};

match (actual.started, self.started, self.restart) {
(_, Some(false), true) => {
match (actual.started, self.started, self.reload, self.restart) {
(_, Some(false), true, _) => {
return Err(handle.response.is_failed(
request,
&String::from("started:false and reload:true conflict"),
));
}
(_, Some(false), _, true) => {
return Err(handle.response.is_failed(
request,
&String::from("started:false and restart:true conflict"),
));
}
(true, Some(true), true) => {
(_, _, true, true) => {
return Err(handle.response.is_failed(
request,
&String::from("reload:true and restart:true conflict"),
));
}
(true, Some(true), true, false) => {
changes.push(Field::Reload);
}
(true, None, true, false) => {
changes.push(Field::Reload); /* a little weird, but we know what you mean */
}
(false, None, true, false) => {
return Err(handle.response.is_failed(
request,
&String::from(
"reload:true requires the service to already be started or started:true",
),
));
}
(true, Some(true), false, true) => {
changes.push(Field::Restart);
}
(true, None, true) => {
(true, None, false, true) => {
changes.push(Field::Restart); /* a little weird, but we know what you mean */
}
(false, None, true) => {
(false, None, false, true) => {
changes.push(Field::Start); /* a little weird, but we know what you mean */
}
(false, Some(true), _) => {
(false, Some(true), _, _) => {
changes.push(Field::Start);
}
(true, Some(false), false) => {
(true, Some(false), false, false) => {
changes.push(Field::Stop);
}
_ => {}
Expand All @@ -179,6 +213,8 @@ impl IsAction for SystemdServiceAction {
self.do_start(handle, request)?;
} else if request.changes.contains(&Field::Stop) {
self.do_stop(handle, request)?;
} else if request.changes.contains(&Field::Reload) {
self.do_reload(handle, request)?;
} else if request.changes.contains(&Field::Restart) {
self.do_restart(handle, request)?;
}
Expand Down Expand Up @@ -293,6 +329,15 @@ impl SystemdServiceAction {
let cmd = format!("systemctl restart '{}'", self.service);
handle.remote.run(request, &cmd, CheckRc::Checked)
}

pub fn do_reload(
&self,
handle: &Arc<TaskHandle>,
request: &Arc<TaskRequest>,
) -> Result<Arc<TaskResponse>, Arc<TaskResponse>> {
let cmd = format!("systemctl reload '{}'", self.service);
handle.remote.run(request, &cmd, CheckRc::Checked)
}
}

/// Classify the stdout of `systemctl is-enabled <unit>` into an [`Enablement`]
Expand Down
1 change: 1 addition & 0 deletions src/tasks/fields.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ pub enum Field {
Location,
Mode,
Owner,
Reload,
Restart,
Shell,
Start,
Expand Down
24 changes: 24 additions & 0 deletions tests/modules/services/sd_service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ fn test_systemd_service_task_basic() {
service: "nginx".to_string(),
enabled: None,
started: None,
reload: None,
restart: None,
with: None,
and: None,
Expand All @@ -27,6 +28,7 @@ fn test_systemd_service_task_enabled_started() {
service: "postgresql".to_string(),
enabled: Some("yes".to_string()),
started: Some("yes".to_string()),
reload: None,
restart: None,
with: None,
and: None,
Expand All @@ -45,6 +47,7 @@ fn test_systemd_service_task_restart() {
service: "httpd".to_string(),
enabled: None,
started: None,
reload: None,
restart: Some("yes".to_string()),
with: None,
and: None,
Expand All @@ -61,6 +64,7 @@ fn test_systemd_service_task_disabled_stopped() {
service: "firewalld".to_string(),
enabled: Some("no".to_string()),
started: Some("no".to_string()),
reload: None,
restart: None,
with: None,
and: None,
Expand Down Expand Up @@ -145,3 +149,23 @@ restart: "yes"
assert!(task.enabled.is_none());
assert!(task.started.is_none());
}

#[test]
fn test_systemd_service_task_reload_only() {
let yaml = r#"
name: Reload application
service: myapp
reload: "yes"
"#;

let task: Result<SystemdServiceTask, _> = serde_yaml::from_str(yaml);
assert!(task.is_ok());

let task = task.unwrap();
assert_eq!(task.name, Some("Reload application".to_string()));
assert_eq!(task.service, "myapp");
assert_eq!(task.reload, Some("yes".to_string()));
assert!(task.restart.is_none());
assert!(task.enabled.is_none());
assert!(task.started.is_none());
}
1 change: 1 addition & 0 deletions tests/tasks/fields.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ fn test_all_field_variants() {
Field::Groups,
Field::Mode,
Field::Owner,
Field::Reload,
Field::Restart,
Field::Shell,
Field::Start,
Expand Down
Loading