forked from ronivay/XenOrchestraInstallerUpdater
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathxo-install.sh
executable file
·787 lines (650 loc) · 25.2 KB
/
xo-install.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
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
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
#!/bin/bash
#########################################################################
# Title: XenOrchestraInstallerUpdater #
# Author: Roni Väyrynen #
# Repository: https://github.com/ronivay/XenOrchestraInstallerUpdater #
#########################################################################
SAMPLE_CONFIG_FILE="$(dirname $0)/sample.xo-install.cfg"
CONFIG_FILE="$(dirname $0)/xo-install.cfg"
# Deploy default configuration file if the user doesn't have their own yet.
if [[ ! -e "$CONFIG_FILE" ]]; then
cp $SAMPLE_CONFIG_FILE $CONFIG_FILE
fi
# See this file for all script configuration variables.
source $CONFIG_FILE
# Set some default variables if sourcing config file fails for some reason
PORT=${PORT:-80}
INSTALLDIR=${INSTALLDIR:-"/etc/xo"}
BRANCH=${BRANCH:-"master"}
LOGFILE=${LOGFILE:-"$(dirname $0)/xo-install.log"}
AUTOUPDATE=${AUTOUPDATE:-"true"}
PRESERVE=${PRESERVE:-"3"}
XOUSER=${XOUSER:-"root"}
CONFIGPATH="$(getent passwd $XOUSER | cut -d: -f6)"
# Set path where new source is cloned/pulled
XO_SRC_DIR="$INSTALLDIR/xo-src/xen-orchestra"
# Set variables for stdout print
COLOR_N='\e[0m'
COLOR_GREEN='\e[1;32m'
COLOR_RED='\e[1;31m'
COLOR_BLUE='\e[1;34m'
OK="[${COLOR_GREEN}ok${COLOR_N}]"
FAIL="[${COLOR_RED}fail${COLOR_N}]"
INFO="[${COLOR_BLUE}info${COLOR_N}]"
PROGRESS="[${COLOR_BLUE}..${COLOR_N}]"
# Protocol to use for webserver. If both of the X.509 certificate files exist,
# then assume that we want to enable HTTPS for the server.
if [[ -e $PATH_TO_HTTPS_CERT ]] && [[ -e $PATH_TO_HTTPS_KEY ]]; then
HTTPS=true
else
HTTPS=false
fi
function CheckUser {
# Make sure the script is ran as root
if [[ ! "$(id -u)" == "0" ]]; then
echo -e "${FAIL} This script needs to be ran as root"
exit 0
fi
}
function ErrorHandling {
echo -e "${FAIL} Something went wrong, exiting. Check $LOGFILE for more details and use rollback feature if needed"
if [[ -d $INSTALLDIR/xo-builds/xen-orchestra-$TIME ]]; then
echo -e "${FAIL} Removing $INSTALLDIR/xo-builds/xen-orchestra-$TIME because of failed installation."
rm -rf $INSTALLDIR/xo-builds/xen-orchestra-$TIME
fi
}
function InstallDependenciesCentOS {
set -e
trap ErrorHandling ERR INT
# Install necessary dependencies for XO build
# only run automated node install if package not found
if [[ -z $(rpm -qa | grep ^node) ]]; then
echo
echo -ne "${PROGRESS} Installing node.js"
curl -s -L https://rpm.nodesource.com/setup_12.x | bash - >/dev/null
echo -e "\r${OK} Installing node.js"
fi
# only install yarn repo and package if not found
if [[ -z $(rpm -qa | grep yarn) ]]; then
echo
echo -ne "${PROGRESS} Installing yarn"
curl -s -o /etc/yum.repos.d/yarn.repo https://dl.yarnpkg.com/rpm/yarn.repo >/dev/null && \
yum -y install yarn >/dev/null
echo -e "\r${OK} Installing yarn"
fi
# only install epel-release if doesn't exist
if [[ -z $(rpm -qa | grep epel-release) ]]; then
echo
echo -ne "${PROGRESS} Installing epel-repo"
yum -y install epel-release >/dev/null
echo -e "\r${OK} Installing epel-repo"
fi
# only install libvhdi-tools if vhdimount is not present
if [[ -z $(which vhdimount) ]]; then
echo
echo -ne "${PROGRESS} Installing libvhdi-tools from forensics repository"
rpm -ivh https://forensics.cert.org/cert-forensics-tools-release-el7.rpm >/dev/null
sed -i 's/enabled=1/enabled=0/g' /etc/yum.repos.d/cert-forensics-tools.repo
yum --enablerepo=forensics install -y libvhdi-tools >/dev/null
echo -e "\r${OK} Installing libvhdi-tools from forensics repository"
fi
# install
echo
echo -ne "${PROGRESS} Installing build dependencies, redis server, python, git, nfs-utils, cifs-utils"
yum -y install gcc gcc-c++ make openssl-devel redis libpng-devel python git nfs-utils cifs-utils >/dev/null
echo -e "\r${OK} Installing build dependencies, redis server, python, git, nfs-utils, cifs-utils"
echo
echo -ne "${PROGRESS} Enabling and starting redis service"
/bin/systemctl enable redis >/dev/null && /bin/systemctl start redis >/dev/null
echo -e "\r${OK} Enabling and starting redis service"
echo
echo -ne "${PROGRESS} Enabling and starting rpcbind service"
/bin/systemctl enable rpcbind >/dev/null && /bin/systemctl start rpcbind >/dev/null
echo -e "\r${OK} Enabling and starting rpcbind service"
} 2>$LOGFILE
function InstallDependenciesDebian {
set -e
trap ErrorHandling ERR INT
# Install necessary dependencies for XO build
echo
echo -ne "${PROGRESS} Running apt-get update"
apt-get update >/dev/null
echo -e "\r${OK} Running apt-get update"
# Install apt-transport-https and ca-certificates because of yarn https repo url
echo
echo -ne "${PROGRESS} Installing apt-transport-https and ca-certificates packages to support https repos"
apt-get install -y apt-transport-https ca-certificates >/dev/null
echo -e "\r${OK} Installing apt-transport-https and ca-certificates packages to support https repos"
if [[ $OSVERSION == "10" ]]; then
echo
echo -ne "${PROGRESS} Debian 10, so installing gnupg also"
apt-get install gnupg -y >/dev/null
echo -e "\r${OK} Debian 10, so installing gnupg also"
fi
# install curl for later tasks if missing
if [[ -z $(which curl) ]]; then
echo
echo -ne "${PROGRESS} Installing curl"
apt-get install -y curl >/dev/null
echo -e "\r${OK} Installing curl"
fi
# install setcap for non-root port binding if missing
if [[ -z $(which setcap) ]]; then
echo
echo -ne "${PROGRESS} Installing setcap"
apt-get install -y libcap2-bin >/dev/null
echo -e "\r${OK} Installing setcap"
fi
# only install yarn repo and package if not found
if [[ -z $(dpkg -l | grep yarn) ]]; then
echo
echo -ne "${PROGRESS} Installing yarn"
curl -sS https://dl.yarnpkg.com/debian/pubkey.gpg | apt-key add - >/dev/null
echo "deb https://dl.yarnpkg.com/debian/ stable main" | tee /etc/apt/sources.list.d/yarn.list >/dev/null
apt-get update >/dev/null
apt-get install -y yarn >/dev/null
echo -e "\r${OK} Installing yarn"
fi
# only run automated node install if package not found
if [[ -z $(dpkg -l | grep node) ]] || [[ -z $(which npm) ]]; then
echo
echo -ne "${PROGRESS} Installing node.js"
curl -sL https://deb.nodesource.com/setup_12.x | bash - >/dev/null
apt-get install -y nodejs >/dev/null
echo -e "\r${OK} Installing node.js"
fi
# install packages
echo
echo -ne "${PROGRESS} Installing build dependencies, redis server, python, git, libvhdi-utils, lvm2, nfs-common, cifs-utils"
apt-get install -y build-essential redis-server libpng-dev git python-minimal libvhdi-utils lvm2 nfs-common cifs-utils >/dev/null
echo -e "\r${OK} Installing build dependencies, redis server, python, git, libvhdi-utils, lvm2, nfs-common, cifs-utils"
echo
echo -ne "${PROGRESS} Enabling and starting redis service"
/bin/systemctl enable redis-server >/dev/null && /bin/systemctl start redis-server >/dev/null
echo -e "\r${OK} Enabling and starting redis service"
echo
echo -ne "${PROGRESS} Enabling and starting rpcbind service"
/bin/systemctl enable rpcbind >/dev/null && /bin/systemctl start rpcbind >/dev/null
echo -e "\r${OK} Enabling and starting rpcbind service"
} 2>$LOGFILE
function UpdateNodeYarn {
if [[ $AUTOUPDATE == "true" ]]; then
if [ $OSNAME == "CentOS" ]; then
echo
echo -ne "${PROGRESS} Checking updates for nodejs and yarn"
yum update -y nodejs yarn > /dev/null
echo -e "\r${OK} Checking updates for nodejs and yarn"
else
echo
echo -ne "${PROGRESS} Checking updates for nodejs and yarn"
apt-get install -y --only-upgrade nodejs yarn > /dev/null
echo -e "\r${OK} Checking updates for nodejs and yarn"
fi
fi
} 2>$LOGFILE
function InstallXOPlugins {
set -e
trap ErrorHandling ERR INT
if [[ "$PLUGINS" ]] && [[ ! -z "$PLUGINS" ]]; then
if [[ "$PLUGINS" == "all" ]]; then
echo
echo -ne "${PROGRESS} Installing plugins"
find "$INSTALLDIR/xo-builds/xen-orchestra-$TIME/packages/" -maxdepth 1 -mindepth 1 -not -name "xo-server" -not -name "xo-web" -not -name "xo-server-cloud" -exec ln -sn {} "$INSTALLDIR/xo-builds/xen-orchestra-$TIME/packages/xo-server/node_modules/" \;
else
echo
echo -ne "${PROGRESS} Installing plugins"
local PLUGINSARRAY=($(echo "$PLUGINS" | tr ',' ' '))
for x in "${PLUGINSARRAY[@]}"; do
if [[ $(find $INSTALLDIR/xo-builds/xen-orchestra-$TIME/packages -type d -name "$x") ]]; then
ln -sn $INSTALLDIR/xo-builds/xen-orchestra-$TIME/packages/$x $INSTALLDIR/xo-builds/xen-orchestra-$TIME/packages/xo-server/node_modules/
fi
done
fi
cd $INSTALLDIR/xo-builds/xen-orchestra-$TIME && yarn >/dev/null && yarn build >/dev/null
echo -e "\r${OK} Installing plugins"
else
echo
echo -e "${INFO} No plugins to install"
fi
} 2>$LOGFILE
function InstallXO {
set -e
trap ErrorHandling ERR INT
TIME=$(date +%Y%d%m%H%M)
# Create user if doesn't exist (if defined)
if [ $XOUSER ]; then
if [[ -z $(getent passwd $XOUSER) ]]; then
echo
echo -ne "${PROGRESS} Creating missing $XOUSER user"
useradd -s /sbin/nologin $XOUSER
echo -e "\r${OK} Creating missing $XOUSER user"
sleep 2
fi
fi
# Create installation directory if doesn't exist already
if [[ ! -d "$INSTALLDIR" ]] ; then
echo -ne "${PROGRESS} Creating missing basedir to $INSTALLDIR"
mkdir -p "$INSTALLDIR"
echo -e "\r${OK} Creating missing basedir to $INSTALLDIR"
fi
# Create missing xo-builds directory if doesn't exist already
if [[ ! -d "$INSTALLDIR/xo-builds" ]]; then
echo -ne "${PROGRESS} Creating missing xo-builds directory to $INSTALLDIR/xo-builds"
mkdir "$INSTALLDIR/xo-builds"
echo -e "\r${OK} Creating missing xo-builds directory to $INSTALLDIR/xo-builds"
fi
echo
echo -e "${INFO} Fetching Xen Orchestra source code"
echo
if [[ ! -d "$XO_SRC_DIR" ]]; then
mkdir -p "$XO_SRC_DIR"
git clone https://github.com/vatesfr/xen-orchestra "$XO_SRC_DIR"
else
cd "$XO_SRC_DIR"
git pull
cd $(dirname $0)
fi
# Deploy the latest xen-orchestra source to the new install directory.
echo
echo -ne "${INFO} Creating install directory: $INSTALLDIR/xo-builds/xen-orchestra-$TIME"
rm -rf "$INSTALLDIR/xo-builds/xen-orchestra-$TIME"
cp -r "$XO_SRC_DIR" "$INSTALLDIR/xo-builds/xen-orchestra-$TIME"
if [[ "$BRANCH" == "release" ]]; then
cd $INSTALLDIR/xo-builds/xen-orchestra-$TIME
TAG=$(git describe --tags $(git rev-list --tags --max-count=1))
echo
echo -e "${INFO} Checking out latest tagged release '$TAG'"
git checkout $TAG 2> /dev/null # Suppress the detached-head message.
cd $(dirname $0)
elif [[ "$BRANCH" != "master" ]]; then
echo
echo -e "${INFO} Checking out source code from branch '$BRANCH'"
cd $INSTALLDIR/xo-builds/xen-orchestra-$TIME
git checkout $BRANCH
cd $(dirname $0)
fi
# Check if the new repo is any different from the currently-installed
# one. If not, then skip the build and delete the repo we just cloned.
# Get the commit ID of the to-be-installed xen-orchestra.
cd $INSTALLDIR/xo-builds/xen-orchestra-$TIME
NEW_REPO_HASH=$(git rev-parse HEAD)
NEW_REPO_HASH_SHORT=$(git rev-parse --short HEAD)
cd $(dirname $0)
# Get the commit ID of the currently-installed xen-orchestra (if one
# exists).
if [[ -L $INSTALLDIR/xo-server ]] && [[ ! -z $(readlink -e $INSTALLDIR/xo-server) ]]; then
cd $INSTALLDIR/xo-server
OLD_REPO_HASH=$(git rev-parse HEAD)
OLD_REPO_HASH_SHORT=$(git rev-parse --short HEAD)
cd $(dirname $0)
else
# If there's no existing installation, then we definitely want
# to proceed with the bulid.
OLD_REPO_HASH=""
OLD_REPO_HASH_SHORT=""
fi
# If the new install is no different from the existing install, then don't
# proceed with the build.
if [[ "$NEW_REPO_HASH" == "$OLD_REPO_HASH" ]]; then
echo
echo -e "${INFO} No changes to xen-orchestra since previous install. Skipping xo-server and xo-web build."
echo -e "${INFO} Cleaning up install directory: $INSTALLDIR/xo-builds/xen-orchestra-$TIME"
rm -rf $INSTALLDIR/xo-builds/xen-orchestra-$TIME
return 0
fi
# Now that we know we're going to be building a new xen-orchestra, make
# sure there's no already-running xo-server process.
if [[ $(ps aux | grep xo-server | grep -v grep) ]]; then
echo
echo -ne "${PROGRESS} Shutting down xo-server"
/bin/systemctl stop xo-server || { echo -e "${FAIL} failed to stop service, exiting..." ; exit 1; }
echo -e "\r${OK} Shutting down xo-server"
fi
# If this isn't a fresh install, then list the upgrade the user is making.
if [[ ! -z "$OLD_REPO_HASH" ]]; then
echo
echo -e "${INFO} Updating xen-orchestra from '$OLD_REPO_HASH_SHORT' to '$NEW_REPO_HASH_SHORT'"
fi
echo
echo -e "${INFO} xo-server and xo-web build quite a while. Grab a cup of coffee and lay back"
echo
echo -ne "${PROGRESS} Running installation"
cd $INSTALLDIR/xo-builds/xen-orchestra-$TIME && yarn >/dev/null && yarn build >/dev/null
echo -e "\r${OK} Running installation"
# Install plugins
InstallXOPlugins
echo
echo -e "${INFO} Fixing binary path in systemd service configuration file"
sed -i "s#ExecStart=.*#ExecStart=$INSTALLDIR\/xo-server\/bin\/xo-server#" $INSTALLDIR/xo-builds/xen-orchestra-$TIME/packages/xo-server/xo-server.service
echo
echo -e "${INFO} Adding WorkingDirectory parameter to systemd service configuration file"
sed -i "/ExecStart=.*/a WorkingDirectory=$INSTALLDIR/xo-server" $INSTALLDIR/xo-builds/xen-orchestra-$TIME/packages/xo-server/xo-server.service
if [[ "$XOUSER" != "root" ]]; then
echo -e "${INFO} Adding user to systemd config"
sed -i "/SyslogIdentifier=.*/a User=$XOUSER" $INSTALLDIR/xo-builds/xen-orchestra-$TIME/packages/xo-server/xo-server.service
if [ "$PORT" -le "1024" ]; then
NODEBINARY="$(which node)"
if [[ -L "$NODEBINARY" ]]; then
NODEBINARY="$(readlink -e $NODEBINARY)"
fi
if [[ ! -z $NODEBINARY ]]; then
echo -ne "${PROGRESS} Attempting to set cap_net_bind_service permission for $NODEBINARY"
setcap 'cap_net_bind_service=+ep' $NODEBINARY >/dev/null \
&& echo -e "\r${OK} Attempting to set cap_net_bind_service permission for $NODEBINARY" || { echo -e "\r${FAIL} Attempting to set cap_net_bind_service permission for $NODEBINARY" ; echo " Non-privileged user might not be able to bind to <1024 port. xo-server won't start most likely" ; }
else
echo -e "${FAIL} Can't find node executable, or it's a symlink to non existing file. Not trying to setcap. xo-server won't start most likely"
fi
fi
fi
if [[ ! -f $CONFIGPATH/.config/xo-server/config.toml ]] || [[ "$CONFIGUPDATE" == "true" ]]; then
echo -e ${INFO} "Fixing relative path to xo-web installation in xo-server configuration file"
INSTALLDIRESC=$(echo $INSTALLDIR | sed 's/\//\\\//g')
sed -i "s/#'\/any\/url' = '\/path\/to\/directory'/'\/' = '$INSTALLDIRESC\/xo-web\/dist\/'/" $INSTALLDIR/xo-builds/xen-orchestra-$TIME/packages/xo-server/sample.config.toml
sleep 2
if [[ $PORT != "80" ]]; then
echo -e "${INFO} Changing port in xo-server configuration file"
sed -i "s/port = 80/port = $PORT/" $INSTALLDIR/xo-builds/xen-orchestra-$TIME/packages/xo-server/sample.config.toml
sleep 2
fi
if $HTTPS ; then
echo -e "${INFO} Enabling HTTPS in xo-server configuration file"
sed -i "s%# cert = '.\/certificate.pem'%cert = '$PATH_TO_HTTPS_CERT'%" $INSTALLDIR/xo-builds/xen-orchestra-$TIME/packages/xo-server/sample.config.toml
sed -i "s%# key = '.\/key.pem'%key = '$PATH_TO_HTTPS_KEY'%" $INSTALLDIR/xo-builds/xen-orchestra-$TIME/packages/xo-server/sample.config.toml
sed -i "s/# redirectToHttps/redirectToHttps/" $INSTALLDIR/xo-builds/xen-orchestra-$TIME/packages/xo-server/sample.config.toml
sleep 2
fi
echo -e "${INFO} Activating modified configuration file"
mkdir -p $CONFIGPATH/.config/xo-server
mv -f $INSTALLDIR/xo-builds/xen-orchestra-$TIME/packages/xo-server/sample.config.toml $CONFIGPATH/.config/xo-server/config.toml
fi
echo
echo -e "${INFO} Symlinking fresh xo-server install/update to $INSTALLDIR/xo-server"
ln -sfn $INSTALLDIR/xo-builds/xen-orchestra-$TIME/packages/xo-server $INSTALLDIR/xo-server
sleep 2
echo -e "${INFO} Symlinking fresh xo-web install/update to $INSTALLDIR/xo-web"
ln -sfn $INSTALLDIR/xo-builds/xen-orchestra-$TIME/packages/xo-web $INSTALLDIR/xo-web
if [ $XOUSER ]; then
chown -R $XOUSER:$XOUSER $INSTALLDIR/xo-builds/xen-orchestra-$TIME
if [ ! -d /var/lib/xo-server ]; then
mkdir /var/lib/xo-server 2>/dev/null
fi
chown -R $XOUSER:$XOUSER /var/lib/xo-server
fi
# fix to prevent older installations to not update because systemd service is not symlinked anymore
if [[ $(find /etc/systemd/system -maxdepth 1 -type l -name "xo-server.service") ]]; then
rm -f /etc/systemd/system/xo-server.service
fi
echo
echo -e "${INFO} Replacing systemd service configuration file"
/bin/cp -f $INSTALLDIR/xo-builds/xen-orchestra-$TIME/packages/xo-server/xo-server.service /etc/systemd/system/xo-server.service
sleep 2
echo -e "${INFO} Reloading systemd configuration"
echo
/bin/systemctl daemon-reload
sleep 2
echo
echo -e "${INFO} Starting xo-server..."
/bin/systemctl start xo-server >/dev/null
# no need to exit/trap on errors anymore
set +e
trap - ERR INT
timeout 60 bash <<-"EOF"
while [[ -z $(journalctl -u xo-server | sed -n 'H; /Starting XO Server/h; ${g;p;}' | grep "https\{0,1\}:\/\/\[::\]:$PORT") ]]; do
echo " waiting for port to be open"
sleep 10
done
EOF
if [[ $(journalctl -u xo-server | sed -n 'H; /Starting XO Server/h; ${g;p;}' | grep "https\{0,1\}:\/\/\[::\]:$PORT") ]]; then
echo
echo -e " ${COLOR_GREEN}WebUI started in port $PORT. Make sure you have firewall rules in place to allow access.${COLOR_N}"
echo -e " ${COLOR_GREEN}Default username: [email protected] password: admin${COLOR_N}"
echo
echo -e "${INFO} Installation successful. Enabling xo-server to start on reboot"
/bin/systemctl enable xo-server > /dev/null
else
echo
echo -e "${FAIL} Looks like there was a problem when starting xo-server/reading journalctl. Please see logs for more details"
journalctl -u xo-server -n 50 >> $LOGFILE
exit 1
fi
} 2>$LOGFILE
function UpdateXO {
InstallXO
if [[ "$PRESERVE" != "0" ]]; then
# remove old builds. leave as many as defined in PRESERVE variable
echo
echo -ne "${PROGRESS} Removing old installations. Leaving $PRESERVE latest"
find $INSTALLDIR/xo-builds/ -maxdepth 1 -type d -name "xen-orchestra*" -printf "%T@ %p\n" | sort -n | cut -d' ' -f2- | head -n -$PRESERVE | xargs -r rm -r
echo -e "\r${OK} Removing old installations. Leaving $PRESERVE latest"
else
echo -e "${INFO} PRESERVE variable is set to 0. This needs to be at least 1. Not doing a cleanup"
fi
} 2>$LOGFILE
function HandleArgs {
case "$1" in
--update)
UpdateNodeYarn
UpdateXO
;;
--install)
if [ $OSNAME == "CentOS" ]; then
InstallDependenciesCentOS
InstallXO
exit 0
else
InstallDependenciesDebian
InstallXO
exit 0
fi
;;
--rollback)
RollBackInstallation
exit 0
;;
*)
StartUpScreen
;;
esac
}
function RollBackInstallation {
INSTALLATIONS=($(find $INSTALLDIR/xo-builds/ -maxdepth 1 -type d -name "xen-orchestra-*"))
if [[ $(echo ${#INSTALLATIONS[@]}) -le 1 ]]; then
echo -e "${INFO} Only one installation exists, nothing to change"
exit 0
fi
echo "Which installation to roll back?"
echo
local PS3="Pick a number. CTRL+C to exit: "
select INSTALLATION in "${INSTALLATIONS[@]}"; do
case $INSTALLATION in
*xen-orchestra*)
echo
echo -e "${INFO} Setting $INSTALLDIR/xo-server symlink to $INSTALLATION/packages/xo-server"
ln -sfn $INSTALLATION/packages/xo-server $INSTALLDIR/xo-server
echo -e "${INFO} Setting $INSTALLDIR/xo-web symlink to $INSTALLATION/packages/xo-web"
ln -sfn $INSTALLATION/packages/xo-web $INSTALLDIR/xo-web
echo
echo -e "${INFO} Replacing xo.server.service systemd configuration file"
/bin/cp -f $INSTALLATION/packages/xo-server/xo-server.service /etc/systemd/system/xo-server.service
/bin/systemctl daemon-reload
echo
echo -e "${INFO} Restarting xo-server..."
/bin/systemctl restart xo-server
echo
break
;;
*)
echo -e "${FAIL} Try again"
;;
esac
done
}
function CheckOS {
if [ -f /etc/centos-release ] ; then
OSVERSION=$(grep -Eo "[0-9]" /etc/centos-release | head -1)
OSNAME="CentOS"
if [[ ! $OSVERSION == "7" ]]; then
echo -e "${FAIL} Only CentOS 7 supported"
exit 0
fi
elif [[ -f /etc/os-release ]]; then
OSVERSION=$(grep ^VERSION_ID /etc/os-release | cut -d'=' -f2 | grep -Eo "[0-9]{1,2}" | head -1)
OSNAME=$(grep ^NAME /etc/os-release | cut -d'=' -f2 | sed 's/"//g' | awk '{print $1}')
if [[ $OSNAME == "Debian" ]] && [[ ! $OSVERSION =~ ^(8|9|10)$ ]]; then
echo -e "${FAIL} Only Debian 8/9/10 supported"
exit 0
elif [[ $OSNAME == "Ubuntu" ]] && [[ ! $OSVERSION =~ ^(16|18)$ ]]; then
echo -e "${FAIL} Only Ubuntu 16/18 supported"
exit 0
fi
else
echo -e "${FAIL} Only CentOS 7 / Ubuntu 16/18 and Debian 8/9 supported"
exit 0
fi
} 2>$LOGFILE
function CheckSystemd {
if [ -z $(which systemctl) ]; then
echo -e "${FAIL} This tool is designed to work with systemd enabled systems only"
exit 0
fi
}
function CheckDocker {
if [ -z $(which docker) ]; then
echo
echo -e "${FAIL} Docker needs to be installed for this to work"
exit 0
fi
}
function CheckCertificate {
if [[ "$HTTPS" == "true" ]]; then
local CERT="$(openssl x509 -modulus -noout -in "$PATH_TO_HTTPS_CERT" | openssl md5)"
local KEY="$(openssl rsa -modulus -noout -in "$PATH_TO_HTTPS_KEY" | openssl md5)"
if [[ "$CERT" != "$KEY" ]]; then
echo
echo -e "${INFO} $PATH_TO_HTTPS_CERT:"
echo -e "${INFO} $CERT"
echo -e "${INFO} $PATH_TO_HTTPS_KEY:"
echo -e "${INFO} $KEY"
echo
echo -e "{$FAIL} MD5 of your TLS key and certificate dont match. Please check files and try again."
exit 1
fi
fi
} 2>$LOGFILE
function PullDockerImage {
echo
docker pull ronivay/xen-orchestra
echo
echo
echo -e "${INFO} Image pulled. Run container:"
echo " docker run -itd -p 80:80 ronivay/xen-orchestra"
echo
echo -e "${INFO} If you want to persist xen-orchestra and redis data, use volume flags like:"
echo " docker run -itd -p 80:80 -v /path/to/data/xo-server:/var/lib/xo-server -v /path/to/data/redis:/var/lib/redis ronivay/xen-orchestra"
} 2>$LOGFILE
function StartUpScreen {
echo "-----------------------------------------"
echo
echo "This script will automatically install/update Xen-Orchestra"
echo
echo "- By default xo-server will be running as root to prevent issues with permissions and port binding."
echo " uncomment and edit XOUSER variable in this script to run service as unprivileged user"
echo " (Notice that you might have to make other changes depending on your system for this to work)"
echo " This method only changes the user which runs the service. Other install tasks like node packages are still ran as root"
echo
echo "- Option 2. actually creates a new build from sources but works as an update to installations originally done with this tool"
echo " NodeJS and Yarn packages are updated automatically. Check AUTOUPDATE variable to disable this"
echo " Data stored in redis and /var/lib/xo-server/data will not be touched during update procedure."
echo " X (defined in PRESERVE variable) number of latest installations will be preserved and older ones are deleted after successful update. Fresh installation is symlinked as active"
echo " Rollback to another installation with --rollback"
echo
echo "- To run option 2. without interactive mode (as cronjob for automated updates for example) use --update"
echo
echo "Following options will be used for installation:"
echo
echo "OS: $OSNAME $OSVERSION"
echo "Basedir: $INSTALLDIR"
if [ $XOUSER ]; then
echo "User: $XOUSER"
else
echo "User: root"
fi
echo "Port: $PORT"
echo "Git Branch for source: $BRANCH"
echo "Following plugins will be installed: "$PLUGINS""
echo "Number of previous installations to preserve: $PRESERVE"
echo
echo "Errorlog is stored to $LOGFILE for debug purposes"
echo
echo "Xen Orchestra configuration will be stored to $CONFIGPATH/.config/xo-server/config.toml, if you don't want it to be replaced with every update, set CONFIGUPDATE to false in xo-install.cfg"
echo "-----------------------------------------"
echo
echo "1. Autoinstall"
echo "2. Update / Install without packages"
echo "3. Deploy docker container"
echo "4. Rollback to another existing installation"
echo "5. Exit"
echo
read -p ": " option
case $option in
1)
if [[ $(ps aux | grep xo-server | grep -v grep) ]]; then
echo "Looks like xo-server process is already running, consider running update instead. Continue anyway?"
read -p "[y/N]: " answer
case $answer in
y)
echo "Stopping xo-server..."
/bin/systemctl stop xo-server || { echo -e "${FAIL} failed to stop service, exiting..." ; exit 1; }
;;
n)
exit 0
;;
*)
exit 0
;;
esac
fi
if [ $OSNAME == "CentOS" ]; then
InstallDependenciesCentOS
InstallXO
exit 0
else
InstallDependenciesDebian
InstallXO
exit 0
fi
;;
2)
UpdateNodeYarn
UpdateXO
exit 0
;;
3)
CheckDocker
echo
PullDockerImage
;;
4)
RollBackInstallation
exit 0
;;
5)
exit 0
;;
*)
echo "Please choose one of the options"
echo
exit 0
;;
esac
}
CheckUser
CheckOS
CheckSystemd
CheckCertificate
if [[ $# == "1" ]]; then
HandleArgs "$1"
exit 0
else
StartUpScreen
fi