-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsetup_stack.sh
More file actions
executable file
·256 lines (201 loc) · 6.35 KB
/
setup_stack.sh
File metadata and controls
executable file
·256 lines (201 loc) · 6.35 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
#!/usr/bin/env bash
set -euo pipefail
if [[ "${EUID}" -ne 0 ]]; then
echo "❌ Please run as root (sudo -s; ./setup_stack.sh)" >&2;
# exit 1
fi
# Defaults
LOG_FILE="/var/log/setup_stack.log"
# Default components
COMPONENTS=""
print_help() {
cat <<'USAGE'
Usage: ./setup_stack.sh [options]
Options:
--php=8.2|8.3|8.4 PHP version (Ondřej PPA)
--mongo=skip|6.0|7.0 MongoDB version (official repo) or skip
--nginx=distro|stable|mainline
--components=kernel,php,mysql,nginx,redis,rabbitmq,grafana,composer,nodejs
--log-file=/path/to.log Path for combined console+file log
--yes Auto-confirm all blocks
--help Show this help
Examples:
./setup_stack.sh --components=kernel,php,mysql,nginx,redis,rabbitmq,grafana,composer,nodejs --yes
USAGE
}
# ── CLI-парсер
for arg in "$@"; do
case "$arg" in
--php=*) PHP_VERSION="${arg#*=}";;
--mongo=*) MONGO_VERSION="${arg#*=}";;
--nginx=*) NGINX_CHANNEL="${arg#*=}";;
--components=*) COMPONENTS="${arg#*=}";;
--log-file=*) LOG_FILE="${arg#*=}";;
--help|-h) print_help; exit 0;;
*) echo "⚠️ Unknown option: $arg"; print_help; exit 2;;
esac
done
# ── Логування
mkdir -p "$(dirname "$LOG_FILE")"
exec > >(tee -a "$LOG_FILE") 2>&1
echo "🗒 Logging to: $LOG_FILE"
# ── Виконання однієї команди (без [dry-run])
run_sh() {
local cmd="$*"
echo "▶️ $cmd"
local start_ts end_ts rc
start_ts=$(date +%s)
bash -lc "$cmd"
rc=$?
end_ts=$(date +%s)
echo " ⏱ $(( end_ts - start_ts ))s, exit=$rc"
return $rc
}
confirm_and_run_block() {
local component="${1-}"
local title="${2-}"
: "${title:?title is required}"
# macOS-safe init
local -a cmds
cmds=()
local _line=""
# читаємо heredoc зі stdin (команди блоку)
while IFS= read -r _line; do
[[ -z "$_line" ]] && continue
[[ "$_line" =~ ^[[:space:]]*# ]] && continue
cmds+=("$_line")
done
echo
echo "────────────────────────────────────────────────────────"
echo "🧪 BLOCK (dry-run): ${title}"
echo "────────────────────────────────────────────────────────"
if ((${#cmds[@]})); then
for c in "${cmds[@]}"; do
echo "🧩 [dry-run] $c"
done
else
echo "🧩 [dry-run] (no commands)"
fi
echo
echo "To run commands type 'y' and press Enter."
# запит підтвердження з /dev/tty, щоб не конфліктувати з heredoc
local confirm
read -r confirm < /dev/tty
confirm=$(printf '%s' "${confirm}" | tr '[:upper:]' '[:lower:]')
if [[ "$confirm" != "y" && "$confirm" != "yes" ]]; then
echo "⏭️ Пропущено: ${title}"
echo
return 0
fi
echo "🚀 Виконую блок: ${title}"
echo
local rc=0
for c in "${cmds[@]}"; do
if ! run_sh "$c"; then
rc=$?
echo "❌ Помилка у блоці: ${title} (команда: $c)"
return $rc
fi
done
echo "✅ Готово: ${title}"
echo
}
# ── Обробка компонетів
_components_raw="${COMPONENTS:-}"
_components_norm=$(printf '%s' "${_components_raw}" | tr '[:upper:]' '[:lower:]' | tr -d ' ')
IFS=',' read -r -a SELECTED_COMPONENTS <<< "${_components_norm}"
# якщо нічого не вибрано → нічого не запускати
should_run() {
local comp="$1"
[[ ${#SELECTED_COMPONENTS[@]} -eq 0 ]] && return 1
local item
for item in "${SELECTED_COMPONENTS[@]}"; do
[[ "$item" == "$comp" ]] && return 0
done
return 1
}
if should_run kernel; then
confirm_and_run_block "kernel" "Basic update and basic applications" <<'CMDS'
apt update -y
apt install -y curl wget gnupg2 ca-certificates software-properties-common apt-transport-https lsb-release
apt install -y vim git mc
apt autoremove
CMDS
fi
if should_run php; then
confirm_and_run_block "php" "Install PHP ${PHP_VERSION}" <<CMDS
add-apt-repository -y ppa:ondrej/php
apt update -y
apt install -y php${PHP_VERSION}
apt install -y php${PHP_VERSION}-fpm
apt install -y php${PHP_VERSION}-cli
apt install -y php${PHP_VERSION}-common
apt install -y php${PHP_VERSION}-zip
apt install -y php${PHP_VERSION}-mbstring
apt install -y php${PHP_VERSION}-xml
apt install -y php${PHP_VERSION}-curl
apt install -y php${PHP_VERSION}-intl
apt install -y php${PHP_VERSION}-gd
apt install -y php${PHP_VERSION}-mysql
apt install -y php${PHP_VERSION}-amqp
apt install -y php${PHP_VERSION}-bcmath
apt install -y php${PHP_VERSION}-dom
apt install -y php${PHP_VERSION}-soap
php -v
CMDS
fi
if should_run composer; then
confirm_and_run_block "composer" "Installation of the Composer" <<'CMDS'
apt install unzip
cd ~
curl -sS https://getcomposer.org/installer -o /tmp/composer-setup.php
php /tmp/composer-setup.php --install-dir=/usr/local/bin --filename=composer
composer --version
CMDS
fi
if should_run nginx; then
confirm_and_run_block "nginx" "Installation and run of Nginx" <<'CMDS'
apt update -y
apt install -y nginx
systemctl enable nginx
systemctl start nginx
nginx -t
CMDS
fi
if should_run nodejs; then
confirm_and_run_block "nodejs" "Installation of nodejs 20.00" <<'CMDS'
curl -sL https://deb.nodesource.com/setup_20.x -o nodesource_setup.sh
chmod +x nodesource_setup.sh
./nodesource_setup.sh
apt install nodejs
node -v
npm --version
CMDS
fi
if should_run certbot; then
confirm_and_run_block "certbot" "Installation of certbot for HTTPS" <<'CMDS'
snap install --classic certbot
CMDS
fi
if should_run mysql; then
confirm_and_run_block "mysql" "Installation of MySQL Server" <<'CMDS'
apt-get install -y mysql-server
systemctl enable mysql
systemctl start mysql
echo "To setup MySql security you may perform next:";
echo "";
echo "";
echo "";
echo "";
echo "";
CMDS
fi
if should_run "supervisor"; then
confirm_and_run_block "supervisor" "Installation of supervisor" <<'CMDS'
apt update
apt install supervisor
CMDS
fi
echo "🎉 DONE. See logs at: {$LOG_FILE}"
echo
exit 1