-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathrun
482 lines (393 loc) · 13 KB
/
run
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
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
#!/bin/bash
#======================================================================
# Performs the deployment and configuration of the server to hosting.
#
# USAGE:
# run [--debug] [--version] [--help] [--nocolor] [--tested]
# [--log=file path] [--lang=en|ru|...]
#
# NAME:
# HTAN Installer
#
# PROJECT:
# Hosting tools by Aleksey Nemiro (HTAN)
#
# REQUIREMENTS:
# Debian 8, HTAN Tools, HTAN Installers
#
# AUTHOR:
# Aleksey Nemiro
# http://aleksey.nemiro.ru/
# https://github.com/alekseynemiro/
#
# VERSION:
# 1.0.103
#
# CREATED:
# 14.09.2015
#
# REVISION:
# 03.07.2016
#
# COPYRIGHT:
# © Aleksey Nemiro, 2015-2016. All rights reserved.
#
# LICENSE:
# Apache License Version 2.0
# http://www.apache.org/licenses/LICENSE-2.0
#======================================================================
GetHTANRealPath()
{
if [[ -L "$1" ]]; then
# is symbolic link, get parent
echo "$(readlink "$1")"
else
echo "$1"
fi
}
# Base path
readonly HTAN_BASE_PATH="$(cd "$(dirname "$(GetHTANRealPath $0)")" && pwd)"
declare HTAN_STARTED=false
declare HTAN_START_FROM
# Installers to run
declare -a HTAN_TASK_RUN=(\
sudo etckeeper xinetd ssh mc ntp
postfix nginx apache2
php mono vsftpd
mysql phpmyadmin postgresql phppgadmin
svn fail2ban iptables rcs adduser nmap
ssa htan-runner sysstat
)
# Include tools
source "$HTAN_BASE_PATH/tools/common"
source "$HTAN_BASE_PATH/tools/config"
source "$HTAN_BASE_PATH/tools/package"
source "$HTAN_BASE_PATH/tools/service"
source "$HTAN_BASE_PATH/tools/web"
# Custom installers list
if [[ -n "${HTAN_PARSE_ARGS_RESULT[install]}" ]]; then
HTAN_TASK_RUN=()
SetIFS ','
read -r -a HTAN_TASK_RUN <<< "${HTAN_PARSE_ARGS_RESULT[install]}"
ResetIFS
# sudo required
if ! echo "${HTAN_TASK_RUN[@]}" | grep -q -w "$sudo$" && ! PackageInstalled sudo; then
HTAN_TASK_RUN=(sudo "${HTAN_TASK_RUN[@]}")
fi
fi
if [[ -n "${HTAN_PARSE_ARGS_RESULT[start-from]}" ]]; then
HTAN_START_FROM="${HTAN_PARSE_ARGS_RESULT[start-from]}"
fi
#======================================================================
# Displays text of introduction.
#
# ARGUMENTS:
# None
#
# RETURNS:
# None
#======================================================================
function Welcome()
{
Debug "Welcome"
Line =
Message "$(gettext "Hi %s!")" "$USER"
Message "$(gettext "This script will help to configure the server for web hosting.")"
Line =
# Parse files of installers and generate installation details
local -A run_info
local -A run_info_optional
local run_installer_name run_installer_cat run_installer_optional
local run_installer_htan_key run_installer_htan_value
local run_no_comment=0 run_installer_htan_section
for run_installer in "${HTAN_TASK_RUN[@]}"; do
if [[ ! -f "$HTAN_BASE_PATH/installers/$run_installer" ]]; then
Debug " %s not found." "$run_installer"
Message "$(gettext "File %s was not found.")" "$HTAN_BASE_PATH/installers/$run_installer" -s="$DEF_STYLE_ERROR"
continue
fi
run_no_comment=0
run_installer_htan_section=false
run_installer_name=""
run_installer_cat=""
run_installer_optional=false
Debug " Parse %s …" "$run_installer"
# read line
SetIFS ':'
while read -r run_installer_htan_key run_installer_htan_value; do
# checking key
if [[ -z "${run_installer_htan_key:0:1}" || "${run_installer_htan_key:0:1}" != "#" ]]; then
((run_no_comment++))
if (( run_no_comment > 5 )); then
Debug " block comments ended."
break;
else
continue
fi
fi
run_installer_htan_key="${run_installer_htan_key#"${run_installer_htan_key%%[!#|[:space:]]*}"}"
if [[ "${run_installer_htan_key:0:5}" != "HTAN_" ]]; then
if [[ $run_installer_htan_section == true && ! -z "$run_installer_htan_key" ]]; then
Debug " section ended."
break
else
continue
fi
fi
run_installer_htan_value="$(Trim "$run_installer_htan_value")"
if [[ -z "$run_installer_htan_value" ]]; then
read -r run_installer_htan_value
run_installer_htan_value="${run_installer_htan_value#"${run_installer_htan_value%%[!#|[:space:]]*}"}"
fi
Debug " %s = %s" "$run_installer_htan_key" "$run_installer_htan_value"
run_installer_htan_section=true
if [[ "$run_installer_htan_key" == "HTAN_NAME" ]]; then
run_installer_name="$run_installer_htan_value"
elif [[ "$run_installer_htan_key" == "HTAN_CATEGORY" ]]; then
run_installer_cat="$run_installer_htan_value"
elif [[ "$run_installer_htan_key" == "HTAN_OPTIONAL" ]]; then
run_installer_optional="$run_installer_htan_value"
fi
done < "$HTAN_BASE_PATH/installers/$run_installer"
if [[ -z "$run_installer_name" ]]; then
run_installer_name="$run_installer"
fi
if [[ -z "$run_installer_cat" ]]; then
run_installer_cat="General"
fi
if [[ "$run_installer_cat" != "Configuration" ]]; then
run_installer_cat="$(gettext "$run_installer_cat"; echo)"
fi
run_installer_name="$(gettext "$run_installer_name"; echo)"
# if [[ "$run_installer_optional" == true ]]; then
# run_installer_name="$run_installer_name [optional]"
# fi
if $HTAN_INSTALL_CONFIRMED; then
# without optional
run_installer_optional=false
fi
if [[ "$run_installer_optional" == true ]]; then
if [[ -z "${run_info_optional[$run_installer_cat]}" ]]; then
run_info_optional[$run_installer_cat]="$run_installer_name"
else
run_info_optional[$run_installer_cat]+=$'\n'
run_info_optional[$run_installer_cat]+="$run_installer_name"
fi
else
if [[ -z "${run_info[$run_installer_cat]}" ]]; then
run_info[$run_installer_cat]="$run_installer_name"
else
run_info[$run_installer_cat]+=$'\n'
run_info[$run_installer_cat]+="$run_installer_name"
fi
fi
done
local run_details="" ri_items=() ri_item=""
# Output details
if [[ -n "${run_info[@]}" ]]; then
run_details+="$(Message "$(gettext "To this server will be installed the following components:")")\n"
run_details+="$(Line -)"
for ri_k in "${!run_info[@]}"; do
if [[ "$ri_k" == "Configuration" ]]; then
continue
fi
run_details+="$(Message "* $ri_k")\n"
SetIFS
while read -r ri_item; do
run_details+="$(Message " - $ri_item")\n"
done <<< "${run_info[$ri_k]}"
ResetIFS
done
# PressAnyKey
fi
if [[ -n "${run_info_optional[@]}" ]]; then
if [[ -n "${run_info[@]}" ]]; then
run_details+="$(Line -)"
fi
run_details+="$(Message "$(gettext "Optionally can be installed the following components:")")\n"
run_details+="$(Line -)"
for ri_k in "${!run_info_optional[@]}"; do
if [[ "$ri_k" == "Configuration" ]]; then
continue
fi
run_details+="$(Message "* $ri_k")\n"
SetIFS
while read -r ri_item; do
run_details+="$(Message " - $ri_item")\n"
done <<< "${run_info_optional[$ri_k]}"
ResetIFS
done
# PressAnyKey
fi
if [[ -n "${run_info[Configuration]}" ]]; then
if [[ -n "${run_info[@]}" || -n "${run_info_optional[@]}" ]]; then
run_details+="$(Line -)"
fi
run_details+="$(Message "$(gettext "Will be configured the following components:")")\n"
run_details+="$(Line -)"
run_details+="$(Message "* $(gettext "System configuration")")\n"
SetIFS
while read -r ri_item; do
run_details+="$(Message " - $ri_item")\n"
done <<< "${run_info[Configuration]}"
ResetIFS
# PressAnyKey
fi
# run_details+="$(Line =)"
printf "$run_details" | less -RXFE --prompt="$(gettext "HELP -- Press RETURN for more, or Q for skip")"
# small pause
read -t 0.5 -n 1 key > /dev/null
Line =
if ! Confirm "$(gettext "Do you want to continue?")" -yes; then
Line
Message "$(gettext "Goodbye!")"
Line "~"
exit 0
fi
}
if [[ "$HTAN_RESTARTED" != "true" ]]; then
Welcome
fi
unset HTAN_RESTARTED
Line
Message "$(gettext "Let's go!")"
Line
# Get server ip
GetServerIP
Line
# Check status
declare HTAN_CONTINUE=false HTAN_LAST_INSTALLER=""
if [[ -f "$HTAN_BASE_PATH/.htan-processing" ]]; then
Message "$(gettext "Probably the previous installation process was not completed or was completed incorrectly.\nPress Y, to continue where it left off.\nPress N, to start the installation again.")"
Line
if Confirm "$(gettext "Do you want to continue the previous session?")" -yes; then
HTAN_CONTINUE=true
HTAN_LAST_INSTALLER="$(<$HTAN_BASE_PATH/.htan-processing)"
else
Line
fi
fi
if [[ "$HTAN_CONTINUE" == false ]]; then
# Get server info
si_ram_size_kb=$(cat /proc/meminfo | grep MemTotal: | sed -r "s/MemTotal:\s+([0-9]+)\s*kB/\1/")
si_ram_size_mb=$((si_ram_size_kb / 1000))
si_cpu_name="$(Trim "$(cat /proc/cpuinfo | grep "model name" | cut -f2 -d':')")"
si_hdd_size="$(df --total -h | tail -n 1 | sed -E 's/total *([^ ]*).*/\1/')"
# Create server info file
ServerInfo "$(cat /etc/*release | grep "^PRETTY_NAME=*" | sed -r 's/PRETTY_NAME="(.+)"/\1/')"
ServerInfo "\n"
ServerInfo "--------------------------------------------\n"
ServerInfo "$(gettext "Hostname"): $HOSTNAME\n"
ServerInfo "$(gettext "Server IP"): $HTAN_SERVER_IP\n\n"
ServerInfo "$(gettext "CPU"): $si_cpu_name\n"
ServerInfo "$(gettext "RAM"): $si_ram_size_mb MB\n"
ServerInfo "$(gettext "HDD"): $si_hdd_size\n"
ServerInfo "--------------------------------------------\n\n"
# Update packages
PackageUpdate
Line
PackageUpgradeAll
fi
# Remove CD-ROM entry from the sources.list
sed -i '/cdrom/d' /etc/apt/sources.list
# Include and run installers
for run_installer in "${HTAN_TASK_RUN[@]}"; do
if [[ -n "$HTAN_START_FROM" && "$HTAN_STARTED" == false && "$HTAN_START_FROM" != "$run_installer" ]]; then
continue
fi
HTAN_STARTED=true
if [[ "$HTAN_CONTINUE" == true && "$run_installer" != "$HTAN_LAST_INSTALLER" ]]; then
continue
elif [[ "$HTAN_CONTINUE" == true && "$run_installer" == "$HTAN_LAST_INSTALLER" ]]; then
HTAN_CONTINUE=false
# skip, because it is completed
continue
fi
if [[ ! -f "$HTAN_BASE_PATH/installers/$run_installer" ]]; then
continue
fi
source "$HTAN_BASE_PATH/installers/$run_installer"
printf "%s" "$run_installer" > "$HTAN_BASE_PATH/.htan-processing"
done
# Delete session info
if [[ -f "$HTAN_BASE_PATH/.htan-processing" ]]; then
rm "$HTAN_BASE_PATH/.htan-processing"
fi
Line
# Commit changes
if PackageInstalled etckeeper; then
etckeeper_changes=$(sudo bash -c 'cd /etc; git status --porcelain --untracked-files=no | wc -l')
if (( etckeeper_changes > 0 )); then
c="sudo bash -c 'cd /etc; etckeeper commit \"HTAN commit\"' >> $HTAN_LOG 2>&1"
Execute -c="$c" -m="Commit changes …" -l -sp="$DEF_STYLE_ACTION_PROC" -sc="$DEF_STYLE_ACTION_COMP"
Line
fi
fi
# Add installed packages list to the server info file
if [[ -n "$HTAN_INSTALLED" ]]; then
ServerInfo "$(gettext "Installed packages:")\n"
ServerInfo "--------------------------------------------\n"
for program_info in "${HTAN_INSTALLED[@]}"; do
if [[ -z "$program_info" ]]; then
continue
fi
ServerInfo "* $program_info\n"
done
ServerInfo "--------------------------------------------\n\n"
fi
ServerInfo "Domain registration:\nhttp://www.reg.ru/?rid=76963"
ServerInfo "\nVDS hosting:\nhttps://en.ihor.ru/vds?from=112887"
ServerInfo "\n\n-- \nCreated by HTAN v$HTAN_VERSION, $(date +'%d.%m.%Y')"
ServerInfo "\nhttp://www.adminstock.net/"
# Настройка сервера завершена!
Message "$(gettext "The server is configured!")" -s="$STYLE_LGREEN"
Line
# Send server info by email
if [[ -f "$HTAN_SERVER_INFO" ]]; then
Message "$(gettext "Information to access the components of the server can be found in the file: %s.")" "$HTAN_SERVER_INFO"
if PackageInstalled postfix; then
Line
if Confirm "$(gettext "Do you want to send the file by email?")" -yes; then
Line
if EnterEmail --optional; then
if [[ -f "$HTAN_LOG.gz" ]]; then
sudo rm "$HTAN_LOG.gz" >> $HTAN_LOG 2>&1
fi
sudo gzip -k $HTAN_LOG >> $HTAN_LOG 2>&1
Line
Message "$(gettext "Sending a message …")" -n
{
(echo "$(cat $HTAN_SERVER_INFO)" | mail -s "Details of server $HTAN_SERVER_IP" -t "$HTAN_ENTERED_EMAIL" -a "Content-Type: text/plain; charset=UTF-8" -A "$HTAN_LOG.gz" >> $HTAN_LOG 2>&1; wait) && OK
} ||
{
Fail
}
if [[ -f "$HTAN_LOG.gz" ]]; then
sudo rm "$HTAN_LOG.gz" >> $HTAN_LOG 2>&1
fi
fi
fi
fi
Line -
fi
# Show log
if [[ -f $HTAN_LOG ]]; then
if Confirm "$(gettext "Do you want to see the log?")" -no; then
nano $HTAN_LOG # | less -RXFE --prompt="$(gettext "HELP -- Press RETURN for more, or Q for exit")"
wait
fi
Line
fi
Message "$(gettext "Recommended to reboot the server.")"
Line
if Confirm "$(gettext "Do you want to reboot now?")" -yes; then
Line
Message "$(gettext "Goodbye!")"
Line "~"
sudo shutdown -r now
else
Line "~"
fi
exit 0