1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 | import { GitProject } from "@atomist/automation-client";
|
18 | import {
|
19 | DefaultGoalNameGenerator,
|
20 | FulfillableGoal,
|
21 | FulfillableGoalDetails,
|
22 | FulfillableGoalWithRegistrations,
|
23 | Fulfillment,
|
24 | getGoalDefinitionFrom,
|
25 | Goal,
|
26 | GoalFulfillmentCallback,
|
27 | ImplementationRegistration,
|
28 | RepoContext,
|
29 | SdmGoalEvent,
|
30 | SoftwareDeliveryMachine,
|
31 | testProgressReporter,
|
32 | } from "@atomist/sdm";
|
33 | import {
|
34 | KubernetesFulfillmentGoalScheduler,
|
35 | KubernetesFulfillmentOptions,
|
36 | } from "../../pack/k8s/KubernetesFulfillmentGoalScheduler";
|
37 | import {
|
38 | isConfiguredInEnv,
|
39 | KubernetesGoalScheduler,
|
40 | } from "../../pack/k8s/KubernetesGoalScheduler";
|
41 | import { KubernetesJobDeletingGoalCompletionListenerFactory } from "../../pack/k8s/KubernetesJobDeletingGoalCompletionListener";
|
42 | import { toArray } from "../../util/misc/array";
|
43 | import {
|
44 | CacheEntry,
|
45 | cachePut,
|
46 | cacheRestore,
|
47 | } from "../cache/goalCaching";
|
48 | import { dockerContainerScheduler } from "./docker";
|
49 | import { k8sContainerScheduler } from "./k8s";
|
50 | import {
|
51 | runningAsGoogleCloudFunction,
|
52 | runningInK8s,
|
53 | } from "./util";
|
54 |
|
55 | export const ContainerRegistrationGoalDataKey = "@atomist/sdm/container";
|
56 |
|
57 |
|
58 |
|
59 |
|
60 |
|
61 |
|
62 |
|
63 |
|
64 |
|
65 | export function container<T extends ContainerRegistration>(displayName: string, registration: T): FulfillableGoal {
|
66 | return new Container({ displayName }).with(registration);
|
67 | }
|
68 |
|
69 | export const ContainerProgressReporter = testProgressReporter({
|
70 | test: /docker 'network' 'create'/i,
|
71 | phase: "starting up",
|
72 | }, {
|
73 | test: /docker 'network' 'rm'/i,
|
74 | phase: "shutting down",
|
75 | }, {
|
76 | test: /docker 'run' .* '--workdir=[a-zA-Z\/]*' .* '--network-alias=([a-zA-Z \-_]*)'/i,
|
77 | phase: "running $1",
|
78 | }, {
|
79 | test: /atm:phase=(.*)/i,
|
80 | phase: "$1",
|
81 | });
|
82 |
|
83 |
|
84 |
|
85 |
|
86 | export interface ContainerPort {
|
87 | |
88 |
|
89 |
|
90 |
|
91 | containerPort: number;
|
92 | }
|
93 |
|
94 |
|
95 |
|
96 |
|
97 | export interface ContainerVolumeMount {
|
98 |
|
99 | mountPath: string;
|
100 |
|
101 | name: string;
|
102 | }
|
103 |
|
104 | export interface ContainerSecrets {
|
105 | env?: Array<{ name: string } & GoalContainerSecret>;
|
106 | fileMounts?: Array<{ mountPath: string } & GoalContainerSecret>;
|
107 | }
|
108 |
|
109 |
|
110 |
|
111 |
|
112 | export interface GoalContainer {
|
113 |
|
114 | name: string;
|
115 |
|
116 | image: string;
|
117 | |
118 |
|
119 |
|
120 |
|
121 | args?: string[];
|
122 | |
123 |
|
124 |
|
125 |
|
126 | command?: string[];
|
127 | |
128 |
|
129 |
|
130 | env?: Array<{ name: string, value: string }>;
|
131 | |
132 |
|
133 |
|
134 | ports?: ContainerPort[];
|
135 | |
136 |
|
137 |
|
138 | volumeMounts?: ContainerVolumeMount[];
|
139 | |
140 |
|
141 |
|
142 | secrets?: ContainerSecrets;
|
143 | }
|
144 |
|
145 | export interface GoalContainerProviderSecret {
|
146 | provider: {
|
147 | type: "docker" | "npm" | "maven2" | "scm" | "atomist";
|
148 | names?: string[];
|
149 | };
|
150 | }
|
151 |
|
152 | export interface GoalContainerEncryptedSecret {
|
153 | encrypted: string;
|
154 | }
|
155 |
|
156 | export interface GoalContainerSecret {
|
157 | value: GoalContainerProviderSecret | GoalContainerEncryptedSecret;
|
158 | }
|
159 |
|
160 |
|
161 |
|
162 |
|
163 | export interface GoalContainerVolume {
|
164 |
|
165 | hostPath: {
|
166 |
|
167 | path: string;
|
168 | };
|
169 |
|
170 | name: string;
|
171 | }
|
172 |
|
173 |
|
174 |
|
175 |
|
176 | export const ContainerProjectHome = "/atm/home";
|
177 |
|
178 |
|
179 |
|
180 |
|
181 | export const ContainerInput = "/atm/input";
|
182 |
|
183 |
|
184 |
|
185 |
|
186 | export const ContainerOutput = "/atm/output";
|
187 |
|
188 |
|
189 |
|
190 |
|
191 | export const ContainerResult = `${ContainerOutput}/result.json`;
|
192 |
|
193 |
|
194 |
|
195 |
|
196 | export interface GoalContainerSpec {
|
197 | |
198 |
|
199 |
|
200 |
|
201 |
|
202 |
|
203 |
|
204 |
|
205 |
|
206 | containers: GoalContainer[];
|
207 | |
208 |
|
209 |
|
210 | volumes?: GoalContainerVolume[];
|
211 | }
|
212 |
|
213 |
|
214 |
|
215 |
|
216 |
|
217 | export type ContainerSpecCallback =
|
218 | (r: ContainerRegistration, p: GitProject, g: Container, e: SdmGoalEvent, c: RepoContext) => Promise<GoalContainerSpec>;
|
219 |
|
220 |
|
221 |
|
222 |
|
223 | export interface ContainerRegistration extends Partial<ImplementationRegistration>, GoalContainerSpec {
|
224 | |
225 |
|
226 |
|
227 |
|
228 | callback?: ContainerSpecCallback;
|
229 | |
230 |
|
231 |
|
232 |
|
233 |
|
234 | input?: Array<{ classifier: string }>;
|
235 | |
236 |
|
237 |
|
238 |
|
239 |
|
240 | output?: CacheEntry[];
|
241 | }
|
242 |
|
243 |
|
244 |
|
245 |
|
246 |
|
247 | export type ContainerScheduler = (goal: Container, registration: ContainerRegistration) => void;
|
248 |
|
249 | export interface ContainerGoalDetails extends FulfillableGoalDetails {
|
250 | |
251 |
|
252 |
|
253 |
|
254 |
|
255 | scheduler?: ContainerScheduler;
|
256 | }
|
257 |
|
258 |
|
259 |
|
260 |
|
261 | export class Container extends FulfillableGoalWithRegistrations<ContainerRegistration> {
|
262 |
|
263 | public readonly details: ContainerGoalDetails;
|
264 |
|
265 | constructor(details: ContainerGoalDetails = {}, ...dependsOn: Goal[]) {
|
266 | const prefix = "container" + (details.displayName ? `-${details.displayName}` : "");
|
267 | const detailsToUse = { ...details, isolate: true };
|
268 | super(getGoalDefinitionFrom(detailsToUse, DefaultGoalNameGenerator.generateName(prefix)), ...dependsOn);
|
269 | this.details = detailsToUse;
|
270 | }
|
271 |
|
272 | public register(sdm: SoftwareDeliveryMachine): void {
|
273 | super.register(sdm);
|
274 |
|
275 | const goalSchedulers = toArray(sdm.configuration.sdm.goalScheduler) || [];
|
276 | if (runningInK8s()) {
|
277 |
|
278 | if (!goalSchedulers.some(gs => gs instanceof KubernetesGoalScheduler)) {
|
279 | if (!process.env.ATOMIST_ISOLATED_GOAL && isConfiguredInEnv("kubernetes", "kubernetes-all")) {
|
280 | sdm.configuration.sdm.goalScheduler = [...goalSchedulers, new KubernetesGoalScheduler()];
|
281 | sdm.addGoalCompletionListener(new KubernetesJobDeletingGoalCompletionListenerFactory(sdm).create());
|
282 | }
|
283 | }
|
284 | } else if (runningAsGoogleCloudFunction()) {
|
285 | const options: KubernetesFulfillmentOptions = sdm.configuration.sdm?.k8s?.fulfillment;
|
286 | if (!goalSchedulers.some(gs => gs instanceof KubernetesFulfillmentGoalScheduler)) {
|
287 | sdm.configuration.sdm.goalScheduler = [
|
288 | ...goalSchedulers,
|
289 | new KubernetesFulfillmentGoalScheduler(options),
|
290 | ];
|
291 | }
|
292 | }
|
293 | }
|
294 |
|
295 | public with(registration: ContainerRegistration): this {
|
296 | super.with(registration);
|
297 |
|
298 | registration.name = (registration.name || `container-${this.definition.displayName}`).replace(/\.+/g, "-");
|
299 | if (!this.details.scheduler) {
|
300 | if (runningInK8s() || runningAsGoogleCloudFunction()) {
|
301 | this.details.scheduler = k8sContainerScheduler;
|
302 | } else {
|
303 | this.details.scheduler = dockerContainerScheduler;
|
304 | }
|
305 | }
|
306 | this.details.scheduler(this, registration);
|
307 | if (registration.input && registration.input.length > 0) {
|
308 | this.withProjectListener(cacheRestore({ entries: registration.input }));
|
309 | }
|
310 | if (registration.output && registration.output.length > 0) {
|
311 | this.withProjectListener(cachePut({ entries: registration.output }));
|
312 | }
|
313 | return this;
|
314 | }
|
315 |
|
316 | public addFulfillment(fulfillment: Fulfillment): this {
|
317 | return super.addFulfillment(fulfillment);
|
318 | }
|
319 |
|
320 | public addFulfillmentCallback(cb: GoalFulfillmentCallback): this {
|
321 | return super.addFulfillmentCallback(cb);
|
322 | }
|
323 | }
|