Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 48 additions & 0 deletions bin/lib/release-common.sh
Original file line number Diff line number Diff line change
@@ -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
}
53 changes: 5 additions & 48 deletions bin/rollback-release.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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}"
Expand Down Expand Up @@ -37,36 +39,8 @@ Usage: $0 [previous|<sha>] [--release-root <path>] [--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
Expand Down Expand Up @@ -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
Expand Down
52 changes: 3 additions & 49 deletions bin/update-release.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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
Expand Down