-
Notifications
You must be signed in to change notification settings - Fork 4
/
habidat.sh
executable file
·707 lines (649 loc) · 16.3 KB
/
habidat.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
#!/bin/bash
set +x
# general
if [ -f ./setup.env ]
then
source setup.env
fi
red=`tput setaf 1`
green=`tput setaf 2`
yellow=`tput setaf 3`
magenta=`tput setaf 5`
bold=`tput bold`
reset=`tput sgr0`
usage() {
prefix "Usage: habidat.sh ${txtunderline}COMMAND${txtreset}"
prefix
prefix "Commands:"
prefix " help show help."
prefix " install <module>|all [force] install module or all modules"
prefix " remove <module> remove module (caution: all module data is lost)"
prefix " start <module>|all start module or all modules"
prefix " restart <module>|all restart module or all modules"
prefix " stop <module>|all stop module or all modules"
prefix " up <module>|all [experimental!] up module or all modules (start and/or create containers)"
prefix " down <module>|all [experimental!]down module or all modules (stop and remove containers)"
prefix " update <module>|all update module or all modules"
prefix " pull <module>|all pull module or all modules"
prefix " build <module>|all [experimental!] build module or all modules (only if you changed the compose files to build images)"
prefix " export <module>|all [options] export module data"
prefix " import <module>|all <filename>|list import module data or list available filenames"
prefix " modules list module status"
prefix
}
upper() {
echo -n "$1" | tr '[a-z]' '[A-Z]'
}
prefixm() {
local p=`echo -n "$1" | tr '[a-z]' '[A-Z]'`
p="$p "
p="${p:0:12}"
p="${magenta}${bold}$p${reset}| "
local c="s/^/$p/"
sed -u -l 1 "$c"
# while read line
#do
#echo
#done
}
prefix() {
local p="$HABIDAT_TITLE "
p="${p:0:12}"
p="${green}${bold}$p${reset}| "
local c="s/^/$p/"
echo $1 | sed -u "$c"
}
prefixr() {
local p="$HABIDAT_TITLE "
p="${p:0:12}"
p="${red}${bold}$p${reset}| "
local c="s/^/$p/"
echo $1 | sed -u "$c"
}
print_done() {
prefix "DONE"
}
update_installed_modules() {
if [ -d "store/auth" ]
then
HABIDAT_USER_INSTALLED_MODULES="nginx,auth"
if [ -d "store/nextcloud" ]
then
HABIDAT_USER_INSTALLED_MODULES="$HABIDAT_USER_INSTALLED_MODULES,nextcloud"
fi
if [ -d "store/discourse" ]
then
HABIDAT_USER_INSTALLED_MODULES="$HABIDAT_USER_INSTALLED_MODULES,discourse"
fi
if [ -d "store/direktkredit" ]
then
HABIDAT_USER_INSTALLED_MODULES="$HABIDAT_USER_INSTALLED_MODULES,direktkredit"
fi
if [ -d "store/dokuwiki" ]
then
HABIDAT_USER_INSTALLED_MODULES="$HABIDAT_USER_INSTALLED_MODULES,dokuwiki"
fi
if [ -d "store/mediawiki" ]
then
HABIDAT_USER_INSTALLED_MODULES="$HABIDAT_USER_INSTALLED_MODULES,mediawiki"
fi
if [ -d "store/mailtrain" ]
then
HABIDAT_USER_INSTALLED_MODULES="$HABIDAT_USER_INSTALLED_MODULES,mailtrain"
fi
sed -i '/HABIDAT_USER_INSTALLED_MODULES/d' store/auth/user.env
echo "HABIDAT_USER_INSTALLED_MODULES=$HABIDAT_USER_INSTALLED_MODULES" >> store/auth/user.env
#docker compose -f store/auth/docker-compose.yml -p $HABIDAT_DOCKER_PREFIX-auth up -d user
fi
}
check_exists() {
if [ -d "store/$1" ]
then
if [ "$2" == "force" ]
then
prefix "Force reinstall $1, removing old installation...."
bash -c "docker compose -f store/$1/docker-compose.yml -p $HABIDAT_DOCKER_PREFIX-$1 down -v --remove-orphans" | prefixm "$1"
rm -rf "store/$1"
else
return 1
fi
fi
return 0
}
remove_module() {
if [ ! -d "store/$1" ]
then
prefix "Module $1 not installed, skip removing..."
exit 1
fi
prefix "Removing $1 module...."
if [ -f "$1/remove.sh" ]
then
cd $1
./remove.sh ${@:2} | prefixm $1
cd ..
else
docker compose -f "store/$1/docker-compose.yml" -p "$HABIDAT_DOCKER_PREFIX-$1" down -v --remove-orphans | prefixm "$1"
rm -rf "store/$1"
fi
update_installed_modules
}
start_module() {
if [ ! -d "store/$1" ]
then
prefix "Module $1 not installed, skip starting..."
return 0
fi
prefix "Starting $1 module...."
if [ -f "$1/start.sh" ]
then
cd $1
./start.sh | prefixm $1
cd ..
else
docker compose -f "store/$1/docker-compose.yml" -p "$HABIDAT_DOCKER_PREFIX-$1" start | prefixm "$1"
fi
}
restart_module() {
if [ ! -d "store/$1" ]
then
prefix "Module $1 not installed, skip restarting..."
return 0
fi
prefix "Restarting $1 module...."
if [ -f "$1/restart.sh" ]
then
cd $1
./restart.sh | prefixm $1
cd ..
else
docker compose -f "store/$1/docker-compose.yml" -p "$HABIDAT_DOCKER_PREFIX-$1" restart | prefixm "$1"
fi
}
stop_module() {
if [ ! -d "store/$1" ]
then
prefix "Module $1 not installed, skip stopping..."
return 0
fi
prefix "Stopping $1 module...."
if [ -f "$1/stop.sh" ]
then
cd $1
./stop.sh | prefixm $1
cd ..
else
docker compose -f "store/$1/docker-compose.yml" -p "$HABIDAT_DOCKER_PREFIX-$1" stop | prefixm "$1"
fi
}
down_module() {
if [ ! -d "store/$1" ]
then
prefix "Module $1 not installed, skip downing..."
return 0
fi
prefix "Downing $1 module...."
if [ -f "$1/down.sh" ]
then
cd $1
./down.sh | prefixm $1
cd ..
else
docker compose -f "store/$1/docker-compose.yml" -p "$HABIDAT_DOCKER_PREFIX-$1" down | prefixm "$1"
fi
}
up_module() {
if [ ! -d "store/$1" ]
then
prefix "Module $1 not installed, skip upping..."
return 0
fi
prefix "Upping $1 module...."
if [ -f "$1/up.sh" ]
then
cd "$1"
./up.sh | prefixm $1
cd ..
else
docker compose -f "store/$1/docker-compose.yml" -p "$HABIDAT_DOCKER_PREFIX-$1" up -d | prefixm "$1"
fi
}
pull_module() {
if [ ! -d "store/$1" ]
then
prefix "Module $1 not installed, skip pulling..."
return 0
fi
prefix "Pulling $1 module...."
if [ -f "$1/pull.sh" ]
then
cd "$1"
./pull.sh | prefixm $1
cd ..
else
docker compose -f "store/$1/docker-compose.yml" -p "$HABIDAT_DOCKER_PREFIX-$1" pull | prefixm "$1"
fi
}
build_module() {
if [ ! -d "store/$1" ]
then
prefix "Module $1 not installed, skip building..."
return 0
fi
prefix "Building $1 module...."
if [ -f "$1/build.sh" ]
then
cd "$1"
./build.sh | prefixm $1
cd ..
else
docker compose -f "store/$1/docker-compose.yml" -p "$HABIDAT_DOCKER_PREFIX-$1" build | prefixm "$1"
fi
}
setup_module() {
prefix "Setup $1 module..."
cd "$1"
./setup.sh ${@:2} | prefixm $1
cp version "../store/$1"
if [ -f dependencies ]
then
cp dependencies "../store/$1"
fi
cd ..
update_installed_modules
print_done
}
update_module() {
if [ "$1" == "nginx" ] || [ "$1" == "auth" ] || [ "$1" == "nextcloud" ] || [ "$1" == "discourse" ] || [ "$1" == "mediawiki" ] || [ "$1" == "dokuwiki" ] || [ "$1" == "direktkredit" ] || [ "$1" == "mailtrain" ]
then
if [ ! -d "store/$1" ]
then
prefixr "Module $1 not installed, cannot update"
return 0
fi
versionInstalled=$(cat store/$1/version)
versionSetup=$(cat $1/version)
if [ -z "$versionSetup" ] && [ "$2" != "force" ]
then
prefixr "Module $1: Setup version not found, cannot update, use force option to update anyway"
return 1
elif [ -z "$versionInstalled" ] && [ "$2" != "force" ]
then
prefixr "Module $1: Installed version not found, cannot update. Use force option to update anyway"
return 1
elif [ "$versionSetup" == "$versionInstalled" ] && [ "$2" != "force" ]
then
prefix "Module $1 is up to date, version $versionInstalled, use force option to update anyway"
return 0
elif [ "v$versionSetup" < "v$versionInstalled" ] && [ "$2" != "force" ]
then
prefixr "Module $1: installed version $versionInstalled is higher than setup version $versionSetup, downgrad not possible. Use force option to update anyway"
return 1
else
if [ ! -f "$1/update.sh" ]
then
prefixr "Module $1 has no update.sh script, cannot update"
return 1
fi
prefix "Updating module $1 from $versionInstalled to $versionSetup..."
cd "$1"
if [ "$2" == "force" ]
then
./update.sh ${@:3} | prefixm $1
else
./update.sh ${@:2} | prefixm $1
fi
cp version "../store/$1"
cd ..
print_done
fi
else
prefixr "Module $1 unknown, available modules are: nginx, auth, nextcloud, discourse, mediawiki, dokuwiki, direktkredit, mailtrain"
return 1
fi
}
export_module() {
if [ "$1" == "nginx" ] || [ "$1" == "auth" ] || [ "$1" == "nextcloud" ] || [ "$1" == "discourse" ] || [ "$1" == "mediawiki" ] || [ "$1" == "dokuwiki" ] || [ "$1" == "direktkredit" ] || [ "$1" == "mailtrain" ]
then
if [ ! -d "store/$1" ]
then
prefixr "Module $1 not installed, cannot export"
return 0
fi
if [ ! -f "$1/export.sh" ]
then
prefixr "Module $1 has no export.sh script, cannot export"
return 0
fi
cd "$1"
./export.sh $2 | prefixm $1
cd ..
print_done
else
prefixr "Module $1 unknown, available modules are: nginx, auth, nextcloud, discourse, mediawiki, dokuwiki, direktkredit, mailtrain"
return 1
fi
}
import_module() {
if [ "$1" == "nginx" ] || [ "$1" == "auth" ] || [ "$1" == "nextcloud" ] || [ "$1" == "discourse" ] || [ "$1" == "mediawiki" ] || [ "$1" == "dokuwiki" ] || [ "$1" == "direktkredit" ] || [ "$1" == "mailtrain" ]
then
if [ ! -d "store/$1" ]
then
prefixr "Module $1 not installed, cannot export"
return 0
fi
if [ ! -f "$1/import.sh" ]
then
prefixr "Module $1 has no import.sh script, cannot import"
return 0
fi
if [ "$2" == "list" ]; then
echo "Listing available filenames for import"
ls -ltr $HABIDAT_BACKUP_DIR/$HABIDAT_DOCKER_PREFIX/$1
return 0
else
if [ ! -f $HABIDAT_BACKUP_DIR/$HABIDAT_DOCKER_PREFIX/$1/$2 ]; then
echo "Import file $2 not found"
return 0
fi
cd "$1"
./import.sh $2 | prefixm $1
cd ..
print_done
fi
else
prefixr "Module $1 unknown, available modules are: nginx, auth, nextcloud, discourse, mediawiki, dokuwiki, direktkredit, mailtrain"
return 1
fi
}
check_dependencies () {
prefix "Checking dependencies for module $1..."
if [ ! -f "$1/dependencies" ]
then
return
fi
while read -r module
do
if [ ! -d "store/$module" ]
then
prefix "$module ${red}[NOT INSTALLED]${reset}"
dependencies_missing+=" "$module
else
prefix "$module ${green}[INSTALLED]${reset}"
fi
done < "$1/dependencies"
if [ ! -z "$dependencies_missing" ]
then
read -p "There are missing dependencies, do you want to install them? [y/n] " -n 1 -r
echo
if [[ ! $REPLY =~ ^[Yy]$ ]]
then
prefixr "Please install dependencies first, abort..."
exit 1
else
for setupModule in $dependencies_missing
do
dependencies_missing=""
check_dependencies $setupModule
setup_module $setupModule
done
fi
fi
}
check_child_dependecies () {
prefix "Checking child dependencies for module $1..."
dependencies_installed="false"
for installedModule in store/*/
do
dependenciesFile=$installedModule"dependencies"
if [ -f $dependenciesFile ]
then
while read -r module
do
if [ "$module" == "$1" ]
then
dependency=$(basename $(dirname $dependenciesFile))
echo "$dependency ${red}[INSTALLED]${reset}"
dependencies_installed="true"
fi
done < "$dependenciesFile"
fi
done
if [ $dependencies_installed == "true" ]
then
prefix "Please remove child dependencies first, abort..."
exit 1
fi
}
print_admin_credentials() {
if [ -f store/auth/passwords.env ]
then
source store/auth/passwords.env
prefix "habi*DAT admin credentials: username is ${bold}admin${reset}, password is ${bold}$HABIDAT_ADMIN_PASSWORD${reset}"
fi
}
if [ "$1" == "install" ]
then
if [ -z "$2" ]
then
usage
exit 1
fi
if [ $2 == "all" ]
then
for setupModule in "nginx" "auth" "nextcloud" "discourse" "dokuwiki" "direktkredit" "mailtrain"
do
check_exists "$setupModule" "$3"
if [ $? == "1" ] && [ "force" != "$3" ]
then
prefix "Module $setupModule already installed, skipping..."
else
check_dependencies $setupModule
setup_module $setupModule
fi
done
print_admin_credentials
else
check_exists "$2" "$3"
if [ "$?" == "1" ] && [ "$2" != "mediawiki" ]
then
prefixr "Module $2 already installed, update module or remove module first"
exit 1
fi
if [ "$2" == "nginx" ] || [ "$2" == "auth" ] || [ "$2" == "nextcloud" ] || [ "$2" == "discourse" ] || [ "$2" == "mediawiki" ] || [ "$2" == "dokuwiki" ] || [ "$2" == "direktkredit" ] || [ "$2" == "mailtrain" ]
then
check_dependencies $2
setup_module ${@:2}
print_admin_credentials
else
prefixr "Module $2 unknown, available modules are: nginx, auth, nextcloud, discourse, mediawiki, dokuwiki, direktkredit, mailtrain"
exit 1
fi
fi
elif [ "$1" == "rm" ]
then
if [ -z "$2" ]
then
usage
exit 1
fi
if [ "$2" == "nginx" ] || [ "$2" == "auth" ] || [ "$2" == "nextcloud" ] || [ "$2" == "discourse" ] || [ "$2" == "mediawiki" ] || [ "$2" == "dokuwiki" ] || [ "$2" == "direktkredit" ] || [ "$2" == "mailtrain" ]
then
read -p "Do you really want to remove module $2 (all data will be lost) [y/n] " -n 1 -r
echo
if [[ ! $REPLY =~ ^[Yy]$ ]]
then
exit 1
fi
check_child_dependecies $2
remove_module ${@:2}
else
prefixr "Module $2 unknown, available modules are: nginx, auth, nextcloud, discourse, mediawiki, dokuwiki, direktkredit, mailtrain"
fi
elif [ "$1" == "modules" ]
then
for module in "nginx" "auth" "nextcloud" "discourse" "mediawiki" "dokuwiki" "direktkredit" "mailtrain"
do
if [ -d "store/$module" ]
then
prefix "$module ${green}[INSTALLED]${reset}"
else
prefix "$module ${yellow}[NOT INSTALLED]${reset}"
fi
done
elif [ "$1" == "update" ]
then
if [ -z "$2" ]
then
usage
exit 1
elif [ "$2" == "all" ]
then
for updateModule in "nginx" "auth" "nextcloud" "discourse" "mediawiki" "dokuwiki" "direktkredit" "mailtrain"
do
update_module $updateModule ${@:3}
done
else
update_module ${@:2}
fi
elif [ "$1" == "start" ]
then
if [ -z "$2" ]
then
usage
exit 1
elif [ "$2" == "all" ]
then
for mod in "nginx" "auth" "nextcloud" "discourse" "mediawiki" "dokuwiki" "direktkredit" "mailtrain"
do
start_module $mod
done
else
start_module $2
fi
elif [ "$1" == "build" ]
then
if [ -z "$2" ]
then
usage
exit 1
elif [ "$2" == "all" ]
then
for mod in "nginx" "auth" "nextcloud" "discourse" "mediawiki" "dokuwiki" "direktkredit" "mailtrain"
do
build_module $mod
done
else
build_module $2
fi
elif [ "$1" == "restart" ]
then
if [ -z "$2" ]
then
usage
exit 1
elif [ "$2" == "all" ]
then
for mod in "nginx" "auth" "nextcloud" "discourse" "mediawiki" "dokuwiki" "direktkredit" "mailtrain"
do
restart_module $mod
done
else
restart_module $2
fi
elif [ "$1" == "stop" ]
then
if [ -z "$2" ]
then
usage
exit 1
elif [ "$2" == "all" ]
then
for mod in "nginx" "auth" "nextcloud" "discourse" "mediawiki" "dokuwiki" "direktkredit" "mailtrain"
do
stop_module $mod
done
else
stop_module $2
fi
elif [ "$1" == "down" ]
then
if [ -z "$2" ]
then
usage
exit 1
elif [ "$2" == "all" ]
then
for mod in "nginx" "auth" "nextcloud" "discourse" "mediawiki" "dokuwiki" "direktkredit" "mailtrain"
do
down_module $mod
done
else
down_module $2
fi
elif [ "$1" == "up" ]
then
if [ -z "$2" ]
then
usage
exit 1
elif [ "$2" == "all" ]
then
for mod in "nginx" "auth" "nextcloud" "discourse" "mediawiki" "dokuwiki" "direktkredit" "mailtrain"
do
up_module $mod
done
else
up_module $2
fi
elif [ "$1" == "pull" ]
then
if [ -z "$2" ]
then
usage
exit 1
elif [ "$2" == "all" ]
then
for mod in "nginx" "auth" "nextcloud" "discourse" "mediawiki" "dokuwiki" "direktkredit" "mailtrain"
do
pull_module $mod
done
else
pull_module $2
fi
elif [ "$1" == "export" ]
then
if [ -z "$2" ]
then
usage
exit 1
elif [ "$2" == "all" ]
then
for exportModule in "nginx" "auth" "nextcloud" "discourse" "mediawiki" "dokuwiki" "direktkredit" "mailtrain"
do
export_module $exportModule $3
done
else
export_module $2 $3
fi
elif [ "$1" == "import" ]
then
if [ $# -ne 3 ]
then
usage
exit 1
elif [ "$2" == "all" ]
then
echo "Import can only be done per module"
exit 1
else
import_module $2 $3
fi
elif [ "$1" == "help" ]
then
usage
exit 0
else
usage
exit 1
fi