|
| 1 | +package cli |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "os" |
| 7 | + "path/filepath" |
| 8 | + |
| 9 | + "github.com/spf13/cobra" |
| 10 | + |
| 11 | + "github.com/open-source-cloud/devstack/internal/config" |
| 12 | + "github.com/open-source-cloud/devstack/internal/docker" |
| 13 | + "github.com/open-source-cloud/devstack/internal/generate" |
| 14 | + "github.com/open-source-cloud/devstack/internal/hooks" |
| 15 | + "github.com/open-source-cloud/devstack/internal/lock" |
| 16 | + "github.com/open-source-cloud/devstack/internal/orchestrate" |
| 17 | + "github.com/open-source-cloud/devstack/internal/state" |
| 18 | + "github.com/open-source-cloud/devstack/internal/workspace" |
| 19 | + "github.com/open-source-cloud/devstack/internal/xdg" |
| 20 | +) |
| 21 | + |
| 22 | +// newUpCmd wires `devstack up [project...]` — the onboarding saga (spec 09): it |
| 23 | +// reconciles the ledger, then drives preflight → network → generate → |
| 24 | +// shared(health-gated) → compose-up → hooks, resumable and compensating. |
| 25 | +func newUpCmd(g *GlobalOpts) *cobra.Command { |
| 26 | + var ( |
| 27 | + build bool |
| 28 | + noHooks bool |
| 29 | + noPreflight bool |
| 30 | + profile string |
| 31 | + ) |
| 32 | + cmd := &cobra.Command{ |
| 33 | + Use: "up [project...]", |
| 34 | + Short: "Bring the workspace up (network, shared infra, generate, compose up, hooks)", |
| 35 | + Long: "up takes a workspace from config to a running, health-gated stack in one\n" + |
| 36 | + "idempotent command. Phases record their state so a re-run skips satisfied\n" + |
| 37 | + "work and a crash mid-run resumes; a failure compensates the mutating phases\n" + |
| 38 | + "(refs/containers) but never destroys data (volumes/DBs survive).", |
| 39 | + RunE: func(cmd *cobra.Command, args []string) error { |
| 40 | + d, closeFn, err := buildUpDeps(cmd) |
| 41 | + if err != nil { |
| 42 | + return err |
| 43 | + } |
| 44 | + defer closeFn() |
| 45 | + d.Projects = args |
| 46 | + d.Build = build |
| 47 | + d.NoHooks = noHooks |
| 48 | + d.NoPreflight = noPreflight |
| 49 | + d.Profile = profile |
| 50 | + |
| 51 | + // Self-healing reconcile before the saga (spec 09): prune ref rows for |
| 52 | + // projects no longer live. Best-effort — never blocks `up`. |
| 53 | + _, _ = d.Manager.Reconcile(cmd.Context()) |
| 54 | + |
| 55 | + phases, err := orchestrate.BuildUp(d) |
| 56 | + if err != nil { |
| 57 | + return err |
| 58 | + } |
| 59 | + saga := &orchestrate.Saga{Workspace: d.Model.Workspace.Name, DB: d.DB, LockPath: d.LockPath} |
| 60 | + |
| 61 | + // Plain/quiet stream each phase as it completes; --json collects them. |
| 62 | + if !g.JSON && !g.Quiet { |
| 63 | + w := cmd.OutOrStdout() |
| 64 | + saga.Emit = func(r orchestrate.Record) { fmt.Fprintln(w, orchestrate.FormatPlain(r)) } |
| 65 | + } |
| 66 | + records, runErr := saga.Run(cmd.Context(), phases) |
| 67 | + |
| 68 | + if g.JSON { |
| 69 | + if err := writeJSON(cmd, records); err != nil { |
| 70 | + return err |
| 71 | + } |
| 72 | + } |
| 73 | + if runErr != nil { |
| 74 | + return runErr |
| 75 | + } |
| 76 | + return nil |
| 77 | + }, |
| 78 | + } |
| 79 | + cmd.Flags().BoolVar(&build, "build", false, "build images before starting (compose build)") |
| 80 | + cmd.Flags().BoolVar(&noHooks, "no-hooks", false, "skip lifecycle hooks") |
| 81 | + cmd.Flags().BoolVar(&noPreflight, "no-preflight", false, "skip the preflight checks") |
| 82 | + cmd.Flags().StringVar(&profile, "profile", "", "env-overlay profile for ${profile}") |
| 83 | + return cmd |
| 84 | +} |
| 85 | + |
| 86 | +// newDownCmd wires `devstack down [project...]` — stop project stacks, run |
| 87 | +// preDown hooks first, drop their ref rows. The external network and volumes are |
| 88 | +// never touched; shared services are left running (autostop is X1 config + spec 03). |
| 89 | +func newDownCmd(g *GlobalOpts) *cobra.Command { |
| 90 | + cmd := &cobra.Command{ |
| 91 | + Use: "down [project...]", |
| 92 | + Short: "Stop this workspace's project stacks and release their refs", |
| 93 | + RunE: func(cmd *cobra.Command, args []string) error { |
| 94 | + d, closeFn, err := buildUpDeps(cmd) |
| 95 | + if err != nil { |
| 96 | + return err |
| 97 | + } |
| 98 | + defer closeFn() |
| 99 | + |
| 100 | + projects := args |
| 101 | + if len(projects) == 0 { |
| 102 | + for name := range d.Model.Projects { |
| 103 | + projects = append(projects, name) |
| 104 | + } |
| 105 | + } |
| 106 | + ctx := cmd.Context() |
| 107 | + w := cmd.OutOrStdout() |
| 108 | + type result struct { |
| 109 | + Project string `json:"project"` |
| 110 | + Status string `json:"status"` |
| 111 | + Error string `json:"error,omitempty"` |
| 112 | + } |
| 113 | + var results []result |
| 114 | + var firstErr error |
| 115 | + for _, p := range projects { |
| 116 | + if _, ok := d.Model.Projects[p]; !ok { |
| 117 | + return fmt.Errorf("project %q is not in this workspace", p) |
| 118 | + } |
| 119 | + status := "stopped" |
| 120 | + if err := downProject(ctx, d, p); err != nil { |
| 121 | + status = "failed" |
| 122 | + results = append(results, result{Project: p, Status: status, Error: err.Error()}) |
| 123 | + if firstErr == nil { |
| 124 | + firstErr = err |
| 125 | + } |
| 126 | + continue |
| 127 | + } |
| 128 | + results = append(results, result{Project: p, Status: status}) |
| 129 | + if !g.JSON && !g.Quiet { |
| 130 | + fmt.Fprintf(w, "[ok] down %s\n", p) |
| 131 | + } |
| 132 | + } |
| 133 | + if g.JSON { |
| 134 | + if err := writeJSON(cmd, map[string]any{"down": results}); err != nil { |
| 135 | + return err |
| 136 | + } |
| 137 | + } |
| 138 | + return firstErr |
| 139 | + }, |
| 140 | + } |
| 141 | + return cmd |
| 142 | +} |
| 143 | + |
| 144 | +// downProject runs a project's preDown hooks (warn-by-default), composes the |
| 145 | +// stack down (never -v: volumes survive), and drops its ref rows. |
| 146 | +func downProject(ctx context.Context, d orchestrate.UpDeps, project string) error { |
| 147 | + outDir := filepath.Join(d.Model.ProjectDir(project), generate.GenDir) |
| 148 | + composeFile := filepath.Join(outDir, generate.ComposeFile) |
| 149 | + if _, err := os.Stat(composeFile); err != nil { |
| 150 | + composeFile = "" // fall back to label-driven `compose -p <proj> down` |
| 151 | + } |
| 152 | + |
| 153 | + p := d.Model.Projects[project] |
| 154 | + if len(p.Hooks.PreDown) > 0 { |
| 155 | + runner := &hooks.Runner{ |
| 156 | + Execer: hooks.OSExecer{BaseDir: d.Model.ProjectDir(project), Project: "devstack-" + project, File: composeFile}, |
| 157 | + Ledger: d.DB, |
| 158 | + Lock: func(ctx context.Context, fn func() error) error { return lock.WithLock(ctx, d.LockPath, fn) }, |
| 159 | + } |
| 160 | + // preDown defaults to warn so a broken teardown hook can't trap a workspace. |
| 161 | + if _, err := runner.RunPhase(ctx, p.Hooks.PreDown, hooks.PhaseOpts{ |
| 162 | + Project: project, Phase: "preDown", DefaultOnFailure: hooks.OnWarn, |
| 163 | + }); err != nil { |
| 164 | + return err |
| 165 | + } |
| 166 | + } |
| 167 | + |
| 168 | + cp := docker.Compose{Project: "devstack-" + project, File: composeFile, Dir: outDir, Runner: docker.ExecRunner{}} |
| 169 | + if err := cp.Down(ctx, false); err != nil { |
| 170 | + return err |
| 171 | + } |
| 172 | + if _, err := d.Manager.RegisterDown(ctx, project); err != nil { |
| 173 | + return err |
| 174 | + } |
| 175 | + return nil |
| 176 | +} |
| 177 | + |
| 178 | +// buildUpDeps assembles the up/down dependencies from the current directory. It |
| 179 | +// uses the real Engine SDK client (up/down require a daemon — the preflight |
| 180 | +// phase reports an unreachable daemon clearly). |
| 181 | +func buildUpDeps(cmd *cobra.Command) (orchestrate.UpDeps, func(), error) { |
| 182 | + var zero orchestrate.UpDeps |
| 183 | + cwd, err := os.Getwd() |
| 184 | + if err != nil { |
| 185 | + return zero, nil, err |
| 186 | + } |
| 187 | + model, err := config.Load(cwd) |
| 188 | + if err != nil { |
| 189 | + return zero, nil, err |
| 190 | + } |
| 191 | + ctx := cmd.Context() |
| 192 | + dc, err := docker.NewClient(ctx) |
| 193 | + if err != nil { |
| 194 | + return zero, nil, fmt.Errorf("docker client: %w", err) |
| 195 | + } |
| 196 | + db, err := state.Open(ctx, xdg.DataHome(), dc.ContextName()) |
| 197 | + if err != nil { |
| 198 | + _ = dc.Close() |
| 199 | + return zero, nil, err |
| 200 | + } |
| 201 | + lockPath := filepath.Join(xdg.RuntimeDir(), "devstack.lock") |
| 202 | + mgr := &workspace.Manager{Model: model, DB: db, Docker: dc, Source: builtinSource(), LockPath: lockPath} |
| 203 | + d := orchestrate.UpDeps{ |
| 204 | + Model: model, DB: db, Docker: dc, Manager: mgr, |
| 205 | + Source: mgr.Source, LockPath: lockPath, |
| 206 | + } |
| 207 | + closeFn := func() { |
| 208 | + db.Close() |
| 209 | + _ = dc.Close() |
| 210 | + } |
| 211 | + return d, closeFn, nil |
| 212 | +} |
0 commit comments