-
Notifications
You must be signed in to change notification settings - Fork 27
Expand file tree
/
Copy pathquickstart_linux_common.source
More file actions
228 lines (201 loc) · 6 KB
/
quickstart_linux_common.source
File metadata and controls
228 lines (201 loc) · 6 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
#!/usr/bin/bash -e
# QuickStart Common Linux Tools
# author: tonynv@amazon.com
#
# Supported only while bootstrapping Amazon ec2:
#
# Official vendor images
# -Red Hat Enterprise Linux 7 https://aws.amazon.com/marketplace/pp/B00KWBZVK6
# -Centos 7 https://aws.amazon.com/marketplace/pp/B00O7WM7QW
# -Ubuntu 16.04 (LTS) https://aws.amazon.com/marketplace/pp/B01JBL2M0O
# -Amazon Linux 2016.09.1 https://aws.amazon.com/marketplace/pp/B00CIYTQTC
# -SUSE Linux Enterprise 12 https://aws.amazon.com/marketplace/pp/B00PMM99PI
# Supported Edge releases
# -Fedora 25 https://alt.fedoraproject.org/cloud/
# -Ubuntu 16.04 https://cloud-images.ubuntu.com/releases/14.04/14.04/
#
# Configuration
#
PROGRAM='QuickStart Linux Common Tools'
# Usage
#
# To use the functions defined here (source this file):
# Example:
# source quickstart_linux_common.source
# or
# source /dev/stdin <<< "$(curl -s https://quickstart-reference.s3.amazonaws.com/linux/utilities/quickstart_linux_common.source)"
# # To print os type to std out
# get_os-type
# # to assign the os type to a variable OS
# get_os-type OS
#
# Detects operating system type and return value
# If no variable is passed in function will print to std-out
#
function qs_get-ostype () {
local __return=$1
DETECTION_STRING="/etc/*-release"
if [[ $(ls ${DETECTION_STRING}) ]]; then
OS=$(cat /etc/*-release \
| grep PRETTY_NAME \
| tr -d \" \
| awk -F= '{print $2}'\
| awk '{print $1,$2}' \
| sed 's/Linux//g' \
| sed 's/[^a-zA-Z]//g')
if [ $? -eq 0 ] && [ "$__return" ];then
eval $__return="${OS}"
return 0
elif [ $OS ]; then
echo $OS
return 0;
else
echo "Unknown"
fi
else
if [ "$__return" ]; then
__return="Unknown"
return 1
else
echo "Unknown"
return 1;
fi
fi
}
# Returns operating system version or return 1
# If no variable is passed in function will print to std-out
#
function qs_get-osversion () {
local __return=$1
DETECTION_STRING="/etc/*-release"
if [[ $(ls ${DETECTION_STRING}) ]]; then
OSLEVEL=$(cat ${DETECTION_STRING} \
| grep VERSION_ID \
| tr -d \" \
| awk -F= '{print $2}')
if [ $? -eq 0 ] && [ "$__return" ];then
eval $__return="${OSLEVEL}"
return 0
elif [ $OS ]; then
echo $OSLEVEL
return 0;
else
echo "Unknown"
fi
else
if [ "$__return" ]; then
__return="Unknown"
return 1
else
echo "Unknown"
return 1;
fi
fi
}
# If python is install returns default python path
# If no variable is passed in function will print to std-out
#
function qs_get-python-path () {
local __return=$1
# Set PYTHON_EXECUTEABLE to default python version
if command -v python > /dev/null 2>&1; then
PYTHON_EXECUTEABLE=$(which python)
else
PYTHON_EXECUTEABLE=$(which python3)
fi
#Return python path or return code (1)
if [ $PYTHON_EXECUTEABLE ] && [ "$__return" ]; then
eval $__return="${PYTHON_EXECUTEABLE}"
return 0
elif [ $PYTHON_EXECUTEABLE ]; then
echo $PYTHON_EXECUTEABLE
return 0;
else
echo "Python Not installed"
return 1
fi
}
# Installs pip from bootstrap.pypa
#
function qs_bootstrap_pip () {
qs_get-python-path PYTHON_EXECUTEABLE
if [ $? -eq 0 ] ;then
curl --silent \
--show-error \
--retry 5 \
https://bootstrap.pypa.io/get-pip.py | sudo $PYTHON_EXECUTEABLE
else
echo $PYTHON_EXECUTEABLE
exit 1
fi
}
# Updates supported operating systems to latest
# or
# exit with code (1)
#
# If no variable is passed in function will print to std-out
#
function qs_update-os () {
# Assigns values to INSTANCE_OSTYPE
qs_get-ostype INSTANCE_OSTYPE
qs_get-osversion INSTANCE_OSVERSION
if [ "$INSTANCE_OSTYPE" == "Amazon" ]; then
yum update -y
elif [ "$INSTANCE_OSTYPE" == "Ubuntu" ]; then
apt update && apt upgrade -y
elif [ "$INSTANCE_OSTYPE" == "RedHat" ]; then
yum update -y
elif [ "$INSTANCE_OSTYPE" == "CentOS" ]; then
yum update -y
elif [ "$INSTANCE_OSTYPE" == "fedora" ]; then
dnf update -y
elif [ "$INSTANCE_OSTYPE" == "SUSE" ]; then
zypper refresh && zypper update
else
exit 1
fi
}
function available_functions () {
echo "--------------------------------"
echo "Available quickstart_function ()
#qs_get-ostype
#qs_get-osversion
#qs_get-python-path
#qs_bootstrap_pip
#qs_update-os"
echo "--------------------------------"
}
function install_dependancies () {
# Install dependencies
# Assigns values to INSTANCE_
#
qs_get-ostype INSTANCE_OSTYPE
qs_get-osversion INSTANCE_OSVERSION
check_cmd () {
if hash $1 &>/dev/null; then
echo "Dependencies Met!"
return 0
else
echo "Installing Dependencies"
return 1
fi
}
if [ "$INSTANCE_OSTYPE" == "Amazon" ]; then
check_cmd curl 'yum clean all && yum install curl -y'
elif [ "$INSTANCE_OSTYPE" == "Ubuntu" ]; then
check_cmd curl;[[ $? -eq 1 ]] && apt update && apt install curl -y || return 0
elif [ "$INSTANCE_OSTYPE" == "RedHat" ]; then
check_cmd curl;[[ $? -eq 1 ]] && yum clean && yum install curl -y || return 0
elif [ "$INSTANCE_OSTYPE" == "CentOS" ]; then
check_cmd curl;[[ $? -eq 1 ]] && yum clean && yum install curl -y || return 0
elif [ "$INSTANCE_OSTYPE" == "fedora" ]; then
check_cmd curl;[[ $? -eq 1 ]] && dnf clean && dnf install curl -y || return 0
elif [ "$INSTANCE_OSTYPE" == "SUSE" ]; then
check_cmd curl;[[ $? -eq 1 ]] && zypper refresh && zypper install curl -y || return 0
else
echo "[FAIL] : Dependencies not satisfied!"
exit 1
fi
}
# Run on initialization
install_dependancies