-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstart.go
More file actions
862 lines (728 loc) · 23.5 KB
/
start.go
File metadata and controls
862 lines (728 loc) · 23.5 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
package main
import (
"encoding/json"
"fmt"
"net"
"net/http"
"os"
"os/exec"
"path/filepath"
"runtime"
"strconv"
"strings"
"syscall"
"time"
"github.com/cavaliergopher/grab/v3"
"github.com/manifoldco/promptui"
"github.com/spf13/cobra"
)
var startWithDB bool
var startCmd = &cobra.Command{
Use: "start [--db system|project]",
Short: "Start the Apito engine and console",
Long: `Start the Apito engine and console with automatic setup and downloads. Optionally start a system or project database in Docker mode.`,
Run: func(cmd *cobra.Command, args []string) {
// Check if --db flag is set
dbType, _ := cmd.Flags().GetString("db")
// If --db specified, run database helper first
if dbType != "" {
if dbType != "system" && dbType != "project" {
print_error("Invalid database type. Use 'system' or 'project'")
return
}
startDatabaseInteractive(dbType)
} else {
startApito()
}
},
}
func init() {
startCmd.Flags().String("db", "", "Start a database via Docker before services (system|project)")
}
func startApito() {
print_step("🚀 Starting Apito Engine and Console")
fmt.Println()
// Ensure required directories/files exist
if err := ensureBaseDirs(); err != nil {
print_error("Failed to prepare directories: " + err.Error())
return
}
// Determine run mode (docker/manual)
mode, err := determineRunMode()
if err != nil {
print_error("Failed to determine run mode: " + err.Error())
return
}
print_status("Run mode: " + mode)
switch mode {
case "manual":
// Step 1: Check port availability (interactive) only in manual mode
print_status("Step 1: Checking port availability...")
// Ensure apito is initialized
homeDir, err := os.UserHomeDir()
if err != nil {
print_error("Unable to determine home directory: " + err.Error())
return
}
apitoDir := filepath.Join(homeDir, ".apito")
if _, statErr := os.Stat(apitoDir); os.IsNotExist(statErr) {
print_error("Apito is not initialized. Please run: apito init")
return
}
// Validate critical assets in both modes
binDir := filepath.Join(apitoDir, "bin")
envFile := filepath.Join(binDir, ".env")
dbDir := filepath.Join(apitoDir, "db")
if _, err := os.Stat(envFile); os.IsNotExist(err) {
print_error("Missing ~/.apito/bin/.env. Please run: apito init")
return
}
if _, err := os.Stat(dbDir); os.IsNotExist(err) {
if err := os.MkdirAll(dbDir, 0755); err != nil {
print_error("Failed to create db directory: " + err.Error())
return
}
}
if err := checkPortAvailabilityInteractive(); err != nil {
print_warning("Port availability check encountered issues: " + err.Error())
}
print_success("Port availability check completed")
if mode == "manual" {
// Step 2: Check and download engine
print_status("Step 2: Checking engine binary...")
if err := ensureEngineBinary(); err != nil {
print_error("Failed to ensure engine binary: " + err.Error())
return
}
print_success("Engine binary ready")
fmt.Println()
}
// Step 3: Check if engine is already running
print_status("Step 3: Checking if engine is already running...")
if running, _, _ := serviceRunning("engine"); running {
print_warning("Engine is already running")
print_status("Skipping engine startup")
} else {
print_status("Engine is not running, will start it")
}
fmt.Println()
// Step 4: Check and download console
print_status("Step 4: Checking console files...")
if err := ensureConsoleFiles(); err != nil {
print_error("Failed to ensure console files: " + err.Error())
return
}
print_success("Console files ready")
fmt.Println()
// Step 5: Check and install Caddy
print_status("Step 5: Checking Caddy installation...")
if err := ensureCaddyInstalled(); err != nil {
print_error("Failed to ensure Caddy installation: " + err.Error())
return
}
print_success("Caddy ready")
fmt.Println()
// Step 6: Start engine (if not already running)
if running, _, _ := serviceRunning("engine"); !running {
print_status("Step 6: Starting engine...")
if err := startManagedService("engine"); err != nil {
print_error("Failed to start engine: " + err.Error())
return
}
print_success("Engine started successfully")
fmt.Println()
}
// Step 7: Start console with Caddy
print_status("Step 7: Starting console with Caddy...")
if err := setupCaddyfile(); err != nil {
print_error("Failed to prepare console config: " + err.Error())
return
}
if running, _, _ := serviceRunning("console"); !running {
if err := startManagedService("console"); err != nil {
print_error("Failed to start console: " + err.Error())
return
}
}
print_success("Console started successfully")
fmt.Println()
case "docker":
print_status("Docker mode: skipping local port pre-check (Docker will map ports)")
// Check for component updates before starting
print_status("Checking for component updates...")
updates, err := checkForComponentUpdates()
if err != nil {
print_warning("Could not check for updates: " + err.Error())
} else if len(updates) > 0 {
// Prompt user for updates
componentsToUpdate := promptForComponentUpdates(updates)
if len(componentsToUpdate) > 0 {
for _, component := range componentsToUpdate {
update := updates[component]
// Pull the new image
if err := pullDockerImage(component, update.LatestVersion); err != nil {
print_error(fmt.Sprintf("Failed to pull %s: %v", component, err))
continue
}
// Update config.yml
if err := updateComponentVersion(component, update.LatestVersion); err != nil {
print_error(fmt.Sprintf("Failed to update config for %s: %v", component, err))
continue
}
print_success(fmt.Sprintf("Updated %s to %s", component, update.LatestVersion))
}
// Regenerate docker-compose.yml with new versions
print_status("Updating docker-compose.yml...")
if _, err := writeComposeFile(); err != nil {
print_error("Failed to update docker-compose.yml: " + err.Error())
} else {
print_success("docker-compose.yml updated")
}
} else {
print_status("Skipping updates, using current versions")
}
} else {
print_success("All components are up to date")
}
fmt.Println()
// Docker mode - start services
print_step("🐳 Starting Docker Services")
if err := ensureEnvFileReady(); err != nil {
print_error("Failed to ensure .env file: " + err.Error())
return
}
if err := ensureDockerAndComposeAvailable(); err != nil {
print_error("Docker not available: " + err.Error())
return
}
if err := dockerComposeUp(); err != nil {
print_error("Failed to start Docker services: " + err.Error())
return
}
fmt.Println()
print_success("Docker services started (engine:5050, console:4000)")
default:
print_error("Invalid run mode: " + mode)
return
}
fmt.Println()
print_success("🎉 Apito is now running!")
print_status("Engine: http://localhost:5050")
print_status("Console: http://localhost:4000")
print_status("Services run in the background.")
print_status("First time? Run 'apito logs engine' for login credentials.")
print_status("To stop services, run 'apito stop'")
fmt.Println()
}
func ensureEngineBinary() error {
homeDir, err := os.UserHomeDir()
if err != nil {
return fmt.Errorf("error finding home directory: %w", err)
}
enginePath := filepath.Join(homeDir, ".apito", "bin", "engine")
// Check if engine binary exists
if _, err := os.Stat(enginePath); err == nil {
print_status("Engine binary already exists")
return nil
}
print_status("Engine binary not found, downloading...")
// Get latest release tag
releaseTag, err := getLatestReleaseTag()
if err != nil {
return fmt.Errorf("error fetching latest release tag: %w", err)
}
// Download engine based on system architecture
if err := downloadEngine(releaseTag, homeDir); err != nil {
return fmt.Errorf("error downloading engine: %w", err)
}
return nil
}
func downloadEngine(releaseTag, homeDir string) error {
baseURL := fmt.Sprintf("https://github.com/apito-io/engine/releases/download/%s/", releaseTag)
var assetName string
switch runtime.GOOS {
case "linux":
if runtime.GOARCH == "arm64" {
assetName = "engine-linux-arm64.zip"
} else {
assetName = "engine-linux-amd64.zip"
}
case "darwin":
if runtime.GOARCH == "arm64" {
assetName = "engine-darwin-arm64.zip"
} else {
assetName = "engine-darwin-amd64.zip"
}
case "windows":
if runtime.GOARCH == "arm64" {
assetName = "engine-windows-arm64.zip"
} else {
assetName = "engine-windows-amd64.zip"
}
default:
return fmt.Errorf("unsupported OS: %s", runtime.GOOS)
}
assetURL := baseURL + assetName
print_status("Downloading engine from: " + assetURL)
// Download to temp directory
resp, err := grab.Get(os.TempDir(), assetURL)
if err != nil {
return fmt.Errorf("error downloading file: %w", err)
}
// Show download progress
t := time.NewTicker(500 * time.Millisecond)
defer t.Stop()
Loop:
for {
select {
case <-t.C:
fmt.Printf(" transferred %v / %v bytes (%.2f%%)\n",
resp.BytesComplete(),
resp.Size(),
100*resp.Progress())
case <-resp.Done:
break Loop
}
}
if err := resp.Err(); err != nil {
return fmt.Errorf("download failed: %w", err)
}
print_status("Downloaded file saved to: " + resp.Filename)
// Extract into a unique temp directory
tmpDir, err := os.MkdirTemp("", "apito-engine-*")
if err != nil {
return fmt.Errorf("error creating temp dir: %w", err)
}
defer os.RemoveAll(tmpDir)
if _, err := extractArchiveToTemp(resp.Filename); err != nil {
return fmt.Errorf("error extracting file: %w", err)
}
// Locate engine binary
engineBinary := "engine"
if runtime.GOOS == "windows" {
engineBinary += ".exe"
}
sourcePath, err := findBinaryInDir(tmpDir, engineBinary)
if err != nil {
return fmt.Errorf("unable to locate engine binary: %w", err)
}
// Move to ~/.apito/bin/engine
destDir := filepath.Join(homeDir, ".apito", "bin")
if err := os.MkdirAll(destDir, 0755); err != nil {
return fmt.Errorf("error creating bin directory: %w", err)
}
destPath := filepath.Join(destDir, engineBinary)
if err := os.Rename(sourcePath, destPath); err != nil {
return fmt.Errorf("error moving engine binary: %w", err)
}
if err := os.Chmod(destPath, 0755); err != nil {
return fmt.Errorf("error making binary executable: %w", err)
}
print_status("Engine binary extracted to: " + destPath)
return nil
}
func isEngineRunning() bool {
// Check ENGINE_PID in config
homeDir, err := os.UserHomeDir()
if err != nil {
return false
}
config, err := getConfig(filepath.Join(homeDir, ".apito"))
if err != nil {
return false
}
pidStr, ok := config["ENGINE_PID"]
if !ok {
return false
}
pid, err := strconv.Atoi(pidStr)
if err != nil {
return false
}
// Check if process exists
process, err := os.FindProcess(pid)
if err != nil {
return false
}
// Try to send signal 0 to check if process is running
if err := process.Signal(syscall.Signal(0)); err != nil {
return false
}
return true
}
func ensureConsoleFiles() error {
homeDir, err := os.UserHomeDir()
if err != nil {
return fmt.Errorf("error finding home directory: %w", err)
}
consoleDir := filepath.Join(homeDir, ".apito", "console")
// Check if console directory exists
if _, err := os.Stat(consoleDir); err == nil {
print_status("Console files already exist")
return nil
}
print_status("Console files not found, downloading...")
// Get latest console release
consoleReleaseTag, err := getLatestConsoleReleaseTag()
if err != nil {
return fmt.Errorf("error fetching latest console release: %w", err)
}
// Download console
if err := downloadConsole(consoleReleaseTag, homeDir); err != nil {
return fmt.Errorf("error downloading console: %w", err)
}
return nil
}
func getLatestConsoleReleaseTag() (string, error) {
resp, err := http.Get("https://api.github.com/repos/apito-io/console/releases/latest")
if err != nil {
return "", fmt.Errorf("error fetching latest console release: %w", err)
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return "", fmt.Errorf("failed to fetch latest console release: status code %d", resp.StatusCode)
}
var result struct {
TagName string `json:"tag_name"`
}
if err := json.NewDecoder(resp.Body).Decode(&result); err != nil {
return "", fmt.Errorf("error decoding console response: %w", err)
}
return result.TagName, nil
}
func downloadConsole(releaseTag, homeDir string) error {
baseURL := fmt.Sprintf("https://github.com/apito-io/console/releases/download/%s/", releaseTag)
assetURL := baseURL + fmt.Sprintf("console-%s.zip", releaseTag)
print_status("Downloading console from: " + assetURL)
// Download the file
resp, err := grab.Get(homeDir, assetURL)
if err != nil {
return fmt.Errorf("error downloading console: %w", err)
}
// Show download progress
t := time.NewTicker(500 * time.Millisecond)
defer t.Stop()
Loop:
for {
select {
case <-t.C:
fmt.Printf(" transferred %v / %v bytes (%.2f%%)\n",
resp.BytesComplete(),
resp.Size(),
100*resp.Progress())
case <-resp.Done:
break Loop
}
}
// Check for errors
if err := resp.Err(); err != nil {
return fmt.Errorf("console download failed: %w", err)
}
print_status("Console downloaded to: " + resp.Filename)
// Extract to console directory
consoleDir := filepath.Join(homeDir, ".apito", "console")
if _, err := extractArchiveToTemp(resp.Filename); err != nil {
return fmt.Errorf("error extracting console: %w", err)
}
print_status("Console extracted to: " + consoleDir)
return nil
}
func ensureCaddyInstalled() error {
// Check if Caddy is already installed
if foundPath, err := exec.LookPath("caddy"); err == nil {
print_status("Caddy is already installed")
// Persist discovered path for future use
if homeDir, herr := os.UserHomeDir(); herr == nil {
_ = updateEnvConfig(filepath.Join(homeDir, ".apito", "bin"), "CADDY_PATH", foundPath)
}
return nil
}
print_status("Caddy not found, downloading and installing...")
// Get latest Caddy release
caddyReleaseTag, err := getLatestCaddyReleaseTag()
if err != nil {
return fmt.Errorf("error fetching latest Caddy release: %w", err)
}
// Download and install Caddy
installedPath, err := downloadAndInstallCaddy(caddyReleaseTag)
if err != nil {
return fmt.Errorf("error installing Caddy: %w", err)
}
// Save installed path to config for later use
if homeDir, herr := os.UserHomeDir(); herr == nil {
_ = updateEnvConfig(filepath.Join(homeDir, ".apito", "bin"), "CADDY_PATH", installedPath)
}
return nil
}
func getLatestCaddyReleaseTag() (string, error) {
resp, err := http.Get("https://api.github.com/repos/caddyserver/caddy/releases/latest")
if err != nil {
return "", fmt.Errorf("error fetching latest Caddy release: %w", err)
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return "", fmt.Errorf("failed to fetch latest Caddy release: status code %d", resp.StatusCode)
}
var result struct {
TagName string `json:"tag_name"`
}
if err := json.NewDecoder(resp.Body).Decode(&result); err != nil {
return "", fmt.Errorf("error decoding Caddy response: %w", err)
}
return result.TagName, nil
}
func downloadAndInstallCaddy(releaseTag string) (string, error) {
// Determine Caddy asset name based on system
var assetName string
switch runtime.GOOS {
case "linux":
if runtime.GOARCH == "arm64" {
assetName = "caddy_" + strings.TrimPrefix(releaseTag, "v") + "_linux_arm64.tar.gz"
} else {
assetName = "caddy_" + strings.TrimPrefix(releaseTag, "v") + "_linux_amd64.tar.gz"
}
case "darwin":
if runtime.GOARCH == "arm64" {
assetName = "caddy_" + strings.TrimPrefix(releaseTag, "v") + "_mac_arm64.tar.gz"
} else {
assetName = "caddy_" + strings.TrimPrefix(releaseTag, "v") + "_mac_amd64.tar.gz"
}
case "windows":
assetName = "caddy_" + strings.TrimPrefix(releaseTag, "v") + "_windows_amd64.zip"
default:
return "", fmt.Errorf("unsupported OS for Caddy: %s", runtime.GOOS)
}
baseURL := fmt.Sprintf("https://github.com/caddyserver/caddy/releases/download/%s/", releaseTag)
assetURL := baseURL + assetName
print_status("Downloading Caddy from: " + assetURL)
// Download Caddy
resp, err := grab.Get(os.TempDir(), assetURL)
if err != nil {
return "", fmt.Errorf("error downloading Caddy: %w", err)
}
// Extract Caddy into a unique temp directory to avoid name collisions
tmpDir, err := os.MkdirTemp("", "apito-caddy-*")
if err != nil {
return "", fmt.Errorf("error creating temp dir: %w", err)
}
defer os.RemoveAll(tmpDir)
if _, err := extractArchiveToTemp(resp.Filename); err != nil {
return "", fmt.Errorf("error extracting Caddy: %w", err)
}
// Locate extracted caddy binary
caddyBinary := "caddy"
if runtime.GOOS == "windows" {
caddyBinary += ".exe"
}
sourcePath, err := findBinaryInDir(tmpDir, caddyBinary)
if err != nil {
return "", fmt.Errorf("unable to locate caddy binary after extraction: %w", err)
}
// Move Caddy to a location in PATH
destPath := filepath.Join("/usr/local/bin", caddyBinary)
// Try to move to /usr/local/bin (requires sudo on some systems)
if err := os.Rename(sourcePath, destPath); err != nil {
// Fallback to ~/.apito/bin
homeDir, err := os.UserHomeDir()
if err != nil {
return "", fmt.Errorf("error finding home directory: %w", err)
}
apitoBinDir := filepath.Join(homeDir, ".apito", "bin")
if err := os.MkdirAll(apitoBinDir, 0755); err != nil {
return "", fmt.Errorf("error creating apito bin directory: %w", err)
}
destPath = filepath.Join(apitoBinDir, caddyBinary)
if err := os.Rename(sourcePath, destPath); err != nil {
return "", fmt.Errorf("error moving Caddy binary: %w", err)
}
print_warning("Caddy installed to " + destPath)
print_warning("Please add " + apitoBinDir + " to your PATH")
} else {
print_status("Caddy installed to " + destPath)
}
// Make it executable
if err := os.Chmod(destPath, 0755); err != nil {
return "", fmt.Errorf("error making Caddy executable: %w", err)
}
return destPath, nil
}
func startEngine() error {
homeDir, err := os.UserHomeDir()
if err != nil {
return fmt.Errorf("error finding home directory: %w", err)
}
enginePath := filepath.Join(homeDir, ".apito", "bin", "engine")
// Start engine in background
cmd := exec.Command(enginePath)
cmd.SysProcAttr = &syscall.SysProcAttr{
Setpgid: true,
}
if err := cmd.Start(); err != nil {
return fmt.Errorf("error starting engine: %w", err)
}
// Legacy: previously saved PID to config. Now managed via PID files; keep no-op for compatibility.
print_status("Engine started with PID: " + strconv.Itoa(cmd.Process.Pid))
return nil
}
// setupCaddyfile creates or updates the Caddyfile for serving the console
func setupCaddyfile() error {
homeDir, err := os.UserHomeDir()
if err != nil {
return fmt.Errorf("error finding home directory: %w", err)
}
consoleDir := filepath.Join(homeDir, ".apito", "console")
// Create Caddyfile
caddyfilePath := filepath.Join(homeDir, ".apito", "Caddyfile")
caddyfileContent := fmt.Sprintf(`:4000 {
root * %s
file_server
encode gzip
}`, consoleDir)
if err := os.WriteFile(caddyfilePath, []byte(caddyfileContent), 0644); err != nil {
return fmt.Errorf("error creating Caddyfile: %w", err)
}
return nil
}
// resolveCaddyPath returns the absolute path to the caddy binary if known,
// otherwise falls back to the name "caddy" to let the OS PATH resolution handle it.
func resolveCaddyPath() string {
// Prefer configured path
if homeDir, err := os.UserHomeDir(); err == nil {
if cfg, err := getConfig(filepath.Join(homeDir, ".apito", "bin")); err == nil {
if p, ok := cfg["CADDY_PATH"]; ok && p != "" {
return p
}
}
}
// Next, system PATH
if lp, err := exec.LookPath("caddy"); err == nil {
return lp
}
// Common install locations
if homeDir, err := os.UserHomeDir(); err == nil {
// Prefer managed location inside ~/.apito/bin
candidate := filepath.Join(homeDir, ".apito", "bin", "caddy")
if _, err := os.Stat(candidate); err == nil {
return candidate
}
}
if _, err := os.Stat("/usr/local/bin/caddy"); err == nil {
return "/usr/local/bin/caddy"
}
// Fallback: rely on PATH (will error when executed if not found)
return "caddy"
}
// findFileRecursive moved to utility: findBinaryInDir
// checkPortAvailabilityInteractive checks ports 5050 and 4000 and, if occupied,
// asks the user whether to terminate the process using the port.
func checkPortAvailabilityInteractive() error {
ports := []int{5050, 4000}
for _, port := range ports {
address := ":" + strconv.Itoa(port)
listener, err := net.Listen("tcp", address)
if err != nil {
print_warning(fmt.Sprintf("Port %d is already in use", port))
// Prompt user to free the port
prompt := promptui.Select{
Label: fmt.Sprintf("Port %d is in use. Free it now?", port),
Items: []string{"Yes, kill the process", "No, skip"},
}
_, choice, perr := prompt.Run()
if perr != nil {
// Non-interactive or prompt failed; skip gracefully
print_status(fmt.Sprintf("Skipping freeing port %d (prompt unavailable)", port))
continue
}
if strings.HasPrefix(strings.ToLower(choice), "yes") {
if err := killProcessOnPort(port); err != nil {
print_error(fmt.Sprintf("Failed to free port %d: %v", port, err))
} else {
print_success(fmt.Sprintf("Freed port %d", port))
}
} else {
print_status(fmt.Sprintf("Skipped freeing port %d", port))
}
} else {
listener.Close()
print_status(fmt.Sprintf("Port %d is available", port))
}
}
return nil
}
// killProcessOnPort finds and terminates processes listening on the given TCP port.
func killProcessOnPort(port int) error {
pids, err := pidsForPort(port)
if err != nil {
return err
}
if len(pids) == 0 {
return fmt.Errorf("no process found on port %d", port)
}
// Try graceful termination first
for _, pid := range pids {
_ = exec.Command("kill", "-TERM", pid).Run()
}
// Small delay to allow shutdown
time.Sleep(500 * time.Millisecond)
// Verify and force kill if still present
remaining, _ := pidsForPort(port)
for _, pid := range remaining {
_ = exec.Command("kill", "-9", pid).Run()
}
return nil
}
// pidsForPort returns process IDs listening on the given TCP port using common system tools.
func pidsForPort(port int) ([]string, error) {
// Prefer lsof
cmd := exec.Command("lsof", "-ti", fmt.Sprintf("tcp:%d", port))
out, err := cmd.Output()
if err == nil && len(out) > 0 {
lines := strings.Split(strings.TrimSpace(string(out)), "\n")
return lines, nil
}
// Try alternate lsof args (LISTEN only)
cmd = exec.Command("lsof", "-iTCP:"+strconv.Itoa(port), "-sTCP:LISTEN", "-t")
out, err = cmd.Output()
if err == nil && len(out) > 0 {
lines := strings.Split(strings.TrimSpace(string(out)), "\n")
return lines, nil
}
// Linux fallback: fuser
cmd = exec.Command("fuser", fmt.Sprintf("%d/tcp", port))
out, err = cmd.Output()
if err == nil && len(out) > 0 {
// fuser output is space separated PIDs
fields := strings.Fields(strings.TrimSpace(string(out)))
return fields, nil
}
return []string{}, nil
}
func stopAllServices() {
// Determine the run mode and stop services accordingly
cfg, err := loadCLIConfig()
if err != nil {
print_error("Failed to load configuration: " + err.Error())
return
}
mode := cfg.Mode
if mode == "" {
mode = "manual"
}
switch mode {
case "docker":
// Stop Docker containers
print_status("Stopping Docker containers...")
if err := dockerComposeDown(); err != nil {
print_error("Failed to stop Docker services: " + err.Error())
return
}
print_success("All Docker services stopped")
case "manual":
// Stop managed processes
_ = stopManagedService("console")
_ = stopManagedService("engine")
print_success("All services stopped")
default:
print_error("Unknown run mode: " + mode)
}
}