|
1 | 1 | #!/usr/bin/bash |
2 | 2 | set -xeuo pipefail |
3 | 3 |
|
4 | | -GREENBOOT_CONFIGURATION_FILE=/etc/greenboot/greenboot.conf |
5 | 4 | AGENT_CFG=/var/lib/microshift-test-agent.json |
6 | 5 | SYSTEMD_NOTIFIED=false |
7 | 6 |
|
@@ -58,32 +57,6 @@ _debug_info() { |
58 | 57 | cat "${AGENT_CFG}" || true |
59 | 58 | } |
60 | 59 |
|
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 | | - |
87 | 60 | _get_current_deployment_id() { |
88 | 61 | local -r id="$(rpm-ostree status --booted --json | jq -r ".deployments[0].id")" |
89 | 62 | echo "${id}" |
@@ -147,18 +120,34 @@ if [ ! -f "${AGENT_CFG}" ] ; then |
147 | 120 | exit 0 |
148 | 121 | fi |
149 | 122 |
|
150 | | -current_boot="$(_get_current_boot_number)" |
151 | 123 | current_deployment_id="$(_get_current_deployment_id)" |
152 | 124 |
|
153 | 125 | deploy=$(jq -c ".\"${current_deployment_id}\"" "${AGENT_CFG}") |
154 | 126 | if [[ "${deploy}" == "null" ]]; then |
155 | 127 | exit 0 |
156 | 128 | fi |
157 | 129 |
|
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}" |
160 | 150 |
|
161 | | -_run_actions "${every_boot_actions}" |
162 | 151 | _run_actions "${current_boot_actions}" |
163 | 152 |
|
164 | 153 | # If running under systemd, notify systemd that the service is ready so that |
|
0 commit comments