|
| 1 | +package commands |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "os/exec" |
| 7 | + "path/filepath" |
| 8 | + |
| 9 | + "github.com/ethpandaops/xcli/pkg/config" |
| 10 | + "github.com/ethpandaops/xcli/pkg/ui" |
| 11 | + "github.com/sirupsen/logrus" |
| 12 | + "github.com/spf13/cobra" |
| 13 | +) |
| 14 | + |
| 15 | +// NewXatuCheckCommand creates the xatu check command. |
| 16 | +func NewXatuCheckCommand(log logrus.FieldLogger, configPath string) *cobra.Command { |
| 17 | + return &cobra.Command{ |
| 18 | + Use: "check", |
| 19 | + Short: "Verify xatu environment is ready", |
| 20 | + Long: `Perform health checks on the xatu environment without starting services. |
| 21 | +
|
| 22 | +Verifies: |
| 23 | + - Configuration file exists and is valid |
| 24 | + - Xatu repo exists with docker-compose.yml |
| 25 | + - Docker daemon is running and accessible |
| 26 | + - Docker Compose is available |
| 27 | + - Warns about potential port conflicts with lab stack |
| 28 | +
|
| 29 | +Exit codes: |
| 30 | + 0 - All checks passed |
| 31 | + 1 - One or more checks failed |
| 32 | +
|
| 33 | +Example: |
| 34 | + xcli xatu check`, |
| 35 | + RunE: func(cmd *cobra.Command, args []string) error { |
| 36 | + return runXatuCheck(cmd.Context(), log, configPath) |
| 37 | + }, |
| 38 | + } |
| 39 | +} |
| 40 | + |
| 41 | +func runXatuCheck(ctx context.Context, log logrus.FieldLogger, configPath string) error { |
| 42 | + ui.Header("Running xatu environment health checks...") |
| 43 | + |
| 44 | + allPassed := true |
| 45 | + |
| 46 | + // Check 1: Configuration file |
| 47 | + spinner := ui.NewSpinner("Checking configuration file") |
| 48 | + |
| 49 | + xatuCfg, _, err := config.LoadXatuConfig(configPath) |
| 50 | + if err != nil { |
| 51 | + spinner.Fail(fmt.Sprintf("Configuration file error: %v", err)) |
| 52 | + |
| 53 | + allPassed = false |
| 54 | + } else { |
| 55 | + spinner.Success("Configuration file valid") |
| 56 | + } |
| 57 | + |
| 58 | + // Check 2: Configuration validity |
| 59 | + if xatuCfg != nil { |
| 60 | + spinner = ui.NewSpinner("Validating configuration") |
| 61 | + |
| 62 | + if err := xatuCfg.Validate(); err != nil { |
| 63 | + spinner.Fail(fmt.Sprintf("Configuration validation failed: %v", err)) |
| 64 | + |
| 65 | + allPassed = false |
| 66 | + } else { |
| 67 | + spinner.Success("Configuration valid") |
| 68 | + } |
| 69 | + |
| 70 | + // Check 3: docker-compose.yml |
| 71 | + spinner = ui.NewSpinner("Checking docker-compose.yml") |
| 72 | + |
| 73 | + absRepo, _ := filepath.Abs(xatuCfg.Repos.Xatu) |
| 74 | + composePath := filepath.Join(absRepo, "docker-compose.yml") |
| 75 | + |
| 76 | + if _, statErr := statFile(composePath); statErr != nil { |
| 77 | + spinner.Fail(fmt.Sprintf("docker-compose.yml not found: %s", composePath)) |
| 78 | + |
| 79 | + allPassed = false |
| 80 | + } else { |
| 81 | + spinner.Success("docker-compose.yml found") |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + // Check 4: Docker |
| 86 | + spinner = ui.NewSpinner("Checking Docker daemon") |
| 87 | + |
| 88 | + cmd := exec.CommandContext(ctx, "docker", "info") |
| 89 | + if err := cmd.Run(); err != nil { |
| 90 | + spinner.Fail("Docker daemon not accessible - Ensure Docker Desktop is running") |
| 91 | + |
| 92 | + allPassed = false |
| 93 | + } else { |
| 94 | + spinner.Success("Docker daemon accessible") |
| 95 | + } |
| 96 | + |
| 97 | + // Check 5: Docker Compose |
| 98 | + spinner = ui.NewSpinner("Checking Docker Compose") |
| 99 | + |
| 100 | + cmd = exec.CommandContext(ctx, "docker", "compose", "version") |
| 101 | + if err := cmd.Run(); err != nil { |
| 102 | + spinner.Fail("Docker Compose not available") |
| 103 | + |
| 104 | + allPassed = false |
| 105 | + } else { |
| 106 | + spinner.Success("Docker Compose available") |
| 107 | + } |
| 108 | + |
| 109 | + // Check 6: Port conflict warning with lab stack |
| 110 | + checkPortConflicts(log, configPath) |
| 111 | + |
| 112 | + // Summary |
| 113 | + ui.Blank() |
| 114 | + |
| 115 | + if allPassed { |
| 116 | + ui.Success("All checks passed! Environment is ready.") |
| 117 | + |
| 118 | + ui.Header("Next steps:") |
| 119 | + fmt.Println(" xcli xatu up # Start the xatu stack") |
| 120 | + fmt.Println(" xcli xatu up --build # Start with image rebuild") |
| 121 | + |
| 122 | + return nil |
| 123 | + } |
| 124 | + |
| 125 | + ui.Error("Some checks failed. Please resolve the issues above.") |
| 126 | + |
| 127 | + return fmt.Errorf("environment checks failed") |
| 128 | +} |
| 129 | + |
| 130 | +// checkPortConflicts warns about potential port conflicts between xatu and lab stacks. |
| 131 | +func checkPortConflicts(log logrus.FieldLogger, configPath string) { |
| 132 | + result, err := config.Load(configPath) |
| 133 | + if err != nil || result.Config.Lab == nil || result.Config.Xatu == nil { |
| 134 | + return |
| 135 | + } |
| 136 | + |
| 137 | + // Both stacks are configured - warn about common port conflicts |
| 138 | + ui.Blank() |
| 139 | + ui.Warning("Both lab and xatu stacks are configured. Common port conflicts:") |
| 140 | + fmt.Println(" - Grafana (3000), Prometheus (9090), ClickHouse (8123)") |
| 141 | + ui.Info("Use xatu.envOverrides in .xcli.yaml to remap xatu ports, e.g.:") |
| 142 | + fmt.Println(" xatu:") |
| 143 | + fmt.Println(" envOverrides:") |
| 144 | + fmt.Println(" GRAFANA_PORT: \"3001\"") |
| 145 | +} |
0 commit comments