-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsetup.sh
More file actions
366 lines (324 loc) · 11.4 KB
/
setup.sh
File metadata and controls
366 lines (324 loc) · 11.4 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
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
#!/bin/bash
# Main setup script
# Collects user inputs, saves to config file, and manages execution of steps
CONFIG_FILE="$(dirname "$0")/server-config.env"
STEP_LOG_FILE="$(dirname "$0")/completed-steps.log"
# Default values
MAINTENANCE_USER="bealers" # has sudo access, you'll login with this user day to day
SITE_DOMAIN="bealers.com"
EMAIL="working-email-for-your-ssl-cert@example.com"
PHP_VERSION="8.4"
REPO_URL="https://github.com/your/repo.git" # private repo is ok here
DB_TYPE="sqlite" # mysql, pgsql, sqlite
# TODO, streamline, remove the need for me to set these
REPO_ACCESS_TYPE="https" # ssh, https
USE_DEPLOY_KEY=false
DEPLOY_KEY_PATH=""
# Initialize step tracking
if [ ! -f "$STEP_LOG_FILE" ]; then
touch "$STEP_LOG_FILE"
fi
# Function to check if a step is completed
is_step_completed() {
grep -q "^$1:completed$" "$STEP_LOG_FILE" 2>/dev/null
return $?
}
# Function to mark a step as completed
mark_step_completed() {
if ! is_step_completed "$1"; then
echo "$1:completed" >> "$STEP_LOG_FILE"
fi
}
# Function to load configuration from file
load_config() {
if [ -f "$CONFIG_FILE" ]; then
echo "Loading configuration from $CONFIG_FILE"
source "$CONFIG_FILE"
return 0
else
echo "No configuration file found. Will create a new one."
return 1
fi
}
# Function to save configuration to file
save_config() {
echo "Saving configuration to $CONFIG_FILE"
cat > "$CONFIG_FILE" << EOF
# Server Configuration - Generated on $(date)
MAINTENANCE_USER="$MAINTENANCE_USER"
SITE_DOMAIN="$SITE_DOMAIN"
EMAIL="$EMAIL"
PHP_VERSION="$PHP_VERSION"
REPO_URL="$REPO_URL"
DB_TYPE="$DB_TYPE"
REPO_ACCESS_TYPE="$REPO_ACCESS_TYPE"
USE_DEPLOY_KEY="$USE_DEPLOY_KEY"
DEPLOY_KEY_PATH="$DEPLOY_KEY_PATH"
EOF
}
# UI, annoying and britle, we can do better
gather_inputs() {
echo "==== Server Setup Configuration ===="
read -p "Enter maintenance username (default: $MAINTENANCE_USER): " input
MAINTENANCE_USER=${input:-$MAINTENANCE_USER}
read -p "Enter site domain (default: $SITE_DOMAIN): " input
SITE_DOMAIN=${input:-$SITE_DOMAIN}
read -p "Enter email for SSL certificates (default: $EMAIL): " input
EMAIL=${input:-$EMAIL}
read -p "Enter PHP version (default: $PHP_VERSION): " input
PHP_VERSION=${input:-$PHP_VERSION}
# Repository configuration
read -p "Enter Git repository URL (default: $REPO_URL): " input
REPO_URL=${input:-$REPO_URL}
# Ask about repository access method
echo "How should we access the Git repository?"
echo "1) HTTPS (default - may require credentials for private repos)"
echo "2) SSH (uses SSH keys for authentication)"
read -p "Enter choice [1-2]: " repo_choice
if [[ $repo_choice == "2" ]]; then
REPO_ACCESS_TYPE="ssh"
# Convert HTTPS URL to SSH format if needed
if [[ $REPO_URL == https://github.com/* ]]; then
REPO_URL=${REPO_URL#https://github.com/}
REPO_URL="git@github.com:${REPO_URL}"
fi
# Ask about deploy key
echo "Do you want to use a deploy key for the www-data user?"
echo "1) No - Use maintenance user's SSH key (default)"
echo "2) Yes - Use a dedicated deploy key"
read -p "Enter choice [1-2]: " key_choice
if [[ $key_choice == "2" ]]; then
USE_DEPLOY_KEY=true
echo "Choose deploy key option:"
echo "1) Generate a new deploy key"
echo "2) Provide an existing deploy key"
read -p "Enter choice [1-2]: " deploy_key_choice
if [[ $deploy_key_choice == "2" ]]; then
read -p "Enter path to existing deploy key (private key): " DEPLOY_KEY_PATH
fi
fi
fi
echo "Select database type:"
echo "1) MySQL (default)"
echo "2) PostgreSQL"
echo "3) SQLite"
read -p "Enter choice [1-3]: " db_choice
case $db_choice in
2) DB_TYPE="pgsql" ;;
3) DB_TYPE="sqlite" ;;
*) DB_TYPE="mysql" ;;
esac
# Save the configuration
save_config
# Display summary
echo -e "\n==== Configuration Summary ===="
echo "Maintenance User: $MAINTENANCE_USER"
echo "Site Domain: $SITE_DOMAIN"
echo "Email: $EMAIL"
echo "PHP Version: $PHP_VERSION"
echo "Repository: $REPO_URL"
echo "Repository Access: $REPO_ACCESS_TYPE"
echo "Use Deploy Key: $USE_DEPLOY_KEY"
if [ "$USE_DEPLOY_KEY" = true ] && [ -n "$DEPLOY_KEY_PATH" ]; then
echo "Deploy Key Path: $DEPLOY_KEY_PATH"
fi
echo "Database Type: $DB_TYPE"
echo "=============================="
read -p "Proceed with this configuration? (Y/n): " confirm
if [[ $confirm == "n" || $confirm == "N" ]]; then
echo "Setup aborted."
exit 1
fi
}
# Function to run a specific step
run_step() {
step_number="$1"
step_name="$2"
step_script="$(dirname "$0")/steps/$step_number-$step_name.sh"
if [ ! -f "$step_script" ]; then
echo "Error: Step script not found: $step_script"
return 1
fi
if is_step_completed "$step_number-$step_name"; then
echo "Step $step_number: $step_name - Already completed, skipping."
return 0
fi
echo "==== Running Step $step_number: $step_name ===="
chmod +x "$step_script"
# Export all config variables for the script
export MAINTENANCE_USER
export SITE_DOMAIN
export EMAIL
export PHP_VERSION
export REPO_URL
export DB_TYPE
export REPO_ACCESS_TYPE
export USE_DEPLOY_KEY
export DEPLOY_KEY_PATH
# Run the step script
"$step_script"
step_exit_code=$?
if [ $step_exit_code -eq 0 ]; then
echo "Step $step_number: $step_name - Completed successfully."
mark_step_completed "$step_number-$step_name"
return 0
else
echo "Step $step_number: $step_name - Failed with exit code $step_exit_code."
return $step_exit_code
fi
}
# Function to run all steps
run_all_steps() {
# Define all steps in order
run_step "01" "system-basics" || return $?
run_step "02" "security-baseline" || return $?
run_step "03" "web-stack" || return $?
run_step "04" "application-deploy" || return $?
run_step "05" "advanced-hardening" || return $?
echo "All steps completed successfully!"
return 0
}
# Function to show usage help
show_help() {
echo "Usage: $0 [options] [step]"
echo ""
echo "Options:"
echo " --help Show this help message"
echo " --reconfigure Run configuration wizard again"
echo " --list-steps List all available steps"
echo " --run-from STEP Run from a specific step onwards"
echo " --reset Reset completion status for all steps"
echo " --reset-step STEP Reset completion status for a specific step"
echo ""
echo "Steps:"
echo " 01-system-basics Set up users, SSH, timezone, and locale"
echo " 02-security-baseline Configure firewall and basic security"
echo " 03-web-stack Install and configure web server, PHP, database"
echo " 04-application-deploy Deploy application code and dependencies"
echo " 05-advanced-hardening Additional security measures and hardening"
echo ""
echo "Examples:"
echo " $0 Run all steps (skipping completed ones)"
echo " $0 03-web-stack Run only the web-stack step"
echo " $0 --reset Reset all steps and start fresh"
}
# Function to list available steps
list_steps() {
echo "Available steps:"
echo " 01-system-basics - Set up users, SSH, timezone, and locale"
echo " 02-security-baseline - Configure firewall and basic security"
echo " 03-web-stack - Install and configure web server, PHP, database"
echo " 04-application-deploy - Deploy application code and dependencies"
echo " 05-advanced-hardening - Additional security measures and hardening"
}
# Function to reset completion status for a specific step
reset_step() {
step="$1"
if [ -f "$STEP_LOG_FILE" ]; then
sed -i "/^$step:completed$/d" "$STEP_LOG_FILE"
echo "Reset completion status for step: $step"
fi
}
# Function to reset all steps
reset_all_steps() {
if [ -f "$STEP_LOG_FILE" ]; then
rm "$STEP_LOG_FILE"
touch "$STEP_LOG_FILE"
echo "Reset completion status for all steps."
fi
}
# Main script execution starts here
# Process command-line arguments
if [ $# -gt 0 ]; then
case "$1" in
--help)
show_help
exit 0
;;
--reconfigure)
gather_inputs
;;
--list-steps)
list_steps
exit 0
;;
--run-from)
if [ -z "$2" ]; then
echo "Error: Missing step name after --run-from"
show_help
exit 1
fi
# Reset all steps from the specified one onwards
case "$2" in
01-system-basics)
reset_step "01-system-basics"
reset_step "02-security-baseline"
reset_step "03-web-stack"
reset_step "04-application-deploy"
reset_step "05-advanced-hardening"
;;
02-security-baseline)
reset_step "02-security-baseline"
reset_step "03-web-stack"
reset_step "04-application-deploy"
reset_step "05-advanced-hardening"
;;
03-web-stack)
reset_step "03-web-stack"
reset_step "04-application-deploy"
reset_step "05-advanced-hardening"
;;
04-application-deploy)
reset_step "04-application-deploy"
reset_step "05-advanced-hardening"
;;
05-advanced-hardening)
reset_step "05-advanced-hardening"
;;
*)
echo "Error: Unknown step: $2"
list_steps
exit 1
;;
esac
# Continue with running all steps (skipping completed ones)
;;
--reset)
reset_all_steps
;;
--reset-step)
if [ -z "$2" ]; then
echo "Error: Missing step name after --reset-step"
show_help
exit 1
fi
reset_step "$2"
exit 0
;;
*)
# Check if the argument is a valid step
case "$1" in
01-system-basics|02-security-baseline|03-web-stack|04-application-deploy|05-advanced-hardening)
load_config
step_name="${1#*-}" # Remove the number prefix
step_number="${1%-*}" # Keep only the number
run_step "$step_number" "$step_name"
exit $?
;;
*)
echo "Error: Unknown option or step: $1"
show_help
exit 1
;;
esac
;;
esac
fi
# Default execution path (no or unrecognized arguments)
if ! load_config; then
# No configuration file found, so gather inputs
gather_inputs
fi
# Run all steps (skipping completed ones)
run_all_steps
exit $?