-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathundeploy
More file actions
executable file
·175 lines (143 loc) · 5.17 KB
/
Copy pathundeploy
File metadata and controls
executable file
·175 lines (143 loc) · 5.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
#!/bin/bash
# undeploy - Remove Django deployments safely
#
# Usage: ./undeploy <project> <branch>
#
# Parameters:
# project - Project name (e.g., sampleapp, uno-admin)
# branch - Branch name (slashes converted to hyphens)
#
# Examples:
# ./undeploy sampleapp main
# ./undeploy uno-admin dev
# ./undeploy myapp feature/auth
set -e # Exit on any error
# Source common logging utilities
source "$(dirname "$0")/scripts/logging-utils.sh"
# Show usage information
show_usage() {
cat << EOF
Usage: ./undeploy <project> <branch>
PARAMETERS:
project Project name (e.g., sampleapp, uno-admin)
branch Branch name (slashes converted to hyphens)
BRANCH NAMING:
- Branch names with slashes are converted to hyphens for filesystem compatibility
- Examples: 'main', 'dev', 'qa', 'feature/auth' → 'feature-auth'
EXAMPLES:
# Remove main branch deployment
./undeploy sampleapp main
# Remove dev branch deployment
./undeploy uno-admin dev
# Remove feature branch deployment
./undeploy myapp feature/auth
SAFETY FEATURES:
- Interactive confirmation required
- Automatic backup creation before deletion
- Production environments are protected
- Complete service and database cleanup
- Detailed logging of all operations
NOTES:
- Backups are stored in /srv/deployment-backups/
- Requires sudo privileges for system operations
- Branch names with slashes are automatically converted to hyphens
EOF
}
# Convert branch name to filesystem-safe format
convert_branch_name() {
local branch="$1"
# Replace slashes with hyphens for filesystem compatibility
echo "${branch//\//-}"
}
# Validate parameters
validate_parameters() {
if [[ $# -lt 2 ]]; then
log_error "Missing required parameters"
show_usage
exit 1
fi
PROJECT="$1"
BRANCH="$2"
NORMALIZED_BRANCH=$(convert_branch_name "$BRANCH")
# Validate project name
if [[ ! "$PROJECT" =~ ^[a-zA-Z0-9_-]+$ ]]; then
log_error "Invalid project name '$PROJECT'. Use only letters, numbers, hyphens, and underscores."
exit 1
fi
# Validate branch name
if [[ ! "$BRANCH" =~ ^[a-zA-Z0-9_/-]+$ ]]; then
log_error "Invalid branch name '$BRANCH'. Use only letters, numbers, hyphens, underscores, and forward slashes."
exit 1
fi
log_info "Project: $PROJECT"
log_info "Branch: $BRANCH"
log_info "Normalized branch: $NORMALIZED_BRANCH"
}
# Check if deployment exists
check_deployment_exists() {
local deployment_path="/srv/deployments/$PROJECT/$NORMALIZED_BRANCH"
if [[ ! -d "$deployment_path" ]]; then
log_error "Deployment not found: $PROJECT/$NORMALIZED_BRANCH"
log_error "Path does not exist: $deployment_path"
log_info "Use './status' to see available deployments"
exit 1
fi
log_info "Found deployment: $deployment_path"
echo "$deployment_path"
}
# Main execution function
main() {
local script_dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
local cleanup_script="$script_dir/scripts/cleanup-deployments.sh"
# Create undeploy log with timestamp
local log_timestamp=$(date +%Y%m%d%H%M%S)
local log_file="$script_dir/logs/undeploy-${log_timestamp}.log"
# Ensure logs directory exists
mkdir -p "$script_dir/logs"
# Validate input parameters
validate_parameters "$@"
# Create undeploy header in log
{
echo "[UNDEPLOY] $PROJECT $BRANCH"
echo "========================================"
echo ""
} | tee "$log_file"
log_step "Starting deployment removal process" | tee -a "$log_file"
# Verify cleanup script exists
if [[ ! -f "$cleanup_script" ]]; then
log_error "Cleanup script not found: $cleanup_script" | tee -a "$log_file"
exit 1
fi
# Check if deployment exists
local deployment_path
deployment_path=$(check_deployment_exists 2>&1 | tee -a "$log_file" | tail -1)
# Run the cleanup script
log_step "Executing cleanup script" | tee -a "$log_file"
if "$cleanup_script" "$PROJECT" "$NORMALIZED_BRANCH" 2>&1 | tee -a "$log_file"; then
# Update deployment registry to remove the deployment
log_step "Updating deployment registry" | tee -a "$log_file"
if python3 "$script_dir/scripts/manage-deployments-registry.py" remove --project "$PROJECT" --branch "$NORMALIZED_BRANCH" 2>&1 | tee -a "$log_file"; then
log_info "✓ Deployment registry updated successfully" | tee -a "$log_file"
else
log_warn "⚠ Failed to update deployment registry (non-critical)" | tee -a "$log_file"
fi
log_success "🎉 Deployment removed successfully!" | tee -a "$log_file"
log_info "📋 Full undeploy log: $log_file"
log_info "💾 Backup location: /srv/deployment-backups/"
exit 0
else
log_error "Undeployment failed" | tee -a "$log_file"
log_error "📋 Full undeploy log: $log_file"
exit 1
fi
}
# Handle command line arguments
case "${1:-}" in
-h|--help|help|"")
show_usage
exit 0
;;
*)
main "$@"
;;
esac