-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathamp-tasks.bat
More file actions
1580 lines (1276 loc) · 47 KB
/
Copy pathamp-tasks.bat
File metadata and controls
1580 lines (1276 loc) · 47 KB
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
@echo off
:: ==========================================================
:: AMP-MANAGER.BAT
:: A simple batch script to manage the local dev AMP stack
:: Designed for Windows with Angie, MariaDB, PHP, and mkcert (SSL).
:: Provides configuration checks, tasks for domain management, and CA handling.
:: Author: Nuno Luciano
:: Date: 2026-02-25
:: Version: 1.0.0
:: LICENSE: MIT
:: ==========================================================
chcp 65001 >nul
setlocal EnableDelayedExpansion
:MAIN_LOGIC
:: ENVIRONMENT SETUP
cd /d "%~dp0"
:: drive
set "DRV=%~d0\"
:: HOSTS
set "HOSTS=%windir%\System32\drivers\etc\hosts"
:: PROJECT ROOT = folder where amp-tasks.bat lives
set "PROJECT_ROOT=%~dp0"
set "PROJECT_ROOT=%PROJECT_ROOT:~0,-1%"
set "CONFIG_FOLDER=%PROJECT_ROOT%\config"
set "SERVER_FOLDER=%PROJECT_ROOT%\config\angie-sites"
set "CERT_FOLDER=%PROJECT_ROOT%\config\certs"
set "DATA_FOLDER=%PROJECT_ROOT%\data"
set "WWW_FOLDER=%PROJECT_ROOT%\www"
set "MKCERT=%CONFIG_FOLDER%\mkcert.exe"
:: default
set "URL=https://angie.local/"
:: TASK DISPATCHER
if "%~1"=="" goto :HELP
:: Watchdog
if /i "%~1"=="watch" goto :WATCH
:: Domain management
if /i "%~1"=="scan_domains" goto :SCAN_DOMAINS
if /i "%~1"=="list_domains" goto :LIST_DOMAINS
if /i "%~1"=="new_domain" goto :NEW_DOMAIN
if /i "%~1"=="remove_domain" goto :REMOVE_DOMAIN
if /i "%~1"=="generate_config" goto :GENERATE_CONFIG
if /i "%~1"=="angie_live_status" goto :ANGIE_LIVE_STATUS
:: Status & Health
if /i "%~1"=="status" goto :STATUS
if /i "%~1"=="env_status" goto :ENV_STATUS
if /i "%~1"=="runtime_status" goto :RUNTIME_STATUS
if /i "%~1"=="php_extensions" goto :PHP_EXTENSIONS
:: Certificate Authority
if /i "%~1"=="ca_status" goto :CA_STATUS
if /i "%~1"=="ca_reset" goto :CA_RESET
if /i "%~1"=="ca_uninstall" goto :CA_UNINSTALL
if /i "%~1"=="regenerate_ssl" goto :REGENERATE_SSL
if /i "%~1"=="regenerate_all_ssl" goto :REGENERATE_ALL_SSL
:: SSH Key Management
if /i "%~1"=="ssh_key_status" goto :SSH_KEY_STATUS
if /i "%~1"=="ssh_key_generate" goto :SSH_KEY_GENERATE
:: Docker / Angie
if /i "%~1"=="docker_up" goto :DOCKER_UP
if /i "%~1"=="docker_stop" goto :DOCKER_STOP
if /i "%~1"=="docker_restart" goto :DOCKER_RESTART
if /i "%~1"=="restart_angie" goto :RESTART_ANGIE
if /i "%~1"=="restart_runtime" goto :RESTART_RUNTIME
if /i "%~1"=="docker_env_metrics" goto :DOCKER_SUMMARY
if /i "%~1"=="docker_desktop_launch" goto :DOCKER_DESKTOP_LAUNCH
if /i "%~1"=="db_query" goto :DB_QUERY
:: Workflow
if /i "%~1"=="workflow_action" goto :WORKFLOW_ACTION
if /i "%~1"=="workflow_git" goto :WORKFLOW_GIT
if /i "%~1"=="workflow_sftp" goto :WORKFLOW_SFTP
if /i "%~1"=="workflow_webhook" goto :WORKFLOW_WEBHOOK
:: Maintenance
if /i "%~1"=="clear_cache" goto :CLEAR_CACHE
if /i "%~1"=="clear_logs" goto :CLEAR_LOGS
:: Version
if /i "%~1"=="version" goto :VERSION
goto :HELP
:: Tasks supported
:HELP
echo {"status":"error","message":"Invalid or missing task","supported":["watch","status","env_status","runtime_status","scan_domains","list_domains","new_domain","remove_domain","generate_config","ca_status","ca_reset","ca_uninstall","regenerate_ssl","regenerate_all_ssl","ssh_key_status","ssh_key_generate","docker_desktop_launch","docker_up","docker_stop","restart_angie","restart_runtime","docker_restart","docker_env_metrics","clear_cache","clear_logs","version"]}
exit /b 1
:VERSION
setlocal EnableDelayedExpansion
echo {"status":"ok","version":"1.0.0","build":"2026-03-07","engine":"amp-manager"}
endlocal
exit /b 0
:STATUS
setlocal EnableDelayedExpansion
:: Check Docker version availability
docker --version >nul 2>&1
if errorlevel 1 (
echo {"status":"error","message":"Docker is not recognized as an internal or external command. Please ensure Docker Desktop is installed and in your PATH."}
endlocal & exit /b 1
)
:: Check if Docker is running
docker info >nul 2>&1
if errorlevel 1 (
echo {"status":"error","message":"Docker is installed but not running. Please start Docker Desktop."}
endlocal & exit /b 1
)
:: Get container status
set "RUNNING_COUNT=0"
for /f "tokens=*" %%i in ('docker compose ps --format "{{.Service}}:{{.State}}" 2^>nul') do (
set "LINE=%%i"
if "!LINE:running=!" neq "!LINE!" set /a RUNNING_COUNT+=1
)
echo {"status":"ok","message":"System is operational","docker":"running","containers_running":!RUNNING_COUNT!,"timestamp":"%DATE% %TIME%"}
endlocal
exit /b 0
:PHP_EXTENSIONS
setlocal EnableDelayedExpansion
docker info >nul 2>&1 || (echo {"status":"error","message":"Docker not running"} & endlocal & exit /b 1)
:: Run the command you tested
for /f "usebackq tokens=*" %%i in (`docker compose exec -T php php -r "echo json_encode(get_loaded_extensions());" 2^>nul`) do (
set "EXTS=%%i"
)
if "!EXTS!"=="" (
echo {"status":"error","message":"Failed to retrieve extensions. Is the PHP container running?"}
) else (
echo {"status":"ok","extensions":!EXTS!}
)
endlocal
exit /b 0
:CLEAR_CACHE
setlocal EnableDelayedExpansion
set "TGT=%PROJECT_ROOT%\angie_cache"
if exist "!TGT!" (
for /d %%i in ("!TGT!\*") do rmdir /s /q "%%i" >nul 2>&1
del /q /f "!TGT!\*.*" >nul 2>&1
)
echo {"status":"ok","message":"Cache cleared"}
endlocal
exit /b 0
:CLEAR_LOGS
setlocal EnableDelayedExpansion
set "TGT=%PROJECT_ROOT%\logs"
if exist "!TGT!" (
for /d %%i in ("!TGT!\*") do rmdir /s /q "%%i" >nul 2>&1
del /q /f "!TGT!\*.*" >nul 2>&1
)
echo {"status":"ok","message":"Logs cleared"}
endlocal
exit /b 0
:WORKFLOW_ACTION
setlocal EnableDelayedExpansion
set "WF_DOMAIN=%~2"
set "WF_CMD=%~3"
:: Resolve path
set "WF_PATH=%WWW_FOLDER%\!WF_DOMAIN!"
if not exist "!WF_PATH!" mkdir "!WF_PATH!"
:: Execute
pushd "!WF_PATH!"
!WF_CMD! >"%TEMP%\wf_action.log" 2>&1
set "ERR=!ERRORLEVEL!"
popd
if "!ERR!"=="0" (
echo {"status":"ok","message":"Action completed","command":"!WF_CMD!"}
) else (
set /p LOG=<"%TEMP%\wf_action.log"
echo {"status":"error","message":"Action failed","details":"!LOG!"}
)
endlocal & exit /b !ERR!
:WORKFLOW_GIT
setlocal EnableDelayedExpansion
set "WF_DOMAIN=%~2"
set "WF_CMD=%~3"
:: Resolve path
set "WF_PATH=%WWW_FOLDER%\!WF_DOMAIN!"
if not exist "!WF_PATH!" mkdir "!WF_PATH!"
:: Capability check: git installed?
git --version >nul 2>&1
if errorlevel 1 (
echo {"status":"error","message":"Git is not installed"}
endlocal & exit /b 1
)
:: Execute git command
pushd "!WF_PATH!"
git !WF_CMD! >"%TEMP%\git_cmd.log" 2>&1
set "ERR=!ERRORLEVEL!"
:: If clone into current dir failed, it might be because the dir is not empty (e.g. scaffolded index.php)
if !ERR! neq 0 (
echo !WF_CMD! | findstr /i /c:"clone" | findstr /c:" ." >nul
if !errorlevel! equ 0 (
:: Try to remove default scaffold files and retry
if exist "index.php" del /q "index.php"
if exist "index.html" del /q "index.html"
git !WF_CMD! >"%TEMP%\git_cmd.log" 2>&1
set "ERR=!ERRORLEVEL!"
)
)
popd
if "!ERR!"=="0" (
echo {"status":"ok","message":"Git command completed","command":"git !WF_CMD!"}
) else (
set /p LOG=<"%TEMP%\git_cmd.log"
echo {"status":"error","message":"Git command failed","details":"!LOG!"}
)
endlocal & exit /b !ERR!
:WORKFLOW_SFTP
setlocal EnableDelayedExpansion
:: Parameters: host, username, localPath, remotePath, keyFilePath, keyType
:: keyType: "temp" (delete after use) or "permanent" (keep, e.g. AMP Manager key)
set "WF_HOST=%~2"
set "WF_USER=%~3"
set "WF_LOCAL=%~4"
set "WF_REMOTE=%~5"
set "WF_KEY_FILE=%~6"
set "WF_KEY_TYPE=%~7"
:: Validate required parameters
if "!WF_HOST!"=="" (
echo {"status":"error","message":"SFTP host is required"}
endlocal & exit /b 1
)
if "!WF_USER!"=="" (
echo {"status":"error","message":"SFTP username is required"}
endlocal & exit /b 1
)
if "!WF_KEY_FILE!"=="" (
echo {"status":"error","message":"SFTP requires SSH key authentication. Please select an SSH credential."}
endlocal & exit /b 1
)
if "!WF_LOCAL!"=="" set "WF_LOCAL=."
if "!WF_REMOTE!"=="" set "WF_REMOTE=/"
:: Capability check: sftp installed?
where sftp >nul 2>&1
if errorlevel 1 (
echo {"status":"error","message":"SFTP client is not available on this system"}
endlocal & exit /b 1
)
:: Verify key file exists
if not exist "!WF_KEY_FILE!" (
echo {"status":"error","message":"SSH key file not found: !WF_KEY_FILE!"}
endlocal & exit /b 1
)
:: Resolve local path (relative to domain folder or absolute)
set "WF_LOCAL_PATH=!WF_LOCAL!"
if "!WF_LOCAL:~1,1!" neq ":" (
if not exist "!WF_LOCAL!" (
set "WF_LOCAL_PATH=%WWW_FOLDER%\!WF_LOCAL!"
)
)
:: Verify local path exists
if not exist "!WF_LOCAL_PATH!" (
echo {"status":"error","message":"Local path not found: !WF_LOCAL_PATH!"}
:: Cleanup temp key file before exit (only if temp)
if /i "!WF_KEY_TYPE!"=="temp" del /f /q "!WF_KEY_FILE!" >nul 2>&1
endlocal & exit /b 1
)
:: Create temp batch file for SFTP commands
set "SFTP_BATCH=%TEMP%\amp_sftp_cmd_!RANDOM!.txt"
:: Determine if local is a file or directory
set "IS_DIR=false"
if exist "!WF_LOCAL_PATH!\*" set "IS_DIR=true"
:: Write SFTP commands to batch file
if "!IS_DIR!"=="true" (
echo cd "!WF_REMOTE!" > "!SFTP_BATCH!"
echo put -r "!WF_LOCAL_PATH!" >> "!SFTP_BATCH!"
) else (
echo put "!WF_LOCAL_PATH!" "!WF_REMOTE!" >> "!SFTP_BATCH!"
)
:: Execute SFTP
sftp -i "!WF_KEY_FILE!" -o StrictHostKeyChecking=no -o BatchMode=yes -b "!SFTP_BATCH!" "!WF_USER!@!WF_HOST!" >"%TEMP%\sftp_output.txt" 2>&1
set "SFTP_ERR=!ERRORLEVEL!"
:: Cleanup: Only delete key file if it's a temp file (not permanent AMP key)
if /i "!WF_KEY_TYPE!"=="temp" (
del /f /q "!WF_KEY_FILE!" >nul 2>&1
)
:: Always delete temp batch file
del /f /q "!SFTP_BATCH!" >nul 2>&1
:: Read output for error details
set "SFTP_OUTPUT="
if exist "%TEMP%\sftp_output.txt" (
for /f "usebackq delims=" %%a in ("%TEMP%\sftp_output.txt") do (
set "line=%%a"
set "line=!line:"='!"
if not defined SFTP_OUTPUT (
set "SFTP_OUTPUT=!line!"
) else (
set "SFTP_OUTPUT=!SFTP_OUTPUT! | !line!"
)
)
del /f /q "%TEMP%\sftp_output.txt" >nul 2>&1
)
if "!SFTP_ERR!"=="0" (
echo {"status":"ok","message":"SFTP transfer completed","host":"!WF_HOST!","local":"!WF_LOCAL_PATH!","remote":"!WF_REMOTE!"}
) else (
if not defined SFTP_OUTPUT set "SFTP_OUTPUT=Unknown SFTP error"
echo {"status":"error","message":"SFTP transfer failed","details":"!SFTP_OUTPUT!","host":"!WF_HOST!"}
)
endlocal & exit /b !SFTP_ERR!
:WORKFLOW_WEBHOOK
setlocal EnableDelayedExpansion
set "WF_URL=%~2"
set "WF_PAYLOAD=%~3"
:: Use powershell to send webhook
powershell -NoProfile -Command ^
"try { $r = Invoke-WebRequest -Method Post -Uri '!WF_URL!' -Body '!WF_PAYLOAD!' -ContentType 'application/json' -UseBasicParsing; Write-Output '{\"ok\":true,\"status\":' + $r.StatusCode + '}' } catch { Write-Output '{\"ok\":false,\"error\":\"' + $_.Exception.Message + '\"}' }" ^
>"%TEMP%\webhook_output.json" 2>&1
set "ERR=!ERRORLEVEL!"
:: Read result
set /p RESULT=<"%TEMP%\webhook_output.json"
:: Cleanup
del "%TEMP%\webhook_output.json" >nul 2>&1
:: Return JSON to UI
if "!ERR!"=="0" (
echo {"status":"ok","message":"Webhook executed","response":!RESULT!}
) else (
echo {"status":"error","message":"Webhook failed","details":!RESULT!}
)
endlocal & exit /b !ERR!
:SCAN_DOMAINS
:: ALL DOMAINS FROM WINDOWS FILE HOSTS
setlocal EnableDelayedExpansion
set "COUNT=0"
set "DOMAINS=["
set "COMMA="
for /f "tokens=2 delims= " %%D in ('findstr /i /r /c:"127.0.0.1" "%HOSTS%" ^| findstr /i ".local"') do (
set "DOMAIN=%%D"
set "DOMAIN=!DOMAIN: =!"
:: Skip if domain starts with # (comment)
if not "!DOMAIN:~0,1!"=="#" (
if not "!DOMAIN!"=="" (
set "DOMAINS=!DOMAINS!!COMMA!"!DOMAIN!""
set "COMMA=,"
set /a COUNT+=1
)
)
)
set "DOMAINS=!DOMAINS!]"
echo {"status":"ok","count":!COUNT!,"domains":!DOMAINS!}
endlocal
exit /b 0
:LIST_DOMAINS
:: ONLY RETURNS DOMAINS CREATED AND MANAGED BY AMP-MANAGER
:: Also validates SSL certificates by comparing CA vs cert timestamps
setlocal EnableDelayedExpansion
set "OUTPUT=["
set "COMMA="
:: Check if root CA files exist and get timestamp
set "CA_OK=false"
set "CA_TIMESTAMP=0"
if exist "%MKCERT%" (
for /f "delims=" %%i in ('"%MKCERT%" -CAROOT 2^>nul') do set "CAROOT=%%i"
if defined CAROOT (
if exist "!CAROOT!\rootCA.pem" if exist "!CAROOT!\rootCA-key.pem" (
set "CA_OK=true"
:: Get CA rootCA.pem timestamp (Windows file time in ticks)
for %%T in ("!CAROOT!\rootCA.pem") do set "CA_TIMESTAMP=%%~tT"
)
)
)
if exist "%SERVER_FOLDER%\*.conf" (
for %%F in ("%SERVER_FOLDER%\*.conf") do (
set "DOMAIN=%%~nF"
set "HAS_CONF=true"
set "HAS_SSL=false"
set "SSL_VALID=false"
:: Only report SSL=true when BOTH certificate AND CA exist
set "CERT_FILE=%CERT_FOLDER%\!DOMAIN!.pem"
if exist "!CERT_FILE!" if "!CA_OK!"=="true" set "HAS_SSL=true"
:: Compare timestamps to determine SSL validity
if "!HAS_SSL!"=="true" (
for %%T in ("!CERT_FILE!") do (
set "CERT_TIMESTAMP=%%~tT"
:: If cert timestamp >= CA timestamp, SSL is valid (generated after CA was installed/reset)
if "!CERT_TIMESTAMP!" geq "!CA_TIMESTAMP!" set "SSL_VALID=true"
)
)
set "OUTPUT=!OUTPUT!!COMMA!{"domain":"!DOMAIN!","config":!HAS_CONF!,"ssl":!HAS_SSL!,"ssl_valid":!SSL_VALID!}"
set "COMMA=,"
)
)
set "OUTPUT=!OUTPUT!]"
echo {"status":"ok","domains":!OUTPUT!}
endlocal
exit /b 0
:NEW_DOMAIN
setlocal EnableDelayedExpansion
set "NAME=%~2"
set "SCAFFOLD_FLAG=%~3"
if "%NAME%"=="" echo {"status":"error","message":"No project name provided"} & exit /b 1
:: Clean name domain (lowercase)
set "NAME=!NAME: =!"
set "NAME=!NAME:.local=!"
:: Convert to lowercase
for /f "delims=" %%i in ('powershell -NoProfile -Command "'!NAME!'.ToLower()"') do set "NAME=%%i"
set "DOMAIN=!NAME!.local"
:: Domain already exists?
findstr /i /c:"127.0.0.1 !DOMAIN!" "%HOSTS%" >nul
if !errorlevel! equ 0 (
echo {"status":"error","message":"Domain already exists in hosts file"} & exit /b 1
)
:: SAFETY BACKUP of hosts before any modification
copy /y "%HOSTS%" "%HOSTS%.bak" >nul 2>&1
set "TARGET_DIR=%WWW_FOLDER%\!DOMAIN!"
set "SCAFFOLDED=false"
if not exist "!TARGET_DIR!\" (
mkdir "!TARGET_DIR!" >nul 2>&1
if exist "!TARGET_DIR!\" set "SCAFFOLDED=true"
if /i "!SCAFFOLD_FLAG!"=="scaffold" (
if exist "%WWW_FOLDER%\_scaffold\" (
robocopy "%WWW_FOLDER%\_scaffold" "!TARGET_DIR!" /E /NFL /NDL /NJH /NJS >nul 2>&1
if errorlevel 8 (
set "SCAFFOLD_WARNING=Failed to copy scaffold files"
> "!TARGET_DIR!\index.php" echo ^<?php echo "Welcome to !DOMAIN!"; ?^>
) else if exist "!TARGET_DIR!\index.php" (
powershell -Command "(Get-Content '!TARGET_DIR!\index.php') -replace '\{\{DOMAIN\}\}', '!DOMAIN!' | Set-Content '!TARGET_DIR!\index.php'" >nul 2>&1
)
) else (
set "SCAFFOLD_WARNING=Scaffold template not found"
> "!TARGET_DIR!\index.php" echo ^<?php echo "Welcome to !DOMAIN!"; ?^>
)
)
)
:: Add to hosts
set "HOSTS_ADDED=false"
(echo 127.0.0.1 !DOMAIN! # AMPMANAGER)>>"%HOSTS%" && set "HOSTS_ADDED=true"
set "WARNING="
if "!HOSTS_ADDED!"=="false" (
set "WARNING=Failed to add entry to hosts file. Please run AMP Manager as Administrator or add '127.0.0.1 !DOMAIN!' manually."
)
if defined SCAFFOLD_WARNING (
if defined WARNING (
set "WARNING=!WARNING! | !SCAFFOLD_WARNING!"
) else (
set "WARNING=!SCAFFOLD_WARNING!"
)
)
ipconfig /flushdns >nul 2>&1
:: Generate certificates
"%MKCERT%" -cert-file "%CERT_FOLDER%\!DOMAIN!.pem" -key-file "%CERT_FOLDER%\!DOMAIN!-key.pem" "!DOMAIN!" >nul 2>&1
if errorlevel 1 (
del "%CERT_FOLDER%\!DOMAIN!.pem" >nul 2>&1
del "%CERT_FOLDER%\!DOMAIN!-key.pem" >nul 2>&1
echo {"status":"error","message":"mkcert failed to generate certificate for !DOMAIN!"}
exit /b 1
)
:: Create config from template
if not exist "%SERVER_FOLDER%" mkdir "%SERVER_FOLDER%"
set "CONF_FILE=%SERVER_FOLDER%\!DOMAIN!.conf"
set "TEMPLATE=%PROJECT_ROOT%\config\default.local.conf"
if not exist "!TEMPLATE!" (
echo {"status":"error","message":"Template not found: !TEMPLATE!"} & exit /b 1
)
copy /y "!TEMPLATE!" "!CONF_FILE!" >nul
:: Replace {{DOMAIN}}
powershell -NoProfile -Command ^
"(Get-Content '!CONF_FILE!') -replace '{{DOMAIN}}','!DOMAIN!' | Set-Content '!CONF_FILE!'"
:: Update docker-compose.override.yml
call :ADD_EXTRA_HOST "!DOMAIN!"
:: Restart angie so it loads the new config
pushd "%PROJECT_ROOT%"
docker compose up -d angie >nul 2>&1
set "RESTART_ERR=!ERRORLEVEL!"
popd
if "!RESTART_ERR!"=="0" (
set "ESCAPED_TARGET_DIR=!TARGET_DIR:\=\\!"
set "ESCAPED_CONF_FILE=!CONF_FILE:\=\\!"
set "ESCAPED_CERT_FILE=%CERT_FOLDER%\!DOMAIN!.pem"
set "ESCAPED_CERT_FILE=!ESCAPED_CERT_FILE:\=\\!"
set "ESCAPED_KEY_FILE=%CERT_FOLDER%\!DOMAIN!-key.pem"
set "ESCAPED_KEY_FILE=!ESCAPED_KEY_FILE:\=\\!"
set "ESCAPED_HOSTS=%HOSTS:\=\\%"
if defined WARNING (
echo {"status":"ok","warning":"!WARNING!","domain":"!DOMAIN!","folder":"!ESCAPED_TARGET_DIR!","scaffolded":!SCAFFOLDED!,"config":"!ESCAPED_CONF_FILE!","cert":"!ESCAPED_CERT_FILE!","key":"!ESCAPED_KEY_FILE!","hosts_added":!HOSTS_ADDED!,"docker_override_updated":true,"steps":[{"name":"scaffold","label":"Project folder","success":!SCAFFOLDED!,"path":"!ESCAPED_TARGET_DIR!"},{"name":"ssl","label":"SSL certificate","success":true,"path":"!ESCAPED_CERT_FILE!"},{"name":"config","label":"Angie configuration","success":true,"path":"!ESCAPED_CONF_FILE!"},{"name":"hosts","label":"Hosts file entry","success":!HOSTS_ADDED!,"path":"!ESCAPED_HOSTS!"}]}
) else (
echo {"status":"ok","domain":"!DOMAIN!","folder":"!ESCAPED_TARGET_DIR!","scaffolded":!SCAFFOLDED!,"config":"!ESCAPED_CONF_FILE!","cert":"!ESCAPED_CERT_FILE!","key":"!ESCAPED_KEY_FILE!","hosts_added":!HOSTS_ADDED!,"docker_override_updated":true,"steps":[{"name":"scaffold","label":"Project folder","success":!SCAFFOLDED!,"path":"!ESCAPED_TARGET_DIR!"},{"name":"ssl","label":"SSL certificate","success":true,"path":"!ESCAPED_CERT_FILE!"},{"name":"config","label":"Angie configuration","success":true,"path":"!ESCAPED_CONF_FILE!"},{"name":"hosts","label":"Hosts file entry","success":!HOSTS_ADDED!,"path":"!ESCAPED_HOSTS!"}]}
)
) else (
set "ESCAPED_TARGET_DIR=!TARGET_DIR:\=\\!"
set "ESCAPED_CONF_FILE=!CONF_FILE:\=\\!"
set "ESCAPED_CERT_FILE=%CERT_FOLDER%\!DOMAIN!.pem"
set "ESCAPED_CERT_FILE=!ESCAPED_CERT_FILE:\=\\!"
set "ESCAPED_KEY_FILE=%CERT_FOLDER%\!DOMAIN!-key.pem"
set "ESCAPED_KEY_FILE=!ESCAPED_KEY_FILE:\=\\!"
set "ESCAPED_HOSTS=%HOSTS:\=\\%"
set "FINAL_WARNING=Failed to restart Angie. The domain was created but may not be accessible until Angie is restarted."
if defined WARNING set "FINAL_WARNING=!FINAL_WARNING! | !WARNING!"
echo {"status":"ok","warning":"!FINAL_WARNING!","domain":"!DOMAIN!","folder":"!ESCAPED_TARGET_DIR!","scaffolded":!SCAFFOLDED!,"config":"!ESCAPED_CONF_FILE!","cert":"!ESCAPED_CERT_FILE!","key":"!ESCAPED_KEY_FILE!","hosts_added":!HOSTS_ADDED!,"docker_override_updated":true,"steps":[{"name":"scaffold","label":"Project folder","success":!SCAFFOLDED!,"path":"!ESCAPED_TARGET_DIR!"},{"name":"ssl","label":"SSL certificate","success":true,"path":"!ESCAPED_CERT_FILE!"},{"name":"config","label":"Angie configuration","success":true,"path":"!ESCAPED_CONF_FILE!"},{"name":"hosts","label":"Hosts file entry","success":!HOSTS_ADDED!,"path":"!ESCAPED_HOSTS!"}]}
)
endlocal
exit /b 0
:: REMOVE AMP-MANAGED DOMAIN
:REMOVE_DOMAIN
setlocal EnableDelayedExpansion
:: Normalize domain
set "DOMAIN=%~2"
if "%DOMAIN%"=="" echo {"status":"error","message":"No domain provided"} & exit /b 1
set "DOMAIN=%DOMAIN: =%"
set "DOMAIN=%DOMAIN:.local=%"
set "DOMAIN=%DOMAIN%.local"
set "CONF_FILE=%SERVER_FOLDER%\%DOMAIN%.conf"
set "CERT_FILE=%CERT_FOLDER%\%DOMAIN%.pem"
set "KEY_FILE=%CERT_FOLDER%\%DOMAIN%-key.pem"
:: Create backup before any modification
set "HOSTS_BACKUP=%HOSTS%.bak"
copy /y "%HOSTS%" "!HOSTS_BACKUP!" >nul 2>&1
if not exist "!HOSTS_BACKUP!" (
echo {"status":"error","message":"Failed to create hosts backup"} & exit /b 1
)
:: Check if domain exists in hosts
set "EXISTS_IN_HOSTS=false"
findstr /i /c:"127.0.0.1 %DOMAIN%" "%HOSTS%" >nul && set "EXISTS_IN_HOSTS=true"
:: Check if any files exist
set "ANY_FILE_EXISTS=false"
if exist "!CONF_FILE!" set "ANY_FILE_EXISTS=true"
if exist "!CERT_FILE!" set "ANY_FILE_EXISTS=true"
if exist "!KEY_FILE!" set "ANY_FILE_EXISTS=true"
:: If nothing exists at all, then it's an error
if "!EXISTS_IN_HOSTS!"=="false" if "!ANY_FILE_EXISTS!"=="false" (
echo {"status":"error","message":"Domain not found (no files or hosts entry)"} & exit /b 1
)
:: Remove hosts entry (using findstr for robustness)
set "HOSTS_REMOVED=false"
if "!EXISTS_IN_HOSTS!"=="true" (
:: Create temp file with lines that don't contain the domain
set "TEMP_HOSTS=%TEMP%\hosts_temp_!RANDOM!.txt"
:: Use findstr /v to exclude lines containing the domain
findstr /v /i "%DOMAIN%" "%HOSTS%" > "!TEMP_HOSTS!" 2>nul
:: Verify temp file exists and has content
if exist "!TEMP_HOSTS!" (
for %%A in ("!TEMP_HOSTS!") do set "TEMP_SIZE=%%~zA"
if !TEMP_SIZE! gtr 0 (
copy /y "!TEMP_HOSTS!" "%HOSTS%" >nul 2>&1 && set "HOSTS_REMOVED=true"
)
del "!TEMP_HOSTS!" >nul 2>&1
)
) else (
:: Not in hosts, so technically "removed" or "not present"
set "HOSTS_REMOVED=true"
)
:: Validate hosts file is not corrupted after modification
if "!HOSTS_REMOVED!"=="true" (
for %%A in ("%HOSTS%") do set "HOSTS_SIZE=%%~zA"
if !HOSTS_SIZE! lss 10 (
:: Hosts file corrupted (less than 10 bytes), restore from backup
copy /y "!HOSTS_BACKUP!" "%HOSTS%" >nul 2>&1
set "HOSTS_REMOVED=false"
set "WARNING=Hosts file was corrupted during removal. Restored from backup."
)
)
ipconfig /flushdns >nul 2>&1
:: Remove from docker-compose.override.yml (TUI sync pattern)
set "OV_FILE=%PROJECT_ROOT%\docker-compose.override.yml"
set "TEMP_OV=%PROJECT_ROOT%\docker-compose.override.tmp"
set "MASTER_COMPOSE=%PROJECT_ROOT%\docker-compose.yml"
set "DOCKER_CLEANED=false"
if exist "!OV_FILE!" (
powershell -NoProfile -Command "$c = Get-Content '!OV_FILE!'; $c | Where-Object { $_ -notmatch '!DOMAIN!' } | Set-Content '!TEMP_OV!'"
if exist "!TEMP_OV!" (
move /y "!TEMP_OV!" "!OV_FILE!" >nul
set "DOCKER_CLEANED=true"
)
docker info >nul 2>&1
if !errorlevel! equ 0 (
docker compose -f "!MASTER_COMPOSE!" -f "!OV_FILE!" up -d --no-deps php >nul 2>&1
)
)
:: Remove config + SSL
set "CONF_REMOVED=false"
if exist "!CONF_FILE!" (
del /f /q "!CONF_FILE!" >nul 2>&1 && set "CONF_REMOVED=true"
) else (
set "CONF_REMOVED=true"
)
set "SSL_REMOVED=false"
set "SSL_FILES_EXIST=false"
if exist "!CERT_FILE!" set "SSL_FILES_EXIST=true"
if exist "!KEY_FILE!" set "SSL_FILES_EXIST=true"
if "!SSL_FILES_EXIST!"=="true" (
del /f /q "!CERT_FILE!" >nul 2>&1
del /f /q "!KEY_FILE!" >nul 2>&1 && set "SSL_REMOVED=true"
) else (
set "SSL_REMOVED=true"
)
:: Reload Angie
set "ANGIE_RELOADED=false"
docker compose exec -T angie angie -s reload >nul 2>&1 && set "ANGIE_RELOADED=true"
set "ESCAPED_CONF_FILE=!CONF_FILE:\=\\!"
set "ESCAPED_CERT_FILE=!CERT_FILE:\=\\!"
set "ESCAPED_HOSTS=%HOSTS:\=\\%"
:: Build warning message (WARNING may already be set from validation)
if "!EXISTS_IN_HOSTS!"=="false" (
if defined WARNING (
set "WARNING=Domain was not found in hosts file. !WARNING!"
) else (
set "WARNING=Domain was not found in hosts file."
)
)
if defined HOSTS_BACKUP (
set "ESCAPED_HOSTS_BACKUP=!HOSTS_BACKUP:\=\\!"
)
echo {"status":"ok","warning":"!WARNING!","domain":"%DOMAIN%","amp_managed":true,"hosts_removed":!HOSTS_REMOVED!,"config_removed":!CONF_REMOVED!,"ssl_removed":!SSL_REMOVED!,"angie_reloaded":!ANGIE_RELOADED!,"docker_override_cleaned":!DOCKER_CLEANED!,"backup":"!ESCAPED_HOSTS_BACKUP!","steps":[{"name":"config","label":"Remove configuration","success":!CONF_REMOVED!,"path":"!ESCAPED_CONF_FILE!"},{"name":"ssl","label":"Remove SSL certificates","success":!SSL_REMOVED!,"path":"!ESCAPED_CERT_FILE!"},{"name":"hosts","label":"Remove hosts entry","success":!HOSTS_REMOVED!,"path":"!ESCAPED_HOSTS!"},{"name":"reload","label":"Reload Angie server","success":!ANGIE_RELOADED!}]}
endlocal
exit /b 0
:: GENERATE CONFIG + SSL FOR A DOMAIN
:GENERATE_CONFIG
setlocal EnableDelayedExpansion
set "DOMAIN=%~2"
if "%DOMAIN%"=="" echo {"status":"error","message":"No domain provided"} & exit /b 1
:: Normalize domain
set "DOMAIN=%DOMAIN: =%"
set "DOMAIN=%DOMAIN:.local=%"
set "DOMAIN=%DOMAIN%.local"
set "CONF_FILE=%SERVER_FOLDER%\%DOMAIN%.conf"
set "CERT_FILE=%CERT_FOLDER%\%DOMAIN%.pem"
set "KEY_FILE=%CERT_FOLDER%\%DOMAIN%-key.pem"
:: Normal domains use default.local.conf
set "TEMPLATE=%PROJECT_ROOT%\config\default.local.conf"
if not exist "!TEMPLATE!" (
echo {"status":"error","message":"Template not found: !TEMPLATE!"} & exit /b 1
)
copy /y "!TEMPLATE!" "!CONF_FILE!" >nul
:: Replace {{DOMAIN}}
powershell -NoProfile -Command ^
"(Get-Content '!CONF_FILE!') -replace '{{DOMAIN}}','%DOMAIN%' | Set-Content '!CONF_FILE!'"
:: GENERATE SSL
:GEN_SSL
"%MKCERT%" -cert-file "!CERT_FILE!" -key-file "!KEY_FILE!" "%DOMAIN%" >nul 2>&1
if errorlevel 1 (
del "!CERT_FILE!" >nul 2>&1
del "!KEY_FILE!" >nul 2>&1
echo {"status":"error","message":"mkcert failed to generate certificate for !DOMAIN!"}
exit /b 1
)
set "ESCAPED_CONF_FILE=!CONF_FILE:\=\\!"
set "ESCAPED_CERT_FILE=!CERT_FILE:\=\\!"
set "ESCAPED_KEY_FILE=!KEY_FILE:\=\\!"
echo {"status":"ok","domain":"%DOMAIN%","config":"!ESCAPED_CONF_FILE!","cert":"!ESCAPED_CERT_FILE!","key":"!ESCAPED_KEY_FILE!"}
endlocal
exit /b 0
:ANGIE_LIVE_STATUS
setlocal EnableDelayedExpansion
:: Fetch status from inside the container to bypass host-side networking/SSL/CORS issues
:: We use docker compose exec to target the service correctly regardless of container name
:: -L follows redirects (301), -k ignores SSL issues for internal localhost check
docker compose exec -T angie curl -s -L -k http://localhost/status/api/ 2>nul
if errorlevel 1 (
docker compose exec -T angie wget -qO- --no-check-certificate http://localhost/status/api/ 2>nul
if errorlevel 1 (
echo {"status":"error","message":"Failed to fetch Angie status from inside container"}
)
)
endlocal
exit /b 0
:ADD_EXTRA_HOST
setlocal EnableDelayedExpansion
set "DOMAIN=%~1"
set "OV_FILE=%PROJECT_ROOT%\docker-compose.override.yml"
:: Create override file if missing
if not exist "!OV_FILE!" (
>"!OV_FILE!" echo services:
>>"!OV_FILE!" echo php:
>>"!OV_FILE!" echo extra_hosts:
>>"!OV_FILE!" echo.
)
:: Add entry if missing
:: findstr /i /c:"!DOMAIN!:host-gateway" "!OV_FILE!" >nul
:: replace with exact match
findstr /i /r /c:"^[ ]*-[ ]*\"!DOMAIN!:host-gateway\"" "!OV_FILE!" >nul
if errorlevel 1 (
powershell -NoProfile -Command ^
"Add-Content -Path '!OV_FILE!' -Value ' - \"!DOMAIN!:host-gateway\"' -Encoding ascii" >nul 2>&1
)
:: Recreate PHP container so Angie sees the new host
docker compose -f "%PROJECT_ROOT%\docker-compose.yml" -f "!OV_FILE!" up -d --no-deps php >nul 2>&1
endlocal
:: endlocal prevent variables to leak into NEW_DOMAIN or other subroutines
exit /b 0
:CA_STATUS
setlocal EnableDelayedExpansion
set "MKCERT_PRESENT=fail"
set "CAROOT_OK=fail"
set "CAROOT="
set "VALID_UNTIL=Unknown"
:: Check mkcert exists
if exist "%MKCERT%" set "MKCERT_PRESENT=ok"
:: Get system CAROOT directly from mkcert
if "%MKCERT_PRESENT%"=="ok" (
for /f "delims=" %%i in ('powershell -NoProfile -Command ^& "%MKCERT%" -CAROOT 2^>^&1') do (
set "CAROOT=%%i"
)
)
:: Validate CAROOT
if defined CAROOT (
if exist "%CAROOT%\rootCA.pem" if exist "%CAROOT%\rootCA-key.pem" (
set "CAROOT_OK=ok"
:: Get expiration date via powershell
for /f "delims=" %%v in ('powershell -NoProfile -Command "(New-Object System.Security.Cryptography.X509Certificates.X509Certificate2('%CAROOT%\rootCA.pem')).NotAfter.ToString('yyyy-MM-dd')" 2^>nul') do (
set "VALID_UNTIL=%%v"
)
)
)
:: Escape backslashes for JSON
set "ESCAPED_CAROOT=%CAROOT:\=\\%"
echo {"status":"ok","mkcert":"%MKCERT_PRESENT%","caroot_ok":"%CAROOT_OK%","location":"!ESCAPED_CAROOT!","valid_until":"!VALID_UNTIL!"}
endlocal
exit /b 0
:CA_RESET
setlocal EnableDelayedExpansion
:: mkcert must exist
if not exist "%MKCERT%" (
echo {"status":"error","message":"mkcert not found"}
endlocal & exit /b 1
)
:: Uninstall existing CA
"%MKCERT%" -uninstall >nul 2>&1
:: Get CAROOT
set "CAROOT="
for /f "delims=" %%i in ('"%MKCERT%" -CAROOT 2^>nul') do set "CAROOT=%%i"
:: Remove CAROOT folder if it exists
if defined CAROOT if exist "!CAROOT!" (
rmdir /s /q "!CAROOT!" >nul 2>&1
)
:: Install new CA
"%MKCERT%" -install >nul 2>&1
:: Wait a moment for Windows cert store
timeout /t 2 >nul
:: Refresh CAROOT
set "CAROOT="
for /f "delims=" %%i in ('"%MKCERT%" -CAROOT 2^>nul') do set "CAROOT=%%i"
:: Validate new CA
if defined CAROOT (
if exist "!CAROOT!\rootCA.pem" if exist "!CAROOT!\rootCA-key.pem" (
set "ESCAPED_CAROOT=!CAROOT:\=\\!"
echo {"status":"ok","message":"CA reset and reinstalled","caroot":"!ESCAPED_CAROOT!"}
endlocal & exit /b 0
)
)
echo {"status":"error","message":"CA reset failed"}
endlocal
exit /b 1
:CA_UNINSTALL
setlocal EnableDelayedExpansion
if not exist "%MKCERT%" (
echo {"status":"error","message":"mkcert not found"}
endlocal & exit /b 1
)
:: Uninstall CA
"%MKCERT%" -uninstall >nul 2>&1
:: Get CAROOT
set "CAROOT="
for /f "delims=" %%i in ('"%MKCERT%" -CAROOT 2^>nul') do set "CAROOT=%%i"
:: Remove CAROOT folder
if defined CAROOT if exist "!CAROOT!" (
rmdir /s /q "!CAROOT!" >nul 2>&1
)
echo {"status":"ok","message":"CA uninstalled"}
endlocal
exit /b 0
:REGENERATE_SSL
:: Regenerate SSL certificate for a single domain
setlocal EnableDelayedExpansion
set "DOMAIN=%~2"
if "%DOMAIN%"=="" echo {"status":"error","message":"No domain provided"} & exit /b 1
:: Normalize domain
set "DOMAIN=%DOMAIN: =%"
set "DOMAIN=%DOMAIN:.local=%"
set "DOMAIN=%DOMAIN%.local"
:: Verify mkcert exists
if not exist "%MKCERT%" (
echo {"status":"error","message":"mkcert not found"}
exit /b 1
)
set "CERT_FILE=%CERT_FOLDER%\%DOMAIN%.pem"
set "KEY_FILE=%CERT_FOLDER%\%DOMAIN%-key.pem"
:: Verify domain exists in hosts
findstr /i /c:"%DOMAIN%" "%HOSTS%" >nul
if errorlevel 1 (
echo {"status":"error","message":"Domain not found: %DOMAIN%"}
exit /b 1
)
:: Generate SSL
"%MKCERT%" -cert-file "!CERT_FILE!" -key-file "!KEY_FILE!" "%DOMAIN%" >nul 2>&1
if errorlevel 1 (
echo {"status":"error","message":"mkcert failed to regenerate SSL for %DOMAIN%"}
exit /b 1
)
set "ESCAPED_CERT_FILE=!CERT_FILE:\=\\!"
set "ESCAPED_KEY_FILE=!KEY_FILE:\=\\!"
endlocal
echo {"status":"ok","domain":"%DOMAIN%","cert":"!ESCAPED_CERT_FILE!","key":"!ESCAPED_KEY_FILE!"}
exit /b 0
:REGENERATE_ALL_SSL
:: Regenerate SSL certificates for all existing domains
setlocal EnableDelayedExpansion
:: Verify mkcert exists
if not exist "%MKCERT%" (
echo {"status":"error","message":"mkcert not found"}
exit /b 1
)
set "COUNT=0"
set "FAILED=0"
set "SUCCESS_DOMAINS=["
:: Loop through all domain config files
for %%F in ("%SERVER_FOLDER%\*.conf") do (
set "FILENAME=%%~nF"
set "DOMAIN=!FILENAME!"
set "CERT_FILE=%CERT_FOLDER%\!DOMAIN!.pem"
set "KEY_FILE=%CERT_FOLDER%\!DOMAIN!-key.pem"
:: Generate SSL for this domain
"%MKCERT%" -cert-file "!CERT_FILE!" -key-file "!KEY_FILE!" "!DOMAIN!" >nul 2>&1
if errorlevel 1 (
set /a FAILED+=1
) else (
set /a COUNT+=1
if !COUNT! equ 1 (
set "SUCCESS_DOMAINS=!SUCCESS_DOMAINS!"!DOMAIN!""
) else (
set "SUCCESS_DOMAINS=!SUCCESS_DOMAINS!,"!DOMAIN!""
)
)
)
set "SUCCESS_DOMAINS=!SUCCESS_DOMAINS!]"
endlocal