Skip to content

Commit 45ce0c2

Browse files
authored
Merge pull request #63 from say-paul/systemd-service-unification
merge greenboot-success.target inside heathcheck service
2 parents 47b697d + bfb1510 commit 45ce0c2

10 files changed

Lines changed: 33 additions & 49 deletions

File tree

.github/spellcheck-ignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,5 @@ ro
88
rw
99
RO
1010
RW
11-
grubenv
11+
grubenv
12+
msdos

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ Place shell scripts representing *health checks* that **MAY FAIL** in the `/etc/
3737
Place shell scripts you want to run *after* a boot has been declared **successful** (green) in `/etc/greenboot/green.d`.
3838
Place shell scripts you want to run *after* a boot has been declared **failed** (red) in `/etc/greenboot/red.d`.
3939

40-
Unless greenboot is enabled by default in your distribution, enable it by running `systemctl enable greenboot-healthcheck.service greenboot-success.target`.
40+
Unless greenboot is enabled by default in your distribution, enable it by running `systemctl enable greenboot-healthcheck.service`.
4141
It will automatically start during the next boot process and run its checks.
4242

4343
When you `ssh` into the machine after that, a boot status message will be shown:

src/lib/greenboot.rs

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,9 @@ pub fn run_diagnostics(skipped: Vec<String>) -> Result<Vec<String>> {
2121

2222
// Run required checks
2323
for path in GREENBOOT_INSTALL_PATHS {
24-
let greenboot_required_path = format!("{}/check/required.d/", path);
24+
let greenboot_required_path = format!("{path}/check/required.d/");
2525
if !Path::new(&greenboot_required_path).is_dir() {
26-
log::warn!("skipping test as {} is not a dir", greenboot_required_path);
26+
log::warn!("skipping test as {greenboot_required_path} is not a dir");
2727
continue;
2828
}
2929
path_exists = true;
@@ -43,7 +43,7 @@ pub fn run_diagnostics(skipped: Vec<String>) -> Result<Vec<String>> {
4343

4444
// Run wanted checks
4545
for path in GREENBOOT_INSTALL_PATHS {
46-
let greenboot_wanted_path = format!("{}/check/wanted.d/", path);
46+
let greenboot_wanted_path = format!("{path}/check/wanted.d/");
4747
let result = run_scripts("wanted", &greenboot_wanted_path, Some(&skipped));
4848
all_skipped.extend(result.skipped);
4949

@@ -61,8 +61,7 @@ pub fn run_diagnostics(skipped: Vec<String>) -> Result<Vec<String>> {
6161

6262
if !missing_disabled.is_empty() {
6363
log::warn!(
64-
"The following disabled scripts were not found in any directory: {:?}",
65-
missing_disabled
64+
"The following disabled scripts were not found in any directory: {missing_disabled:?}"
6665
);
6766
}
6867

@@ -74,7 +73,7 @@ pub fn run_red() -> Vec<Box<dyn Error>> {
7473
let mut errors = Vec::new();
7574

7675
for path in GREENBOOT_INSTALL_PATHS {
77-
let red_path = format!("{}/red.d/", path);
76+
let red_path = format!("{path}/red.d/");
7877
let result = run_scripts("red", &red_path, None); // Pass None for disabled scripts
7978
errors.extend(result.errors);
8079
}
@@ -87,7 +86,7 @@ pub fn run_green() -> Vec<Box<dyn Error>> {
8786
let mut errors = Vec::new();
8887

8988
for path in GREENBOOT_INSTALL_PATHS {
90-
let green_path = format!("{}/green.d/", path);
89+
let green_path = format!("{path}/green.d/");
9190
let result = run_scripts("green", &green_path, None); // Pass None for disabled scripts
9291
errors.extend(result.errors);
9392
}
@@ -106,7 +105,7 @@ fn run_scripts(name: &str, path: &str, disabled_scripts: Option<&[String]>) -> S
106105
skipped: Vec::new(),
107106
};
108107

109-
let entries = match glob(&format!("{}*", path)) {
108+
let entries = match glob(&format!("{path}*")) {
110109
Ok(e) => {
111110
let valid: Vec<_> = e
112111
.filter_map(Result::ok)
@@ -139,7 +138,7 @@ fn run_scripts(name: &str, path: &str, disabled_scripts: Option<&[String]>) -> S
139138
// Check if script/binary should be skipped
140139
if let Some(disabled) = disabled_scripts {
141140
if disabled.contains(&file_name.to_string()) {
142-
log::info!("Skipping disabled script: {}", file_name);
141+
log::info!("Skipping disabled script: {file_name}");
143142
result.skipped.push(file_name.to_string());
144143
continue;
145144
}

src/lib/handler.rs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -60,14 +60,11 @@ pub fn handle_rollback() -> Result<()> {
6060
Some(counter) if counter <= 0 => {
6161
log::info!("Greenboot will now attempt to rollback to a previous deployment.");
6262
if let Some(deployment_cmd) = detect_os_deployment() {
63-
log::info!(
64-
"Deployment manager '{}' detected, attempting rollback.",
65-
deployment_cmd
66-
);
63+
log::info!("Deployment manager '{deployment_cmd}' detected, attempting rollback.");
6764
let status = Command::new(deployment_cmd)
6865
.arg("rollback")
6966
.status()
70-
.context(format!("Failed to execute '{} rollback'", deployment_cmd))?;
67+
.context(format!("Failed to execute '{deployment_cmd} rollback'"))?;
7168

7269
if !status.success() {
7370
bail!(

src/lib/mount.rs

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -41,18 +41,14 @@ pub fn remount_boot_ro(mounts_path: &Path) -> Result<(), MountError> {
4141
Ok(())
4242
} else {
4343
let error_message = String::from_utf8_lossy(&output.stderr);
44-
warn!(
45-
"Failed to remount /boot as RO using shell: {}",
46-
error_message
47-
);
44+
warn!("Failed to remount /boot as RO using shell: {error_message}");
4845
Err(MountError::RemountFailed(error_message.to_string()))
4946
}
5047
}
5148
Err(e) => {
52-
warn!("Failed to execute mount command: {}", e);
49+
warn!("Failed to execute mount command: {e}");
5350
Err(MountError::RemountFailed(format!(
54-
"Failed to execute mount: {}",
55-
e
51+
"Failed to execute mount: {e}"
5652
)))
5753
}
5854
}
@@ -81,18 +77,14 @@ pub fn remount_boot_rw(mounts_path: &Path) -> Result<(), MountError> {
8177
Ok(())
8278
} else {
8379
let error_message = String::from_utf8_lossy(&output.stderr);
84-
warn!(
85-
"Failed to remount /boot as RW using shell: {}",
86-
error_message
87-
);
80+
warn!("Failed to remount /boot as RW using shell: {error_message}");
8881
Err(MountError::RemountFailed(error_message.to_string()))
8982
}
9083
}
9184
Err(e) => {
92-
warn!("Failed to execute mount command: {}", e);
85+
warn!("Failed to execute mount command: {e}");
9386
Err(MountError::RemountFailed(format!(
94-
"Failed to execute mount: {}",
95-
e
87+
"Failed to execute mount: {e}"
9688
)))
9789
}
9890
}

src/main.rs

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ fn check_previous_rollback() -> Result<bool> {
135135
// Check for specific success indicators
136136
let success = journal_output.contains("Rollback successful");
137137

138-
log::debug!("Rollback detection result: {}", success);
138+
log::debug!("Rollback detection result: {success}");
139139
Ok(success)
140140
}
141141

@@ -167,10 +167,7 @@ fn health_check() -> Result<()> {
167167
status
168168
}
169169
Err(e) => {
170-
log::warn!(
171-
"Failed to check previous rollback status: {}. Defaulting to false.",
172-
e
173-
);
170+
log::warn!("Failed to check previous rollback status: {e}. Defaulting to false.");
174171
false
175172
}
176173
};
@@ -194,7 +191,7 @@ fn health_check() -> Result<()> {
194191
"Greenboot healthcheck passed - status is GREEN",
195192
previous_rollback,
196193
)?)
197-
.unwrap_or_else(|e| log::error!("cannot set motd: {}", e));
194+
.unwrap_or_else(|e| log::error!("cannot set motd: {e}"));
198195
set_boot_status(true, GRUB_PATH, MOUNT_INFO_PATH)?;
199196
Ok(())
200197
}
@@ -205,18 +202,18 @@ fn health_check() -> Result<()> {
205202
"Greenboot healthcheck failed - status is RED",
206203
previous_rollback,
207204
)?)
208-
.unwrap_or_else(|e| log::error!("cannot set motd: {}", e));
205+
.unwrap_or_else(|e| log::error!("cannot set motd: {e}"));
209206
let errors = run_red();
210207
if !errors.is_empty() {
211208
log::error!("There is a problem with red script runner");
212209
errors.iter().for_each(|e| log::error!("{e}"));
213210
}
214211

215212
set_boot_status(false, GRUB_PATH, MOUNT_INFO_PATH)
216-
.unwrap_or_else(|e| log::error!("cannot set boot_status: {}", e));
213+
.unwrap_or_else(|e| log::error!("cannot set boot_status: {e}"));
217214
set_boot_counter(config.max_reboot, GRUB_PATH, MOUNT_INFO_PATH)
218-
.unwrap_or_else(|e| log::error!("cannot set boot_counter: {}", e));
219-
handle_reboot(false).unwrap_or_else(|e| log::error!("cannot reboot: {}", e));
215+
.unwrap_or_else(|e| log::error!("cannot set boot_counter: {e}"));
216+
handle_reboot(false).unwrap_or_else(|e| log::error!("cannot reboot: {e}"));
220217
bail!("greenboot healthcheck failed")
221218
}
222219
}
@@ -239,7 +236,7 @@ fn trigger_rollback() -> Result<()> {
239236
// This function parses a string expected in bash-array format like
240237
// `( "item1" "item2" ... )` into a Vec<String>.
241238
fn parse_bash_array_string(raw_str: &str) -> Vec<String> {
242-
log::debug!("Attempting to parse raw bash-array string: '{}'", raw_str);
239+
log::debug!("Attempting to parse raw bash-array string: '{raw_str}'");
243240

244241
if raw_str.starts_with('(') && raw_str.ends_with(')') {
245242
// Remove the outer parentheses
@@ -252,14 +249,13 @@ fn parse_bash_array_string(raw_str: &str) -> Vec<String> {
252249
.filter(|s| !s.is_empty())
253250
.collect();
254251

255-
log::debug!("Parsed list from bash-array string: {:?}", parsed_list);
252+
log::debug!("Parsed list from bash-array string: {parsed_list:?}");
256253
parsed_list
257254
} else if !raw_str.trim().is_empty() {
258255
// If the string is not empty but doesn't match the expected format,
259256
// log a warning and return an empty list.
260257
log::warn!(
261-
"String ('{}') is not in the expected bash-array format '( \"item1\" ... )'. Treating as empty list.",
262-
raw_str
258+
"String ('{raw_str}') is not in the expected bash-array format '( \"item1\" ... )'. Treating as empty list."
263259
);
264260
vec![]
265261
} else {

tests/greenboot-bootc-anaconda-iso.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ FROM ${BASE_IMAGE_URL}
168168
COPY greenboot-*.rpm /tmp/
169169
RUN dnf install -y \
170170
/tmp/greenboot-*.rpm && \
171-
systemctl enable greenboot-healthcheck.service greenboot-rollback.service greenboot-success.target
171+
systemctl enable greenboot-healthcheck.service
172172
RUN sed -i "s/GREENBOOT_MAX_BOOT_ATTEMPTS=3/GREENBOOT_MAX_BOOT_ATTEMPTS=5/g" /etc/greenboot/greenboot.conf
173173
RUN sed -i 's#DISABLED_HEALTHCHECKS=()#DISABLED_HEALTHCHECKS=("01_repository_dns_check.sh" "not_exit.sh")#g' /etc/greenboot/greenboot.conf
174174
COPY passing_binary /etc/greenboot/check/required.d/

tests/greenboot-bootc-qcow2.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ FROM ${BASE_IMAGE_URL}
168168
COPY greenboot-*.rpm /tmp/
169169
RUN dnf install -y \
170170
/tmp/greenboot-*.rpm && \
171-
systemctl enable greenboot-healthcheck.service greenboot-rollback.service greenboot-success.target
171+
systemctl enable greenboot-healthcheck.service
172172
RUN sed -i "s/GREENBOOT_MAX_BOOT_ATTEMPTS=3/GREENBOOT_MAX_BOOT_ATTEMPTS=5/g" /etc/greenboot/greenboot.conf
173173
RUN sed -i 's#DISABLED_HEALTHCHECKS=()#DISABLED_HEALTHCHECKS=("01_repository_dns_check.sh" "not_exit.sh")#g' /etc/greenboot/greenboot.conf
174174
COPY passing_binary /etc/greenboot/check/required.d/

tests/greenboot-ostree.sh

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ sudo systemctl start libvirtd
129129
sudo virsh list --all > /dev/null
130130

131131
# Set a customized dnsmasq configuration for libvirt so we always get the
132-
# same address on bootup.
132+
# same address on boot up.
133133
greenprint "💡 Setup libvirt network"
134134
sudo tee /tmp/integration.xml > /dev/null << EOF
135135
<network xmlns:dnsmasq='http://libvirt.org/schemas/network/dnsmasq/1.0'>
@@ -320,8 +320,6 @@ version = "*"
320320
name = "sssd"
321321
version = "*"
322322
323-
[customizations.services]
324-
enabled = ["greenboot-success.target"]
325323
326324
[[customizations.user]]
327325
name = "${SSH_USER}"

usr/lib/systemd/system/greenboot-healthcheck.service

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,4 @@ PrivateMounts=yes
1717
[Install]
1818
RequiredBy=boot-complete.target
1919
WantedBy=multi-user.target
20+
Also=greenboot-success.target

0 commit comments

Comments
 (0)