-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdelete-project.sh
More file actions
305 lines (262 loc) · 8.04 KB
/
delete-project.sh
File metadata and controls
305 lines (262 loc) · 8.04 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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
#!/bin/bash
# Delete Project Script
# Removes Git repository, work tree, and Nginx configuration for a project
set -e
# Color output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color
# Function to print colored messages
print_info() {
echo -e "${GREEN}[INFO]${NC} $1"
}
print_error() {
echo -e "${RED}[ERROR]${NC} $1"
}
print_warning() {
echo -e "${YELLOW}[WARNING]${NC} $1"
}
print_step() {
echo -e "${BLUE}[STEP]${NC} $1"
}
# Check if running as root
if [ "$EUID" -eq 0 ]; then
print_error "Please do not run this script as root or with sudo"
exit 1
fi
# Check if script is run with required arguments
if [ "$#" -lt 2 ]; then
print_error "Usage: $0 <project-name> <domain-name> [git-dir] [work-tree]"
echo ""
echo "Arguments:"
echo " project-name : Name of your project"
echo " domain-name : Domain name for Nginx"
echo " git-dir : Optional custom Git directory (default: /home/$USER/workspace/git/<project-name>)"
echo ""
echo "Example: $0 myapp example.com"
echo "Example: $0 myapp example.com /home/$USER/custom/git/myapp"
exit 1
fi
PROJECT_NAME="$1"
DOMAIN_NAME="$2"
GIT_DIR="${3:-/home/$USER/workspace/git/${PROJECT_NAME}}"
WORK_TREE="/usr/share/nginx/${PROJECT_NAME}"
HTML_SYMLINK="/home/$USER/workspace/html/${PROJECT_NAME}"
NGINX_CONF="/etc/nginx/sites-available/${DOMAIN_NAME}"
NGINX_ENABLED="/etc/nginx/sites-enabled/${DOMAIN_NAME}"
INFO_FILE="/home/${USER}/${PROJECT_NAME}_setup_info.txt"
echo ""
print_warning "=== Delete Project ==="
echo ""
echo " Project Name : ${PROJECT_NAME}"
echo " Domain Name : ${DOMAIN_NAME}"
echo " Git Directory : ${GIT_DIR}"
echo " Work Tree : ${WORK_TREE}"
echo " HTML Symlink : ${HTML_SYMLINK}"
echo " Nginx Config : ${NGINX_CONF}"
echo " Info File : ${INFO_FILE}"
echo ""
# Check what exists
echo "Checking existing resources..."
echo ""
EXISTS_GIT=false
EXISTS_WORK=false
EXISTS_SYMLINK=false
EXISTS_NGINX_CONF=false
EXISTS_NGINX_ENABLED=false
EXISTS_INFO=false
if [ -d "${GIT_DIR}" ]; then
print_warning "✓ Git directory exists: ${GIT_DIR}"
EXISTS_GIT=true
else
print_info "✗ Git directory not found: ${GIT_DIR}"
fi
if [ -d "${WORK_TREE}" ]; then
print_warning "✓ Work tree exists: ${WORK_TREE}"
EXISTS_WORK=true
else
print_info "✗ Work tree not found: ${WORK_TREE}"
fi
if [ -L "${HTML_SYMLINK}" ] || [ -e "${HTML_SYMLINK}" ]; then
print_warning "✓ HTML symlink exists: ${HTML_SYMLINK}"
EXISTS_SYMLINK=true
else
print_info "✗ HTML symlink not found: ${HTML_SYMLINK}"
fi
if [ -f "${NGINX_CONF}" ]; then
print_warning "✓ Nginx config exists: ${NGINX_CONF}"
EXISTS_NGINX_CONF=true
else
print_info "✗ Nginx config not found: ${NGINX_CONF}"
fi
if [ -L "${NGINX_ENABLED}" ] || [ -f "${NGINX_ENABLED}" ]; then
print_warning "✓ Nginx enabled link exists: ${NGINX_ENABLED}"
EXISTS_NGINX_ENABLED=true
else
print_info "✗ Nginx enabled link not found: ${NGINX_ENABLED}"
fi
if [ -f "${INFO_FILE}" ]; then
print_warning "✓ Setup info file exists: ${INFO_FILE}"
EXISTS_INFO=true
else
print_info "✗ Setup info file not found: ${INFO_FILE}"
fi
echo ""
# Check if anything exists
if ! $EXISTS_GIT && ! $EXISTS_WORK && ! $EXISTS_SYMLINK && ! $EXISTS_NGINX_CONF && ! $EXISTS_NGINX_ENABLED && ! $EXISTS_INFO; then
print_info "No resources found for project '${PROJECT_NAME}'. Nothing to delete."
exit 0
fi
# Confirm deletion
print_warning "WARNING: This will permanently delete all project files and configuration!"
print_warning "This action cannot be undone."
echo ""
read -p "Are you sure you want to delete this project? (yes/NO): " -r
echo
if [[ ! $REPLY =~ ^[Yy][Ee][Ss]$ ]]; then
print_info "Deletion cancelled."
exit 0
fi
# Double confirmation
echo ""
print_warning "FINAL CONFIRMATION"
read -p "Type the project name '${PROJECT_NAME}' to confirm deletion: " -r
echo
if [[ "$REPLY" != "$PROJECT_NAME" ]]; then
print_error "Project name does not match. Deletion cancelled."
exit 1
fi
echo ""
print_info "Starting deletion process..."
echo ""
STEP=1
# Disable Nginx site
if $EXISTS_NGINX_ENABLED; then
print_step "${STEP}. Disabling Nginx site..."
if sudo rm -f "${NGINX_ENABLED}"; then
print_info "Nginx site disabled"
else
print_error "Failed to disable Nginx site"
fi
((STEP++))
fi
# Remove Nginx configuration
if $EXISTS_NGINX_CONF; then
print_step "${STEP}. Removing Nginx configuration..."
if sudo rm -f "${NGINX_CONF}"; then
print_info "Nginx configuration removed"
else
print_error "Failed to remove Nginx configuration"
fi
((STEP++))
fi
# Test and reload Nginx if any Nginx changes were made
if $EXISTS_NGINX_CONF || $EXISTS_NGINX_ENABLED; then
print_step "${STEP}. Testing Nginx configuration..."
if sudo nginx -t; then
print_info "Nginx configuration is valid"
print_step "$((STEP + 1)). Reloading Nginx..."
if sudo systemctl reload nginx; then
print_info "Nginx reloaded successfully"
else
print_warning "Failed to reload Nginx. You may need to reload it manually."
fi
else
print_warning "Nginx configuration test failed. You may need to check Nginx manually."
fi
((STEP+=2))
fi
# Remove symlink
if $EXISTS_SYMLINK; then
print_step "${STEP}. Removing HTML symlink..."
if rm -f "${HTML_SYMLINK}"; then
print_info "HTML symlink removed: ${HTML_SYMLINK}"
else
print_error "Failed to remove HTML symlink"
fi
((STEP++))
fi
# Remove work tree
if $EXISTS_WORK; then
print_step "${STEP}. Removing work tree..."
if sudo rm -rf "${WORK_TREE}"; then
print_info "Work tree removed: ${WORK_TREE}"
else
print_error "Failed to remove work tree"
fi
((STEP++))
fi
# Remove Git repository
if $EXISTS_GIT; then
print_step "${STEP}. Removing Git repository..."
if rm -rf "${GIT_DIR}"; then
print_info "Git repository removed: ${GIT_DIR}"
else
print_error "Failed to remove Git repository"
fi
((STEP++))
fi
# Remove setup info file
if $EXISTS_INFO; then
print_step "${STEP}. Removing setup info file..."
if rm -f "${INFO_FILE}"; then
print_info "Setup info file removed: ${INFO_FILE}"
else
print_error "Failed to remove setup info file"
fi
((STEP++))
fi
# Check for SSL certificates
echo ""
print_info "Checking for SSL certificates..."
if sudo certbot certificates 2>/dev/null | grep -q "${DOMAIN_NAME}"; then
print_warning "SSL certificate found for ${DOMAIN_NAME}"
echo ""
read -p "Do you want to delete the SSL certificate? (y/N): " -n 1 -r
echo
if [[ $REPLY =~ ^[Yy]$ ]]; then
print_step "${STEP}. Removing SSL certificate..."
if sudo certbot delete --cert-name "${DOMAIN_NAME}"; then
print_info "SSL certificate removed"
else
print_warning "Failed to remove SSL certificate. You may need to remove it manually:"
echo " sudo certbot delete --cert-name ${DOMAIN_NAME}"
fi
else
print_info "SSL certificate kept. You can remove it later with:"
echo " sudo certbot delete --cert-name ${DOMAIN_NAME}"
fi
else
print_info "No SSL certificate found for ${DOMAIN_NAME}"
fi
echo ""
echo "=========================================="
print_info "✓ Project Deletion Complete!"
echo "=========================================="
echo ""
print_info "The following have been removed:"
echo ""
if $EXISTS_GIT; then
echo " • Git repository: ${GIT_DIR}"
fi
if $EXISTS_WORK; then
echo " • Work tree: ${WORK_TREE}"
fi
if $EXISTS_SYMLINK; then
echo " • HTML symlink: ${HTML_SYMLINK}"
fi
if $EXISTS_NGINX_CONF; then
echo " • Nginx configuration: ${NGINX_CONF}"
fi
if $EXISTS_NGINX_ENABLED; then
echo " • Nginx enabled link: ${NGINX_ENABLED}"
fi
if $EXISTS_INFO; then
echo " • Setup info file: ${INFO_FILE}"
fi
echo ""
print_warning "Remember to remove the Git remote from your local machine:"
echo -e " ${BLUE}git remote remove dev-server${NC}"
echo ""