forked from MRColorR/money4band
-
Notifications
You must be signed in to change notification settings - Fork 0
/
runme.ps1
1333 lines (1222 loc) · 60.4 KB
/
runme.ps1
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/pwsh
set-executionpolicy -scope CurrentUser -executionPolicy Bypass -Force
### Variables and constants ###
## Script variables ##
# Script version #
$SCRIPT_VERSION = "2.2.0" # used for checking updates
# Script name #
$SCRIPT_NAME = $MyInvocation.MyCommand.Name # save the script name in a variable, not the full path
# Script URL for update #
$UPDATE_SCRIPT_URL = "https://raw.githubusercontent.com/MRColorR/money4band/main/$SCRIPT_NAME"
# Script debug log file #
$DEBUG_LOG = "debug_$SCRIPT_NAME.log"
## Env file related constants and variables ##
# .env file prototype link #
$ENV_SRC = 'https://github.com/MRColorR/money4band/raw/main/.env'
# Env file default #
$DEVICE_NAME_PLACEHOLDER = 'yourDeviceName'
$script:DEVICE_NAME = 'yourDeviceName'
# Proxy config #
$script:PROXY_CONF = $false
$script:CURRENT_PROXY = ''
$script:NEW_STACK_PROXY = ''
## Config file related constants and variables ##
$script:CONFIG_JSON_FILE = "config.json"
## Docker compose related constants and variables ##
# docker compose yaml file name #
$DKCOM_FILENAME = "docker-compose.yaml"
# docker compose yaml prototype file link #
$DKCOM_SRC = "https://github.com/MRColorR/money4band/raw/main/$DKCOM_FILENAME"
### Docker installer script for Windows source link ##
$DKINST_WIN_SRC = 'https://github.com/MRColorR/money4band/raw/main/.resources/.scripts/install-docker-win.ps1'
### Docker installer script for Mac source link ##
$DKINST_MAC_SRC = 'https://github.com/MRColorR/money4band/raw/main/.resources/.scripts/install-docker-mac.ps1'
## Dashboard related constants and variables ##
# Dashboard URL #
$script:DASHBOARD_URL = 'http://localhost:8081/'
### Resources, Scripts and Files folders ###
$script:RESOURCES_DIR = "$PWD\.resources"
$script:CONFIG_DIR = "$RESOURCES_DIR\.www\.configs"
$script:SCRIPTS_DIR = "$RESOURCES_DIR\.scripts"
$script:FILES_DIR = "$RESOURCES_DIR\.files"
## Architecture and OS related constants and variables ##
# Architecture default. Also define a map for the recognized architectures #
$script:ARCH = 'unknown'
$script:DKARCH = 'unknown'
$arch_map = @{
"x86_64" = "amd64";
"amd64" = "amd64";
"aarch64" = "arm64";
"arm64" = "arm64";
"x86" = "x86";
}
# OS default. Also define a map for the recognized OSs #
$script:OS_TYPE = 'unknown'
# Define the OS type map
$os_map = @{
"win32nt" = "Windows"
"windows_nt" = "Windows"
"windows" = "Windows"
"linux" = "Linux";
"darwin" = "MacOS";
"macos" = "MacOS";
"macosx" = "MacOS";
"osx" = "MacOS";
"cygwin" = "Cygwin";
"mingw" = "MinGw";
"msys" = "Msys";
"freebsd" = "FreeBSD";
}
## Colors ##
# Colors used inside the script #
$colors = @{
"default" = [System.ConsoleColor]::White
"green" = [System.ConsoleColor]::Green
"blue" = [System.ConsoleColor]::Blue
"red" = [System.ConsoleColor]::Red
"yellow" = [System.ConsoleColor]::Yellow
"magenta" = [System.ConsoleColor]::Magenta
"cyan" = [System.ConsoleColor]::Cyan
"purple" = [System.ConsoleColor]::DarkMagenta
}
# Color functions #
function colorprint($color, $text) {
$color = $color.ToLower()
$prevColor = [System.Console]::ForegroundColor
if ($colors.ContainsKey($color)) {
[System.Console]::ForegroundColor = $colors[$color]
Write-Output $text
[System.Console]::ForegroundColor = $prevColor
}
else {
Write-Output "Unknown color: $color. Available colors are: $($colors.Keys -join ', ')"
}
}
# Function to manage unexpected choices of flags #
function fn_unknown($REPLY) {
colorprint "Red" "Unknown choice $REPLY, please choose a valid option"
}
# Function to exit the script gracefully #
function fn_bye {
colorprint "Green" "Share this app with your friends thank you!"
print_and_log "Green" "Exiting the application...Bye!Bye!"
exit 0
}
### Log, Update and Utility functions ###
## Enable or disable logging using debug mode ##
# Check if the first argument is -d or --debug if so, enable debug mode
if ($args[0] -eq '-d' -or $args[0] -eq '--debug') {
$script:DEBUG = $true
# shift the arguments array to remove the debug flag consumed
$args = $args[1..$args.Length]
debug "[DEBUG]: Debug mode enabled."
}
else {
$script:DEBUG = $false
}
# Function to write debug messages to the debug log file #
function debug($text) {
if ($script:DEBUG) {
[DateTime]::Now.ToString("yyyy-MM-dd HH:mm:ss") + " - $text" | Out-File -FilePath $script:DEBUG_LOG -Append
}
}
# Function to print an info message that will be also logged to the debug log file #
function print_and_log($color, $message) {
colorprint $color $message
debug "[INFO]: $message"
}
# Function to print an error message and write it to the debug log file #
function errorprint_and_log($text) {
Write-Error $text
debug "[ERROR]: $text"
}
# Function to print criticals errors that will stop the script execution, write them to the debug log file and exit the script with code 1 #
function fn_fail($text) {
errorprint_and_log $text
Read-Host -Prompt "Press Enter to exit..."
exit 1
}
## Utility functions ##
# Function to detect OS
function detect_os {
debug "Detecting OS..."
try {
if ($PSVersionTable.Platform) {
$OSStr = $PSVersionTable.Platform.ToString().ToLower()
}
elseif ($env:OS) {
$OSStr = $env:OS.ToString().ToLower()
}
else {
$OSStr = (uname -s).ToLower()
}
# check if OSStr contains any known OS substring
$script:OS_TYPE = $os_map.Keys | Where-Object { $OSStr.Contains($_) } | Select-Object -First 1 | ForEach-Object { $os_map[$_] }
}
catch {
debug "Neither PS OS detection commands nor uname were found, OS detection failed. OS type will be set to 'unknown'."
$script:OS_TYPE = 'unknown'
}
debug "OS type detected: $script:OS_TYPE"
}
# Function to detect OS architecture and set the relative Docker architecture
function detect_architecture {
debug "Detecting system architecture..."
try {
# Try to use the new PowerShell command
if (Get-Command 'System.Runtime.InteropServices.RuntimeInformation::OSArchitecture' -ErrorAction SilentlyContinue) {
$archStr = [System.Runtime.InteropServices.RuntimeInformation]::OSArchitecture.ToString().ToLower()
}
# Fallback to using uname if on a Unix-like system
elseif (Get-Command 'uname' -ErrorAction SilentlyContinue) {
$archStr = (uname -m).ToLower()
}
# Final fallback to older PowerShell/Windows method
else {
$archStr = $env:PROCESSOR_ARCHITECTURE.ToLower()
}
$script:ARCH = $archStr
$script:DKARCH = $arch_map[$archStr]
if ($null -eq $script:DKARCH) {
$script:DKARCH = "unknown"
}
}
catch {
debug "Neither PS arch detection commands nor uname were found, architecture detection failed. Architecture will be set to 'unknown'."
$script:ARCH = 'unknown'
$script:DKARCH = 'unknown'
}
debug "System architecture detected: $script:ARCH, Docker architecture has been set to $script:DKARCH"
}
# experimanetal function that provide support for installing packages using Chocolatey
function fn_install_packages {
param(
[Parameter(Mandatory = $true)]
[string[]] $REQUIRED_PACKAGES
)
if ($script:OS_TYPE -eq "Windows") {
# Check if Chocolatey is installed
if (-not(Get-Command 'choco' -ErrorAction SilentlyContinue)) {
colorprint "Yellow" "Chocolatey is not installed, this script will now attempt to install it for you."
colorprint "Yellow" "Installing Chocolatey..."
$ProgressPreference = 'SilentlyContinue'
Invoke-Expression ((New-Object System.Net.WebClient).DownloadString('https://chocolatey.org/install.ps1'))
$ProgressPreference = 'Continue'
# check if the installation was successful
if (-not(Get-Command 'choco' -ErrorAction SilentlyContinue)) {
fn_fail "Chocolatey installation failed. Please install Chocolatey manually and then try again."
}
colorprint "Green" "Chocolatey installed successfully."
}
# Install required packages
foreach ($package in $REQUIRED_PACKAGES) {
if (-not(choco list --local-only --exact $package)) {
colorprint "Yellow" "$package not installed, Trying to install it now..."
$ProgressPreference = 'SilentlyContinue'
if (-not (choco install $package -y)) {
colorprint "Red" "Failed to install $package. Please install it manually and then try again."
}
$ProgressPreference = 'Continue'
else {
colorprint "Green" "$package installed successfully."
}
}
else {
colorprint "Green" "$package already installed."
}
}
}
elseif ($script:OS_TYPE -eq "MacOS") {
# Check if Homebrew is installed
if (-not(Get-Command 'brew' -ErrorAction SilentlyContinue)) {
colorprint "Yellow" "Homebrew is not installed, this script will now attempt to install it for you."
colorprint "Yellow" "Installing Homebrew..."
$ProgressPreference = 'SilentlyContinue'
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
$ProgressPreference = 'Continue'
# check if the installation was successful
if (-not(Get-Command 'brew' -ErrorAction SilentlyContinue)) {
fn_fail "Homebrew installation failed. Please install Homebrew manually and then try again."
}
else {
colorprint "Green" "Homebrew installed successfully."
}
}
# Install required packages
foreach ($package in $REQUIRED_PACKAGES) {
if (-not(brew list --versions $package)) {
print_and_log "Default" "$package not installed, Trying to install it now..."
$ProgressPreference = 'SilentlyContinue'
if (-not (brew install $package)) {
print_and_log "Failed to install $package. Please install it manually and then try again."
}
$ProgressPreference = 'Continue'
else {
colorprint "Green" "$package installed successfully."
}
}
else {
colorprint "Green" "$package already installed."
}
}
}
elseif ($script:OS_TYPE -eq "Linux") {
# Check which package manager is installed
if (Get-Command apt -ErrorAction SilentlyContinue) {
PKG_MANAGER = "apt"
PKG_CHECK="dpkg -l"
PKG_INSTALL="sudo apt install -y"
}
elseif (Get-Command yum -ErrorAction SilentlyContinue) {
PKG_MANAGER = "yum"
PKG_CHECK="rpm -qa"
PKG_INSTALL="sudo yum install -y"
}
elseif (Get-Command dnf -ErrorAction SilentlyContinue) {
PKG_MANAGER = "dnf"
PKG_CHECK="rpm -q"
PKG_INSTALL="sudo dnf install -y"
}
elseif (Get-Command pacman -ErrorAction SilentlyContinue) {
PKG_MANAGER = "pacman"
PKG_CHECK="pacman -Q"
PKG_INSTALL="sudo pacman -S --noconfirm"
}
elseif (Get-Command zypper -ErrorAction SilentlyContinue) {
PKG_MANAGER = "zypper"
PKG_CHECK="rpm -q"
PKG_INSTALL="sudo zypper install -y"
}
elseif (Get-Command apk -ErrorAction SilentlyContinue) {
PKG_MANAGER = "apk"
PKG_CHECK="apk info"
PKG_INSTALL="sudo apk add"
}
elseif (Get-Command emerge -ErrorAction SilentlyContinue) {
PKG_MANAGER = "emerge"
PKG_CHECK="qlist -I"
PKG_INSTALL="sudo emerge --ask n"
}
else {
print_and_log "Red" "Your package manager has not been recognized by this script. Please install the following packages manually: $($REQUIRED_PACKAGES -join ', ')"
Read-Input -Prompt "Press enter to continue"
return
}
debug "Package manager detected: $PKG_MANAGER"
# Install required packages
foreach ($package in $REQUIRED_PACKAGES) {
# Using Invoke-Expression to execute the package check command
if (-not (Invoke-Expression "$PKG_CHECK $package")) {
print_and_log "Default" "$package not installed, Trying to install it now..."
$ProgressPreference = 'SilentlyContinue'
# Using Invoke-Expression to execute the package install command
if (-not (Invoke-Expression "$PKG_INSTALL $package")) {
print_and_log "Red" "Failed to install $package. Please install it manually and then try again."
}
else {
colorprint "Green" "$package installed successfully."
}
$ProgressPreference = 'Continue'
}
else {
colorprint "Green" "$package already installed."
}
}
}
else {
print_and_log "Red" "Your operating system has not been recognized or is not supported by this function. Please install the following packages manually: $($REQUIRED_PACKAGES -join ', ')"
Read-Input -Prompt "Press enter to continue"
return
}
debug "Required packages installation completed."
}
### Sub-menu Functions ###
# Shows the liks of the apps
function fn_showLinks {
Clear-Host
colorprint "Green" "Use CTRL+Click to open links or copy them:"
$configPath = Join-Path -Path $CONFIG_DIR -ChildPath $CONFIG_JSON_FILE
$configData = Get-Content -Path $configPath -Raw | ConvertFrom-Json
# Iterate over the top-level keys (app types) in the JSON
foreach ($appType in $configData.PSObject.Properties.Name) {
colorprint "Yellow" "---$appType---"
# Iterate over the apps in each type
foreach ($app in $configData.$appType) {
colorprint "Default" $app.name
colorprint "Blue" $app.link
}
}
Read-Host -Prompt "Press Enter to go back to mainmenu"
debug "Links shown, going back to main menu."
}
<#
.SYNOPSIS
Function that will attempt to install Docker on different OSs
.DESCRIPTION
This function will attempt to install Docker on different OSs. It will ask the user to choose the OS and then it will launch the appropriate script to install Docker on the selected OS. If Docker is already installed it will ask the user if he wants to proceed with the installation anyway.
.EXAMPLE
Just call fn_dockerInstall
.NOTES
This function has been tested until v 2.0.0 on windows and mac but not on linux yet. The new version has not been tested as its assume that the logic is the same as the previous one just more refined.
#>
function fn_dockerInstall {
Clear-Host
debug "DockerInstall function started"
colorprint "Yellow" "This menu item will launch a script that will attempt to install Docker"
colorprint "Yellow" "Use it only if you do not know how to perform the manual Docker installation described at https://docs.docker.com/get-docker/ as the automatic script in some cases and depending on the OS you are using may fail to install Docker correctly."
while ($true) {
$yn = (Read-Host -Prompt "Do you wish to proceed with the Docker automatic installation Y/N?").ToLower()
if ($yn -eq 'y' -or $yn -eq 'yes') {
debug "User decided to install Docker through the script. Checking if Docker is already installed."
try {
$dockerVersion = docker --version
if ($dockerVersion) {
debug "Docker is already installed. Asking user if he wants to continue with the installation anyway."
while ($true) {
colorprint "Yellow" "Docker seems to be installed already. Do you want to continue with the installation anyway? (Y/N)"
$yn = (Read-Host).ToLower()
if ($yn -eq 'n' -or $yn -eq 'no'){
debug "User decided to abort the Docker re-install."
colorprint "Blue" "Returning to main menu..."
sleep 1
return
}
elseif ($yn -eq 'y' -or $yn -eq 'yes' ) {
debug "User decided to continue with the Docker re-install anyway."
break
}
else {
colorprint "Red" "Please answer yes or no."
}
}
}
}
catch {
print_and_log "DEFAULT" "Proceeding with Docker installation."
}
Clear-Host
print_and_log "Yellow" "Installing Docker for $script:OS_TYPE"
$InstallStatus = $false;
Switch ($OSTYPE) {
"Linux" {
Clear-Host
print_and_log "Yellow" "Starting Docker for Linux auto installation script"
$ProgressPreference = 'SilentlyContinue'
Invoke-WebRequest https://get.docker.com -o "$SCRIPTS_DIR/get-docker.sh"
$ProgressPreference = 'Continue'
sudo sh get-docker.sh;
$InstallStatus = $true;
}
"Windows" {
Clear-Host
print_and_log "Yellow" "Starting Docker for Windows auto installation script"
$ProgressPreference = 'SilentlyContinue'
Invoke-WebRequest $DKINST_WIN_SRC -o "$SCRIPTS_DIR\install-docker-win.ps1"
$ProgressPreference = 'Continue'
Start-Process PowerShell -Verb RunAs "-noprofile -executionpolicy bypass -command `"$SCRIPTS_DIR\install-docker-win.ps1 -filesPath $FILES_DIR`"" -Wait
$InstallStatus = $true;
}
"MacOS" {
Clear-Host
print_and_log "Yellow" "Starting Docker for MacOS auto installation script"
$ProgressPreference = 'SilentlyContinue'
Invoke-WebRequest $DKINST_MAC_SRC -o "$SCRIPTS_DIR\install-docker-mac.ps1"
$ProgressPreference = 'Continue'
colorprint "Yellow" "Select your CPU type"
colorprint "Yellow" "1) Apple silicon M1, M2...CPUs"
colorprint "Yellow" "2) Intel i5, i7...CPUs"
$cpuSel = Read-Host
switch ($cpuSel) {
1 {
Start-Process PowerShell -Verb RunAs "-noprofile -executionpolicy bypass -command `"$SCRIPTS_DIR\install-docker-mac.ps1 -filesPath $FILES_DIR`"" -Wait
$InstallStatus = $true;
}
2 {
Start-Process PowerShell -Verb RunAs "-noprofile -executionpolicy bypass -command `"$SCRIPTS_DIR\install-docker-mac.ps1 -filesPath $FILES_DIR -IntelCPU `"" -Wait
$InstallStatus = $true;
}
Default { fn_unknown "$cpuSel" }
}
}
DEFAULT {
print_and_log "Red" "Your operating system (${OSTYPE}) has not been recognized or is not supported by this function. Please install Docker manually and then try again."
}
}
if ($InstallStatus) {
colorprint "Green" "Script completed. If no errors appeared Docker should be installed. Please restart your machine and then proceed to .env file config and stack startup."
}
else {
colorprint "Red" "Something went wrong (maybe bad choice or incomplete installation), failed to install Docker automatically. Please try to install Docker manually by following the instructions on Docker website."
}
Read-Host -Prompt "Press enter to go back to mainmenu"
break
}
elseif ($yn -eq 'n' -or $yn -eq 'no') {
Clear-Host
colorprint "Blue" "Docker unattended installation canceled. Make sure you have Docker installed before proceeding with the other steps."
Read-Host -prompt "Press enter to go back to the menu"
return
}
else {
colorprint "Red" "Please answer yes or no."
}
}
}
<#
.SYNOPSIS
Function that will setup notifications about containers updates using shoutrrr
.DESCRIPTION
This function will setup notifications about containers updates using shoutrrr. It will ask the user to enter a link for notifications and then it will update the .env file and the docker-compose.yaml file accordingly.
.EXAMPLE
Just call fn_setupNotifications
.NOTES
This function has been tested until v 2.0.0. The new version has not been tested as its assume that the logic is the same as the previous one just more refined.
#>
function fn_setupNotifications {
Clear-Host
while ($true) {
colorprint "Yellow" "Do you wish to setup notifications about apps images updates (Yes to receive notifications and apply updates, No to just silently apply updates) Y/N?"
$yn = Read-Host
$yn = $yn.ToLower()
if ($yn -eq 'y' -or $yn -eq 'yes') {
debug "User decided to setup notifications about apps images updates."
colorprint "Yellow" "This step will setup notifications about containers updates using shoutrrr"
colorprint "Default" "The resulting SHOUTRRR_URL should have the format: <app>://<token>@<webhook>."
colorprint "Default" "Where <app> is one of the supported messaging apps on Shoutrrr (e.g. Discord), and <token> and <webhook> are specific to your messaging app."
colorprint "Default" "To obtain the SHOUTRRR_URL, create a new webhook for your messaging app and rearrange its URL to match the format above."
colorprint "Default" "For more details, visit https://containrrr.dev/shoutrrr/ and select your messaging app."
colorprint "Default" "Now a Discord notification setup example will be shown (Remember: you can also use a different supported app)."
Read-Host -Prompt "Press enter to continue"
Clear-Host
colorprint "Purple" "Create a new Discord server, go to server settings > integrations, and create a webhook."
colorprint "Purple" "Your Discord Webhook-URL will look like this: https://discordapp.com/api/webhooks/YourWebhookid/YourToken."
colorprint "Purple" "To obtain the SHOUTRRR_URL, rearrange it to look like this: discord://YourToken@YourWebhookid."
Read-Host -Prompt "Press enter to proceed."
Clear-Host
while ($true) {
colorprint "Yellow" "NOW INSERT BELOW THE LINK FOR NOTIFICATIONS using THE SAME FORMAT WRITTEN ABOVE e.g.: discord://yourToken@yourWebhookid"
$SHOUTRRR_URL = Read-Host
if ($SHOUTRRR_URL -match '^[a-zA-Z]+://') {
# Replace the lines in .env and $DKCOM_FILENAME
(Get-Content .\.env).replace('# SHOUTRRR_URL=', "SHOUTRRR_URL=") | Set-Content .\.env
$CURRENT_VALUE = (Get-Content .\.env | Select-String -Pattern "SHOUTRRR_URL=" -SimpleMatch).ToString().Split("=")[1]
(Get-Content .\.env).replace("SHOUTRRR_URL=${CURRENT_VALUE}", "SHOUTRRR_URL=${SHOUTRRR_URL}") | Set-Content .\.env
(Get-Content .\$DKCOM_FILENAME).replace('# - WATCHTOWER_NOTIFICATIONS=shoutrrr', " - WATCHTOWER_NOTIFICATIONS=shoutrrr") | Set-Content .\$DKCOM_FILENAME
(Get-Content .\$DKCOM_FILENAME).replace('# - WATCHTOWER_NOTIFICATION_URL', " - WATCHTOWER_NOTIFICATION_URL") | Set-Content .\$DKCOM_FILENAME
(Get-Content .\$DKCOM_FILENAME).replace('# - WATCHTOWER_NOTIFICATIONS_HOSTNAME', " - WATCHTOWER_NOTIFICATIONS_HOSTNAME") | Set-Content .\$DKCOM_FILENAME
(Get-Content .\.env).replace("NOTIFICATIONS_CONFIGURATION_STATUS=0", "NOTIFICATIONS_CONFIGURATION_STATUS=1") | Set-Content .\.env
colorprint "DEFAULT" "Notifications setup complete. If the link is correct, you will receive a notification for each update made on the app container images."
Read-Host -p "Press enter to continue"
break
}
else {
colorprint "Red" "Invalid link format. Please make sure to use the correct format."
while ($true) {
colorprint "Yellow" "Do you wish to try again or leave the notifications disabled and continue with the setup script? (Yes to try again, No to continue without notifications) Y/N?"
$yn = Read-Host
$yn = $yn.ToLower()
if ($yn -eq 'y' -or $yn -eq 'yes') {
break
}
elseif ($yn -eq 'n' -or $yn -eq 'no') {
return
}
else {
colorprint "Red" "Please answer yes or no."
}
}
}
}
}
elseif ($yn -eq 'n' -or $yn -eq 'no') {
debug "User chose to skip notifications setup"
colorprint "Blue" "Noted: all updates will be applied automatically and silently"
Read-Host -Prompt "Press enter to continue"
break
}
else {
colorprint "Red" "Please answer yes or no."
}
}
Clear-Host
debug "Notifications setup ended."
}
<#
.SYNOPSIS
This function will manage the setup of each app in the stack
.DESCRIPTION
This function will manage the setup of each app in the stack. It will ask the user to enter the required data for each app and then it will update the .env file and the docker-compose.yaml file accordingly.
.PARAMETER app
App name and image are required parameters. The app name is used to identify the app in the setup process.
.PARAMETER image
the image is used to feryfy if the image supports the current architecture and to update the docker-compose.yaml file accordingly.
.PARAMETER flags
Optional parameter. If the app requires an email to be setup, this parameter will be used to update the .env file.
.EXAMPLE
fn_setupApp -app "HONEYGAIN" -image "honeygain/honeygain" -email "email" -password "password"
.NOTES
This function has been tested until v 2.0.0. The new version has not been tested as its assume that the logic is the same as the previous one just more refined.
#>
function fn_setupApp {
param (
[Parameter(Mandatory = $true)]
[string]$app,
[Parameter(Mandatory = $true)]
[string]$image,
[Parameter(Mandatory = $false)]
[string[]]$flags
)
$APP_NAME = $app
$APP_IMAGE = $image
$uuid = $false
$email = $false
$password = $false
$apikey = $false
$userid = $false
$uuid = $false
$cid = $false
$token = $false
$customScript = $null
$manual = $false
#Write-Output "passed parameters: APP: $app, IMG: $image, FLAGS: $flags"
#Read-Host -Prompt "This is for debug Press enter to continue"
$CURRENT_APP = $APP_NAME
if ($app) { (Get-content $script:DKCOM_FILENAME) -replace "#ENABLE_${CURRENT_APP}", "" | Set-Content $script:DKCOM_FILENAME }
debug "Enabled ${CURRENT_APP} in $script:DKCOM_FILENAME"
for ($i = 0; $i -lt $flags.Count; $i++) {
switch ($flags[$i]) {
"--email" { $email = $true }
"--password" { $password = $true }
"--apikey" { $apikey = $true }
"--userid" { $userid = $true }
"--uuid" { $uuid = $true }
"--cid" { $cid = $true }
"--token" { $token = $true }
"--customScript" {
$customScript = $flags[$i + 1] # consider the element after --customScript as the script name
$i++ # increment the index to skip the next element
}
"--manual" { $manual = $true }
default { colorprint "RED" "Unknown flag: $($flags[$i])" }
}
}
if ($email) {
debug "Starting email setup for ${CURRENT_APP} app"
while ($true) {
colorprint "GREEN" "Enter your ${CURRENT_APP} Email:"
$APP_EMAIL = Read-Host
if ($APP_EMAIL -match '^[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[a-zA-Z]{2,}$') {
(Get-Content .env) -replace "your${CURRENT_APP}Mail", $APP_EMAIL | Set-Content .env
break
}
else {
colorprint "RED" "Invalid email address. Please try again."
}
}
}
if ($password) {
debug "Starting password setup for ${CURRENT_APP} app"
while ($true) {
colorprint "DEFAULT" "Note: If you are using login with Google, remember to set also a password for your ${CURRENT_APP} account!"
colorprint "GREEN" "Enter your ${CURRENT_APP} Password:"
$APP_PASSWORD = Read-Host
if ($APP_PASSWORD) {
(Get-Content .env) -replace "your${CURRENT_APP}Pw", $APP_PASSWORD | Set-Content .env
break
}
else {
colorprint "RED" "Password cannot be empty. Please try again."
}
}
}
if ($apikey) {
debug "Starting APIKey setup for ${CURRENT_APP} app"
colorprint "DEFAULT" "Find/Generate your APIKey inside your ${CURRENT_APP} dashboard/profile."
colorprint "GREEN" "Enter your ${CURRENT_APP} APIKey:"
$APP_APIKEY = Read-Host
(Get-Content .env) -replace "your${CURRENT_APP}APIKey", $APP_APIKEY | Set-Content .env
}
if ($userid) {
debug "Starting UserID setup for ${CURRENT_APP} app"
colorprint "DEFAULT" "Find your UserID inside your ${CURRENT_APP} dashboard/profile."
colorprint "GREEN" "Enter your ${CURRENT_APP} UserID:"
$APP_USERID = Read-Host
(Get-Content .env) -replace "your${CURRENT_APP}UserID", $APP_USERID | Set-Content .env
}
if ($uuid) {
debug "Starting UUID setup for ${CURRENT_APP} app"
colorprint "DEFAULT" "Starting UUID generation/import for ${CURRENT_APP}"
$SALT = "$script:DEVICE_NAME$((Get-Random))"
$UUID = New-Object System.Security.Cryptography.MD5CryptoServiceProvider
$hash = $UUID.ComputeHash([System.Text.Encoding]::ASCII.GetBytes($SALT))
$UUID = ([System.BitConverter]::ToString($hash) -replace '-', '')
while ($true) {
colorprint "YELLOW" "Do you want to use a previously registered sdk-node-uuid for ${CURRENT_APP}? (yes/no)"
$USE_EXISTING_UUID = Read-Host
$USE_EXISTING_UUID = $USE_EXISTING_UUID.ToLower()
if ($USE_EXISTING_UUID -eq "yes" -or $USE_EXISTING_UUID -eq "y") {
while ($true) {
colorprint "GREEN" "Please enter the 32 char long alphanumeric part of the existing sdk-node-uuid for ${CURRENT_APP}:"
colorprint "DEFAULT" "E.g. if existing registered node is sdk-node-b86301656baefekba8917349bdf0f3g4 then enter just b86301656baefekba8917349bdf0f3g4"
$EXISTING_UUID = Read-Host
if ($EXISTING_UUID -notmatch '^[a-f0-9]{32}$') {
colorprint "RED" "Invalid UUID entered, it should be an md5 hash and 32 characters long."
colorprint "DEFAULT" "Do you want to try again? (yes/no)"
$TRY_AGAIN = Read-Host
$TRY_AGAIN = $TRY_AGAIN.ToLower()
if ($TRY_AGAIN -eq "no" -or $TRY_AGAIN -eq "n") { break }
}
else {
$UUID = $EXISTING_UUID
break
}
}
break
}
elseif ($USE_EXISTING_UUID -eq "no" -or $USE_EXISTING_UUID -eq "n") {
break
}
else {
colorprint "RED" "Please answer yes or no."
}
}
$UUID = $UUID.ToLower()
(Get-Content .env) -replace "your${CURRENT_APP}MD5sum", $UUID | Set-Content .env
colorprint "DEFAULT" "${CURRENT_APP} UUID setup: done"
colorprint "BLUE" "Save the following link somewhere to claim your ${CURRENT_APP} node after completing the setup and starting the apps stack: https://earnapp.com/r/sdk-node-$UUID."
colorprint "DEFAULT" "A new file containing this link has been created for you in the current directory"
"https://earnapp.com/r/sdk-node-$UUID" | Out-File -FilePath 'ClaimEarnappNode.txt'
}
if ($cid) {
debug "Starting CID setup for ${CURRENT_APP} app"
colorprint "DEFAULT" "Find your CID, you can fetch it from your dashboard https://packetstream.io/dashboard/download?linux# then click on ->View your configuration file<-."
colorprint "GREEN" "Enter your ${CURRENT_APP} CID:"
$APP_CID = Read-Host
(Get-Content .env) -replace "your${CURRENT_APP}CID", $APP_CID | Set-Content .env
}
if ($token) {
debug "Starting Token setup for ${CURRENT_APP} app"
colorprint "DEFAULT" "Find your Token inside your ${CURRENT_APP} dashboard/profile."
colorprint "GREEN" "Enter your ${CURRENT_APP} Token:"
$APP_TOKEN = Read-Host
(Get-Content .env) -replace "your${CURRENT_APP}Token", $APP_TOKEN | Set-Content .env
}
if ($customScript) {
$SCRIPT_NAME = "${customScript}.ps1"
$SCRIPT_PATH = Join-Path -Path $script:SCRIPTS_DIR -ChildPath $SCRIPT_NAME
debug "Starting custom script execution for ${CURRENT_APP} app using $SCRIPT_NAME, from $SCRIPT_PATH"
if (Test-Path -Path $SCRIPT_PATH) {
Set-Content $SCRIPT_PATH -Value (Get-Content $SCRIPT_PATH) -Encoding UTF8
colorprint "DEFAULT" "Executing custom script: $SCRIPT_NAME"
Start-Process PowerShell -Verb RunAs "-noprofile -executionpolicy bypass -command `"cd '$pwd'; & '$SCRIPT_PATH';`"" -wait
}
else {
colorprint "RED" "Custom script '$SCRIPT_NAME' not found in the scripts directory."
}
}
if ($manual) {
debug "Starting manual setup for ${CURRENT_APP} app"
colorprint "Yellow" "${CURRENT_APP} requires further manual configuration."
colorprint "Yellow" "Please after completing this automated setup follow the manual steps described on the app's website."
}
debug "Finished parsing arguments of setupApp function for ${CURRENT_APP} app"
# App Docker image architecture adjustments
$TAG = 'latest'
# Ensure $supported_tags is an array
$supported_tags = @()
# Send a request to DockerHub for a list of tags
$page_index = 1
$page_size = 500
$ProgressPreference = 'SilentlyContinue'
$json = Invoke-WebRequest -Uri "https://registry.hub.docker.com/v2/repositories/${APP_IMAGE}/tags?page=${page_index}&page_size=${page_size}" -UseBasicParsing | ConvertFrom-Json
$ProgressPreference = 'Continue'
# Filter out the tags that do not support the specified architecture
$json.results | ForEach-Object {
$ntag = $_.name
if (($_.images | Where-Object { $_.architecture -eq $DKARCH })) {
$supported_tags += $ntag
}
}
# Check if there are any tags that support the given architecture
if ($supported_tags) {
colorprint "default" "There are $($supported_tags.Count) tags supporting $DKARCH arch for this image"
colorprint "default" "Let's see if $TAG tag is in there"
# Check if 'latest' tag is among them
if ($supported_tags -contains $TAG) {
colorprint "green" "OK, $TAG tag present and it supports $DKARCH arch, nothing to do"
}
else {
colorprint "yellow" "$TAG tag does not support $DKARCH arch but other tags do, the newer tag supporting $DKARCH will be selected"
# Replace 'latest' tag with the first one that supports the given architecture in your Docker compose file
$newTag = $supported_tags[0]
(Get-Content $script:DKCOM_FILENAME).replace("${APP_IMAGE}:$TAG", "${APP_IMAGE}:$newTag") | Set-Content $DKCOM_FILENAME
}
}
else {
colorprint "yellow" "No native image tag found for $DKARCH arch, emulation layer will try to run this app image anyway."
#colorprint "default" "If an emulation layer is not already installed, the script will try to install it now. Please provide your sudo password if prompted."
}
debug "Finished Docker image architecture adjustments for ${CURRENT_APP} app. Its image tag is now " + (Get-Content $script:DKCOM_FILENAME | Select-String -Pattern "${APP_IMAGE}:" -SimpleMatch).ToString().Split(":")[1]
Write-Host "$app configuration complete, press enter to continue to the next app"
Read-Host
debug "Finished setupApp function for ${CURRENT_APP} app"
}
<#
.SYNOPSIS
Function that will setup the proxy for the apps in the stack
.DESCRIPTION
This function will setup the proxy for the apps in the stack. It will ask the user to enter the proxy to use and then it will update the .env file and the docker-compose.yaml file accordingly.
.EXAMPLE
Just call fn_setupProxy
.NOTES
This function has been tested until v 2.0.0. The new version has not been tested as its assume that the logic is the same as the previous one just more refined.
#>
function fn_setupProxy() {
debug "Starting setupProxy function"
if ($script:PROXY_CONF -eq $false) {
while ($true) {
colorprint "YELLOW" "Do you wish to setup a proxy for the apps in this stack Y/N?"
colorprint "DEFAULT" "Note that if you want to run multiple instances of the same app, you will need to configure different env files in different project folders (copy the project to multiple different folders and configure them using different proxies)."
$yn = Read-Host
if ($yn.ToLower() -eq 'y' -or $yn.ToLower() -eq 'yes') {
Clear-Host
debug "User chose to setup a proxy"
colorprint "YELLOW" "Proxy setup started."
$script:RANDOM_VALUE = Get-Random
colorprint "GREEN" "Insert the designed proxy to use. Eg: protocol://proxyUsername:proxyPassword@proxy_url:proxy_port or just protocol://proxy_url:proxy_port if auth is not needed:"
$script:NEW_STACK_PROXY = Read-Host
# An unique name for the stack is chosen so that even if multiple stacks are started with different proxies the names do not conflict
# ATTENTION: if a random value has been already added to the project and devicename during a previous setup it should remain the same to mantain consistency withthe devices name registered on the apps sites but the proxy url could be changed
(Get-Content .\.env).replace("COMPOSE_PROJECT_NAME=money4band", "COMPOSE_PROJECT_NAME=money4band_$($script:RANDOM_VALUE)") | Set-Content .\.env
(Get-Content .\.env).replace("DEVICE_NAME=$($script:DEVICE_NAME)", "DEVICE_NAME=$($script:DEVICE_NAME)$($script:RANDOM_VALUE)") | Set-Content .\.env
# Obtaining the line of STACK_PROXY= in the .env file and then replace the line with the new proxy also uncomment the line if it was commented
(Get-Content .\.env).replace("# STACK_PROXY=", "STACK_PROXY=") | Set-Content .\.env # if it was already uncommented it does nothing
$CURRENT_VALUE = $(Get-Content .\.env | Select-String -Pattern "STACK_PROXY=" -SimpleMatch).ToString().Split("=")[1]
(Get-Content .\.env).replace("STACK_PROXY=${CURRENT_VALUE}", "STACK_PROXY=$($script:NEW_STACK_PROXY)") | Set-Content .\.env
(Get-Content "$script:DKCOM_FILENAME").replace("#ENABLE_PROXY", "") | Set-Content "$script:DKCOM_FILENAME"
(Get-Content "$script:DKCOM_FILENAME").replace("# network_mode", "network_mode") | Set-Content "$script:DKCOM_FILENAME"
$script:PROXY_CONF = $true
(Get-Content .\.env).replace("PROXY_CONFIGURATION_STATUS=0", "PROXY_CONFIGURATION_STATUS=1") | Set-Content .\.env
colorprint "DEFAULT" "Ok, $script:NEW_STACK_PROXY will be used as proxy for all apps in this stack"
Read-Host -p "Press enter to continue"
debug "Proxy setup finished"
break
}
elseif ($yn.ToLower() -eq 'n' -or $yn.ToLower() -eq 'no') {
debug "User chose not to setup a proxy"
colorprint "BLUE" "Ok, no proxy added to configuration."
Start-Sleep -Seconds 1
break
}
else {
colorprint "RED" "Please answer yes or no."
}
}
}
}
<#
.SYNOPSIS
Function that will setup the .env file and the docker compose file
.DESCRIPTION
This function will setup the .env file and the docker compose file. It will ask the user to enter the required data for each app and then it will update the .env file and the docker-compose.yaml file accordingly.
.EXAMPLE
Just call fn_setupEnv
.NOTES
This function has been tested until v 2.0.0. The new version has not been tested as its assume that the logic is the same as the previous one just more refined.
#>
function fn_setupEnv() {
param(
[string]$app_type
)
print_and_log "Yellow" "Starting setupEnv function for $app_type"
# Check if .env file is already configured if 1 then it is already configured, if 0 then it is not configured
$ENV_CONFIGURATION_STATUS = (Get-Content .\.env | Select-String -Pattern "ENV_CONFIGURATION_STATUS=" -SimpleMatch).ToString().Split("=")[1]
debug "Current ENV_CONFIGURATION_STATUS: $ENV_CONFIGURATION_STATUS"
$PROXY_CONFIGURATION_STATUS = (Get-Content .\.env | Select-String -Pattern "PROXY_CONFIGURATION_STATUS=" -SimpleMatch).ToString().Split("=")[1]
debug "Current PROXY_CONFIGURATION_STATUS: $PROXY_CONFIGURATION_STATUS"
$NOTIFICATIONS_CONFIGURATION_STATUS = (Get-Content .\.env | Select-String -Pattern "NOTIFICATIONS_CONFIGURATION_STATUS=" -SimpleMatch).ToString().Split("=")[1]
debug "Current NOTIFICATIONS_CONFIGURATION_STATUS: $NOTIFICATIONS_CONFIGURATION_STATUS"
while ($true) {
colorprint "YELLOW" "Do you wish to proceed with the .env file guided setup Y/N? (This will also adapt the $($script:DKCOM_FILENAME) file accordingly)"
$yn = Read-Host
if ($yn.ToLower() -eq 'y' -or $yn.ToLower() -eq 'yes') {
Clear-Host
debug "User chose to proceed with the .env file guided setup for $app_type"
if (($ENV_CONFIGURATION_STATUS -eq 1) -and ($app_type -eq "apps")) {
colorprint "DEFAULT" "The current .env file appears to have already been configured. Do you wish to reset it? (Y/N)"
$yn = Read-Host
if ($yn.ToLower() -eq 'y' -or $yn.ToLower() -eq 'yes') {
colorprint "DEFAULT" "Resetting .env file..."
$ProgressPreference = 'SilentlyContinue'
Invoke-WebRequest -Uri $script:ENV_SRC -OutFile ".env"
Invoke-WebRequest -Uri $script:DKCOM_SRC -OutFile "$($script:DKCOM_FILENAME)"
$ProgressPreference = 'Continue'
Clear-Host
}
else {
colorprint "Blue" "Keeping the existing .env file"
Read-Host -p "Press enter to continue"
}
}
elseif (($ENV_CONFIGURATION_STATUS -eq 1) -and ($app_type -ne "apps")) {
print_and_log "Blue" "Proceeding with $app_type setup whitout resetting the .env file as it should already be configured by te main apps setup"
Read-Host -p "Press enter to continue"
}
colorprint "YELLOW" "beginning env file guided setup"
$script:CURRENT_APP = ''
if (Get-Content .\.env | Select-String "DEVICE_NAME=${DEVICE_NAME_PLACEHOLDER}") {
debug "Device name is still the default one, asking user to change it"
colorprint "YELLOW" "PLEASE ENTER A NAME FOR YOUR DEVICE:"
$script:DEVICE_NAME = Read-Host
(Get-Content .\.env).replace("DEVICE_NAME=${DEVICE_NAME_PLACEHOLDER}", "DEVICE_NAME=$script:DEVICE_NAME") | Set-Content .\.env
}
else {
debug "Device name is already set, skipping user input"
$script:DEVICE_NAME = (Get-Content .\.env | Select-String -Pattern "DEVICE_NAME=" -SimpleMatch).ToString().Split("=")[1]
}
Clear-Host
if ($PROXY_CONFIGURATION_STATUS -eq 1) {
$script:CURRENT_PROXY = (Get-Content .\.env | Select-String -Pattern "STACK_PROXY=" -SimpleMatch).ToString().Split("=")[1]
print_and_log "BLUE" "Proxy is already set up."
while ($true) {
colorprint "YELLOW" "The current proxy is: ${CURRENT_PROXY}. Do you wish to change it? (Y/N)"
$yn = Read-Host
if ($yn.ToLower() -eq 'y' -or $yn.ToLower() -eq 'yes') {
$script:PROXY_CONF = $false
debug "User chose to change the proxy that was already configured"
fn_setupProxy
break
}
elseif ($yn.ToLower() -eq 'n' -or $yn.ToLower() -eq 'no') {
debug "User chose not to change the proxy that was already configured"
colorprint "BLUE" "Keeping the existing proxy."
Read-Host -p "Press enter to continue"
break
}
else {
colorprint "RED" "Please answer yes or no."
}
}
}
else {
debug "Asking user if they want to setup a proxy as it is not already configured"
fn_setupProxy
}
Clear-Host
debug "Loading $app_type from ${CONFIG_JSON_FILE}"
$apps = Get-Content "$script:CONFIG_DIR/${CONFIG_JSON_FILE}" | ConvertFrom-Json | Select-Object -ExpandProperty $app_type
debug "$app_type loaded from ${CONFIG_JSON_FILE}"
foreach ($app in $apps) {
Clear-Host
colorprint "YELLOW" "PLEASE REGISTER ON THE PLATFORMS USING THE FOLLOWING LINKS, YOU'LL NEED TO ENTER SOME DATA BELOW:"
colorprint "GREEN" "Use CTRL+Click to open links or copy them:"
$name = $app.name
$link = $app.link
$image = $app.image
$flags = $app.flags
$script:CURRENT_APP = $name.ToUpper()
while ($true) {
# check if the ${CURRENT_APP} is already enabled in the $DKCOM_FILENAME file and if it is not (if there is a #ENABLE_$CURRENTAPP) then ask the user if they want to enable it
if ((Get-Content $script:DKCOM_FILENAME | Select-String -Pattern "#ENABLE_${CURRENT_APP}", -SimpleMatch)) {
colorprint "YELLOW" "Do you wish to enable and use $($script:CURRENT_APP)? (Y/N)"
$yn = Read-Host