-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver
executable file
·195 lines (162 loc) · 4.75 KB
/
server
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
#!/bin/bash
set -euo pipefail
directory="$(dirname -- "$(readlink -f -- "$0" || greadlink -f -- "$0")")"
# Define colors
RED='\033[0;31m'
GREEN='\033[0;32m'
CYAN='\033[0;36m'
YELLOW='\033[1;33m'
NC='\033[0m' # No color
### Prevent "unbound array" issue
declare -A descriptions=()
declare -A options=()
declare -a positional=()
### ========== ARGUMENT PARSER ==========
parse_args() {
while [[ $# -gt 0 ]]; do
case "$1" in
--*=*) key="${1%%=*}"; val="${1#*=}"; options["$key"]="$val";;
--*) options["$1"]=true;;
-*) short="${1:1}"; options["-$short"]=true;;
*) positional+=("$1");;
esac
shift
done
}
### ========== DESCRIPTION PARSER ==========
get_description() {
local fn="cmd_$1"
local desc=""
if declare -f "$fn" >/dev/null; then
desc=$(declare -f "$fn" | grep -Eo '^#\s.*' | head -1 | sed 's/^# //' || true)
fi
# fallback gracefully, no "unbound" issue due to empty map
echo "${desc:-${descriptions[$1]-}}"
}
### ========== USAGE ==========
print_usage() {
printf "${CYAN}Usage:${NC}\n"
printf "$0 <command> [options]\n\n"
printf "${CYAN}Available Commands:${NC}\n"
for cmd in $(declare -F | awk '{print $3}' | grep '^cmd_' | sed 's/^cmd_//'); do
printf " ${YELLOW}%-15s${NC} %s\n" "$cmd" "$(get_description "$cmd")"
done
}
### ========== COMMAND DISPATCHER ==========
run_command() {
local command="${1,,}"
shift || true
if declare -f "cmd_${command}" >/dev/null; then
"cmd_${command}" "$@"
else
printf "${RED}Invalid command: %s${NC}\n" "$command"
print_usage
exit 1
fi
}
### ========== INIT ==========
init() {
source "$directory/docker/utilities/core"
source "$directory/docker/utilities/domain"
source "$directory/docker/utilities/profiles"
check_required_commands "docker" "readlink"
check_file_existence "/.env"
ensure_files_exist "/docker/.env" "/configuration/php/php.ini"
update_env "$directory/docker/.env" "WORKING_DIR" "$directory"
update_env "$directory/docker/.env" "USER" "$(id -un)"
}
### ========== COMMANDS ==========
# Start Docker services (foreground)
cmd_up() { docker_compose up; }
# Start Docker services (background) + reload HTTP
cmd_start() { docker_compose up -d; reload_http_containers; }
# Alias for start
cmd_reload() { cmd_start; }
# Stop Docker services
cmd_stop() { docker_compose down; }
# Alias for stop
cmd_down() { cmd_stop; }
# Stop & start Docker services
cmd_reboot() { cmd_stop; cmd_start; }
# Alias for reboot
cmd_restart() { cmd_reboot; }
# Rebuild Docker images with no cache
cmd_rebuild() { docker_compose down && docker_compose build --no-cache --pull "${positional[@]:1}"; }
# Validate and show docker-compose config
cmd_config() { docker_compose config; }
# Access SERVER_TOOLS container
cmd_tools() { docker exec -it SERVER_TOOLS bash; }
# Launch LazyDocker in SERVER_TOOLS
cmd_lzd() { docker exec -it SERVER_TOOLS lazydocker; }
cmd_lazydocker() { cmd_lzd; }
cmd_lazy-docker() { cmd_lzd; }
# HTTP container management (e.g., reload)
cmd_http() {
[[ "${positional[1]:-}" == "reload" ]] && reload_http_containers
}
# Access PHP container for a domain
cmd_core() {
if [[ -z "${positional[1]:-}" ]]; then
printf "${RED}Please provide a domain (e.g., $0 core test.com).${NC}\n"
exit 1
fi
launch_php_container "${positional[1]}"
}
# Setup tasks (permissions, domain, profiles, env)
cmd_setup() {
case "${positional[1]:-}" in
permissions)
assign_permissions
printf "${GREEN}Necessary Permissions have been assigned.${NC}\n"
;;
domain)
while true; do
configure_server
read -e -r -p "$(printf "${YELLOW}Do you want to configure another site? (y/n):${NC} ")" CONTINUE
[[ "$CONTINUE" != "y" ]] && break
done
printf "${GREEN}All configurations have been completed.${NC}\n"
;;
profiles)
prompt_common_vars
for service in "${!services_and_profiles[@]}"; do
process_service "$service"
done
printf "${GREEN}Setup completed.${NC}\n"
;;
env)
local env_file="$directory/.env"
[[ "${positional[2]:-}" == "docker" ]] && env_file="$directory/docker/.env"
process_env_file "$env_file"
;;
*)
printf "${RED}Invalid setup command: %s${NC}\n" "${positional[1]:-}"
;;
esac
}
# Install components (e.g., certificate)
cmd_install() {
case "${positional[1]:-}" in
certificate)
install_root_ca
;;
*)
printf "${RED}Invalid install command: %s${NC}\n" "${positional[1]:-}"
;;
esac
}
# Display this help message
cmd_help() { print_usage; }
### ========== MAIN ==========
main() {
if [ $# -eq 0 ]; then
printf "${RED}Error: No command provided.${NC}\n"
print_usage
exit 1
fi
parse_args "$@"
init
run_command "${positional[0]}"
unset directory
}
main "$@"