-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgreen
More file actions
executable file
·211 lines (185 loc) · 8.69 KB
/
Copy pathgreen
File metadata and controls
executable file
·211 lines (185 loc) · 8.69 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
#!/usr/bin/env bb
;; doks launcher — one managed Kubernetes cluster (DigitalOcean DOKS or Vultr
;; VKE) through the colors-compute library, with an optional deployment-owned
;; DigitalOcean container registry integrated with the cluster.
;;
;; Desired state lives in colors.yml, found by walking up from the working
;; directory. Secrets and tokens are never written there: every credential key
;; is supplied at runtime through a COLORS_PAR_* environment variable, which is
;; overlaid onto the matching flat key.
;;
;; COLORS_PAR_PROFILE is the exception — doks refuses to run when it is set.
;; The profile names this project's work directory, OpenTofu state keys and
;; cloud resources, and overriding it from the environment can only point doks
;; at another project's state.
;;
;; This file is both the skill payload and the repository entry point — ./green
;; in the project root is a symlink to it. It deliberately holds no logic of its
;; own: validation, the graph and the steps all live in the `doks` library,
;; where the test suite reaches them. A copied payload is the one place in this
;; project where code cannot be tested, so nothing that can live elsewhere
;; should live here. scripts/launcher.sh covers what remains.
(require '[babashka.fs :as fs]
'[clojure.java.io :as io]
'[clojure.string :as str])
(def script-dir
(let [file (if (= "NO_SOURCE_PATH" *file*)
(fs/path (fs/cwd) "green")
(fs/path *file*))]
(str (fs/parent (fs/canonicalize file)))))
;; ---------------------------------------------------------------------------
;; Dependency bootstrap
;;
;; Nothing in this stack is published to Clojars, so a standalone copy of this
;; launcher resolves `doks` as an immutable git dependency and lets tools.deps
;; pull `green` and `colors-compute` transitively from doks's own deps.edn. When
;; the classpath already provides doks — running inside the project, where
;; bb.edn supplies a local root — bootstrap is skipped. DOKS_LIB_ROOT,
;; GREEN_LIB_ROOT and COLORS_COMPUTE_LIB_ROOT override immutable dependencies
;; with working trees.
(def ^:private launcher-contract
"Minimum io.github.getcolors.doks contract this launcher requires. Guarded at
run time so a stale pin fails loudly instead of rendering documents from an
older commit."
1)
;; Managed by `bb pin` — do not edit by hand.
;;
;; nil before this repository has been pushed and stamped: a launcher that
;; cannot resolve its own library says so rather than inventing a SHA or
;; silently falling back to whatever is lying around. `bb pin` refuses to stamp
;; a dirty or unpushed HEAD for the same reason.
(def ^:private doks-sha "8120674e81d88827c4e8b6eaa899b0c5fbaf829c")
(def ^:private doks-git-url "https://github.com/getcolors/doks.git")
(def ^:private green-git-url "https://github.com/getcolors/green.git")
(def ^:private compute-git-url "https://github.com/getcolors/colors-compute.git")
(defn- lib-coord
[env-var url sha deps-root]
(if-let [root (System/getenv env-var)]
{:local/root (if deps-root (str (io/file root deps-root)) root)}
(when sha
(cond-> {:git/url url :git/sha sha}
deps-root (assoc :deps/root deps-root)))))
(def ^:private resolved?
(some? (io/resource "io/github/getcolors/doks/tools.clj")))
(def ^:private bootstrap-error
(when (and (System/getProperty "babashka.version") (not resolved?))
(let [doks (lib-coord "DOKS_LIB_ROOT" doks-git-url doks-sha nil)
green (lib-coord "GREEN_LIB_ROOT" green-git-url nil nil)
compute (lib-coord "COLORS_COMPUTE_LIB_ROOT" compute-git-url nil "green")]
(if-not doks
(str "this launcher carries no doks pin.\n"
"It has not been stamped by `bb pin`, which cannot run until the "
"doks repository has been pushed.\n"
"Point it at a checkout meanwhile: DOKS_LIB_ROOT=/path/to/doks")
(do ((requiring-resolve 'babashka.deps/add-deps)
{:deps (cond-> {'io.github.getcolors/doks doks}
green (assoc 'io.github.getcolors/green green)
compute (assoc 'io.github.getcolors/colors-compute compute))})
nil)))))
;; Nothing is required at the top level, deliberately. A bootstrap that resolved
;; nothing leaves green.cli off the classpath too, and a bare `require` here
;; would abort with "could not locate green/cli.clj" — burying the message that
;; actually says what to do. Load defensively instead and let contract-error
;; report.
(def ^:private load-error
(or bootstrap-error
(try
(require 'green.cli 'io.github.getcolors.doks.workflow)
nil
(catch Throwable t (or (ex-message t) (str (class t)))))))
(defn- doks-contract
[]
(or (some-> (requiring-resolve 'io.github.getcolors.doks.utils/contract) deref)
0))
(defn- contract-error
"An outcome map when the resolved `doks` predates this launcher."
[]
(let [resolved (if load-error 0 (doks-contract))]
(when (< resolved launcher-contract)
{:green/exit 2
:green/err (str "this launcher requires doks contract " launcher-contract
" but resolved " resolved "\n"
(if doks-sha
;; `take` rather than `subs`: doks-sha is nil until
;; `bb pin` stamps it, and a launcher that threw while
;; explaining why it could not start would be worse
;; than useless.
(str "the pinned commit " (apply str (take 7 doks-sha))
" predates this launcher; update the skill or repin")
"no doks library could be resolved")
(when load-error (str "\n" load-error)))})))
;; ---------------------------------------------------------------------------
;; Desired-state file resolution
(defn- find-up
"The nearest `name` at or above `dir`, or nil at the filesystem root."
[name dir]
(loop [dir (.getAbsoluteFile (io/file dir))]
(when dir
(let [candidate (io/file dir name)]
(if (.exists candidate)
(str candidate)
(recur (.getParentFile dir)))))))
(defn- default-file
"The nearest colors.yml at or above the working directory, falling back to one
beside this launcher. Walking up means green can be run from any subdirectory
of a project and still find the one desired state."
[]
(or (find-up "colors.yml" ".")
(let [beside-script (io/file script-dir "colors.yml")]
(when (.exists beside-script) (str beside-script)))
"colors.yml"))
(defn- file-arg?
[arg]
(or (= "-f" arg)
(= "--file" arg)
(str/starts-with? (str arg) "--file=")))
(defn default-args
[args]
(let [args (vec args)]
(cond-> args
(not-any? file-arg? args) (conj "-f" (default-file)))))
;; ---------------------------------------------------------------------------
;; CLI
(def commands #{"build" "create" "check" "kubeconfig" "registry" "delete"})
(def usage
(str "Usage: green <build|create|check|kubeconfig|registry|delete> "
"[-f|--file colors.yml] [--dry-run]\n"
"\n"
" build render the work directory only — contact nothing\n"
" create converge the managed cluster and, if configured, the registry\n"
" check verify every node is Ready and the registry is integrated\n"
" kubeconfig re-materialize the private kubeconfig from state\n"
" registry write a short-lived docker push config for the registry\n"
" delete guarded: unlink the registry, destroy the cluster, then the registry"))
(defn run
"REPL-friendly entry point that returns the final outcome map."
[& args]
(let [args (vec args)
command (first args)]
(cond
(contains? #{"help" "--help" "-h"} command)
{:green/exit 0 :green/err usage}
(contains? commands command)
;; Resolved rather than aliased: neither namespace is known to have loaded
;; until contract-error has passed.
(or (contract-error)
((requiring-resolve 'green.cli/run-cli)
@(requiring-resolve 'io.github.getcolors.doks.workflow/workflow)
(default-args args)
@(requiring-resolve 'io.github.getcolors.doks.workflow/cli-options)))
:else {:green/exit 2 :green/err usage})))
(defn -main
[& args]
(let [{:green/keys [exit err trace]} (apply run args)]
(when err
(binding [*out* (if (zero? (or exit 0)) *out* *err*)]
(println err)
(when trace (println trace))))
(System/exit (or exit 0))))
(defn- launched-as-script?
[]
(when-let [bb-file (System/getProperty "babashka.file")]
(= (str (fs/canonicalize bb-file))
(str (fs/canonicalize *file*)))))
(when (launched-as-script?)
(apply -main *command-line-args*))