@@ -58,32 +58,6 @@ _debug_info() {
5858 cat " ${AGENT_CFG} " || true
5959}
6060
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-
8761_get_current_deployment_id () {
8862 local -r id=" $( rpm-ostree status --booted --json | jq -r " .deployments[0].id" ) "
8963 echo " ${id} "
@@ -147,18 +121,37 @@ if [ ! -f "${AGENT_CFG}" ] ; then
147121 exit 0
148122fi
149123
150- current_boot=" $( _get_current_boot_number) "
151124current_deployment_id=" $( _get_current_deployment_id) "
152125
153126deploy=$( jq -c " .\" ${current_deployment_id} \" " " ${AGENT_CFG} " )
154127if [[ " ${deploy} " == " null" ]]; then
155128 exit 0
156129fi
157130
158- every_boot_actions=$( echo " ${deploy} " | jq -c " .\" every\" " )
159- current_boot_actions=$( echo " ${deploy} " | jq -c " .\" ${current_boot} \" " )
131+ current_boot_actions=$( echo " ${deploy} " | jq -c " [.[]] | flatten" )
132+
133+ # greenboot-rs takes a different approach compared to bash greenboot implementation.
134+ # bash greenboot: when deployment is staged, boot_counter is set immediately,
135+ # when host boots again, the variable is present on 1st boot of new deployment.
136+ # greenboot-rs: boot_counter is set only when the healthchecks fail for the new deployment.
137+ #
138+ # Therefore, test-agent cannot depend on boot_counter anymore to do
139+ # actions on first boot of the new deployment.
140+ # For this reason, the way how the test agent config is interpreted changed:
141+ # the .deployment.number is no longer the "ordinal boot number" (i.e. 1st, 2nd, 3rd boot of the deployment)
142+ # but "how many boots this action should be active".
143+ #
144+ # After collecting actions for the current boot, numbers are decremented, and if reach 0,
145+ # removed from the config.
146+ jq \
147+ --arg key " ${current_deployment_id} " \
148+ ' .[$key] |= ( \
149+ with_entries( \
150+ if (.key | test("^[0-9]+$")) then .key |= (tonumber - 1 | tostring) else . end) \
151+ | with_entries(select(.key != "0")) \
152+ )' " ${AGENT_CFG} " > " ${AGENT_CFG} .tmp" \
153+ && mv " ${AGENT_CFG} .tmp" " ${AGENT_CFG} "
160154
161- _run_actions " ${every_boot_actions} "
162155_run_actions " ${current_boot_actions} "
163156
164157# If running under systemd, notify systemd that the service is ready so that
0 commit comments