-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathinstaller
executable file
·391 lines (343 loc) · 10.7 KB
/
installer
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
#!/bin/bash
# Default config var
LIBREI_VERSION="0.8.11"
LIBREI_DEBUG_DEFAULT=true
LIBREI_REQUIREMENTS_CENTOS="git curl jq docker"
ANSIBLE_RPM_FILENAME="ansible-2.7.9-1.el7.ans.noarch.rpm"
ANSIBLE_RPM_URL="https://releases.ansible.com/ansible/rpm/release/epel-7-x86_64/${ANSIBLE_RPM_FILENAME}"
TMP_DEST=/tmp/libre-installer-tmp
INSTALLER_BIN=/usr/local/bin/libre-installer
ANSIBLE_DEST=/usr/local/lib/libre-ansible
INSTALLER_URL=https://goo.gl/T8Dw9J
THIS_SCRIPT="$(realpath $0)"
# config var
LIBRE_TOOLREPO_URL=https://github.com/startxfr/libre
LIBRE_TOOLREPO_BRANCH=master
LIBRE_TOOLREPO_BASE=/
LBI_DEBUG=$LIBREI_DEBUG_DEFAULT
# default command line options
LIBRE_TYPE=bootstrap
UPDATE=n
ASK_BECOME_PASS=
# get and store the operating system name
source /etc/os-release
OS="$NAME"
OS_NAME="$PRETTY_NAME"
# use only our Ansible config
unset ANSIBLE_CONFIG
# use xterm
export TERM=xterm
if [[ "${LIBREI_DEBUG,,}" == "true" ||
"${LIBREI_DEBUG,,}" == "false" ]] ; then
LBI_DEBUG=$LIBREI_DEBUG
fi
# return if debug is activated
function debug_enabled {
if [[ "${LBI_DEBUG,,}" == "true" ]] ; then
return 0
else
return 1
fi
}
# echo if debug is activated
function debug {
if debug_enabled; then
echo "$@" >&2
fi
}
# echo an error
function error {
echo " ERROR ! $@" >&2
}
# echo an error and exit
function fatal {
echo " FATAL ! $@" >&2
exit 1
}
# echo an error and exit
function warning {
echo " WARN ! $@" >&2
}
# echo an error and exit
function success {
echo "SUCCESS : $@"
}
# check for super-user identity
if [ "$(id -u)" != "0" ]; then
error "This script must be run as root or you must have sudo"
error "Try to execute 'sudo -i' and run again this command"
exit 1
fi
# Display libre installer header
function print_header {
cat <<-'BANNER'
_ _ _ _ _ _ _
| | (_) |__ _ __ ___ (_)_ __ ___| |_ __ _| | | ___ _ __
| | | | '_ \| '__/ _ \_____| | '_ \/ __| __/ _` | | |/ _ \ '__|
| |___| | |_) | | | __/_____| | | | \__ \ || (_| | | | __/ |
|_____|_|_.__/|_| \___| |_|_| |_|___/\__\__,_|_|_|\___|_|
BANNER
}
# Display information
function print_info {
print_header
echo "installer : v$LIBREI_VERSION"
if debug_enabled; then
echo "repo : $LIBRE_TOOLREPO_URL"
echo "branch : $LIBRE_TOOLREPO_BRANCH"
echo "base : $LIBRE_TOOLREPO_BASE"
else
echo "libre-src : $LIBRE_TOOLREPO_URL @$LIBRE_TOOLREPO_BRANCH$LIBRE_TOOLREPO_BASE"
fi
echo "OS : $OS_NAME"
echo "host : $HOSTNAME"
echo "type : $LIBRE_TYPE"
echo
}
function check_type {
[ $# -eq 1 ] || {
return 1
}
local types=(bootstrap instructor student)
for t in "${types[@]}"; do
[ "$1" == "$t" ] && return 0
done
return 2
}
function prep_dir {
[ $# -eq 1 ] || {
return 1
}
local dir="$1"
#if [ -w $dir/.. ]; then
# rm -rf $dir
# success "Remove previous $dir directory"
#fi
mkdir -p "$dir" &> /dev/null || {
error "Unable to create $dir"
return 2
}
if [ ! -w "$dir" ]; then
error "$dir is not writable"
return 3
fi
return 0
}
# Display information
function prep_env {
if [ ! -f $INSTALLER_BIN ]; then
rm -rf $INSTALLER_BIN
install -T -m 0755 -o root -g root "$THIS_SCRIPT" $INSTALLER_BIN
fi
prep_dir $TMP_DEST &&
prep_dir $ANSIBLE_DEST || exit 1
return 0
}
# installer update system
function install_deps {
if [ "$UPDATE" == "y" ]; then
echo "Updating OS $OS installation (cloud take minutes...)"
yum update -y &> /dev/null
fi
if [ ! -f /tmp/libre-is-updated ]; then
echo "Removing package PyYAML"
# PyYAML will be reinstalled with yq
yum remove -y PyYAML &> /dev/null || fatal "Unable to uninstall PyYAML"
echo "Download Ansible RPM file"
curl -L $ANSIBLE_RPM_URL \
-o /tmp/${ANSIBLE_RPM_FILENAME} &> /dev/null ||
fatal "Unable to download Ansible RPM file"
# epel and python-pip packages must be installed separately,
# otherwise pip does not settle, probably a yum bug
echo "Installing packages for $OS (could take minutes on fresh install or slow internet)"
(
yum install -y epel-release &&
yum install -y python-pip &&
pip install --upgrade --force-reinstall pip==9.0.3 &&
pip install --upgrade docker &&
yum remove -y python-pip &&
yum install -y python-pip &&
pip install --upgrade pip &&
pip install --upgrade yq &&
pip install --upgrade Jinja2 &&
pip install --upgrade botocore boto3 boto &&
yum install -y /tmp/${ANSIBLE_RPM_FILENAME} &&
yum install -y $LIBREI_REQUIREMENTS_CENTOS &&
cat <<-EOF >> /etc/boto.cfg
[Boto]
use_endpoint_heuristics = True
EOF
) &> /dev/null || {
fatal "Unable to install dependencies"
}
touch /tmp/libre-is-updated
else
echo "Skip updating packages because /tmp/libre-is-updated is found"
fi
}
# installer install libre
function fetch_installer {
pushd $TMP_DEST &> /dev/null
echo "Clone tools repository $LIBRE_TOOLREPO_URL"
if debug_enabled; then
git clone $LIBRE_TOOLREPO_URL .
else
git clone $LIBRE_TOOLREPO_URL . &> /dev/null
fi
if [ "$?" -eq 0 ]; then
echo "Change branch for $LIBRE_TOOLREPO_BRANCH"
if debug_enabled; then
git checkout $LIBRE_TOOLREPO_BRANCH
else
git checkout $LIBRE_TOOLREPO_BRANCH &> /dev/null
fi
echo "Copy libre playbook into $ANSIBLE_DEST"
rm -rf $ANSIBLE_DEST
prep_dir $ANSIBLE_DEST
cp -rf $TMP_DEST$LIBRE_TOOLREPO_BASE/ansible/* $ANSIBLE_DEST/
fi
popd &> /dev/null
}
# installer install libre
function clean_installer {
echo "Remove $TMP_DEST"
rm -rf $TMP_DEST
}
# installer check post-installation
function check_installation {
if [ ! -d $ANSIBLE_DEST ]; then
fatal "$ANSIBLE_DEST doesn't exist"
fi
}
# installer installation process
function install_libre {
print_info
perform_installation
pushd $ANSIBLE_DEST &> /dev/null
echo "Start $LIBRE_TYPE installation"
if debug_enabled; then
mv ansible.cfg ansible.cfg.back
cp -f ansible.debug.cfg ansible.cfg
ansible-playbook $ASK_BECOME_PASS playbooks/$LIBRE_TYPE/install.yml
retc=$?
mv -f ansible.cfg.back ansible.cfg
else
ansible-playbook $ASK_BECOME_PASS playbooks/$LIBRE_TYPE/install.yml &> /dev/null
retc=$?
fi
if [ $retc -eq 0 ]; then
echo "Reload bash profile"
source /etc/profile.d/libre.sh &> /dev/null
echo -e $(cat /tmp/libre.txt)
if [[ "$LIBRE_TYPE" == "bootstrap" && "$IS_INTERACTIVE" == "yes" ]]; then
read -p "Do you want to continue by provisionning a classroom (y/N) : " doInstall
if [[ "$doInstall" == "y" || "$doInstall" == "yes" ]]; then
libre-infra config
echo -e $(cat /tmp/libre.txt)
fi
elif [[ "$LIBRE_TYPE" == "instructor" && "$IS_INTERACTIVE" == "yes" ]]; then
read -p "Do you want to continue by installing training session (y/N) : " doInstall
if [[ "$doInstall" == "y" || "$doInstall" == "yes" ]]; then
libre-session install
echo -e $(cat /tmp/libre.txt)
fi
else
success "You can continue by running libre-session install"
fi
else
echo -e $(cat /tmp/libre.txt)
error "libre $LIBRE_TYPE installation failed to completed"
error "try to run again ./libre-installer install $LIBRE_TYPE"
error "or report bug to support team"
exit 1
fi
popd &> /dev/null
}
# do installation process
function perform_installation {
prep_env
install_deps
fetch_installer
clean_installer
check_installation
}
# installer uninstall process
function uninstall_libre {
print_info
perform_uninstallation
}
# do uninstall process
function perform_uninstallation {
if [ -d $ANSIBLE_DEST ]; then
pushd $ANSIBLE_DEST &> /dev/null
ansible-playbook playbooks/$LIBRE_TYPE/uninstall.yml
popd &> /dev/null
fi
}
# reinstaller remove libre environment
function reinstall_libre {
print_info
perform_uninstallation
echo "Refresh libre-installer"
rm -f $INSTALLER_BIN
curl -s -L $INSTALLER_URL -o $INSTALLER_BIN &> /dev/null
chmod +x $INSTALLER_BIN
$INSTALLER_BIN install $LIBRE_TYPE
}
# Display installer usage
function print_usage {
print_header
cat <<-EOF
Startx Formation training environment installer
Usage:
$0 sub-command [-t INSTALL_TYPE] [-Kiu] [command]
Options:
-t INSTALL_TYPE install LIBRE_TYPE training environment
INSTALL_TYPE can be one of:
- bootstrap (default)
- instructor
- student
-K prompt for Ansible's privilege escalation password
-i interactive and prompt for session setup on instructor post-install
-u fully update the system during installation
Availables sub-commands:
install install LIBRE TMS and bootstrap, instructor or student
environment (interactive)
uninstall uninstall training environment (and libre TMS)
reinstall re-install training environment (and libre TMS)
EOF
}
while getopts :Kt:u c; do
case "$c" in
K)
ASK_BECOME_PASS="-K"
;;
i)
IS_INTERACTIVE="yes"
;;
t)
check_type "$OPTARG" || {
error "Invalid argument for option -t : $OPTARG"
print_usage
exit 1
}
LIBRE_TYPE="$OPTARG"
;;
u)
UPDATE=y
;;
'?')
error "Unsupported option : -$OPTARG"
print_usage
exit 1
;;
esac
done
shift $((OPTIND - 1))
# Dispatch input arguments
case "$1" in
"install") install_libre ;;
"uninstall") uninstall_libre ;;
"reinstall") reinstall_libre ;;
*) print_usage ;;
esac