-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathdbwebb2.bash
1635 lines (1269 loc) · 40.3 KB
/
dbwebb2.bash
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
#------------------------- HERE ARE THE MAIN COMMANDS -----------------------
#
# Create the config file .dbwebb.config.
#
function createConfig()
{
local first=$1
local noInput=$2
local acronym
local remoteHost
# Check if run as sudo, use SUDO_USER as HOME
# (useful for selfupdate through sudo)
local home="$HOME"
if [[ $SUDO_USER ]]; then
home=$( eval echo "~$SUDO_USER" )
fi
if [ -z $first ]
then
printf "The config-file '$DBW_CONFIG_FILE_NAME' will now be created in your home directory: '$home'"
elif [ $first = "update" ]
then
printf "Your config file will be automatically updated. Then re-run your command.\n"
elif [ $first = "upgrade" ]
then
printf "Your config file will be automatically updated."
# # Temporary to solve upgrades from v1.9.29 to v1.9.32
# unset DBW_SSH_KEY
# To change lab url when upgrading to v2.2.1
unset DBW_LABURL
elif [ $first = "selfupdate" ]
then
# # Temporary to solve upgrades from v1.9.29 to v1.9.32
# unset DBW_SSH_KEY
# To change lab url when upgrading to v2.2.1
unset DBW_LABURL
# To fix upgrade with wrong DBW_SSH_KEY, fix in v2.3.0
unset DBW_SSH_KEY
elif [ $first = "create" ]
then
printf "I will now re-create the configuration file '$DBW_CONFIG_FILE_NAME' in your home directory: '$home'.\n"
fi
if [[ ! $noInput ]]; then
DBW_USER=${DBW_USER:-$USER}
printf "\nWhat is your student acronym? [$DBW_USER] "
read acronym
unset DBW_BASEURL
else
DBW_USER=${DBW_USER:-noname}
fi
acronym=${acronym:-$DBW_USER}
sshKey=${DBW_SSH_KEY:-$home/.ssh/dbwebb}
remoteHost=${DBW_HOST:-ssh.student.bth.se}
remoteDir=${DBW_REMOTE_BASEDIR:-dbwebb-kurser}
remoteWwwHost=${DBW_WWW_HOST:-http://www.student.bth.se/}
remoteWww=${DBW_REMOTE_WWWDIR:-www/dbwebb-kurser}
baseurl=${DBW_BASEURL:-http://www.student.bth.se/~$acronym/$remoteDir}
laburl=${DBW_LABURL:-https://lab.dbwebb.se}
echo "DBW_USER='$acronym'" > "$DBW_CONFIG_FILE"
echo "DBW_HOST='$remoteHost'" >> "$DBW_CONFIG_FILE"
echo "DBW_WWW_HOST='$remoteWwwHost'" >> "$DBW_CONFIG_FILE"
echo "DBW_SSH_KEY=\"$sshKey\"" >> "$DBW_CONFIG_FILE"
echo "DBW_REMOTE_BASEDIR='$remoteDir'" >> "$DBW_CONFIG_FILE"
echo "DBW_REMOTE_WWWDIR='$remoteWww'" >> "$DBW_CONFIG_FILE"
echo "DBW_BASEURL='$baseurl'" >> "$DBW_CONFIG_FILE"
echo "DBW_LABURL='$laburl'" >> "$DBW_CONFIG_FILE"
printf "$MSG_DONE The config file '$DBW_CONFIG_FILE' is now up-to-date.\n"
}
#
# Create default files.
#
function dbwebb-init-me()
{
checkIfValidCourseRepoOrExit
local mapFile="$DBW_COURSE_DIR/.dbwebb.map"
if [ -f "$mapFile" ]; then
local intro="Creating and initiating the directory structure in 'me/' (will not overwrite existing files)."
local command="createDirsInMeFromMapFile"
local message="to init the directory 'me/'."
executeCommandInSubShell "$intro" "$command" "$message"
return
fi
local meDefault="$DBW_COURSE_DIR/.default/"
local me="$DBW_COURSE_DIR/me/"
local intro="Creating and initiating the directory 'me/' by copying directory structure and files from the directory '.default/' (will not overwrite existing files)."
local command="rsync -av $RSYNC_CHMOD --exclude README.md --ignore-existing \"$meDefault\" \"$me\""
local message="to init the directory 'me/'."
executeCommand "$intro" "$command" "$message"
}
#
# Troubleshoot an installation and check part by part.
#
function dbwebb-trouble()
{
printf "### Lets check the installed commands:\nbash git ssh rsync wget curl.\n"
pressEnterToContinue
cmd="bash"
printf "# %s " "$cmd"
printf "%s " "$( which $cmd )"
printf "%s \n" "$( $cmd --version | head -1 )"
cmd="git"
printf "# %s " "$cmd"
printf "%s " "$( which $cmd )"
printf "%s \n" "$( $cmd --version | head -1 )"
cmd="ssh"
printf "# %s " "$cmd"
printf "%s " "$( which $cmd )"
printf "%s \n" "$( $cmd -V | head -1 )"
cmd="rsync"
printf "# %s " "$cmd"
printf "%s " "$( which $cmd )"
printf "%s \n" "$( $cmd --version | head -1 )"
cmd="wget"
printf "# %s " "$cmd"
printf "%s " "$( which $cmd )"
printf "%s \n" "$( $cmd --version | head -1 )"
cmd="curl"
printf "# %s " "$cmd"
printf "%s " "$( which $cmd )"
printf "%s \n" "$( $cmd --version | head -1 )"
printf "\n### Lets check who you are.\n"
pressEnterToContinue
printf "whoami: '%s'\n" "$( whoami )"
printf "\$HOME: '%s'\n" "$HOME"
printf "\$HOMEPATH (win): '%s'\n" "$HOMEPATH"
printf "\$USERPROFILE (win): '%s'\n" "$USERPROFILE"
printf "\n### Lets check the dbwebb.config file.\n"
pressEnterToContinue
checkIfValidConfigOrExit
printf "\$DBW_CONFIG_FILE: '%s'\n" "$DBW_CONFIG_FILE"
printf "ls -l \$DBW_CONFIG_FILE: '%s'\n" "$( ls -l $DBW_CONFIG_FILE )"
printf "file \$DBW_CONFIG_FILE: '%s'\n" "$( file $DBW_CONFIG_FILE )"
printf "cat \$DBW_CONFIG_FILE: \n%s\n" "$( cat $DBW_CONFIG_FILE )"
printf "\n### Check if we can ping bth.se.\n"
pressEnterToContinue
printf "ping -c 1 bth.se: \n'%s'\n" "$( ping -c 1 bth.se )"
printf "\n### Try login to the student server and execute a command.\n"
pressEnterToContinue
printf "ssh ${DBW_USER}@${DBW_HOST} 'pwd && ls -ld dbwebb-kurser && ls -l dbwebb-kurser':\n"
ssh ${DBW_USER}@${DBW_HOST} 'pwd && ls -ld dbwebb-kurser && ls -l dbwebb-kurser'
printf "\n### Try login to the student server using ssh-keys.\n"
pressEnterToContinue
printf "\$DBW_SSH_KEY: '%s'\n" "$DBW_SSH_KEY"
printf "\$DBW_SSH_KEY_OPTION: '%s'\n" "$DBW_SSH_KEY_OPTION"
printf "ls -ld $HOME/.ssh: '%s'\n" "$( ls -ld $HOME/.ssh )"
printf "ls -l $HOME/.ssh:'%s'\n" "$( ls -l $HOME/.ssh)"
printf "\$SSH_CMD: '%s'\n" "$SSH_CMD"
eval $SSH_CMD pwd
printf "\n### Lets do some checks with your course repo. You must 'cd' into a valid course repo, for example python or htmlphp.\n"
pressEnterToContinue
checkIfValidCourseRepoOrExit
printf "\$DBW_COURSE_DIR: '%s'\n" "$DBW_COURSE_DIR"
printf "ls -ld $DBW_COURSE_DIR/..: \n%s\n" "$( ls -ld $DBW_COURSE_DIR/.. )"
printf "ls -l $DBW_COURSE_DIR/..: \n%s\n" "$( ls -l $DBW_COURSE_DIR/..)"
printf "\n### Lets init the course repo, step by step.\n"
pressEnterToContinue
VERY_VERBOSE="yes"
printf "dbwebb-init-me:\n"
pressEnterToContinue
dbwebb-init-me
printf "dbwebb-init-server:\n"
pressEnterToContinue
dbwebb-init-server
printf "dbwebb-init-structure-dbwebb-kurser:\n"
pressEnterToContinue
dbwebb-init-structure-dbwebb-kurser
printf "dbwebb-init-structure-www-dbwebb-kurser:\n"
pressEnterToContinue
dbwebb-init-structure-www-dbwebb-kurser
}
#
# Init directory structure at the server.
#
function dbwebb-init-server()
{
local intro="Intiating the remote server '$DBW_HOST' by connecting as '$DBW_USER' and creating directories (if needed) where all uploaded files will reside."
# TODO Should use DBW_BASEDIR
local command="$SSH_CMD 'sh -c \"if [ ! -d \"$DBW_REMOTE_BASEDIR\" ]; then mkdir \"$DBW_REMOTE_BASEDIR\"; fi; chmod 700 \"$DBW_REMOTE_BASEDIR\"; echo; echo \"$DBW_REMOTE_BASEDIR:\"; ls -lF \"$DBW_REMOTE_BASEDIR\"; if [ ! -d \"$DBW_REMOTE_WWWDIR\" ]; then mkdir \"$DBW_REMOTE_WWWDIR\"; fi; chmod 755 \"$DBW_REMOTE_WWWDIR\"; echo; echo \"$DBW_REMOTE_WWWDIR:\"; ls -lF \"$DBW_REMOTE_WWWDIR\"\"'"
local message="to init the base dirs on the server."
checkIfValidConfigOrExit
executeCommand "$intro" "$command" "$message"
}
#
# Create default directory structure on the server for dbwebb-kurser/.
#
function dbwebb-init-structure-dbwebb-kurser()
{
local intro="Ensuring that the directory structure exists on the server by syncing directory structure to dbwebb-kurser/ (will not overwrite existing files)."
local command=
local message="to init the directory structure on the server."
checkIfValidConfigOrExit
checkIfValidCourseRepoOrExit
WHAT="$DBW_COURSE_DIR"
WHERE="$DBW_REMOTE_DESTINATION"
ITEM=""
SUBDIR=""
createUploadDownloadPaths
command="rsync -av $RSYNC_CHMOD --exclude example $RSYNC_INCLUDE $RSYNC_EXCLUDE --include='*/' --include='.??*' --exclude='*' -e \"ssh $DBW_SSH_KEY_OPTION\" '$WHAT' '$WHERE'"
executeCommand "$intro" "$command" "$message"
}
#
# Create default directory structure on the server for dbwebb-kurser/.
#
function dbwebb-init-structure-www-dbwebb-kurser()
{
local intro="Ensuring that the directory structure exists on the server by syncing the me/ directory structure to www/dbwebb-kurser (will not overwrite existing files)."
local command=
local message="to init the www-directory structure on the server."
checkIfValidConfigOrExit
checkIfValidCourseRepoOrExit
WHAT="$DBW_COURSE_DIR"
WHERE="$DBW_REMOTE_WWW_DESTINATION"
ITEM=""
SUBDIR=""
createUploadDownloadPaths
command="rsync -av $RSYNC_CHMOD --exclude example $RSYNC_INCLUDE $RSYNC_EXCLUDE --include='*/' --exclude='*' -e \"ssh $DBW_SSH_KEY_OPTION\" '$WHAT' '$WHERE'"
executeCommand "$intro" "$command" "$message"
}
#
# Init course repo and directory structure at the server.
#
function dbwebb-init()
{
dbwebb-init-me
dbwebb-init-server
dbwebb-init-structure-dbwebb-kurser
dbwebb-init-structure-www-dbwebb-kurser
}
#
# Create and use ssh-keys to login.
#
function dbwebb-sshkey()
{
local sshkey="$HOME/.ssh/dbwebb"
if [ ! -d "$HOME/.ssh" ]
then
mkdir "$HOME/.ssh"
fi
printf "First we need to create a ssh key and store it locally."
printf "\nPress enter/return to continue..."
read void
ssh-keygen -f "$sshkey" -N ''
# Bug (?) om Cygwin & win 8
# TODO refactor
#if [ $IS_CYGWIN = "yes" ]; then
# chgrp -vR "$CYGWIN_DEFAULT_GROUP" "$HOME/.ssh"
#fi
chmod 700 "$HOME/.ssh"
chmod 600 "$sshkey" "$sshkey.pub"
intro="I will now install the ssh-key at the remote server."
command="cat '$sshkey.pub' | ssh $DBW_USER@$DBW_HOST 'sh -c \"if [ ! -d .ssh ]; then mkdir .ssh; fi; chmod 700 .ssh; touch .ssh/authorized_keys; cat >> .ssh/authorized_keys\"'"
message="to install the ssh-keys."
executeCommand "$intro" "$command" "$message"
}
#
# Login to the server
#
function dbwebb-login()
{
local intro="I will now login to the server '$DBW_HOST' as '$DBW_USER' using ssh-keys if available."
local command="$SSH_CMD"
local message="to establish the connection."
checkIfValidConfigOrExit
executeCommand "$intro" "$command" "$message"
}
#
# Update course repo to latest version
#
function dbwebb-update()
{
local intro="Update course-repo with latest changes from its master at GitHub."
local command="git pull --rebase"
local message="to update course repo."
checkIfValidCourseRepoOrExit
executeCommand "$intro" "$command" "$message"
}
#
# Display information on the environment
#
function dbwebb-check()
{
printf "Details on installed utilities."
printf "\n------------------------------------"
printf "\nbash: %s" "$( checkCommand bash )"
printf "\ngit: %s" "$( checkCommand git )"
printf "\nssh: %s" "$( checkCommand ssh )"
printf "\nrsync: %s" "$( checkCommand rsync )"
printf "\nwget: %s" "$( checkCommand wget )"
printf "\ncurl: %s" "$( checkCommand curl )"
printf "\n"
printf "\nDetails on the dbwebb-environment."
printf "\n------------------------------------"
printf "\nOperatingsystem: $DBW_OS"
printf "\nCommand issued: $DBW_EXECUTABLE"
printf "\nVersion of dbwebb: $DBW_VERSION"
printf "\nPath to executable: '$DBW_EXECUTABLE_DIR'"
printf "\nConfig-file: '$DBW_CONFIG_FILE'"
printf "\nWorking directory: '$DBW_CURRENT_DIR'"
printf "\nLocal user: '$USER'"
printf "\nLocal homedir: '$HOME'"
printf "\nRemote user: '$DBW_USER'"
printf "\nRemote host: '$DBW_HOST'"
printf "\n"
echo
echo "Details on current course-repo."
echo "------------------------------------"
if [ "$DBW_COURSE_REPO_VALID" = "yes" ]; then
echo "Current course-repo: '$DBW_COURSE'"
echo "Course directory: '$DBW_COURSE_DIR'"
echo "Remote origin: $( cd $DBW_COURSE_DIR && git config --get remote.origin.url )"
echo "Course-repo version: $( cd $DBW_COURSE_DIR && git describe --always )"
echo "Latest update to course repo was:"
echo
(cd "$DBW_COURSE_DIR" && git log -1)
echo
else
echo "This is not a valid course repo."
echo
fi
if contains python "$@"; then
echo "Details on Python installed utilities."
echo "------------------------------------"
echo "python3: $( checkCommand python3 )"
echo "pip3: $( checkCommand pip3 )"
echo
fi
}
#
# Create or re-create the config file.
#
function dbwebb-config()
{
createConfig $1 $2
}
#
# Push/upload results to the server
#
function dbwebb-upload()
{
WHAT="$DBW_COURSE_DIR"
WHERE="$DBW_REMOTE_DESTINATION"
ITEM="$1"
SUBDIR=""
checkIfValidConfigOrExit
checkIfValidCourseRepoOrExit
createUploadDownloadPaths ignoreSubdir
#setChmod
local intro="Uploading the directory '$WHAT' to '$WHERE'."
local command="$RSYNC_CMD $OVERWRITE '$WHAT' '$WHERE'"
local message="to upload data."
executeCommand "$intro" "$command" "$message"
}
#
# Ping the server with a message
#
function dbwebb-ping()
{
local what="$1"
local action="$2"
local extra="$3"
local url="http://www.student.bth.se/~mosstud/dbwebb-ping/"
local subdir=
local where=
local res=
local qs=
local timer=
checkIfValidConfigOrExit
checkIfValidCourseRepoOrExit
subdir="$( mapCmdToDir $what )"
where="$DBW_COURSE_DIR/$subdir"
checkIfValidCombination "$subdir" "$what"
checkIfSubdirExistsOrProposeInit "$where"
timer=$( timeScript )
qs="course=$DBW_COURSE&acronym=$DBW_USER&what=$what&subdir=$subdir&action=$action&extra=$extra&timer=$timer"
qs=${qs// /+}
url="${OPTION_BASE_URL:-$url}?$qs"
veryVerbose "dbwebb-ping to: $url"
res=$( getUrlToStdout "$url" )
veryVerbose "dbwebb-ping response: $res"
}
#
# Pull/download from the server
#
function dbwebb-download()
{
WHAT="$DBW_COURSE_DIR"
WHERE="$DBW_REMOTE_DESTINATION"
ITEM="$1"
SUBDIR=""
local who="$2"
local really="really?"
if [ "$who" != "$DBW_USER" ]; then
WHERE="${DBW_USER}@${DBW_HOST}:~$who/$DBW_REMOTE_BASEDIR/$DBW_COURSE"
fi
checkIfValidConfigOrExit
checkIfValidCourseRepoOrExit
createUploadDownloadPaths
local command=
local overwrite=
if [[ $OVERWRITE ]]; then
command="$RSYNC_DOWNLOAD_DELETE_CMD $OVERWRITE '$WHERE' '$WHAT'"
overwrite="WILL BE"
else
command="$RSYNC_DOWNLOAD_CMD '$WHERE' '$WHAT'"
overwrite="WILL NOT BE"
fi
[[ $YES ]] && really=
local intro="Downloading the directory '$WHERE' to '$WHAT'. Existing local files that are newer $overwrite overwritten."
local message="to download data."
executeCommand "$intro" "$command" "$message" "$really"
}
#
# Validate the uploaded files
#
function dbwebb-validate()
{
checkIfValidConfigOrExit
checkIfValidCourseRepoOrExit
#setChmod
local dry=
[[ $OPTION_DRY ]] && dry="--dry"
WHAT="$DBW_COURSE_DIR"
WHERE="$DBW_REMOTE_DESTINATION"
ITEM="$1"
SUBDIR=""
createUploadDownloadPaths ignoreSubdir
local log="$HOME/.dbwebb-validate.log"
local intro="Uploading the directory '$WHAT' to '$WHERE' for validation."
local command1="$RSYNC_CMD $OVERWRITE '$WHAT' '$WHERE'"
# local command2="rsync -av $RSYNC_CHMOD $OVERWRITE --exclude .git --exclude .gitignore --exclude .default --exclude .solution --exclude .old --include='.??*' --exclude='*' -e \"ssh $DBW_SSH_KEY_OPTION\" '$DBW_COURSE_DIR/' '$DBW_REMOTE_DESTINATION/'"
local command3="$SSH_CMD 'dbwebb-validate1 $dry --course-repo \"$DBW_REMOTE_BASEDIR/$DBW_COURSE\" \"$DBW_REMOTE_BASEDIR/$DBW_COURSE/$SUBDIR\"' 2>&1 | tee '$log'; test \${PIPESTATUS[0]} -eq 0"
local message="to validate course results.\nSaved a log of the output: less -R '$log'"
#executeCommand "$intro" "$command1; $command2; $command3" "$message"
executeCommand "$intro" "$command1; $command3" "$message"
}
#
# Publish the uploaded files
#
function dbwebb-publish()
{
checkIfValidConfigOrExit
checkIfValidCourseRepoOrExit
#setChmod
[[ $OPTION_DRY ]] && dry="--dry"
WHAT="$DBW_COURSE_DIR"
WHERE="$DBW_REMOTE_DESTINATION"
ITEM="$1"
SUBDIR=""
createUploadDownloadPaths ignoreSubdir
local log="$HOME/.dbwebb-publish.log"
local intro="Uploading the directory '$WHAT' to '$WHERE' to validate and publish."
local command1="$RSYNC_CMD $OVERWRITE '$WHAT' '$WHERE'"
# local command2="rsync -av $RSYNC_CHMOD $OVERWRITE --exclude .git --exclude .gitignore --exclude .default --exclude .solution --exclude .old --include='.??*' --exclude='*' -e \"ssh $DBW_SSH_KEY_OPTION\" '$DBW_COURSE_DIR/' '$DBW_REMOTE_DESTINATION/'"
local command3="$SSH_CMD 'dbwebb-validate1 $dry $PUBLISH_OPTIONS --publish --course-repo \"$DBW_REMOTE_BASEDIR/$DBW_COURSE\" --publish-to \"$DBW_REMOTE_WWWDIR/$DBW_COURSE/$SUBDIR\" --publish-root \"$DBW_REMOTE_WWWDIR/$DBW_COURSE\" \"$DBW_REMOTE_BASEDIR/$DBW_COURSE/$SUBDIR\"' 2>&1 | tee '$log'; test \${PIPESTATUS[0]} -eq 0"
local message="to validate and publish course results.\nSaved a log of the output: less -R '$log'"
#executeCommand "$intro" "$command1; $command2; $command3" "$message"
executeCommand "$intro" "$command1; $command3" "$message"
if [ $? -eq 0 ]; then
printf "Your files are now"
else
printf "Some of your files might be"
fi
printf " published on:\n $DBW_BASEURL/$DBW_COURSE/$SUBDIR\n"
}
#
# Publish the uploaded files without validation.
#
function dbwebb-fastpublish()
{
PUBLISH_OPTIONS="--no-validate"
dbwebb-publish "$1" "$2" "$3"
}
dbwebb-publishfast()
{
dbwebb-fastpublish "$*"
}
#
# Publish the uploaded files
#
function dbwebb-purepublish()
{
PUBLISH_OPTIONS="--no-validate --no-minification"
dbwebb-publish "$1" "$2" "$3"
}
dbwebb-publishpure()
{
dbwebb-purepublish "$*"
}
#
# Run command on remote server
#
function dbwebb-run()
{
local cmd="$*"
local cwd=
# Prepare options
if [[ ! $VERY_VERBOSE ]]; then
SILENT="yes"
fi
if [[ $OPTION_CWD ]]; then
cwd="cd $OPTION_CWD; "
fi
checkIfValidConfigOrExit
# Create ssh-command for host
local server=${OPTION_HOSTNAME:-$DBW_HOST}
local ssh_cmd="ssh ${DBW_USER}@${server} $DBW_SSH_KEY_OPTION"
local intro="I will now login to the server '$server' as '$DBW_USER' and execute the command '$cwd$cmd'."
local command="$SSH_CMD -t \"$cwd$cmd\""
local message="to run the command."
executeCommand "$intro" "$command" "$message"
}
#
# Inspect uploaded files
#
function dbwebb-inspect()
{
local course=
local kmom=
local who=
local forWho=" for user '$DBW_USER'"
local forCourse=
local willUpload=
local archive=
local yes=
local useVersion=
local noValidate=
checkIfValidConfigOrExit
if [[ $3 ]]; then
course="$1"
forCourse=" in course '$course'"
kmom="$2"
who="$3"
forWho=" for user '$who'"
if [ "$who" != "$DBW_USER" -a -z "$OPTION_NOARCHIVE" ]; then
archive="--archive $DBW_ARCHIVE"
fi
elif [[ $2 ]]; then
course="$1"
forCourse=" in course '$course'"
kmom="$2"
elif [[ $1 ]]; then
course="$DBW_COURSE"
kmom="$1"
forCourse=" in course repo '$course'"
willUpload=" I will start by uploading the course repo to the remote server."
else
usageInspect
exit 0
fi
inspecUser=${who:=$DBW_USER}
[[ $YES ]] && yes="--yes"
[[ $PORT ]] && port="--port $PORT"
[[ $USE_VERSION ]] && useVersion="--useVersion $USE_VERSION"
[[ $NO_VALIDATE ]] && noValidate="--no-validate"
local intro="I will now inspect '$kmom'${forCourse}${forWho}.$willUpload"
local log="$HOME/.dbwebb-inspect.log"
local command1=
local command2="$SSH_CMD_INTERACTIVE \"dbwebb-inspect1 $yes $noValidate $port $useVersion $archive --publish-url $DBW_BASEURL --publish-to ~$DBW_USER/$DBW_REMOTE_WWWDIR --base-url $DBW_WWW_HOST~$inspecUser/$DBW_REMOTE_BASEDIR ~$inspecUser/$DBW_REMOTE_BASEDIR/$course $kmom\" 2>&1 | tee '$log'; test \${PIPESTATUS[0]} -eq 0"
local message="to inspect the course results.\nSaved a log of the output, review it as:\nless -R '$log'"
# Upload only if
if [[ $willUpload ]]; then
checkIfValidCourseRepoOrExit
#setChmod
command1="$RSYNC_CMD $OVERWRITE '$DBW_COURSE_DIR/' '$DBW_REMOTE_DESTINATION/';"
fi
executeCommand "$intro" "$command1 $command2" "$message"
}
#
# Re-create a lab
#
dbwebb-recreate()
{
dbwebb-create "$1" "$2" "save-answers"
}
#
# Support re-create a lab by saving the lab answer
#
moveLabAnswer()
{
local where="$1"
local postfixFrom="$2"
local postfixTo="$3"
for extension in php js py bash
do
from="$where/answer.$extension$postfixFrom"
to="$where/answer.$extension$postfixTo"
if [ -f "$from" ]; then
echo "(moving answer.$extension$postfixFrom to answer.$extension$postfixTo)"
mv "$from" "$to"
fi
done
}
#
# Create a lab
#
dbwebb-create()
{
local myWget=
local lab="$1"
local version="$2"
local save="$3"
local subdir="$( mapCmdToDir $lab )"
local where="$DBW_COURSE_DIR/$subdir"
if [ -z "$subdir" ]; then
printf "$MSG_FAILED Not a valid combination of '$DBW_COURSE' and '$lab'.\n"
exit 2
fi
checkIfValidConfigOrExit
checkIfValidCourseRepoOrExit
# If no lab version supplied, read it from course repo if available
if [ -z "$version" -a -f "$DBW_COURSE_DIR/.dbwebb/lab.version" ]; then
version=$( cat "$DBW_COURSE_DIR/.dbwebb/lab.version" )
fi
local lab_version=
[[ $version ]] && lab_version=" ($version)"
printf "Creating $DBW_COURSE $lab${lab_version} in '$where'.\n"
# Check if init was run?
if [ ! -d "$where" ]; then
printf "$MSG_FAILED The directory '$where' is missing.\nDid you run the command 'dbwebb init'?\n"
exit 2
fi
# Check if lab is already there
if [ -f "$where/answer.php" -o -f "$where/answer.js" -o -f "$where/answer.py" -o -f "$where/answer.bash" ]; then
if [[ $save ]]; then
moveLabAnswer "$where" "" "_$$"
else
printf "$MSG_FAILED You have already created lab-files at: '$where'\nRemove the files in the directory, then you can generate new files.\n"
exit 2
fi
fi
# Check for wget or curl
myWget="wget -qO"
#myWget="curl -so"
# Get the lab bundle
#http://localhost/git/lab/?action=bundle&key=a07e843ca67d647900e8259729119ddf
#http://localhost/git/lab/?action=bundle&course=linux&lab=lab1&acronym=mos&doGenerate
local bundleQuery="?action=bundle&acronym=$DBW_USER&course=$DBW_COURSE&lab=$lab&version=$version&doGenerate=Submit"
printf "Downloading and extracting lab bundle\n"
[[ $VERY_VERBOSE ]] && echo " ($DBW_LABURL/$bundleQuery)"
[[ $VERY_VERBOSE ]] && echo " ($myWget $where/bundle.tar '$DBW_LABURL/$bundleQuery')"
$myWget "$where/bundle.tar" "$DBW_LABURL/$bundleQuery"
[[ $? > 0 ]] && printf "$MSG_FAILED Failed to download lab bundle.\n" && exit 1
tar -xmf "$where/bundle.tar" -C "$where"
[[ $? > 0 ]] && printf "$MSG_FAILED The downloaded lab bundle is not a tar archive. You may cat it to check for errors.\n" && exit 1
rm -f "$where/bundle.tar"
if [[ $save ]]; then
moveLabAnswer "$where" "_$$" ""
fi
printf "$MSG_DONE You can find the lab and all files here:\n"
echo "'$where'"
ls -lF "$where"
}
#
# Deal with gui inspect
#
dbwebb-gui()
{
local action="${1:-run}"
case "$action" in
help)
usageGui
exit 0
;;
config)
dbwebb-gui-config
exit 0
;;
install \
| selfupdate)
dbwebb-gui-install
exit 0
;;
run)
dbwebb-gui-run
exit 0
;;
version)
dbwebb-gui-version
exit 0
;;
*)
badUsageGui "$MSG_FAILED gui subcommand not recognized."
exit 2
;;
esac
}
#
# Maintain the user config file for dbwebb gui
#
dbwebb-gui-config()
{
local script=
script="$( which dbwebb-inspect-gui )"
if [[ -x "$script" ]]; then
verbose "Execute command for gui bash '$script'..."
bash "$script" config
else
die "dbwebb gui is not installed, check 'dbwebb gui help'."
fi
}
#
# Run gui inspect
#
dbwebb-gui-run()
{
local script=
script="$( which dbwebb-inspect-gui )"
checkIfValidConfigOrExit
checkIfValidCourseRepoOrExit
if [[ -x "$script" ]]; then
verbose "Execute command for gui bash '$script'..."
bash "$script"
else
die "dbwebb gui is not installed, check 'dbwebb gui help'."
fi
}
#
# Install/update gui inspect
#
dbwebb-gui-install()
{
local url="https://raw.githubusercontent.com/dbwebb-se/inspect-gui/master/gui.bash"
local target="/usr/local/bin"
local what="dbwebb-inspect-gui"
printf "Installing (selfupdating) '%s' to '%s' from\n %s\n" "$what" "$target" "$url"
getUrlToFile "$url" "$target/$what" "overwrite-if-exists" \
|| die "Failed to download/install '$what' to '$target' (perhaps sudo?)."
chmod 755 "$target/$what"
printf "The updated version is now: "
dbwebb-gui-version
}
#
# Check the version of gui inspect
#
dbwebb-gui-version()
{
local script=
script="$( which dbwebb-inspect-gui )"
if [[ -x "$script" ]]; then
verbose "Execute command for gui bash '$script'..."
bash "$script" version
else
die "dbwebb gui is not installed, check 'dbwebb gui help'."
fi
}
#
# Deal with exams
#
dbwebb-exam()
{
local action="$1"
local what="$2"
local version="$3"
case "$action" in
help)
usageExam
exit 0
;;
list \
| ls)
dbwebb-exam-list
exit 0
;;
receipt \
| r)
dbwebb-exam-receipt "$what"
exit 0
;;
start \
| checkout \
| c)
dbwebb-exam-start "$what" "$version"
exit 0
;;
stop \
| seal \
| s)
dbwebb-exam-stop "$what"