Skip to content

Commit 59226d2

Browse files
committed
Change test-agent impl to align with greenboot-rs
1 parent 91daea7 commit 59226d2

File tree

1 file changed

+20
-31
lines changed

1 file changed

+20
-31
lines changed

test/agent/microshift-test-agent.sh

Lines changed: 20 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
#!/usr/bin/bash
22
set -xeuo pipefail
33

4-
GREENBOOT_CONFIGURATION_FILE=/etc/greenboot/greenboot.conf
54
AGENT_CFG=/var/lib/microshift-test-agent.json
65
SYSTEMD_NOTIFIED=false
76

@@ -58,32 +57,6 @@ _debug_info() {
5857
cat "${AGENT_CFG}" || true
5958
}
6059

61-
_get_current_boot_number() {
62-
if ! /usr/bin/grub2-editenv list | grep -q boot_counter; then
63-
echo "boot_counter is missing - script only for newly staged deployments"
64-
exit 0
65-
fi
66-
67-
local -r boot_counter=$(/usr/bin/grub2-editenv list | grep boot_counter | sed 's,boot_counter=,,')
68-
local max_boot_attempts
69-
70-
if test -f "${GREENBOOT_CONFIGURATION_FILE}"; then
71-
# shellcheck source=/dev/null
72-
source "${GREENBOOT_CONFIGURATION_FILE}"
73-
fi
74-
75-
if [ -v GREENBOOT_MAX_BOOT_ATTEMPTS ]; then
76-
max_boot_attempts="${GREENBOOT_MAX_BOOT_ATTEMPTS}"
77-
else
78-
max_boot_attempts=3
79-
fi
80-
81-
# When deployment is staged, greenboot sets boot_counter to 3
82-
# and this variable gets decremented on each boot.
83-
# First boot of new deployment will have it set to 2.
84-
echo "$((max_boot_attempts - boot_counter))"
85-
}
86-
8760
_get_current_deployment_id() {
8861
local -r id="$(rpm-ostree status --booted --json | jq -r ".deployments[0].id")"
8962
echo "${id}"
@@ -147,18 +120,34 @@ if [ ! -f "${AGENT_CFG}" ] ; then
147120
exit 0
148121
fi
149122

150-
current_boot="$(_get_current_boot_number)"
151123
current_deployment_id="$(_get_current_deployment_id)"
152124

153125
deploy=$(jq -c ".\"${current_deployment_id}\"" "${AGENT_CFG}")
154126
if [[ "${deploy}" == "null" ]]; then
155127
exit 0
156128
fi
157129

158-
every_boot_actions=$(echo "${deploy}" | jq -c ".\"every\"")
159-
current_boot_actions=$(echo "${deploy}" | jq -c ".\"${current_boot}\"")
130+
current_boot_actions=$(echo "${deploy}" | jq -c "[.[]] | flatten")
131+
132+
# greenboot-rs takes a different approach compared to bash greenboot implementation.
133+
# bash greenboot: when deployment is staged, boot_counter is set immediately,
134+
# when host boots again, the variable is present on 1st boot of new deployment.
135+
# greenboot-rs: boot_counter is set only when the healthchecks fail for the new deployment.
136+
#
137+
# Therefore, test-agent cannot depend on boot_counter anymore to do
138+
# actions on first boot of the new deployment.
139+
# For this reason, the way how the test agent config is interpreted changed:
140+
# the .deployment.number is no longer the "ordinal boot number" (i.e. 1st, 2nd, 3rd boot of the deployment)
141+
# but "how many boots this action should be active".
142+
#
143+
# After collecting actions for the current boot, numbers are decremented, and if reach 0,
144+
# removed from the config.
145+
jq \
146+
--arg key "${current_deployment_id}" \
147+
'.[$key] |= (with_entries(if (.key | test("^[0-9]+$")) then .key |= (tonumber - 1 | tostring) else . end) | with_entries(select(.key != "0")))' \
148+
"${AGENT_CFG}" > "${AGENT_CFG}.tmp" && \
149+
mv "${AGENT_CFG}.tmp" "${AGENT_CFG}"
160150

161-
_run_actions "${every_boot_actions}"
162151
_run_actions "${current_boot_actions}"
163152

164153
# If running under systemd, notify systemd that the service is ready so that

0 commit comments

Comments
 (0)