-
Notifications
You must be signed in to change notification settings - Fork 0
/
update.sh
485 lines (435 loc) · 12.3 KB
/
update.sh
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
483
484
485
#!/bin/bash
#set -u
# Usage text
usage_text=$(cat << EOF
Usage: `basename $0` -p <package> [-r] [-v version]...
Script to detect OS and update package by name.
Package is needed, all other keys are optional.
-p\tpackage name, can be anything, if version is not forced
-r\trestart all services after successful update
-v\tforce exact version, you need to provide correct package name for used OS
-o\tforce os (format is \$os\$major_version, i.e. Debian8, CentOS7, Ubuntu14, etc.)
-h\tdisplay that help
Other parameters are for technical needs, usage in other scripts, etc.
-c\tuse given CTID
-l\tcreate log file, minimal screen output provided
EOF
)
# Enables writing to log, minimal screen output
enable_logging()
{
if [ "$LOGGING" == "1" ]; then
if [ ${CTID} ]; then
LOG_FILE=/root/${CTID}-${PACKAGE}.log
else
LOG_FILE=/root/${PACKAGE}.log
fi
exec 1<&-
exec 1<>$LOG_FILE
exec 2>&1
fi
}
# Parsing arguments recieved
check_args()
{
if [ $# -eq 0 ]; then
echo -e "$usage_text"
exit 0
fi
while getopts "hlrRp:v:o:c:" opt; do
case $opt in
p )
PACKAGE=$OPTARG
;;
v )
FORCE_VERSION='1'
FORCED_VERSION=$OPTARG
;;
r )
RESTART_NEEDED='1'
;;
R )
ONLY_RESTART='1'
;;
l )
LOGGING='1'
;;
o )
FORCE_OS='1'
FORCED_OS=$OPTARG
;;
c )
CTID=$OPTARG
;;
h )
echo -e "$usage_text"
exit 0
;;
\? )
echo "Invalid option: -$OPTARG" >&2
exit 1
;;
: )
echo "Option -$OPTARG requires an argument." >&2
exit 1
;;
esac
done
}
# Exit function
finish()
{
local result=$1
case $result in
OK )
echo -e "RESULT: ${TXT_GRN}OK${TXT_RST}"; exit 0
;;
NOTOK )
echo -e "RESULT: ${TXT_RED}FAIL${TXT_RST}"; exit 1
;;
* )
echo -e "RESULT: ${TXT_YLW}UNKNOWN${TXT_RST}"; exit 2
;;
esac
}
# Check package manager
detect_package_manager()
{
local dpkg=""
local rpm=""
local dpkg=`which dpkg >/dev/null 2>&1; echo $?`
local rpm=`which rpm >/dev/null 2>&1; echo $?`
local result=`echo "$dpkg$rpm"`
case $result in
01 )
package_manager='dpkg'
;;
10 )
package_manager='rpm'
;;
00 )
echo 'You have both dpkg and rpm? Hello, Dr. Frankenstein!'
finish NOTOK
;;
11 )
echo "You don't have neither dpkg, nor rpm. We don't know, what to do here. Exiting."
finish NOTOK
;;
* )
echo "We couldn't detect package manager. Exiting"
finish NOTOK
;;
esac
}
# OS to package manager hash
verify_package_manager()
{
# Do not check package manager, if we need only restart services, mainly because of bash 4.0
if [ $ONLY_RESTART -eq 1 ]; then
return
fi
local os=$1
local -A os_to_pm_hash
os_to_pm_hash["Debian"]="dpkg"
os_to_pm_hash["Ubuntu"]="dpkg"
os_to_pm_hash["CentOS"]="rpm"
local os_name=`echo ${os} | tr -d [:digit:]`
if [ ${os_to_pm_hash[$os_name]-} ]; then
if [ ! "${os_to_pm_hash[$os_name]}" == "$package_manager" ]; then
echo "Your have $package_manager on ${os}. We can't do anything here. Exiting."
finish NOTOK
fi
else
echo "We don't know, what package manager is needed for your OS. You have $package_manager on ${os}. Exiting."
finish NOTOK
fi
}
# Detect OS
detect_os()
{
# Echo CTID if set
if [ $CTID ]; then
echo -e "CTID:\t$CTID"
fi
# Use forced OS if any
if [ $FORCE_OS -eq 1 ]; then
OS=$FORCED_OS
echo -e "Forced OS:\t$OS"
return
fi
local issue_file='/etc/issue'
local os_release_file='/etc/os-release'
local redhat_release_file='/etc/redhat-release'
# First of all, trying os-relese file
if [ -f $os_release_file ]; then
local name=`grep '^NAME=' $os_release_file | awk -F'[" ]' '{print $2}'`
local version=`grep '^VERSION_ID=' $os_release_file | awk -F'[". ]' '{print $2}'`
OS=`echo "${name}${version}"`
verify_package_manager $OS
echo -e "OS:\t${TXT_YLW}${OS}${TXT_RST}"
else
# If not, trying redhat-release file (mainly because of bitrix-env)
if [ -f $redhat_release_file ]; then
OS=`head -1 /etc/redhat-release | sed -re 's/([A-Za-z]+)[^0-9]*([0-9]+).*$/\1\2/'`
verify_package_manager $OS
echo -e "OS:\t${TXT_YLW}${OS}${TXT_RST}"
else
# Else, trying issue file
if [ -f $issue_file ]; then
OS=`head -1 $issue_file | sed -re 's/([A-Za-z]+)[^0-9]*([0-9]+).*$/\1\2/'`
verify_package_manager $OS
echo -e "OS:\t${TXT_YLW}${OS}${TXT_RST}"
else
# If none of that files worked, exit
echo -e "${TXT_RED}Cannot detect OS. Exiting now"'!'"${TXT_RST}"
finish NOTOK
fi
fi
fi
}
# Exit if bash is older then 4.0
check_bash_version()
{
local bash_version=`echo $BASH_VERSION| awk -F. '{print $1}'`
if [ $bash_version -lt 4 ]; then
echo -e "Old bash ($BASH_VERSION). 4.0 or newer needed."
finish NOTOK
fi
}
# Filling os -> safe_version hash
# You need to create file ${package}.list accecible via http on $download_url
# Format:
# OS:package_name:safe_version
# OS2:package_name2:safe_version2
#
# Example:
# File URL:
# http://domain.com/lists/glibc.list
#
# File contents:
# Debian8:libc6:2.19-18+deb8u3
# Debian7:libc6:2.13-38+deb7u10
# Debian6:libc6:2.11.3-4+deb6u11
# Ubuntu14:libc6:2.19-0ubuntu6.7
# Ubuntu12:libc6:2.15-0ubuntu10.13
# CentOS7:glibc:2.17-106.el7_2.4
# CentOS6:glibc:2.12-1.166.el6_7.7
#
find_safe_version()
{
local os=$1
local package=$2
# Use forced version if any
if [ $FORCE_VERSION -eq 1 ]; then
SAFE_VERSION=$FORCED_VERSION
PACKAGE_NAME=$package
return
fi
local -A safe_version_hash
local -A package_name_hash
local hash_os
local hash_pkg
local hash_version
local list=(`wget -q -O- ${download_url}${package}.list`)
for line in ${list[@]}; do
IFS=':' read hash_os hash_pkg hash_version <<< "$line"
package_name_hash["$hash_os"]="$hash_pkg"
safe_version_hash["$hash_os"]="$hash_version"
done
if [ ${package_name_hash[$os]-} ]; then
PACKAGE_NAME=${package_name_hash[$os]}
else
PACKAGE_NAME='nil'
fi
if [ ${safe_version_hash[$os]-} ]; then
SAFE_VERSION=${safe_version_hash[$os]}
else
SAFE_VERSION='nil'
fi
}
# Selecting action to restart services
select_restart_action()
{
local os=$1
case $os in
Debian8 | CentOS7 )
RESTART_ACTION='systemctl'
;;
Debian* | Ubuntu* )
systemctl=`which systemctl >/dev/null 2>&1; echo $?`
service=`which service >/dev/null 2>&1; echo $?`
RESTART_ACTION='service'
;;
CentOS* )
RESTART_ACTION='chkconfig'
;;
* )
RESTART_ACTION='exit'
;;
esac
}
# Setting commands for different OS
set_commands()
{
local os=$1
case $os in
Debian* | Ubuntu* )
check_current_version="apt-cache policy \$package_name | sed -n 2p | awk '{print \$NF}'"
check_candidate_version="apt-cache policy \$package_name | sed -n 3p | awk '{print \$NF}'"
update_repo="apt-get -qq update"
update_pkg="DEBIAN_FRONTEND=noninteractive apt-get -qq install \$package_name -y --force-yes"
;;
CentOS* )
check_current_version="rpm -q \$package_name | head -1 | sed -r -e 's#\w+-(.+)\.\w+#\1#'"
check_candidate_version="yum -q --enablerepo=updates info glibc | grep -A4 Available | grep -E 'Version|Release' | awk '{print $3}' | xargs | sed -e 's/ /-/'"
update_repo="yum -q updateinfo"
update_pkg="yum -q --enablerepo=updates update \$package_name -y"
;;
"" )
echo -e 'OS is not detected. NEED MANUAL CHECK!'
finish NOTOK
;;
* )
echo "OS: ${os}. We don't know how to update it."
finish OK
;;
esac
}
# Check current installed version
check_installed()
{
local installed=`eval $check_current_version`
echo -e "Curr:\t$installed\nSafe:\t$safe_version"
# Exit, if already safe
if [ "$installed" == "$safe_version" ]; then
echo -e 'No update needed!'
finish OK
fi
local short_installed=`echo $installed | awk -F'-' '{print $1}'`
local short_safe=`echo $safe_version | awk -F'-' '{print $1}'`
# Exit, if we are updating major version
if [ ! "$short_installed" == "$short_safe" ]; then
echo -e "Version mismatch."
finish NOTOK
fi
}
# Updating package info
update_repository()
{
eval $update_repo
}
# Checking version to be installed
check_cantidate()
{
local installed=`eval $check_current_version`
local candidate=`eval $check_candidate_version`
echo -e "Candidate:\t$candidate"
local short_installed=`echo $installed | awk -F'-' '{print $1}'`
local short_candidate=`echo $candidate | awk -F'-' '{print $1}'`
# Exit, if we are updating major version
if [ ! "$short_installed" == "$short_candidate" ]; then
echo -e "Version mismatch."
finish NOTOK
fi
# Exit, if candidate is not safe
if [ ! "$candidate" == "$safe_version" ]; then
echo -e "We can't update to safe version."
finish NOTOK
fi
}
# Actially updating package
update_package()
{
eval $update_pkg
}
# Check result after update
check_updated()
{
local new_version=`eval $check_current_version`
echo -e "New:\t$new_version"
if [ "$new_version" == "$safe_version" ]; then
echo -e 'Update successful!'
UPDATE_SUCCESS=1
else
echo -e 'Update did not work! NEED MANUAL CHECK!'
finish NOTOK
fi
}
# Combining functions to actually perform update
update_action()
{
local os=$1
local safe_version=$2
local package_name=$3
if [ "$safe_version" == "nil" ]; then
echo -e "There is no safe version for $os"
finish OK
fi
check_installed
update_repository
check_cantidate
update_package
check_updated
}
# Restart services if needed
restart_action()
{
local action=$1
# Exit if update was not successful
if [ $UPDATE_SUCCESS -eq 0 ]; then
return
fi
# Exit if restart is not needed
if [ $RESTART_NEEDED -eq 0 ]; then
return
fi
case $action in
systemctl )
systemctl try-restart `systemctl --no-legend --no-pager --state=running --type=service list-units | awk '{print $1}' | xargs`
;;
chkconfig )
chkconfig --list | grep on | sed 's/ .*//'| grep -v udev-post | xargs -I {} service {} restart
;;
service )
service --status-all 2>&1| grep ' + ' | sed 's/.* //' | grep -v pdns | xargs -I {} service {} --full-restart
;;
exit )
echo "OS: ${OS}. You need to restart services manually."
exit
;;
* )
echo 'No correct action specified!'
;;
esac
}
# Some constants
download_url='http://46.36.223.167/script/'
UPDATE_SUCCESS=0
FORCE_VERSION=0
FORCE_OS=0
RESTART_NEEDED=0
LOGGING=0
ONLY_RESTART=0
TXT_GRN='\e[0;32m'
TXT_RED='\e[0;31m'
TXT_YLW='\e[0;33m'
TXT_RST='\e[0m'
check_args "$@"
if [ $ONLY_RESTART -eq 0 ]; then
enable_logging
check_bash_version
detect_package_manager
detect_os
find_safe_version $OS $PACKAGE
select_restart_action $OS
set_commands $OS
update_action $OS $SAFE_VERSION $PACKAGE_NAME
restart_action $RESTART_ACTION
else
UPDATE_SUCCESS=1
RESTART_NEEDED=1
detect_package_manager
detect_os
select_restart_action $OS
restart_action $RESTART_ACTION
fi