forked from fedora-iot/greenboot
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add watchdog-triggered boot check #2
Add required.d check for watchdog-triggered boot. Add bats testing for the script. Include greenboot.conf file. Signed-off-by: Jose Noguera <jnoguera@redhat.com>
- Loading branch information
Showing
10 changed files
with
114 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
# Greenboot configuration file | ||
|
||
## Generic | ||
# GREENBOOT_MAX_BOOT_ATTEMPTS=3 | ||
|
||
## Watchdog | ||
### This variable controls | ||
### This value can be "true, TRUE, True..." as it will be lowercased. | ||
### Set it to anything else to disable this check. | ||
GREENBOOT_WATCHDOG_CHECK_ENABLED=true | ||
|
||
### This variable is the number of hours after an upgrade that we consider | ||
### the new deployment as culprit of reboot. | ||
### It has to be a positive integer. Defaults to 24 (hours). | ||
# GREENBOOT_WATCHDOG_GRACE_PERIOD=24 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
load common.bash | ||
|
||
function setup() { | ||
source $GREENBOOT_DEFAULT_CHECK_PATH/required.d/02_watchdog.sh --source-only | ||
} | ||
|
||
@test "Ensure watchdog check is working" { | ||
run check_if_current_boot_is_wd_triggered | ||
[ "$status" -eq 0 ] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
#!/bin/bash | ||
set -eo pipefail | ||
|
||
source_configuration_file() { | ||
GREENBOOT_CONFIGURATION_FILE=/etc/greenboot/greenboot.conf | ||
if test -f "$GREENBOOT_CONFIGURATION_FILE"; then | ||
source $GREENBOOT_CONFIGURATION_FILE | ||
fi | ||
} | ||
|
||
set_grace_period() { | ||
DEFAULT_GRACE_PERIOD=24 # default to 24 hours | ||
|
||
if [ -n "$GREENBOOT_WATCHDOG_GRACE_PERIOD" ]; then | ||
GRACE_PERIOD=$GREENBOOT_WATCHDOG_GRACE_PERIOD | ||
else | ||
GRACE_PERIOD=$DEFAULT_GRACE_PERIOD | ||
fi | ||
} | ||
|
||
check_if_there_is_a_watchdog() { | ||
if wdctl 2>/dev/null ; then | ||
return 0 | ||
else | ||
return 1 | ||
fi | ||
} | ||
|
||
check_if_current_boot_is_wd_triggered() { | ||
if check_if_there_is_a_watchdog ; then | ||
WDCTL_OUTPUT=$(wdctl --flags-only --noheadings | grep -c '1$' || true) | ||
if [ "$WDCTL_OUTPUT" -gt 0 ]; then | ||
# This means the boot was watchdog triggered | ||
# TO-DO: maybe do a rollback here? | ||
echo "Watchdog triggered after recent update" | ||
exit 1 | ||
fi | ||
else | ||
# There's no watchdog, so nothing to be done here | ||
exit 0 | ||
fi | ||
} | ||
|
||
# This is in order to test check_if_current_boot_is_wd_triggered | ||
# function within a container | ||
if [ "${1}" != "--source-only" ]; then | ||
source_configuration_file | ||
if [ "${GREENBOOT_WATCHDOG_CHECK_ENABLED,,}" != "true" ]; then | ||
echo "Watchdog check is disabled" | ||
exit 0 | ||
fi | ||
|
||
set_grace_period | ||
|
||
SECONDS_IN_AN_HOUR=$((60 * 60)) | ||
LAST_DEPLOYMENT_TIMESTAMP=$(rpm-ostree status --json | jq .deployments[0].timestamp) | ||
|
||
HOURS_SINCE_LAST_UPDATE=$((($(date +%s) - "$LAST_DEPLOYMENT_TIMESTAMP") / SECONDS_IN_AN_HOUR)) | ||
if [ "$HOURS_SINCE_LAST_UPDATE" -lt "$GRACE_PERIOD" ]; then | ||
check_if_current_boot_is_wd_triggered | ||
else | ||
exit 0 | ||
fi | ||
fi |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters