-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjscommon.sh
executable file
·230 lines (198 loc) · 7.39 KB
/
jscommon.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
#!/bin/bash
##########################################
# program name : jscommon.sh
# Copyright ? 2021 Jun Su Lee. All rights reserved.
# Author : Jun Su Lee ( [email protected] )
# Description : Common usage for all kind of shell scripts
# For this, I prefer not to put dependencies with other scripts.
#
# Category : DB2 support
# Usage
# Date : Apr.09, 2021
#
# Revision History
# - Nov. 30, 2018 :
##########################################
# To avoid login message
SSH_NO_BANNER="-q -o LogLevel=QUIET -o StrictHostKeyChecking=no" # example full ssh command : ssh -q -o LogLevel=QUIET hostname command
#################################################
# Package manager command to use. yum or apt
#################################################
pkgmgr="yum" # Linus package manager command. set this as yum as it's more popular
version_id=""
if [[ -f /etc/os-release ]]; then ## make sure to run with bash. Otherwise, will get error.
os=$(grep '^ID=' /etc/os-release | awk -F= '{print $2}' | tr -d '"') # remove the quote " if there is
version_id=$(grep '^VERSION_ID=' /etc/os-release | awk -F= '{print $2}' | tr -d '"') # remove the quote " if there is
if [[ "$os" == "rhel" || "$os" == "fedora" || "$os" == "centos" ]]; then
pkgmgr="yum"
elif [[ "$os" == "ubuntu" || "$os" == "debian" ]]; then
pkgmgr="apt"
else
echo "Unsupported OS : $os . Exiting !!"
exit 1
fi
else
echo "Cannot find /etc/os-release file. Is this Linux OS ? Check again. Exiting now !!"
exit 1
fi
# Command return value check and exit 1 if non zero
cmdRetChk(){
if [ $? -ne 0 ]; then
echo "failure. Exit.."
exit 1
else
echo "success !!! "
fi
}
timestamp()
{
date +'%Y-%m-%d %H:%M:%S.%3N'
}
stop_immediately () {
[[ -z "$1" ]] || logger_error "REASON: $1"
exit 1
}
logger ()
{
if [ -z "$PRODUCT_LOGFILE" ]; then
export PRODUCT_LOGFILE=/tmp/kube_install.log.$$
fi
# Print caller's source file, line number and function name
# Skip two functions that are usually top of the stack
# i.e. logger_xxxx and logger
echo "[$(timestamp)] - $(basename ${BASH_SOURCE[2]}):${BASH_LINENO[1]}(${FUNCNAME[2]}) - $*" >> $PRODUCT_LOGFILE
return 0
}
logger_error ()
{
local LEVEL="ERROR"
if [ -n "$1" ]; then
logger " $LEVEL: $1"
echo " $LEVEL: $1"
else
while read IN
do
logger "$LEVEL: $IN"
echo "$LEVEL: $IN"
done
fi
return 0
}
disp_msglvl2(){
echo
echo "**********************************************"
echo $1
echo "**********************************************"
echo
}
print2(){
disp_msglvl2 "$1"
}
disp_msglvl1(){
echo
echo
echo "###########################################################################################"
echo $1
echo "###########################################################################################"
echo
}
print1(){
disp_msglvl1 "$1"
}
# install software if the command does not exist. This is only for the current host.
# For multiple hosts, I don't put a common function here yet as it's more complex.
# call this function like this listing up package name split by a space
# ex) swChk podman jq vim tmux skopeo
## jq : JSON command line
##
swCmdChk(){
disp_msglvl1 "Software check and install if not exist"
for i in "$@"
do
disp_msglvl2 "Checking $i"
which $i
[ $? -ne 0 ] && $pkgmgr install $i -y
done
}
# Install python3 package and necessary libraries
pyChk(){
which python3
if [ $? -ne 0 ] ; then
disp_msglvl2 "installaing python3"
$pkgmgr install python3 -y
fi
which pip3 # On Redhat 8.10, had to install this again. Python3 install didn add this somehow.
if [ $? -ne 0 ] ; then
disp_msglvl2 "installing pip3"
# From the previous logic, this is set when OS is ubuntu
# To install pip3, there are some other steps in ubuntu
if [[ "$pkgmgr" == "apt" ]]; then
sudo add-apt-repository universe -y
sudo $pkgmgr update -y
# To prevent pop up message asking to restart outdated service package.
# For this, needrestart package is necessary. If the conf file does not exist, install the package
if [ ! -f "/etc/needrestart/needrestart.conf" ]; then
echo "/etc/needrestart/needrestart.conf does not exist. Installing needstart package"
sudo $pkgmgr install needrestart -y
fi
# change i (interactive) to a (automatically)
sudo sed -i 's/$nrconf{restart} = '\''i'\'';/$nrconf{restart} = '\''a'\'';/' /etc/needrestart/needrestart.conf
# Then uncomment
sudo sed -i 's/^\#$nrconf{restart}/$nrconf{restart}/' /etc/needrestart/needrestart.conf
# Ubuntu 22.04 : working fine.
# Ubuntu 24.04 . on some systems, gets error: E: Package 'python3-pip' has no installation candidate.
# Didn't have the issue on fresh install ubuntu 24.04. But on some cloud provisioned system, this happens.
# If error happens, will try to get around with other way once.
# However, it may be fine as we can still use 'apt install' to install xml library in 24.04. Therefore, ignore the error for now. JSTODO
sudo $pkgmgr install python3-pip -y
if [ $? -ne 0 ] ; then
print2 "pip3 installation failure on $version_id . Trying other way soon. "
curl -sS https://bootstrap.pypa.io/get-pip.py -o /tmp/get-pip.py
sudo python3 /tmp/get-pip.py --break-system-packages
fi
else ## non ubuntu, mostly Redhat
$pkgmgr install python3-pip -y
fi
fi
disp_msglvl2 "Python necessary library installation"
# need to install library even if there is existing python3
if [[ "$pkgmgr" == "apt" ]]; then
# Ubuntu 24.04 : pip3 install -U pyyaml => Error: externally-managed-environment
# Since Ubuntu 24.04, PEP 668 specification prevents system-wide Python environments from being modified directly via pip
# Therefore, I think there are two options.
# sudo apt install python3-yaml -y
# or
# sudo pip3 install -U pyyaml --break-system-packages
case "$version_id" in
"24.04"|"Some future version to add")
sudo apt install python3-yaml -y
;;
"22.04")
pip3 install -U pyyaml
;;
*)
sudo apt install python3-yaml -y # If unknown version, just try new way.
;;
esac
else
pip3 install -U pyyaml
fi
# JSTODO :
# Let's do test
# JSTODO :
# Also for Ubuntu, consider the case sudo may need password.
}
goChk(){
which go && go version
if [ $? -ne 0 ] ; then
GOVER="1.23.2"
disp_msglvl2 "installaing golang version $GOVER"
wget -P /root https://go.dev/dl/go${GOVER}.linux-amd64.tar.gz
rm -rf /usr/local/go && tar -C /usr/local -xzf /root/go${GOVER}.linux-amd64.tar.gz
export PATH=$PATH:/usr/local/go/bin
echo "export PATH=$PATH:/usr/local/go/bin" >> ~/.bashrc
source ~/.bashrc
go version
which go
fi
}