-
Notifications
You must be signed in to change notification settings - Fork 29
/
Copy pathCODES.sh
executable file
·1490 lines (1268 loc) · 43.3 KB
/
CODES.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
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/bin/bash
# BEGIN-NOTICE
# Copyright (C) 2014 Benjamin Porter
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
# END-NOTICE
# Colors made a little easier
restore='\033[0m'
black='\033[0;30m'
red='\033[0;31m'
green='\033[0;32m'
brown='\033[0;33m'
blue='\033[0;34m'
purple='\033[0;35m'
cyan='\033[0;36m'
light_gray='\033[0;37m'
dark_gray='\033[1;30m'
light_red='\033[1;31m'
light_green='\033[1;32m'
yellow='\033[1;33m'
light_blue='\033[1;34m'
light_purple='\033[1;35m'
light_cyan='\033[1;36m'
white='\033[1;37m'
MAINTAINER_EMAIL='[email protected]'
RUBY_VER='2.4.0'
canvasdir="$HOME"
checkoutname="canvas-lms"
canvaslocation="${canvasdir}/${checkoutname}"
error ()
{
echo -e "${red}${1}${restore}" >&2
}
die ()
{
error "$1" >&2
exit 1
}
white ()
{
echo -ne "${white}${1}${restore}"
}
green ()
{
echo -ne "${green}${1}${restore}"
}
cyan ()
{
echo -ne "${cyan}${1}${restore}"
}
red ()
{
echo -ne "${red}${1}${restore}"
}
yellow ()
{
echo -ne "${yellow}${1}${restore}"
}
runningOSX ()
{
uname -a | grep "Darwin" > /dev/null 2>&1
}
runningFedora ()
{
if $(which lsb_release >/dev/null 2>&1); then
lsb_release -d | grep --color=auto "Fedora" > /dev/null 2>&1
else
uname -a | grep --color=auto "fc" > /dev/null 2>&1
fi
}
runningUbuntu ()
{
if $(which lsb_release >/dev/null 2>&1); then
lsb_release -d | grep --color=auto "Ubuntu" > /dev/null 2>&1
else
uname -a | grep --color=auto "Ubuntu" > /dev/null 2>&1
fi
}
runningArch ()
{
if $(which lsb_release >/dev/null 2>&1); then
lsb_release -d | grep "Arch" >/dev/null 2>&1
else
uname -a | grep --color=auto "ARCH" > /dev/null 2>&1
fi
}
runningMint ()
{
if $(which lsb_release >/dev/null 2>&1); then
lsb_release -d | grep --color=auto "Mint" > /dev/null 2>&1
else
return 1
fi
}
# Derivatives like Mint and Elementary usually run the Ubuntu kernel so this can be an easy way to detect an Ubuntu derivative
runningUbuntuKernel ()
{
uname -a | grep --color=auto "Ubuntu" > /dev/null 2>&1
}
runningWhat ()
{
if runningOSX; then
echo "Mac OS X"
elif runningFedora; then
echo "Fedora Linux"
elif runningUbuntu; then
echo "Ubuntu Linux"
elif runningArch; then
echo "Arch Linux"
elif runningMint; then
echo "Linux Mint"
else
echo "Unknown"
fi
}
runningUnsupported ()
{
if [ "$(runningWhat)" = "Unknown" ]; then
return 0
else
return 1
fi
}
chrubySupported ()
{
if runningOSX; then
return 0
elif runningFedora; then
return 1
elif runningUbuntu; then
return 1
elif runningArch; then
return 0
elif runningMint; then
return 1
else
return 1
fi
}
rubyInstallSupported ()
{
if runningOSX; then
return 0
elif runningFedora; then
return 1
elif runningUbuntu; then
return 1
elif runningArch; then
return 0
elif runningMint; then
return 1
else
return 1
fi
}
installDistroDependencies ()
{
if runningArch; then
cyan "If you haven't recently done a pacman -Syu then stuff might fail. Run one now? (Y/[N]): "
read PACMANSYU
if [[ $PACMANSYU =~ [Yy] ]]; then
sudo pacman -Syu
fi
fi
green "Installing any distro specific dependencies\n"
if runningOSX; then
:
elif runningFedora; then
sudo dnf -y install ruby-devel
sudo dnf -y install libxml2-devel
sudo dnf -y install libxslt-devel
sudo dnf -y install libpqxx-devel
sudo dnf -y install sqlite-devel
sudo dnf -y install postgresql
sudo dnf -y install postgresql-devel
sudo dnf -y install postgresql-server
elif runningUbuntu; then
sudo apt-get update
green "Finished running 'apt-get update'. Installing packages\n"
sudo apt-get -y install ruby-dev
sudo apt-get -y install zlib1g-dev
sudo apt-get -y install rubygems1.9.1
sudo apt-get -y install libxml2-dev
sudo apt-get -y install libxslt1-dev
sudo apt-get -y install libsqlite3-dev
sudo apt-get -y install libhttpclient-ruby
sudo apt-get -y install imagemagick
sudo apt-get -y install libxmlsec1-dev
sudo apt-get -y install python-software-properties
sudo apt-get -y install postgresql
sudo apt-get -y install postgresql-contrib
sudo apt-get -y install libpq-dev
sudo apt-get -y install libpqxx-dev
sudo apt-get -y install ruby-pg
sudo apt-get -y install build-essential
sudo apt-get -y install libglib2.0
elif runningArch; then
sudo pacman -S --needed --noconfirm lsb-release
sudo pacman -S --needed --noconfirm curl
sudo pacman -S --needed --noconfirm libxslt
sudo pacman -S --needed --noconfirm python2
elif runningMint; then
sudo apt-get update
green "Finished running 'apt-get update'. Installing packages\n"
sudo apt-get -y install ruby-dev
sudo apt-get -y install zlib1g-dev
sudo apt-get -y install rubygems1.9.1
sudo apt-get -y install libxml2-dev
sudo apt-get -y install libxslt1-dev
sudo apt-get -y install libsqlite3-dev
sudo apt-get -y install libhttpclient-ruby
sudo apt-get -y install imagemagick
sudo apt-get -y install libxmlsec1-dev
sudo apt-get -y install python-software-properties
sudo apt-get -y install postgresql
sudo apt-get -y install postgresql-contrib
sudo apt-get -y install libpq-dev
sudo apt-get -y install libpqxx-dev
sudo apt-get -y install ruby-pg
sudo apt-get -y install build-essential
sudo apt-get -y install libglib2.0
else
:
fi
}
setLocale ()
{
green "Setting locale\n"
if runningArch && ! $(locale | grep "LANG=en_US.UTF-8" >/dev/null 2>&1); then
cyan "Your locale is not currently set to en_US.UTF-8.\n"
cyan "Press <Enter> and I'll change it for you, or Ctrl+C to quit\n"
read
sudo sh -c "echo 'en_US.UTF-8 UTF-8' >> /etc/locale.gen"
sudo locale-gen
sudo sh -c "echo 'LANG=en_US.UTF-8' > /etc/locale.conf"
. /etc/locale.conf
fi
}
aurinstall ()
{
green "Installing $1 from the aur\n"
AUR_DIR="/tmp/aur"
AUR_BUILD_DIR="$AUR_DIR/build"
AUR_TARBALLS_DIR="$AUR_DIR/tarballs"
mkdir -p "$AUR_BUILD_DIR"
mkdir -p "$AUR_TARBALLS_DIR"
prevdir="$(pwd)"
cd "$AUR_TARBALLS_DIR"
curl -L -O "$1"
tarball="$(basename $1)"
output_dir="$(echo $tarball | sed -e 's/\.tar.*//g')"
cd "$AUR_BUILD_DIR"
tar xf "$AUR_TARBALLS_DIR/$tarball"
cd "$output_dir"
ASROOT=''
[ "$(id -u)" = 0 ] && ASROOT="--asroot"
makepkg $ASROOT --clean --syncdeps --needed --noconfirm --install
cd "$prevdir"
}
hasBrew ()
{
if $(which brew >/dev/null 2>&1); then
green "Brew is installed\n"
return 0
else
yellow "Brew is NOT installed\n"
return 1
fi
}
askreinstallbrew ()
{
cyan "\nBrew is already installed, but sometimes reinstalling it fixes errors."
cyan "\nReinstall brew? (Y/[N]): "
read REINSTALL
if [[ $REINSTALL =~ [Yy] ]]; then
return 0
else
return 1
fi
}
installBrew ()
{
green "Installing brew if necessary\n"
if runningOSX; then
if ! hasBrew || askreinstallbrew; then
cyan "We're going to install the OS X command line tools through brew. You will have to agree to Apple's terms\n"
cyan "Please click the install button in the dialog that will be shown in a minute or so\n"
cyan "Press <Enter> to continue: "
read
ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
read -r -d '' VAR << "__EOF__"
# Added by canvas-lms setup-development script
# This adds the brew bin to your PATH
if $(which brew >/dev/null 2>&1); then
export PATH="$PATH:$(brew --prefix)/bin"
fi
__EOF__
yellow "\nYou will need to have '$(brew --prefix)/bin' in your PATH variable to run brew programs.\n"
yellow "This can be done easily by adding these lines of code to '${HOME}/.bash_profile':\n\n"
white "$VAR\n\n"
yellow "Do this now? (If not make sure you do it manually) ([Y]/N): "
read addLines
if ! [[ $addLines =~ [nN] ]]; then
echo "" >> ~/.bash_profile
echo "$VAR" >> ~/.bash_profile
fi
fi
# make sure brew stuff is in our path
if ! $(echo "$PATH" | sed -e 's/:/\n/g' | grep "$(brew --prefix)/bin" >/dev/null 2>&1); then
yellow "Brew bin is not in PATH. Adding...\n"
export PATH="$PATH:$(brew --prefix)/bin"
white "New PATH is '$PATH'\n"
fi
hasBrew
fi
}
hasRuby ()
{
if $(which ruby >/dev/null 2>&1); then
green "Ruby is installed\n"
return 0
else
yellow "Ruby is NOT installed\n"
return 1
fi
}
installRuby ()
{
green "Installing ruby if necessary\n"
if ! $(which ruby > /dev/null 2>&1); then
if $(runningOSX); then
brew reinstall ruby
elif $(runningFedora); then
sudo dnf -y install ruby
elif $(runningUbuntu); then
sudo apt-get -y install ruby
elif $(runningArch); then
sudo pacman -S --needed --noconfirm ruby
elif $(runningMint); then
sudo apt-get -y install ruby
fi
fi
hasRuby
}
hasNodejs ()
{
NODE=node
NPM=npm
if runningUbuntu || runningMint; then
NODE=nodejs
fi
if $(which $NODE >/dev/null 2>&1) && $(which $NPM >/dev/null 2>&1); then
green "Nodejs is installed\n"
return 0
else
yellow "Nodejs is NOT installed\n"
return 1
fi
}
installNodejs ()
{
green "Installing node.js if necessary\n"
if ! hasNodejs; then
if runningOSX; then
brew reinstall node
elif runningFedora; then
sudo dnf -y install nodejs npm
elif runningUbuntu; then
curl -sL https://deb.nodesource.com/setup_6.x | sudo bash -
sudo apt-get update
sudo apt-get -y install nodejs
elif runningArch; then
sudo pacman -S --needed --noconfirm nodejs
elif runningMint; then
sudo apt-get -y install nodejs
fi
fi
hasNodejs
}
hasChruby ()
{
if $(type chruby 2>&1 | grep "chruby is a function" >/dev/null 2>&1); then
green "Chruby is installed\n"
return 0
else
yellow "Chruby is NOT installed\n"
return 1
fi
}
addChrubySourcingToFile ()
{
green "Adding chruby sourcing to a bash startup file\n"
f="$HOME/.bashrc"
[ -n "$1" ] && f="$1"
if [ -f "$f" ] && $(cat "$f" | egrep "Added by the canvas.lms setup script" >/dev/null 2>&1); then
yellow "Bashrc already has sourcing commands for chruby\n"
else
echo "" >> "$f"
echo "# Added by the canvas-lms setup script" >> "$f"
echo "# These settings make chruby work" >> "$f"
echo "# See https://github.com/postmodern/chruby" >> "$f"
[ -f /usr/local/share/chruby/chruby.sh ] && \
echo "[ -f /usr/local/share/chruby/chruby.sh ] && . /usr/local/share/chruby/chruby.sh" >> "$f"
[ -f /usr/local/share/chruby/auto.sh ] && \
echo "[ -f /usr/local/share/chruby/auto.sh ] && . /usr/local/share/chruby/auto.sh" >> "$f"
[ -f /usr/share/chruby/chruby.sh ] && \
echo "[ -f /usr/share/chruby/chruby.sh ] && . /usr/share/chruby/chruby.sh" >> "$f"
[ -f /usr/share/chruby/auto.sh ] && \
echo "[ -f /usr/share/chruby/auto.sh ] && . /usr/share/chruby/auto.sh" >> "$f"
[ -f /usr/local/opt/chruby/share/chruby/chruby.sh ] && \
echo "[ -f /usr/local/opt/chruby/share/chruby/chruby.sh ] && . /usr/local/opt/chruby/share/chruby/chruby.sh" >> "$f"
[ -f /usr/local/opt/chruby/share/chruby/auto.sh ] && \
echo "[ -f /usr/local/opt/chruby/share/chruby/auto.sh ] && . /usr/local/opt/chruby/share/chruby/auto.sh" >> "$f"
fi
}
sourceChruby ()
{
# source now so chruby works immediately
[ -f /usr/local/share/chruby/chruby.sh ] && . /usr/local/share/chruby/chruby.sh
[ -f /usr/local/share/chruby/auto.sh ] && . /usr/local/share/chruby/auto.sh
[ -f /usr/share/chruby/chruby.sh ] && . /usr/share/chruby/chruby.sh
[ -f /usr/share/chruby/auto.sh ] && . /usr/share/chruby/auto.sh
hasChruby
}
installChruby ()
{
green "Installing chruby if necessary\n"
# ruby-install is installed in a separate method
if ! hasChruby; then
if runningOSX; then
brew reinstall chruby
addChrubySourcingToFile "$HOME/.bash_profile"
elif runningFedora; then
:
elif runningUbuntu; then
:
elif runningArch; then
aurinstall "https://aur.archlinux.org/packages/ch/chruby/chruby.tar.gz"
elif runningMint; then
:
fi
addChrubySourcingToFile
sourceChruby
fi
hasChruby
}
hasRubyinstall ()
{
if $(which ruby-install >/dev/null 2>&1); then
green "Ruby-install is installed\n"
return 0
else
yellow "Ruby-install is NOT installed\n"
return 1
fi
}
installRubyinstall ()
{
green "Installing ruby-install if necessary\n"
if ! hasRubyinstall; then
if runningOSX; then
brew reinstall ruby-install
elif runningFedora; then
:
elif runningUbuntu; then
:
elif runningArch; then
aurinstall "https://aur.archlinux.org/packages/ru/ruby-install-git/ruby-install-git.tar.gz"
elif runningMint; then
:
fi
fi
hasRubyinstall
}
writeChrubyFile ()
{
green "Writing chruby file to repo for version $CHRUBY_VERSION\n"
CHRUBY_VERSION="ruby-$RUBY_VER"
green "Writing Ruby version \"$CHRUBY_VERSION\" to file\n"
echo "$CHRUBY_VERSION" > .ruby-version
}
hasPostgres ()
{
if $(which psql >/dev/null 2>&1); then
green "PostgreSQL is installed\n"
return 0
else
yellow "PostgreSQL is NOT installed\n"
return 1
fi
}
installPostgres ()
{
green "Installing PostgreSQL if necessary\n"
if ! hasPostgres; then
if runningOSX; then
brew reinstall postgresql
elif runningFedora; then
sudo dnf -y install postgresql postgresql-devel postgresql-server
elif runningUbuntu; then
sudo apt-get -y install postgresql postgresql-contrib
elif runningArch; then
sudo pacman -S --needed --noconfirm postgresql
elif runningMint; then
sudo apt-get -y install postgresql postgresql-contrib
fi
fi
hasPostgres
}
addPostgresUser()
{
sudo mkdir -p /var/{lib,log}/postgres
if ! runningOSX; then
sudo useradd --no-create-home --system postgres
fi
if runningOSX; then
sudo chown $(whoami) /var/{lib,log}/postgres
else
sudo chown postgres /var/{lib,log}/postgres
fi
}
configurePostgres ()
{
if ! hasPostgres; then
installPostgres
fi
if ! hasPostgres; then
red "Could not configure Postgres because it does not appear to be installed\n"
return 1
fi
green "Configuring PostgreSQL\n"
addPostgresUser
if runningOSX; then
initdb --locale en_US.UTF-8 -E UTF8 -D /var/lib/postgres/data
pg_ctl -D /var/lib/postgres/data -l /var/log/postgres/server.log start
pg_ctl -D /var/lib/postgres/data stop
fi
if runningFedora; then
sudo postgresql-setup initdb
sudo systemctl start postgresql.service
fi
if runningArch; then
# make sure there is a postgres user
sudo -u postgres initdb --locale en_US.UTF-8 -E UTF8 -D /var/lib/postgres/data
sudo -u postgres pg_ctl -D /var/lib/postgres/data -l /var/log/postgres/server.log start
fi
if runningUbuntu || runningMint; then
sudo -u postgres service postgresql start
fi
if ! runningOSX; then
# Give PG some time to start up ..
cyan "Waiting 5 seconds for the PostgreSQL server to start...\n"
sleep 5
if ! $(sudo -u postgres psql postgres -tAc "SELECT 1 FROM pg_roles WHERE rolname='$(whoami)'" | grep "1" >/dev/null); then
sudo -u postgres createuser --createdb --login --createrole --superuser --replication $(whoami)
fi
fi
runningArch && sudo -u postgres pg_ctl -D /var/lib/postgres/data stop
if runningArch || runningFedora; then
sudo systemctl enable postgresql.service
fi
}
postgresRunning ()
{
if runningArch || runningFedora; then
systemctl status postgresql.service
elif runningUbuntu || runningMint; then
service postgresql status
else
ps auxwww | grep -E "postgres\s-D" >/dev/null 2>&1
fi
if [ "$?" = "0" ]; then
green "PostgreSQL server is running\n"
return 0
else
yellow "PostgreSQL server is NOT running\n"
return 1
fi
}
startPostgres ()
{
green "Starting PostgreSQL\n"
if ! postgresRunning; then
if runningArch || runningFedora; then
sudo systemctl start postgresql
elif runningUbuntu || runningMint; then
sudo -u postgres service postgresql start
else
pg_ctl -D /var/lib/postgres/data -l /var/log/postgres/server.log start
fi
fi
cyan "Waiting 5 seconds for the PostgreSQL server to start...\n"
sleep 5
postgresRunning
}
hasGit ()
{
if $(which git >/dev/null 2>&1); then
green "Git is installed\n"
return 0
else
yellow "Git is NOT installed"
return 1
fi
}
installGit ()
{
green "Installing git if necessary\n"
if ! hasGit; then
if runningOSX; then
brew reinstall git
elif runningFedora; then
sudo dnf -y install git
elif runningUbuntu; then
sudo apt-get -y install git
elif runningArch; then
sudo pacman -S --needed --noconfirm git
elif runningMint; then
sudo apt-get -y install git
fi
fi
hasGit
}
cloneCanvas ()
{
green "Cloning canvas into '$canvaslocation'\n"
cd "$canvasdir"
if [ -d "$checkoutname" ]; then
cyan "You may already have a canvas checkout (the directory exists).\n"
cyan "Delete it and reclone? (Y/[N]): "
read RESP
if [[ $RESP =~ [Yy] ]]; then
# For some reason we don't have permissions to delete some files
# unless we use sudo :(
sudo rm -rf "$checkoutname"
else
return 0
fi
fi
git clone "$CLONE_URL" "$checkoutname"
}
installNpmPackages ()
{
green "Installing required npm assets\n"
[ -n "$NPM" ] || NPM=npm
# If yarn is installed, favor that over npm
if $(which yarn >/dev/null 2>&1); then
NPM="$(which yarn)"
fi
if runningArch; then
# sudo $NPM install --python=python$(python2 --version 2>&1 | sed -e 's/Python //g')
$NPM install --python=python2 || {
sudo chown $(whoami) -R "$HOME/.npm"
$NPM install --python=python2
}
else
$NPM install || {
sudo chown $(whoami) -R "$HOME/.npm"
$NPM install
}
fi
}
assetFailCheckContinue ()
{
yellow "\nThe asset compilation failed.\n"
yellow "You can continue setup but the assets will need to be\n"
yellow "successfully built before you can run Canvas\n"
yellow "(You build them with 'bundle exec rake canvas:compile_assets')\n"
cyan "Continue with setup? ([Y]/N): "
read CONTINUESETUP
if [[ $CONTINUESETUP =~ [Nn] ]]; then
return 1
else
return 0
fi
}
buildCanvasAssets ()
{
green "Compiling Canvas assets\n"
bundle exec rake canvas:compile_assets || {
yellow "The asset compilation failed. This might be a permissions thing.\n"
cyan "Re-run the compile with sudo? (Y/[N]): "
read RECOMPILE
if [[ $RECOMPILE =~ [yY] ]]; then
pathGems
sudo bundle exec rake canvas:compile_assets || {
assetFailCheckContinue
return $?
}
else
assetFailCheckContinue
return $?
fi
}
}
createDatabaseConfigFile ()
{
green "Creating initial database config files\n"
for c in amazon_s3 delayed_jobs domain file_store outgoing_mail security scribd external_migration database; do
cp -v "config/$c.yml.example" "config/$c.yml"
done
}
databaseExists ()
{
if $(psql -lqt | cut -d \| -f 1 | grep -w "$1" >/dev/null 2>&1); then
green "PostgreSQL database '$1' exists\n"
return 0
else
yellow "PostgreSQL database '$1' DOES NOT exist\n"
return 1
fi
}
createDatabases ()
{
green "Creating initial databases if necessary\n"
if $(which createdb >/dev/null 2>&1); then
databaseExists "canvas_development" || createdb canvas_development
databaseExists "canvas_queue_development" || createdb canvas_queue_development
databaseExists "canvas_test" || createdb canvas_test
else
red "The binary 'createdb' is not in PATH. Make sure PostgreSQL is installed correctly\n"
return 1
fi
}
populateDatabases ()
{
createDatabases
green "Populating initial databases\n"
bundle exec rake db:initial_setup
# Required for running tests
if databaseExists "canvas_test"; then
psql -c 'CREATE USER canvas' -d canvas_test
psql -c 'GRANT ALL PRIVILEGES ON DATABASE canvas_test TO canvas' -d canvas_test
psql -c 'GRANT ALL PRIVILEGES ON ALL TABLES IN SCHEMA public TO canvas' -d canvas_test
psql -c 'GRANT ALL PRIVILEGES ON ALL SEQUENCES IN SCHEMA public TO canvas' -d canvas_test
else
red "The test dabases \"canvas_test\" does not exist. Could not set it up\n"
return 1
fi
}
hasCtags ()
{
if $(which ctags >/dev/null 2>&1); then
green "ctags is installed\n"
return 0
else
yellow "ctags is NOT installed\n"
return 1
fi
}
installCtags ()
{
green "Installing ctags if necessary\n"
if ! hasCtags; then
if runningOSX; then
brew reinstall ctags
elif runningFedora; then
sudo dnf -y install ctags
elif runningUbuntu; then
sudo apt-get -y install ctags
elif runningArch; then
sudo pacman -S --needed --noconfirm ctags
elif runningMint; then
sudo apt-get -y install ctags
fi
fi
}
generateCtags ()
{
hasCtags || installCtags
green "Generating ctags tags file\n"
if hasCtags; then
green "Generating ctags tag file\n"
ctags -R --exclude=.git --exclude=log --languages=ruby . $(bundle list --paths | xargs)
fi
}
hasBundler ()
{
if $(which bundler >/dev/null 2>&1); then
green "Bundler is installed\n"
return 0
else
yellow "Bundler is NOT installed\n"
return 1
fi
}
bashSource ()
{
if runningOSX; then
echo "${HOME}/.bash_profile"
else
echo "${HOME}/.bashrc"
fi
}
pathGems ()
{
green "Making sure the gem location is in PATH\n"
read -r -d '' VAR << "__EOF__"
# Added by canvas-lms setup-development script
# This adds the gem bin to your PATH
if ! $(echo $PATH | grep "$(gem env 'GEM_PATHS' | sed -e 's|:|/bin:|g')/bin" >/dev/null 2>&1); then
export PATH="$PATH:$(gem env 'GEM_PATHS' | sed -e 's|:|/bin:|g')/bin"
fi
__EOF__
if ! $(echo $PATH | grep "$(gem env 'GEM_PATHS' | sed -e 's|:|/bin:|g')/bin" >/dev/null 2>&1); then
export PATH="$PATH:$(gem env 'GEM_PATHS' | sed -e 's|:|/bin:|g')/bin"
cyan "The Gems bin wasn't in your PATH. I added it temporarily, but you may want to make it permanent\n"
cyan "This can be done by adding these lines to '$(bashSource)':\n\n"
echo "$VAR"
cyan "\nDo this now? ([Y]/N): "
read ADDLINES
if ! [[ $ADDLINES =~ [nN] ]]; then
echo "" >> "$(bashSource)"
echo "$VAR" >> "$(bashSource)"
fi
fi
}
installBundler ()
{
green "Installing bundler if necessary\n"
pathGems
# Install the latest version possible and set BUNDLE_VER
# Try to read the bundler version straight from the gem file
[ -f Gemfile.d/_before.rb ] && \
BUNDLE_VER=$(ruby -e "$(cat Gemfile.d/_before.rb | grep req_bundler_version | head -1); puts \"#{req_bundler_version_ceiling}\"")
if [ -n "$BUNDLE_VER" ]; then
green "Installing the bundle gem version $BUNDLE_VER\n"
gem install bundler -v "$BUNDLE_VER" || \
{ yellow "Installing bundler without sudo failed. Trying again with sudo...\n"; sudo gem install bundler -v "$BUNDLE_VER"; }
elif ! hasBundler; then
green "Installing the bundle gem newest version\n"
gem install bundler || { yellow "Installing bundler without sudo failed. Trying again with sudo...\n"; sudo gem install bundler; }
fi
hasBundler
}
installGems ()
{
green "Installing bundler gems with bundle install (but no mysql)\n"
pathGems
if runningOSX; then
# Dependency required for building postgres extensions on OS X
ARCHFLAGS="-arch x86_64" gem install pg || { yellow "Installing pg without sudo failed. Trying again with sudo...\n"; sudo ARCHFLAGS="-arch x86_64" gem install pg; }
# Patch required for building the thrift gem on OS X
bundle config build.thrift "--with-cppflags=-D_FORTIFY_SOURCE=0"