-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommon_functions
More file actions
614 lines (539 loc) · 17.2 KB
/
common_functions
File metadata and controls
614 lines (539 loc) · 17.2 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
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
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
#!/bin/bash
# ============================================================================
# This file is meant to hold all common functions to be used across the
# install scripts under this project
# ============================================================================
# Uncomment the following line to debug this script
# set -o xtrace
#=================================================
# GLOBAL VARIABLES DEFINITION
#=================================================
_ORIGINAL_PROXY=''
_PROXY=''
_DOMAIN=',intel.com,.intel.com'
_PACKAGE_MANAGER='apt'
_INSTALLER_CMD='apt-get install -y'
_SSH_CMD=''
#=================================================
# GLOBAL FUNCTIONS
#=================================================
function AddUser {
: 'Adds a user to system [Ubuntu or Centos]. Password disabled.
Positional arguments:
1: Username
2: Password
Optional arguments:
3: sudoer
Defaults to True
If True - User is added to the sudoers group
If False - User is not within sudoers'
AddUser"${_PACKAGE_MANAGER}" $@
}
function AddUserapt {
addToSudoers=${3:-True}
if [ $(UserExist ${1}) == False ]; then
adduser --disabled-password --gecos "" "${1}"
echo "${1}:${2}" | chpasswd
# Add user to sudoers - default is True
if [[ $addToSudoers == True ]]; then
usermod -aG sudo "${1}"
echo "${1} ALL=(root)NOPASSWD:ALL" | EDITOR='tee -a' visudo
echo "Added user ${1}. Belogs to sudoers group".
fi
fi
}
function AddUseryum {
addToSudoers=${3:-True}
if [ $(UserExist ${1}) == False ]; then
adduser -c "${1} user." "${1}"
passwd -l "${1}"
echo "${1}:${2}" | chpasswd
# Add user to sudoers - default is True
if [[ $addToSudoers == True ]]; then
usermod -aG wheel "${1}"
echo "${1} ALL=(root)NOPASSWD:ALL" | EDITOR='tee -a' visudo
echo "Added user ${1}. Belogs to sudoers group".
fi
fi
}
function CreatePartition4lvm {
device="$1"
initial_size="${2:-50G}"
size="${3:-50G}"
[[ -z $device ]] && PrintError 'Must provide device full path'
echo "Creating new partition on $device disk"
parted $device --script print
parted $device --script mkpart extending ext4 $initial_size $size
parnum=$(sfdisk -l /dev/nvme0n1|tail -1|awk '{print $1}'|tail -c2)
parted $device --script set $parnum lvm on
parted $device --script print
}
function CheckBios {
: 'Check that system Bios version contains a given version pattern - via OS.
[CentOS7.4 or WindowsServer2016]
Positional parameters:
1: BIOS version pattern.
2: SystemName or IP to ssh into it.
3: User to authenticate.
4: Password to authenticate.
Note: If user/password fail, will try default ssh-keys.
'
biosv="{1}", sy="${2}"; user="${3}"; passw="${4}"
CheckSsh $sy $user $passw 'False'
[[ $_SSH_CMD == 'error' ]] && continue
# Check BIOS
$_SSH_CMD -q $user@$sy "uname" &>result
cv=False
if [[ $( cat result ) == 'Linux' ]]; then
# if Linux
cv=$( $_SSH_CMD $user@$sy \
"dmidecode --type 0|grep Version|sed 's/.*Version:.//g'" )
else
# if Windows
cv=$( $_SSH_CMD $user@$sy \
"powershell; systeminfo | FINDSTR ^BIOS.Version" )
fi
rm -f result
msg=" - $sy BIOS -"
msg1="- Expected Version: $biosv, Current Version: $cv"
[[ $cv =~ $biosv ]] && echo "$msg MATCHES" || echo "$msg NOT MATCH $msg1"
}
function CheckPing {
: 'Pings a system - Prints only one line to indicate either success or failure.
Positional arguments:
1: SystemName or IP to ping.
Optional arguments:
2: verbose
Defaults to True.
If True - Prints success or failure message.
If False - Prints only failure message, supress success message
If Raw - Prints the ping output as-is
NOTE: SEE ALSO WaitForPing FUNCTION
'
sy="${1}"; verbose="${2:-True}"
result=$( ping -c 1 $sy )
[[ $verbose == Raw ]] && echo $result && continue
_reg='Destination Host Unreachable'
msgS=" - $sy - Successful Ping"
msgF=" - $sy - Failed Ping - Host Unreachable \n $result"
[[ $result =~ $_reg ]] && echo -e "$msgF" || \
[[ $verbose == True ]] && echo $msgS
}
function CheckSsh {
: 'Verify system connection via ssh - etiher with user/ password or ssh-keys
[CentOS7.4 or WindowsServer2016]
Positional arguments:
1: SystemName or IP to ssh into.
2: User to authenticate.
Optional arguments:
3: Password to authenticate.
4: verbose
Defaults to True.
If True - Prints success or failure message.
If False - Prints only failure message, supress success message
Note: If user/password fail, will try default ssh-keys.
'
sy="${1}"; user="${2}"; passw="${3}"; verbose="${4:-True}"
# Check we can ssh - with user-password else try sshkeys
_SSH_CMD="sshpass -p $passw ssh"
sshpass -p $passw ssh $user@$sy "exit"
if [[ $? -ne 0 ]]; then
_SSH_CMD='ssh'
ssh -o preferredAuthentications=publickey -q $user@$sy "exit"
[[ $? -ne 0 ]] && echo "ERROR: UNABLE TO SSH $sy." && _SSH_CMD='error'
fi
[[ $_SSH_CMD != 'error' && $verbose == 'True' ]] && \
echo " - $sy - Able to ssh"
}
function CheckVmm {
: 'Prints current available VMs details to validate its status.
[CentOS7.4 or windowsServer2016]
For linux - Done via virsh
Positional arguments:
1: SystemName or IP to ssh into.
2: User to authenticate.
3: Password to authenticate.
Note: If user/password fail, will try default ssh-keys.
'
sy="${1}"; user="${2}"; passw="${3}"
CheckSsh $sy $user $passw 'False'
[[ $_SSH_CMD == 'error' ]] && continue
# Check vmm
echo -e "\n\n - $sy - Reviewing VMs on system."
$_SSH_CMD -q $user@$sy "uname" &>result
if [[ $( cat result ) == 'Linux' ]]; then
# IF LINUX SYSTEM
# Print current VMs on the host
vmms=$( $_SSH_CMD $user@$sy "virsh list --all" )
echo "$vmms"
[[ $vmms =~ "off" ]] && echo " - $sy - Error: something went wrong."
else
# IF WINDOWS SYSTEM
# Print current VMs on the host
$_SSH_CMD $user@$sy "powershell; get-vm"
cmd='powershell; if(Select-String -Path c:/vms/vm_status.log
-Pattern "heartbeat.failure") { exit 1 }; exit'
$_SSH_CMD $user@$sy $cmd
# On failure get vm_status.log file
if [[ $? -ne 0 ]]; then
fname="./${sy}.vm_status_$(date '+%Y_%m_%d_%H:%M:%S').log"
echo " - $sy - Error: vm_status.log - HAVE HEARBEAT FAILURES"
echo "Collecting file into $fname"
sshpass -p $passw scp $user@$sy:C:/vms/vm_status.log ${fname}
fi
fi
rm -f result
}
function CheckVtx {
# Verify system has Intel Virtual Technology enabled - vtx
if [[ ! -c '/dev/kvm' ]]; then
PrintError "VTX must be enabled on the BIOS"
fi
}
function CheckVtxS {
: 'Verifies system has Intel Virtual Technology enabled (aka vtx)
TODO: Check also for vtd (direct I/O) via OS
[CentOS7.4 or windowsServer2016]
Positional arguments:
1: SystemName or IP to ping.
2: User to authenticate.
3: Password to authenticate.
Note: If user/password fail, will try default ssh-keys.
'
sy="${1}"; user="${2}"; passw="${3}"
CheckSsh $sy $user $passw 'False'
[[ $_SSH_CMD == 'error' ]] && continue
# Check Virtualization
$_SSH_CMD -q $user@$sy "uname" &>result
if [[ $( cat result ) == 'Linux' ]]; then
# if Linux
$_SSH_CMD $user@$sy "[[ -c '/dev/kvm' ]] && exit || exit 1"
else
# if Windows - Ensure hyperV reqs are met
cmd='powershell; "if(systeminfo | FINDSTR ^Hyper-V* | FINDSTR .*No)
{ exit 1 }; exit"'
$_SSH_CMD $user@$sy $cmd
fi
rm -f result
msg=" - $sy - VTX"
[[ $? -eq 0 ]] && echo "$msg ENABLED" || echo "$msg NOT ENABLED"
}
function DisableIPV6 {
if grep "all.disable_ipv6" /etc/sysctl.conf; then
echo "net.ipv6.conf.all.disable_ipv6 = 1" | tee -a /etc/sysctl.conf
fi
if grep "default.disable_ipv6" /etc/sysctl.conf; then
echo "net.ipv6.conf.default.disable_ipv6 = 1" | tee -a /etc/sysctl.conf
fi
sysctl -p
}
function DisableSelinux {
setenforce 0
sed -i 's/^SELINUX=enforcing$/SELINUX=permissive/' /etc/selinux/config
}
function DisableFirewalld {
systemctl disable firewalld
systemctl stop firewalld
}
function EnableNestedVirtualization {
vendor_id=$(lscpu|grep "Vendor ID")
if [[ $vendor_id == *GenuineIntel* ]]; then
kvm_ok=$(cat /sys/module/kvm_intel/parameters/nested)
if [[ $kvm_ok == 'N' ]]; then
rmmod kvm-intel
echo 'options kvm-intel nested=y'| tee -a /etc/modprobe.d/dist.conf
modprobe kvm-intel
fi
else
kvm_ok=$(cat /sys/module/kvm_amd/parameters/nested)
if [[ $kvm_ok == '0' ]]; then
rmmod kvm-amd
echo 'options kvm-amd nested=1' | tee -a /etc/modprobe.d/dist.conf
modprobe kvm-amd
fi
fi
modprobe vhost_net
}
function EnsureNonRoot {
# Ensure script is run as root
if [ "$EUID" -eq "0" ]; then
PrintError "This script must be run as NON root."
fi
}
function EnsureRoot {
# Ensure script is run as root
if [ "$EUID" -ne "0" ]; then
PrintError "This script must be run as root."
fi
}
function GetRandomNumber {
_RANDOM_NUMBER=$(awk -v min=500 -v max=10000 \
'BEGIN{srand(); print int(min+rand()*(max-min+1))}')
}
function HandleOptions {
case "${1}" in
--domain|-d)
SetDomain "${2}"
;;
--proxy|-x)
SetProxy "${2}"
;;
--help|-h)
PrintHelp
;;
*)
PrintError "Invalid Argument."
esac
shift
}
function IsCentos {
# Return True or False
source /etc/os-release
[[ $ID == 'centos' ]] && echo True || echo False
}
function IsUbuntu {
# Return True or False
source /etc/os-release
[[ $ID == 'ubuntu' ]] && echo True || echo False
}
function IsTrusty {
# Return True or False
vc='trusty'
[[ $(IsUbuntu) && $VERSION_CODENAME == $vc ]] && echo True || echo False
}
function IsXenial {
# Return True or False
vc='xenial'
[[ $(IsUbuntu) && $VERSION_CODENAME == $vc ]] && echo True || echo False
}
function IsBionic {
# Return True or False
vc='bionic'
[[ $(IsUbuntu) && $VERSION_CODENAME == $vc ]] && echo True || echo False
}
function PrintError {
:'Prints a given message as an error and quit execution
Positional Arguments:
1: Error message to be printed.
'
echo "************************" >&2
echo "* $(date +"%F %T.%N") ERROR: $1" >&2
echo "************************" >&2
exit 1
}
function PrintHelp {
:'Prints the Help for a commands.
Positional Arguments:
1. A description of script functionallity.
2: The name of the script.
3: String with additional script options help
'
echo " "
echo "Script: $1. Optionally uses given proxy"
echo " "
echo "Usage:"
echo "./$2 [--proxy | -x <http://proxyserver:port>]"
echo " "
[[ ! -z $3 ]] && echo -e "$3"
echo " --domain | -d Uses given domain when needed."
echo " --proxy | -x Uses given proxy server in the installation."
echo " --help Prints current help text. "
echo " "
exit 1
}
function resize_lvm {
[[ -z "$1" ]] && PrintError 'Must send device disk'
device="$1"
fdisk -l $device
echo '--> Creating new physical volume'
partition=$(sfdisk -d $device|tail -1|awk -F ':' '{print $1}'|tr -d ' ')
pvcreate $partition
pvs
echo '--> Adding new Physical volume into volume group'
vgname=$(vgs --noheadings -o vg_name | tr -d ' |\n')
vgextend $vgname $partition
vgs
echo '--> Expanding home logical volume'
lvpath=$(vgs --noheadings -o lv_path | grep home | tr -d ' |\n')
ext_av=$(pvs $partition --noheadings -o seg_size_pe|tail -1|tr -d ' |\n')
lvdisplay $lvpath
lvextend -l "+$ext_av" $lvpath
lvdisplay $lvpath
echo 'Resizing file system'
if [[ -n $(parted $device --script print | grep xfs) ]]; then
xfs_growfs $lvpath
elif [[ -n $(parted $device --script print | grep ext4) ]]; then
resize2fs $lvpath
else [[ -n $(parted $device --script print | grep btrfs) ]]
btrfs filesystem resize max $lvpath
fi
}
function SetDomain {
if [[ -z "${1}" || "${1}" == -* ]]; then
PrintError "Function set_domain - Missing domain data."
else
_DOMAIN=",${1}"
fi
}
function SetFirewallUFW {
echo 'Display available apps'
ufw app list
echo 'Allow SSH connections'
ufw allow OpenSSH
ufw allow ssh
echo 'Enable ufw firewall'
echo yes | ufw enable
ufw status
}
function SetKeyPair {
: 'Creates a key-pair unattended for root or other user
Optional arguments:
1: userName
Defaults to root user.
'
[[ -n $1 ]] && path="/home/$1" || path='/root'
mkdir -p $path
ssh-keygen -q -t rsa -b 4096 -C 'dlux keypair' -N "" -f "$path/.ssh/id_rsa"
chown -R $1:$1 $path/.ssh/
}
function SetLocale {
:' Sets locale to US.
Positional arguments:
1: Home directory to be exported.
'
Set locale
locale-gen en_US
update-locale
export HOME=$1
}
function SetPackageManager {
# Discover os_family and set package manager
# accordingly [apt | yum]
# optionally pass manager as parameter
#distro="${1:-$(lsb_release -is)}"
source /etc/os-release || source /usr/lib/os-release
distro="${1:-$ID}"
case ${distro,,} in
rhel|centos|fedora|yum)
_PACKAGE_MANAGER="$(which dnf 2>/dev/null || which yum)"
_INSTALLER_CMD="${_PACKAGE_MANAGER} -y install"
_PACKAGE_MANAGER="$(echo $_PACKAGE_MANAGER | sed 's/.*\///g')"
;;
ubuntu|debian)
_PACKAGE_MANAGER='apt'
_INSTALLER_CMD='apt-get install -y'
;;
*suse)
_PACKAGE_MANAGER='zypper'
_INSTALLER_CMD='zypper install -y'
;;
*)
PrintError "Unknown package manager for distro: $distro"
esac
}
function SetProxy {
: 'Sets a proxy for package manager and as envVar
Positional arguments:
1: Proxy - sintasix http://server:port
'
if [[ -z "${1}" || "${1}" == -* ]]; then
PrintError "Function set_proxy - Missing proxy data."
fi
SetPackageManager
SetProxyOn"${_PACKAGE_MANAGER}"Conf "$@"
_ORIGINAL_PROXY="${1}"
npx="127.0.0.1,localhost,10.0.0.0/8,192.168.0.0/16${_DOMAIN}"
_PROXY="http_proxy=${1} https_proxy=${1} no_proxy=${npx}"
_PROXY="$_PROXY HTTP_PROXY=${1} HTTPS_PROXY=${1} NO_PROXY=${npx}"
SetProxyOnFile
source ".PROXY"
}
function SetProxyOnaptConf {
if [ -f /etc/apt/apt.conf ]; then
echo "Acquire::http::Proxy \"${1}\";" >> /etc/apt/apt.conf
elif [ -d /etc/apt/apt.conf.d ]; then
fil='/etc/apt/apt.conf.d/70proxy.conf'
echo "Acquire::http::Proxy \"${1}\";" >> $fil
fi
}
function SetProxyOnyumConf {
if grep 'proxy' /etc/yum.conf; then
return
fi
echo "proxy=${1}" >> /etc/yum.conf
}
function SetProxyOnFile {
echo " $_PROXY" | sed "s/ /\nexport /g" > .PROXY
}
function UpdatePackageManager {
SetPackageManager
Update"${_PACKAGE_MANAGER}"PackageManager
}
function UpdateaptPackageManager {
apt-get update
}
function UpdateyumPackageManager {
yum clean expire-cache
#yum check-update
#yum -y update
yum -y --security update
}
function UnsetProxy {
UnsetProxyOn"${_PACKAGE_MANAGER}"Conf "$@"
}
function UnsetProxyOnaptConf {
# Cleanup proxy from apt. If any, remove the first coincedence
if [[ ! -z "${1}" ]]; then
scaped_str=$(echo $1 | sed -s 's/[\/&]/\\&/g')
if [ -f /etc/apt/apt.conf ]; then
sed -i "0,/$scaped_str/{/$scaped_str/d;}" /etc/apt/apt.conf
elif [ -d /etc/apt/apt.conf.d ]; then
fil='/etc/apt/apt.conf.d/70proxy.conf'
sed -i "0,/$scaped_str/{/$scaped_str/d;}" $fil
fi
fi
}
function UnsetProxyOnyumConf {
# Cleanup proxy from yum. If any, remove the first coincedence
scaped_str=$(echo "proxy=$1" | sed -s 's/[\/&]/\\&/g')
sed -i "0,/$scaped_str/{/$scaped_str/d;}" /etc/yum.conf
}
function UserExist {
[[ -n $(compgen -u | grep -i "${1}") ]] && echo True || echo False
}
function ValidIP {
local ip=$1
local stat=1
if [[ $ip =~ ^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$ ]]; then
OIFS=$IFS
IFS='.'
ip=($ip)
IFS=$OIFS
[[ ${ip[0]} -le 255 && ${ip[1]} -le 255 \
&& ${ip[2]} -le 255 && ${ip[3]} -le 255 ]]
stat=$?
fi
echo $stat
}
function WaitForPing {
ip=$1
timeout=${2:-360}
initial_time=$(date +%s)
elapsed_time=$timeout
while [ "$elapsed_time" -le "$timeout" ]; do
ping -q -c 1 ${ip}
rc=$?
if [[ $rc -eq 1 ]]; then
echo "Ping is replying now"
break
fi
sleep 1
current_time=$(date +%s)
elapsed_time=$((current_time-initial_time))
done
return $rc
}
function WriteLog {
[[ -z $log_file ]] && log_file=/var/log/install.log
echo -e "$(date +"%F %T.%N") - $1" 2>&1 | tee -a $log_file
}