package archied import ( "context" "encoding/json" "fmt" "io" "net/http" "os/exec" "strings" "time" "github.com/samcharles93/archie-core/internal/app/statestore" "github.com/samcharles93/archie-core/internal/domain/health" "github.com/samcharles93/archie-core/internal/infrastructure/configuration" ) // instanceUnit names this instance's systemd unit for a base unit name: // unchanged for the default instance and suffixed with the name after // "archie-" otherwise, so several instances install side by side. func instanceUnit(base string) string { if name := configuration.InstanceName(); name != configuration.DefaultInstance { return base + "-" + strings.TrimPrefix(name, configuration.DefaultInstance+"-") } return base } // systemdUnits drives the archie user units through systemctl --user. type systemdUnits struct{} // Managed reports whether a systemd user manager answers. func (systemdUnits) Managed(ctx context.Context) bool { return systemctl(ctx, "show-environment") == nil } func (systemdUnits) Start(ctx context.Context, unit string) error { return systemctl(ctx, "restart", instanceUnit(unit)+".service") } func (systemdUnits) Stop(ctx context.Context, unit string) error { return systemctl(ctx, "stop", instanceUnit(unit)+".service") } func systemctl(ctx context.Context, args ...string) error { ctx, cancel := context.WithTimeout(ctx, 2*time.Minute) defer cancel() output, err := exec.CommandContext(ctx, "systemctl", append([]string{"--user"}, args...)...).CombinedOutput() if err != nil { return fmt.Errorf("systemctl --user %s: %w: %s", strings.Join(args, " "), err, strings.TrimSpace(string(output))) } return nil } // dockerImages pulls through the Docker CLI. type dockerImages struct{} func (dockerImages) Pull(ctx context.Context, ref string) error { ctx, cancel := context.WithTimeout(ctx, 15*time.Minute) defer cancel() if output, err := exec.CommandContext(ctx, "docker", "pull", ref).CombinedOutput(); err != nil { return fmt.Errorf("docker pull %s: %w: %s", ref, err, strings.TrimSpace(string(output))) } return nil } // stateDatabase is the task database, backed up and restored in-process by // the State Store's own recovery code. type stateDatabase struct{ configPath string } func (d stateDatabase) Backup(ctx context.Context, out string) error { _, err := statestore.RunRecovery(ctx, statestore.RecoveryOptions{Operation: statestore.RecoveryBackup, Out: out, Config: d.configPath}) return err } func (d stateDatabase) Restore(ctx context.Context, from string) error { _, err := statestore.RunRecovery(ctx, statestore.RecoveryOptions{Operation: statestore.RecoveryRestore, From: from, Config: d.configPath}) return err } func (d stateDatabase) SchemaVersion(ctx context.Context) (int64, error) { return statestore.SchemaVersion(ctx, statestore.RecoveryOptions{Config: d.configPath}) } // daemonHealth asks archied's /health/detailed, which answers 200 only once // the daemon serves and every readiness probe passes. type daemonHealth struct{ url string } func (h daemonHealth) Healthy(ctx context.Context) (string, error) { ctx, cancel := context.WithTimeout(ctx, 10*time.Second) defer cancel() req, err := http.NewRequestWithContext(ctx, http.MethodGet, h.url+"/health/detailed", nil) if err != nil { return "", err } resp, err := http.DefaultClient.Do(req) if err != nil { return "", err } defer func() { _ = resp.Body.Close() }() body, err := io.ReadAll(io.LimitReader(resp.Body, 1<<20)) if err != nil { return "", err } var report struct { Status health.Status `json:"status"` Version string `json:"version"` } if resp.StatusCode != http.StatusOK || json.Unmarshal(body, &report) != nil || report.Status != health.StatusOK { return report.Version, fmt.Errorf("%s: %s", resp.Status, strings.TrimSpace(string(body))) } return report.Version, nil }