Skip to content

Commit c948927

Browse files
owineclaude
andcommitted
refactor: extract Dockge deployment to dedicated script
Extracted Dockge deployment logic into a reusable modular script following the established pattern for deployment modularity. Changes: - Created scripts/deployment/deploy-dockge.sh (110 lines) - Accepts SSH credentials, timeouts, and compose args - Uses ssh_retry with env command for OP_SERVICE_ACCOUNT_TOKEN - Deploys/rolls back Dockge with timeout protection - Consistent error handling and logging - Updated scripts/deployment/deploy-stacks.sh - Call deploy-dockge.sh before repository update (lines 103-115) - Removed inline Dockge deployment logic (~18 lines) - Added comment noting external Dockge handling - Updated scripts/deployment/rollback-stacks.sh - Call deploy-dockge.sh before repository rollback (lines 111-123) - Removed inline Dockge rollback logic (~18 lines) - Added comment noting external Dockge handling Benefits: - Eliminates code duplication (identical logic in 2 places) - Single source of truth for Dockge deployment - Can be called independently for Dockge-only operations - Consistent with other modular scripts (health-check, cleanup, etc.) - Easier to test and maintain Dockge deployment logic Script Structure: - Follows same pattern as other deployment scripts - Sources ssh-helpers.sh and common.sh for utilities - Validates required arguments (ssh-user, ssh-host, op-token) - Uses SSH with env command to pass OP_SERVICE_ACCOUNT_TOKEN - Provides clear success/failure logging Validated: shellcheck passes on all modified scripts 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
1 parent d14c671 commit c948927

File tree

3 files changed

+140
-35
lines changed

3 files changed

+140
-35
lines changed
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
#!/usr/bin/env bash
2+
#
3+
# Dockge Deployment Script
4+
# Deploys or rolls back the Dockge container management interface
5+
#
6+
# Usage:
7+
# deploy-dockge.sh --ssh-user USER --ssh-host HOST --op-token TOKEN \
8+
# --image-timeout 300 --startup-timeout 120 [--compose-args "args"]
9+
#
10+
# Exit codes:
11+
# 0 - Dockge deployed successfully
12+
# 1 - Deployment failed
13+
#
14+
15+
set -euo pipefail
16+
17+
# Get script directory and source libraries
18+
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
19+
# shellcheck source=lib/ssh-helpers.sh
20+
# shellcheck source=lib/common.sh
21+
source "$SCRIPT_DIR/lib/ssh-helpers.sh"
22+
source "$SCRIPT_DIR/lib/common.sh"
23+
24+
# Default values
25+
SSH_USER=""
26+
SSH_HOST=""
27+
OP_TOKEN=""
28+
IMAGE_PULL_TIMEOUT="300"
29+
SERVICE_STARTUP_TIMEOUT="120"
30+
COMPOSE_ARGS=""
31+
32+
# Parse arguments
33+
while [[ $# -gt 0 ]]; do
34+
case $1 in
35+
--ssh-user)
36+
SSH_USER="$2"
37+
shift 2
38+
;;
39+
--ssh-host)
40+
SSH_HOST="$2"
41+
shift 2
42+
;;
43+
--op-token)
44+
OP_TOKEN="$2"
45+
shift 2
46+
;;
47+
--image-timeout)
48+
IMAGE_PULL_TIMEOUT="$2"
49+
shift 2
50+
;;
51+
--startup-timeout)
52+
SERVICE_STARTUP_TIMEOUT="$2"
53+
shift 2
54+
;;
55+
--compose-args)
56+
COMPOSE_ARGS="$2"
57+
shift 2
58+
;;
59+
*)
60+
log_error "Unknown argument: $1"
61+
exit 1
62+
;;
63+
esac
64+
done
65+
66+
# Validate required arguments
67+
require_var SSH_USER || exit 1
68+
require_var SSH_HOST || exit 1
69+
require_var OP_TOKEN || exit 1
70+
71+
log_info "🚀 Deploying Dockge..."
72+
log_info "Image pull timeout: ${IMAGE_PULL_TIMEOUT}s"
73+
log_info "Service startup timeout: ${SERVICE_STARTUP_TIMEOUT}s"
74+
75+
# Execute Dockge deployment via SSH
76+
ssh_retry 3 5 "ssh -o \"StrictHostKeyChecking no\" $SSH_USER@$SSH_HOST env OP_SERVICE_ACCOUNT_TOKEN=\"$OP_TOKEN\" IMAGE_PULL_TIMEOUT=\"$IMAGE_PULL_TIMEOUT\" SERVICE_STARTUP_TIMEOUT=\"$SERVICE_STARTUP_TIMEOUT\" COMPOSE_ARGS=\"$COMPOSE_ARGS\" /bin/bash -s" << 'EOF'
77+
set -e
78+
79+
# Change to Dockge directory
80+
if ! cd /opt/dockge; then
81+
echo "❌ Failed to change to /opt/dockge directory"
82+
exit 1
83+
fi
84+
85+
echo "Pulling Dockge images..."
86+
# shellcheck disable=SC2086
87+
if ! timeout "$IMAGE_PULL_TIMEOUT" op run --env-file=/opt/compose/compose.env -- docker compose pull; then
88+
echo "❌ Dockge image pull timed out after ${IMAGE_PULL_TIMEOUT}s"
89+
exit 1
90+
fi
91+
92+
echo "Starting Dockge services..."
93+
# shellcheck disable=SC2086
94+
if ! timeout "$SERVICE_STARTUP_TIMEOUT" op run --env-file=/opt/compose/compose.env -- docker compose up -d --remove-orphans $COMPOSE_ARGS; then
95+
echo "❌ Dockge startup timed out after ${SERVICE_STARTUP_TIMEOUT}s"
96+
exit 1
97+
fi
98+
99+
echo "✅ Dockge deployed successfully"
100+
EOF
101+
102+
# Check SSH command exit status
103+
SSH_EXIT=$?
104+
if [ "$SSH_EXIT" -eq 0 ]; then
105+
log_success "Dockge deployed successfully"
106+
exit 0
107+
else
108+
log_error "Dockge deployment failed"
109+
exit 1
110+
fi

scripts/deployment/deploy-stacks.sh

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,20 @@ log_info "Starting deployment for stacks: $STACKS"
100100
log_info "Target ref: $TARGET_REF"
101101
log_info "Has Dockge: $HAS_DOCKGE"
102102

103+
# Deploy Dockge first if needed (must happen before repository update)
104+
if [ "$HAS_DOCKGE" = "true" ]; then
105+
"$SCRIPT_DIR/deploy-dockge.sh" \
106+
--ssh-user "$SSH_USER" \
107+
--ssh-host "$SSH_HOST" \
108+
--op-token "$OP_TOKEN" \
109+
--image-timeout "$IMAGE_PULL_TIMEOUT" \
110+
--startup-timeout "$SERVICE_STARTUP_TIMEOUT" \
111+
--compose-args "$COMPOSE_ARGS" || {
112+
log_error "Dockge deployment failed"
113+
exit 1
114+
}
115+
fi
116+
103117
# Execute deployment via SSH with retry
104118
# Use 'env' on remote side to set environment variables for the remote bash session
105119
ssh_retry 3 10 "ssh -o \"StrictHostKeyChecking no\" $SSH_USER@$SSH_HOST env OP_SERVICE_ACCOUNT_TOKEN=\"$OP_TOKEN\" GIT_FETCH_TIMEOUT=\"$GIT_FETCH_TIMEOUT\" GIT_CHECKOUT_TIMEOUT=\"$GIT_CHECKOUT_TIMEOUT\" IMAGE_PULL_TIMEOUT=\"$IMAGE_PULL_TIMEOUT\" SERVICE_STARTUP_TIMEOUT=\"$SERVICE_STARTUP_TIMEOUT\" VALIDATION_ENV_TIMEOUT=\"$VALIDATION_ENV_TIMEOUT\" VALIDATION_SYNTAX_TIMEOUT=\"$VALIDATION_SYNTAX_TIMEOUT\" /bin/bash -s $STACKS \"$HAS_DOCKGE\" \"$TARGET_REF\" \"$COMPOSE_ARGS\"" << 'EOF'
@@ -156,23 +170,7 @@ ssh_retry 3 10 "ssh -o \"StrictHostKeyChecking no\" $SSH_USER@$SSH_HOST env OP_S
156170
VALIDATION_ENV_TIMEOUT=${VALIDATION_ENV_TIMEOUT:-30}
157171
VALIDATION_SYNTAX_TIMEOUT=${VALIDATION_SYNTAX_TIMEOUT:-30}
158172
159-
if [ "$HAS_DOCKGE" = "true" ]; then
160-
echo "🚀 Deploying Dockge..."
161-
cd /opt/dockge
162-
163-
# Add timeout protection for Dockge operations
164-
if ! timeout $IMAGE_PULL_TIMEOUT op run --env-file=/opt/compose/compose.env -- docker compose pull; then
165-
echo "❌ Dockge image pull timed out after ${IMAGE_PULL_TIMEOUT}s"
166-
exit 1
167-
fi
168-
169-
if ! timeout $SERVICE_STARTUP_TIMEOUT op run --env-file=/opt/compose/compose.env -- docker compose up -d --remove-orphans $COMPOSE_ARGS; then
170-
echo "❌ Dockge startup timed out after ${SERVICE_STARTUP_TIMEOUT}s"
171-
exit 1
172-
fi
173-
174-
echo "✅ Dockge deployed successfully"
175-
fi
173+
# Note: Dockge deployment is now handled by deploy-dockge.sh before this SSH session
176174
177175
echo "Updating repository to $TARGET_REF..."
178176

scripts/deployment/rollback-stacks.sh

Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,20 @@ log_success "Previous SHA validation passed: $PREVIOUS_SHA"
108108
log_info "Initiating rollback to $PREVIOUS_SHA"
109109
log_info "Has Dockge: $HAS_DOCKGE"
110110

111+
# Rollback Dockge first if needed (must happen before repository rollback)
112+
if [ "$HAS_DOCKGE" = "true" ]; then
113+
"$SCRIPT_DIR/deploy-dockge.sh" \
114+
--ssh-user "$SSH_USER" \
115+
--ssh-host "$SSH_HOST" \
116+
--op-token "$OP_TOKEN" \
117+
--image-timeout "$IMAGE_PULL_TIMEOUT" \
118+
--startup-timeout "$SERVICE_STARTUP_TIMEOUT" \
119+
--compose-args "$COMPOSE_ARGS" || {
120+
log_error "Dockge rollback failed"
121+
exit 1
122+
}
123+
fi
124+
111125
# Execute rollback via SSH with retry
112126
ROLLBACK_RESULT=$(ssh_retry 3 10 "ssh -o \"StrictHostKeyChecking no\" $SSH_USER@$SSH_HOST env OP_SERVICE_ACCOUNT_TOKEN=\"$OP_TOKEN\" GIT_FETCH_TIMEOUT=\"$GIT_FETCH_TIMEOUT\" GIT_CHECKOUT_TIMEOUT=\"$GIT_CHECKOUT_TIMEOUT\" IMAGE_PULL_TIMEOUT=\"$IMAGE_PULL_TIMEOUT\" SERVICE_STARTUP_TIMEOUT=\"$SERVICE_STARTUP_TIMEOUT\" VALIDATION_ENV_TIMEOUT=\"$VALIDATION_ENV_TIMEOUT\" VALIDATION_SYNTAX_TIMEOUT=\"$VALIDATION_SYNTAX_TIMEOUT\" /bin/bash -s \"$HAS_DOCKGE\" \"$PREVIOUS_SHA\" \"$COMPOSE_ARGS\" \"$CRITICAL_SERVICES\"" << 'EOF'
113127
set -e
@@ -171,24 +185,7 @@ ROLLBACK_RESULT=$(ssh_retry 3 10 "ssh -o \"StrictHostKeyChecking no\" $SSH_USER@
171185
# Will be converted to space-delimited in rollback-health step for compatibility
172186
echo "DISCOVERED_ROLLBACK_STACKS=$ROLLBACK_STACKS"
173187
174-
# Deploy Dockge first if needed
175-
if [ "$HAS_DOCKGE" = "true" ]; then
176-
echo "🔄 Rolling back Dockge..."
177-
cd /opt/dockge
178-
179-
# Add timeout protection for Dockge operations
180-
if ! timeout $IMAGE_PULL_TIMEOUT op run --env-file=/opt/compose/compose.env -- docker compose pull; then
181-
echo "❌ Dockge image pull timed out after ${IMAGE_PULL_TIMEOUT}s"
182-
exit 1
183-
fi
184-
185-
if ! timeout $SERVICE_STARTUP_TIMEOUT op run --env-file=/opt/compose/compose.env -- docker compose up -d --remove-orphans $COMPOSE_ARGS; then
186-
echo "❌ Dockge startup timed out after ${SERVICE_STARTUP_TIMEOUT}s"
187-
exit 1
188-
fi
189-
190-
echo "✅ Dockge rolled back successfully"
191-
fi
188+
# Note: Dockge rollback is now handled by deploy-dockge.sh before this SSH session
192189
193190
# Shared function to deploy or rollback a single stack
194191
# This eliminates code duplication between deploy and rollback operations

0 commit comments

Comments
 (0)