From 21dfe7e09e96c86848a2caf3f57bc5667e265904 Mon Sep 17 00:00:00 2001 From: Ben Vinegar Date: Sat, 21 Feb 2026 16:59:38 -0500 Subject: [PATCH] deploy: share release and rollback shell helpers --- bin/lib/release-common.sh | 48 +++++++++++++++++++++++++++++++++++ bin/rollback-release.sh | 53 ++++----------------------------------- bin/update-release.sh | 52 +++----------------------------------- 3 files changed, 56 insertions(+), 97 deletions(-) create mode 100644 bin/lib/release-common.sh diff --git a/bin/lib/release-common.sh b/bin/lib/release-common.sh new file mode 100644 index 0000000..152abb4 --- /dev/null +++ b/bin/lib/release-common.sh @@ -0,0 +1,48 @@ +#!/bin/bash +# Shared helpers for release/update/rollback scripts. + +has_systemd() { + command -v systemctl >/dev/null 2>&1 && [ -d /run/systemd/system ] +} + +verify_git_free_release() { + local dir="$1" + + [ -d "$dir" ] || return 1 + [ ! -d "$dir/.git" ] || return 1 + + if find "$dir" -type d -name .git -print -quit | grep -q .; then + return 1 + fi + + return 0 +} + +atomic_symlink_swap() { + local target="$1" + local link_path="$2" + local parent + local tmp_link + + parent="$(dirname "$link_path")" + mkdir -p "$parent" + + tmp_link="$parent/.tmp.$(basename "$link_path").$$" + ln -s "$target" "$tmp_link" + mv -Tf "$tmp_link" "$link_path" +} + +restart_baudbot_service_if_active() { + if has_systemd && systemctl is-enabled baudbot >/dev/null 2>&1; then + if systemctl is-active baudbot >/dev/null 2>&1; then + log "restarting baudbot service" + systemctl restart baudbot + sleep 3 + systemctl is-active baudbot >/dev/null 2>&1 || die "service failed to restart" + else + log "service installed but not active; skipping restart" + fi + else + log "systemd unavailable; skipping restart" + fi +} diff --git a/bin/rollback-release.sh b/bin/rollback-release.sh index 20fd1af..2f8bad6 100755 --- a/bin/rollback-release.sh +++ b/bin/rollback-release.sh @@ -7,6 +7,8 @@ set -euo pipefail +SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" + BAUDBOT_RELEASE_ROOT="${BAUDBOT_RELEASE_ROOT:-/opt/baudbot}" BAUDBOT_RELEASES_DIR="${BAUDBOT_RELEASES_DIR:-$BAUDBOT_RELEASE_ROOT/releases}" BAUDBOT_CURRENT_LINK="${BAUDBOT_CURRENT_LINK:-$BAUDBOT_RELEASE_ROOT/current}" @@ -37,36 +39,8 @@ Usage: $0 [previous|] [--release-root ] [--skip-restart] EOF } -has_systemd() { - command -v systemctl >/dev/null 2>&1 && [ -d /run/systemd/system ] -} - -verify_git_free_release() { - local dir="$1" - - [ -d "$dir" ] || return 1 - [ ! -d "$dir/.git" ] || return 1 - - if find "$dir" -type d -name .git -print -quit | grep -q .; then - return 1 - fi - - return 0 -} - -atomic_symlink_swap() { - local target="$1" - local link_path="$2" - local parent - local tmp_link - - parent="$(dirname "$link_path")" - mkdir -p "$parent" - - tmp_link="$parent/.tmp.$(basename "$link_path").$$" - ln -s "$target" "$tmp_link" - mv -Tf "$tmp_link" "$link_path" -} +# shellcheck source=bin/lib/release-common.sh +source "$SCRIPT_DIR/lib/release-common.sh" TARGET_SPEC="${1:-previous}" if [ "$#" -gt 0 ]; then @@ -153,30 +127,13 @@ run_deploy() { } run_restart_and_health() { - local was_active=0 - if [ -n "$BAUDBOT_ROLLBACK_RESTART_CMD" ]; then log "running restart override" BAUDBOT_ROLLBACK_TARGET_RELEASE="$TARGET_RELEASE" bash -lc "$BAUDBOT_ROLLBACK_RESTART_CMD" elif [ "$BAUDBOT_ROLLBACK_SKIP_RESTART" = "1" ]; then log "skipping restart" else - if has_systemd && systemctl is-enabled baudbot >/dev/null 2>&1; then - if systemctl is-active baudbot >/dev/null 2>&1; then - was_active=1 - fi - - if [ "$was_active" -eq 1 ]; then - log "restarting baudbot service" - systemctl restart baudbot - sleep 3 - systemctl is-active baudbot >/dev/null 2>&1 || die "service failed to restart" - else - log "service installed but not active; skipping restart" - fi - else - log "systemd unavailable; skipping restart" - fi + restart_baudbot_service_if_active fi if [ -n "$BAUDBOT_ROLLBACK_HEALTH_CMD" ]; then diff --git a/bin/update-release.sh b/bin/update-release.sh index 7d0aac0..cbc6eab 100755 --- a/bin/update-release.sh +++ b/bin/update-release.sh @@ -56,9 +56,8 @@ die() { exit 1 } -has_systemd() { - command -v systemctl >/dev/null 2>&1 && [ -d /run/systemd/system ] -} +# shellcheck source=bin/lib/release-common.sh +source "$SCRIPT_DIR/lib/release-common.sh" cleanup() { if [ -n "$CHECKOUT_DIR" ] && [ -d "$CHECKOUT_DIR" ]; then @@ -170,34 +169,6 @@ resolve_branch() { echo "main" } -verify_git_free_release() { - local dir="$1" - - if [ -d "$dir/.git" ]; then - return 1 - fi - - if find "$dir" -type d -name .git -print -quit | grep -q .; then - return 1 - fi - - return 0 -} - -atomic_symlink_swap() { - local target="$1" - local link_path="$2" - local parent - local tmp_link - - parent="$(dirname "$link_path")" - mkdir -p "$parent" - - tmp_link="$parent/.tmp.$(basename "$link_path").$$" - ln -s "$target" "$tmp_link" - mv -Tf "$tmp_link" "$link_path" -} - save_source_metadata() { local repo_url="$1" local branch="$2" @@ -303,30 +274,13 @@ run_deploy() { } run_restart_and_health() { - local was_active=0 - if [ -n "$BAUDBOT_UPDATE_RESTART_CMD" ]; then log "running restart override" BAUDBOT_UPDATE_RELEASE_DIR="$RELEASE_DIR" BAUDBOT_UPDATE_CHECKOUT_DIR="$CHECKOUT_DIR" bash -lc "$BAUDBOT_UPDATE_RESTART_CMD" elif [ "$BAUDBOT_UPDATE_SKIP_RESTART" = "1" ]; then log "skipping restart" else - if has_systemd && systemctl is-enabled baudbot >/dev/null 2>&1; then - if systemctl is-active baudbot >/dev/null 2>&1; then - was_active=1 - fi - - if [ "$was_active" -eq 1 ]; then - log "restarting baudbot service" - systemctl restart baudbot - sleep 3 - systemctl is-active baudbot >/dev/null 2>&1 || die "service failed to restart" - else - log "service installed but not active; skipping restart" - fi - else - log "systemd unavailable; skipping restart" - fi + restart_baudbot_service_if_active fi if [ -n "$BAUDBOT_UPDATE_HEALTH_CMD" ]; then