From e7fd791b9a371aa907365726311a1f78ffba0f31 Mon Sep 17 00:00:00 2001 From: Patryk Matuszak <305846+pmtk@users.noreply.github.com> Date: Wed, 28 Jun 2023 17:23:02 +0200 Subject: [PATCH] test agent --- test/agent/build.sh | 12 +++ test/agent/microshift-test-agent.service | 9 ++ test/agent/microshift-test-agent.sh | 123 +++++++++++++++++++++++ test/agent/microshift-test-agent.spec | 25 +++++ 4 files changed, 169 insertions(+) create mode 100755 test/agent/build.sh create mode 100644 test/agent/microshift-test-agent.service create mode 100755 test/agent/microshift-test-agent.sh create mode 100644 test/agent/microshift-test-agent.spec diff --git a/test/agent/build.sh b/test/agent/build.sh new file mode 100755 index 0000000000..164f08668a --- /dev/null +++ b/test/agent/build.sh @@ -0,0 +1,12 @@ +#!/bin/bash + +set -euo pipefail + +SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" &>/dev/null && pwd)" +RPMBUILD_DIR="$(git rev-parse --show-toplevel)/_output/rpmbuild/" + +mkdir -p "${RPMBUILD_DIR}"/{BUILD,RPMS,SOURCES,SPECS,SRPMS} +mkdir -p "${RPMBUILD_DIR}/SOURCES/test-agent" +cp "${SCRIPT_DIR}"/microshift-test-agent.{service,sh} "${RPMBUILD_DIR}/SOURCES/test-agent" + +rpmbuild --define "_topdir ${RPMBUILD_DIR}" -bb "${SCRIPT_DIR}/microshift-test-agent.spec" diff --git a/test/agent/microshift-test-agent.service b/test/agent/microshift-test-agent.service new file mode 100644 index 0000000000..0c69b9b42f --- /dev/null +++ b/test/agent/microshift-test-agent.service @@ -0,0 +1,9 @@ +[Unit] +Description=MicroShift Test Agent +Before=microshift.service + +[Service] +ExecStart=/bin/bash /usr/bin/microshift-test-agent.sh + +[Install] +WantedBy=multi-user.target diff --git a/test/agent/microshift-test-agent.sh b/test/agent/microshift-test-agent.sh new file mode 100755 index 0000000000..5e3c454c56 --- /dev/null +++ b/test/agent/microshift-test-agent.sh @@ -0,0 +1,123 @@ +#!/bin/bash + +set -xeuo pipefail + +GREENBOOT_CONFIGURATION_FILE=/etc/greenboot/greenboot.conf +AGENT_CFG=/var/lib/microshift-test-agent.json + +# Example config +# { +# "deploy-id": { +# "every": [ "prevent_backup" ], +# "1": [ "fail_greenboot" ], +# "2": [ "..." ], +# "3": [ "..." ] +# } +# } + +CLEANUP_CMDS=() +_cleanup() { + for cmd in "${CLEANUP_CMDS[@]}"; do + ${cmd} + done +} +trap "_cleanup" SIGTERM SIGQUIT INT + +_run_actions() { + local -r actions="${1}" + if [[ "${actions}" == "null" ]]; then + return + fi + + num=$(echo "${actions}" | jq -c ". | length") + for i in $(seq 0 $((num - 1))); do + action=$(echo "${actions}" | jq -c -r ".[${i}]") + + if ! declare -F "${action}"; then + echo "Unknown action: ${action}" + else + "${action}" + fi + done +} + +_debug_info() { + grub2-editenv - list || true + ostree admin status -v || true + rpm-ostree status -v || true + journalctl --list-boots --reverse | head -n6 || true + ls -lah /var/lib/ || true + ls -lah /var/lib/microshift || true + ls -lah /var/lib/microshift-backups || true + cat "${AGENT_CFG}" || true +} + +_get_current_boot_number() { + if ! /usr/bin/grub2-editenv list | grep -q boot_counter; then + echo "boot_counter is missing - script only for newly staged deployments" + exit 0 + fi + + local -r boot_counter=$(/usr/bin/grub2-editenv list | grep boot_counter | sed 's,boot_counter=,,') + local max_boot_attempts + + if test -f "${GREENBOOT_CONFIGURATION_FILE}"; then + # shellcheck source=/dev/null + source "${GREENBOOT_CONFIGURATION_FILE}" + fi + + if [ -v GREENBOOT_MAX_BOOT_ATTEMPTS ]; then + max_boot_attempts="${GREENBOOT_MAX_BOOT_ATTEMPTS}" + else + max_boot_attempts=3 + fi + + # When deployment is staged, greenboot sets boot_counter to 3 + # and this variable gets decremented on each boot. + # First boot of new deployment will have it set to 2. + echo "$((max_boot_attempts - boot_counter))" +} + +prevent_backup() { + local -r path="/var/lib/microshift-backups" + if [[ ! -e "${path}" ]]; then + mkdir -vp "${path}" + + # because of immutable attr, if the file does not exist, it can't be created + touch "${path}/health.json" + fi + # prevents from creating a new backup directory, but doesn't prevent from updating health.json + chattr -V +i "${path}" + CLEANUP_CMDS+=("chattr -V -i ${path}") +} + +fail_greenboot() { + local -r path="/etc/greenboot/check/required.d/99_microshift_test_failure.sh" + cat >"${path}" <