forked from usui-tk/amazon-ec2-userdata
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path2nd-Decision_Linux-Distribution.sh
329 lines (301 loc) · 15.7 KB
/
2nd-Decision_Linux-Distribution.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
#!/bin/bash -v
# Logger
exec > >(tee /var/log/user-data_2nd-decision.log || logger -t user-data -s 2> /dev/console) 2>&1
#-------------------------------------------------------------------------------
# Parameter Settings
#-------------------------------------------------------------------------------
if [ $(uname -m) = "x86_64" ]; then
# [For x86_64] Parameter Settings (BootstrapScript - Script dependent on operating system version)
ScriptForAmazonLinux2="https://raw.githubusercontent.com/usui-tk/amazon-ec2-userdata/master/3rd-Bootstrap_AmazonLinux-2-LTS-HVM.sh"
ScriptForAmazonLinux1="https://raw.githubusercontent.com/usui-tk/amazon-ec2-userdata/master/3rd-Bootstrap_AmazonLinux-1-HVM.sh"
ScriptForRHELv8="https://raw.githubusercontent.com/usui-tk/amazon-ec2-userdata/master/3rd-Bootstrap_RHEL-v8-HVM.sh"
ScriptForRHELv7="https://raw.githubusercontent.com/usui-tk/amazon-ec2-userdata/master/3rd-Bootstrap_RHEL-v7-HVM.sh"
ScriptForRHELv6="https://raw.githubusercontent.com/usui-tk/amazon-ec2-userdata/master/3rd-Bootstrap_RHEL-v6-HVM.sh"
ScriptForRockyLinuxv8="https://raw.githubusercontent.com/usui-tk/amazon-ec2-userdata/master/3rd-Bootstrap_RockyLinux-v8-HVM.sh"
ScriptForAlmaLinuxv8="https://raw.githubusercontent.com/usui-tk/amazon-ec2-userdata/master/3rd-Bootstrap_AlmaLinux-v8-HVM.sh"
ScriptForCentOSv8="https://raw.githubusercontent.com/usui-tk/amazon-ec2-userdata/master/3rd-Bootstrap_CentOS-v8-HVM.sh"
ScriptForCentOSv7="https://raw.githubusercontent.com/usui-tk/amazon-ec2-userdata/master/3rd-Bootstrap_CentOS-v7-HVM.sh"
ScriptForCentOSv6="https://raw.githubusercontent.com/usui-tk/amazon-ec2-userdata/master/3rd-Bootstrap_CentOS-v6-HVM.sh"
ScriptForOracleLinuxv8="https://raw.githubusercontent.com/usui-tk/amazon-ec2-userdata/master/3rd-Bootstrap_OracleLinux-v8-HVM.sh"
ScriptForOracleLinuxv7="https://raw.githubusercontent.com/usui-tk/amazon-ec2-userdata/master/3rd-Bootstrap_OracleLinux-v7-HVM.sh"
ScriptForOracleLinuxv6="https://raw.githubusercontent.com/usui-tk/amazon-ec2-userdata/master/3rd-Bootstrap_OracleLinux-v6-HVM.sh"
ScriptForUbuntu2004="https://raw.githubusercontent.com/usui-tk/amazon-ec2-userdata/master/3rd-Bootstrap_Ubuntu-20.04-LTS-HVM.sh"
ScriptForUbuntu1804="https://raw.githubusercontent.com/usui-tk/amazon-ec2-userdata/master/3rd-Bootstrap_Ubuntu-18.04-LTS-HVM.sh"
ScriptForUbuntu1604="https://raw.githubusercontent.com/usui-tk/amazon-ec2-userdata/master/3rd-Bootstrap_Ubuntu-16.04-LTS-HVM.sh"
ScriptForSLESv15="https://raw.githubusercontent.com/usui-tk/amazon-ec2-userdata/master/3rd-Bootstrap_SLES-v15-HVM.sh"
ScriptForSLESv12="https://raw.githubusercontent.com/usui-tk/amazon-ec2-userdata/master/3rd-Bootstrap_SLES-v12-HVM.sh"
ScriptForDebian10="https://raw.githubusercontent.com/usui-tk/amazon-ec2-userdata/master/3rd-Bootstrap_Debian-10-HVM.sh"
ScriptForDebian9="https://raw.githubusercontent.com/usui-tk/amazon-ec2-userdata/master/3rd-Bootstrap_Debian-9-HVM.sh"
ScriptForPhotonOS3="https://raw.githubusercontent.com/usui-tk/amazon-ec2-userdata/master/3rd-Bootstrap_Photon-3-HVM.sh"
# [For x86_64] Parameter Settings (BootstrapScript - Script independent of operating system version [operation check requires a specific version or higher])
ScriptForFedora="https://raw.githubusercontent.com/usui-tk/amazon-ec2-userdata/master/3rd-Bootstrap_Fedora-HVM.sh"
ScriptForOpenSUSE="https://raw.githubusercontent.com/usui-tk/amazon-ec2-userdata/master/3rd-Bootstrap_OpenSUSE-HVM.sh"
ScriptForKaliLinux="https://raw.githubusercontent.com/usui-tk/amazon-ec2-userdata/master/3rd-Bootstrap_Kali-Linux-HVM.sh"
# elif [ $(uname -m) = "aarch64" ]; then
# echo "To Be Update for aarch64"
# exit 0
# [For aarch64] Parameter Settings (BootstrapScript - Script dependent on operating system version)
# ScriptForAmazonLinux2="https://raw.githubusercontent.com/usui-tk/amazon-ec2-userdata/master/3rd-Bootstrap_AmazonLinux-2-LTS-HVM.sh"
# ScriptForRHELv8="https://raw.githubusercontent.com/usui-tk/amazon-ec2-userdata/master/3rd-Bootstrap_RHEL-v8-HVM.sh"
# ScriptForAlmaLinuxv8="https://raw.githubusercontent.com/usui-tk/amazon-ec2-userdata/master/3rd-Bootstrap_AlmaLinux-v8-HVM.sh"
# ScriptForOracleLinuxv8="https://raw.githubusercontent.com/usui-tk/amazon-ec2-userdata/master/3rd-Bootstrap_OracleLinux-v8-HVM.sh"
# ScriptForUbuntu2004="https://raw.githubusercontent.com/usui-tk/amazon-ec2-userdata/master/3rd-Bootstrap_Ubuntu-20.04-LTS-HVM.sh"
# ScriptForSLESv15="https://raw.githubusercontent.com/usui-tk/amazon-ec2-userdata/master/3rd-Bootstrap_SLES-v15-HVM.sh"
# ScriptForDebian10="https://raw.githubusercontent.com/usui-tk/amazon-ec2-userdata/master/3rd-Bootstrap_Debian-10-HVM.sh"
# else
# echo "None"
# exit 0
fi
#-------------------------------------------------------------------------------
# Define Function
#-------------------------------------------------------------------------------
function lowercase(){
echo "$1" | sed "y/ABCDEFGHIJKLMNOPQRSTUVWXYZ/abcdefghijklmnopqrstuvwxyz/"
}
function uppercase(){
echo "$1" | sed "y/abcdefghijklmnopqrstuvwxyz/ABCDEFGHIJKLMNOPQRSTUVWXYZ/"
}
function get_os_info () {
OS=`lowercase \`uname\``
KERNEL=`uname -r`
MACH=`uname -m`
KERNEL_GROUP=$(echo $KERNEL | cut -f1-2 -d'.')
if [ "${OS}" = "linux" ] ; then
if [ -f /etc/os-release ]; then
source /etc/os-release
DIST_TYPE=$ID
DIST=$NAME
REV=$VERSION_ID
elif [ -f /etc/rocky-release ]; then
DIST_TYPE='Rocky Linux'
DIST=`cat /etc/rocky-release | sed s/\ release.*//`
REV=`cat /etc/rocky-release | sed s/.*release\ // | sed s/\ .*//`
elif [ -f /etc/almalinux-release ]; then
DIST_TYPE='AlmaLinux'
DIST=`cat /etc/almalinux-release | sed s/\ release.*//`
REV=`cat /etc/almalinux-release | sed s/.*release\ // | sed s/\ .*//`
elif [ -f /etc/oracle-release ]; then
DIST_TYPE='Oracle'
DIST=`cat /etc/oracle-release | sed s/\ release.*//`
REV=`cat /etc/oracle-release | sed s/.*release\ // | sed s/\ .*//`
elif [ -f /etc/centos-release ]; then
DIST_TYPE='CentOS'
DIST=`cat /etc/centos-release | sed s/\ release.*//`
REV=`cat /etc/centos-release | sed s/.*release\ // | sed s/\ .*//`
elif [ -f /etc/fedora-release ]; then
DIST_TYPE='Fedora'
DIST=`cat /etc/fedora-release | sed s/\ release.*//`
REV=`cat /etc/fedora-release | sed s/.*release\ // | sed s/\ .*//`
elif [ -f /etc/redhat-release ]; then
DIST_TYPE='RHEL'
DIST=`cat /etc/redhat-release | sed s/\ release.*//`
REV=`cat /etc/redhat-release | sed s/.*release\ // | sed s/\ .*//`
elif [ -f /etc/system-release ]; then
if grep "Amazon Linux" /etc/system-release; then
DIST_TYPE='Amazon'
fi
DIST=`cat /etc/system-release | sed s/\ release.*//`
REV=`cat /etc/system-release | sed s/.*release\ // | sed s/\ .*//`
else
DIST_TYPE=""
DIST=""
REV=""
fi
fi
if [[ -z "${DIST}" || -z "${DIST_TYPE}" ]]; then
echo "Unsupported distribution: ${DIST} and distribution type: ${DIST_TYPE}"
exit 1
fi
LOWERCASE_DIST_TYPE=`lowercase $DIST_TYPE`
UNIQ_OS_ID="${LOWERCASE_DIST_TYPE}-${KERNEL}-${MACH}"
UNIQ_PLATFORM_ID="${LOWERCASE_DIST_TYPE}-${KERNEL_GROUP}."
}
function get_bootstrap_script () {
# Select a Bootstrap script
if [ "${DIST}" = "Amazon Linux AMI" ] || [ "${DIST}" = "Amazon Linux" ] || [ "${DIST_TYPE}" = "amzn" ]; then
if [ "${REV}" = "2" ]; then
# Bootstrap Script for Amazon Linux 2.x [Caution - Workaround]
BootstrapScript=${ScriptForAmazonLinux2}
elif [ $(echo ${REV} | grep -e '201') ]; then
# Bootstrap Script for Amazon Linux 1.x (2011.09 - 2017.09)
BootstrapScript=${ScriptForAmazonLinux1}
else
BootstrapScript=""
fi
elif [ "${DIST}" = "RHEL" ] || [ "${DIST}" = "Red Hat Enterprise Linux Server" ] || [ "${DIST_TYPE}" = "rhel" ]; then
if [ $(echo ${REV} | grep -e '8.') ]; then
# Bootstrap Script for Red Hat Enterprise Linux v8.x
BootstrapScript=${ScriptForRHELv8}
elif [ $(echo ${REV} | grep -e '7.') ]; then
# Bootstrap Script for Red Hat Enterprise Linux v7.x
BootstrapScript=${ScriptForRHELv7}
elif [ $(echo ${REV} | grep -e '6.') ]; then
# Bootstrap Script for Red Hat Enterprise Linux v6.x
BootstrapScript=${ScriptForRHELv6}
else
BootstrapScript=""
fi
elif [ "${DIST}" = "Rocky Linux" ] || [ "${DIST_TYPE}" = "rocky" ]; then
if [ $(echo ${REV} | grep -e '8.') ]; then
# Bootstrap Script for Rocky Linux v8.x
BootstrapScript=${ScriptForRockyLinuxv8}
else
BootstrapScript=""
fi
elif [ "${DIST}" = "AlmaLinux" ] || [ "${DIST_TYPE}" = "almalinux" ]; then
if [ $(echo ${REV} | grep -e '8.') ]; then
# Bootstrap Script for AlmaLinux v8.x
BootstrapScript=${ScriptForAlmaLinuxv8}
else
BootstrapScript=""
fi
elif [ "${DIST}" = "CentOS" ] || [ "${DIST_TYPE}" = "centos" ]; then
if [ "${REV}" = "8" ]; then
# Bootstrap Script for CentOS v8.x
BootstrapScript=${ScriptForCentOSv8}
elif [ "${REV}" = "7" ]; then
# Bootstrap Script for CentOS v7.x
BootstrapScript=${ScriptForCentOSv7}
elif [ $(echo ${REV} | grep -e '6.') ]; then
# Bootstrap Script for CentOS v6.x
BootstrapScript=${ScriptForCentOSv6}
else
BootstrapScript=""
fi
elif [ "${DIST}" = "Oracle Linux Server" ] || [ "${DIST_TYPE}" = "ol" ]; then
if [ $(echo ${REV} | grep -e '8.') ]; then
# Bootstrap Script for Oracle Linux v8.x
BootstrapScript=${ScriptForOracleLinuxv8}
elif [ $(echo ${REV} | grep -e '7.') ]; then
# Bootstrap Script for Oracle Linux v7.x
BootstrapScript=${ScriptForOracleLinuxv7}
elif [ $(echo ${REV} | grep -e '6.') ]; then
# Bootstrap Script for Oracle Linux v6.x
BootstrapScript=${ScriptForOracleLinuxv6}
else
BootstrapScript=""
fi
elif [ "${DIST}" = "Ubuntu" ] || [ "${DIST_TYPE}" = "ubuntu" ]; then
if [ $(echo ${REV} | grep -e '20.04') ]; then
# Bootstrap Script for Ubuntu 20.04 LTS (Focal Fossa)
BootstrapScript=${ScriptForUbuntu2004}
elif [ $(echo ${REV} | grep -e '18.04') ]; then
# Bootstrap Script for Ubuntu 18.04 LTS (Bionic Beaver)
BootstrapScript=${ScriptForUbuntu1804}
elif [ $(echo ${REV} | grep -e '16.04') ]; then
# Bootstrap Script for Ubuntu 16.04 LTS (Xenial Xerus)
BootstrapScript=${ScriptForUbuntu1604}
else
BootstrapScript=""
fi
elif [ "${DIST}" = "SLES" ] || [ "${DIST_TYPE}" = "sles" ]; then
if [ $(echo ${REV} | grep -e '15') ]; then
# Bootstrap Script for SUSE Linux Enterprise Server 15
BootstrapScript=${ScriptForSLESv15}
elif [ $(echo ${REV} | grep -e '12.') ]; then
# Bootstrap Script for SUSE Linux Enterprise Server 12
BootstrapScript=${ScriptForSLESv12}
else
BootstrapScript=""
fi
elif [ "${DIST}" = "Debian GNU/Linux" ] || [ "${DIST_TYPE}" = "debian" ]; then
if [ $(echo ${REV} | grep -e '10') ]; then
# Bootstrap Script for Debian GNU/Linux 10 (Buster)
BootstrapScript=${ScriptForDebian10}
elif [ $(echo ${REV} | grep -e '9') ]; then
# Bootstrap Script for Debian GNU/Linux 9 (Stretch)
BootstrapScript=${ScriptForDebian9}
else
BootstrapScript=""
fi
elif [ "${DIST}" = "VMware Photon OS" ] || [ "${DIST_TYPE}" = "photon" ]; then
if [ $(echo ${REV} | grep -e '3.') ]; then
# Bootstrap Script for VMware Photon OS 3.x
BootstrapScript=${ScriptForPhotonOS3}
else
BootstrapScript=""
fi
elif [ "${DIST}" = "Fedora" ] || [ "${DIST_TYPE}" = "fedora" ]; then
# Bootstrap Script for Fedora
BootstrapScript=${ScriptForFedora}
elif [ "${DIST}" = "openSUSE Leap" ] || [ "${DIST_TYPE}" = "opensuse-leap" ]; then
# Bootstrap Script for openSUSE Leap 15
BootstrapScript=${ScriptForOpenSUSE}
elif [ "${DIST}" = "Kali GNU/Linux" ] || [ "${DIST_TYPE}" = "kali" ]; then
# Bootstrap Script for Kali Linux 2020.x
BootstrapScript=${ScriptForKaliLinux}
else
BootstrapScript=""
fi
# Bootstrap script determination
if [ -z "${BootstrapScript}" ]; then
echo "Unsupported Bootstrap Script Linux distribution"
exit 1
fi
}
#-------------------------------------------------------------------------------
# Main Routine
#-------------------------------------------------------------------------------
# call the os info function to get details
get_os_info
# call the bootstrap script function to get details
get_bootstrap_script
# Information Linux Distribution
KERNEL_VERSION=$(uname -r )
KERNEL_GROUP=$(echo "${KERNEL_VERSION}" | cut -f 1-2 -d'.')
KERNEL_VERSION_WO_ARCH=$(basename ${KERNEL_VERSION} .x86_64)
echo "Distribution of the machine is ${DIST}."
echo "Distribution type of the machine is ${DIST_TYPE}."
echo "Revision of the distro is ${REV}."
echo "Kernel version of the machine is ${KERNEL_VERSION}."
echo "BootstrapScript URL is ${BootstrapScript}"
# Install curl/wget Command
if [ $(compgen -ac | sort | uniq | grep -x curl) ] || [ $(compgen -ac | sort | uniq | grep -x wget) ]; then
echo "Preinstalled curl/wget command - Linux distribution: ${DIST} and distribution type: ${DIST_TYPE}"
else
if [ $(command -v dnf) ]; then
# Package Install curl/wget Tools (Red Hat Enterprise Linux, AlmaLinux, CentOS, Oracle Linux)
dnf clean all
dnf install -y curl wget
elif [ $(command -v yum) ]; then
# Package Install curl/wget Tools (Amazon Linux, Red Hat Enterprise Linux, AlmaLinux, CentOS, Oracle Linux)
yum clean all
yum install -y curl wget
elif [ $(command -v apt-get) ]; then
# Package Install curl/wget Tools (Debian, Ubuntu)
export DEBIAN_FRONTEND=noninteractive
apt clean -y
apt install -y curl wget
elif [ $(command -v zypper) ]; then
# Package Install curl/wget Tools (SUSE Linux Enterprise Server)
zypper clean --all
zypper --quiet --non-interactive install curl wget
elif [ $(command -v tdnf) ]; then
# Package Install curl/wget Tools (VMware Photon OS)
tdnf clean all
tdnf install -y curl wget
else
echo "Unsupported curl/wget command - Linux distribution: ${DIST} and distribution type: ${DIST_TYPE}"
exit 1
fi
fi
#-------------------------------------------------------------------------------
# Bootstrap Script Executite
#-------------------------------------------------------------------------------
if [ -n "${BootstrapScript}" ]; then
# Script execution method #1 : Use curl command
if [ $(compgen -ac | sort | uniq | grep -x curl) ]; then
echo "Bootstrap Script Executite - ${BootstrapScript}"
bash -vc "$(curl -L ${BootstrapScript})"
# Script execution method #2 : Use wget command
elif [ $(compgen -ac | sort | uniq | grep -x wget) ]; then
echo "Bootstrap Script Executite - ${BootstrapScript}"
cd /tmp
wget --tries=5 --no-check-certificate --output-document=BootstrapScript.sh ${BootstrapScript}
chmod 700 BootstrapScript.sh
bash -x BootstrapScript.sh
fi
fi
#-------------------------------------------------------------------------------
# End of File
#-------------------------------------------------------------------------------