-
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathstack-operations.ts
More file actions
358 lines (326 loc) · 13.8 KB
/
stack-operations.ts
File metadata and controls
358 lines (326 loc) · 13.8 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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
"use strict";
// Enables the ability to launch a remote DevOps stack,
// attach the VNICs, assign the Public IP's to the VNIC's,
// or run terminate and destroy operations.
import { common } from "oci-sdk";
import { core } from "oci-sdk";
import { resourcemanager as rm } from "oci-sdk";
import { workrequests as wr } from "oci-sdk";
import { devOpsConfig as dev } from "./devops-config";
const provider: common.ConfigFileAuthenticationDetailsProvider =
new common.ConfigFileAuthenticationDetailsProvider();
const computeClient = new core.ComputeClient({
authenticationDetailsProvider: provider,
});
const networkClient = new core.VirtualNetworkClient({
authenticationDetailsProvider: provider,
});
const jobClient = new rm.ResourceManagerClient({
authenticationDetailsProvider: provider,
});
const workRequestClient = new wr.WorkRequestClient({
authenticationDetailsProvider: provider,
});
const maxTimeInSeconds = 60 * 60;
const maxDelayInSeconds = 30;
const waiterConfiguration: common.WaiterConfiguration = {
terminationStrategy: new common.MaxTimeTerminationStrategy(maxTimeInSeconds),
delayStrategy: new common.ExponentialBackoffDelayStrategy(maxDelayInSeconds),
};
const computeWaiter = computeClient.createWaiters(
workRequestClient,
waiterConfiguration
);
const primaryPrivateIp = `${dev.ociPrivateIp[0]}`;
const secondaryPrivateIp = `${dev.ociPrivateIp[1]}`;
const tertiaryPrivateIp = `${dev.ociPrivateIp[2]}`;
const quaternaryPrivateIp = `${dev.ociPrivateIp[3]}`;
const primarySubnetId = `${dev.ociSubnetId[0]}`;
const secondarySubnetId = `${dev.ociSubnetId[1]}`;
const tertiarySubnetId = `${dev.ociSubnetId[2]}`;
const quaternarySubnetId = `${dev.ociSubnetId[3]}`;
export const stackOperations = {
sleep: async function sleep({ interval }: { interval: number }): Promise<any> {
return new Promise((resolve) => setTimeout(resolve, interval));
},
croak: function croak({ warning, stage }: { warning: string, stage: string }): string {
console.warn(`{ status: "WARNING", message: { condition: "${warning}", stage: "${stage}" } }`);
return `{ status: "WARNING", message: { condition: "${warning}", stage: "${stage}" } }`;
},
choke: function choke({ error, stage }: { error: string, stage: string }): string {
console.error(`{ status: "FAILED", message: { error: "${error}", stage: "${stage}" } }`);
return `{ status: "FAILED", message: { error: "${error}", stage: "${stage}" } }`;
},
createDevopsServer: async function createDevopsServer(): Promise<string> {
// Runs "APPLY" on the DevOps stack and returns the status.
// In this case the stack creates a "preemptible" capacity
// instance from a Custom Image source that has already been
// configured to pull everything it needs from a private
// container registry for a stable, clean - slated build
// environment.
try {
const createJobDetails = {
stackId: `${dev.ociStackId}`,
displayName: `${dev.ociStackName}`,
operation: rm.models.Job.Operation.Apply,
jobOperationDetails: {
operation: "APPLY",
executionPlanStrategy:
rm.models.ApplyJobOperationDetails.ExecutionPlanStrategy.AutoApproved,
},
};
const createJobRequest: rm.requests.CreateJobRequest = {
createJobDetails: createJobDetails,
};
const createJobResponse = await jobClient.createJob(createJobRequest);
const jobStatus = `${createJobResponse.job.lifecycleState}`;
return (jobStatus
? `${jobStatus}`
: this.choke({ error: `${jobStatus}`, stage: "createJobRequest" }));
} catch (error) {
return this.choke({ error: `${error}`, stage: "createJobRequest" });
}
},
getInstanceId: async function getInstanceId(): Promise<string> {
try {
const listInstancesRequest: core.requests.ListInstancesRequest = {
compartmentId: `${dev.compartmentId}`,
availabilityDomain: `${dev.availabilityDomain}`,
displayName: `${dev.displayName}`,
lifecycleState: core.models.Instance.LifecycleState.Running,
};
const listInstancesResponse = await computeClient.listInstances(
listInstancesRequest
);
const instanceId = `${listInstancesResponse.items[0].id}`;
return (instanceId
? `${instanceId}`
: this.choke({ error: `${instanceId}`, stage: "listInstancesRequest" }));
} catch (error) {
return this.choke({ error: `${error}`, stage: "listInstancesRequest" });
}
},
assignPublicIp: `${dev.ociPublicIpId}`,
attachPublicIp: async function attachPublicIp(): Promise<string> {
try {
const listPrivateIpsRequest: core.requests.ListPrivateIpsRequest = {
subnetId: `${primarySubnetId}`,
ipAddress: `${primaryPrivateIp}`,
};
const listPrivateIpsResponse = await networkClient.listPrivateIps(
listPrivateIpsRequest
);
const updatePublicIpDetails = {
displayName: `${dev.ociPublicIpDisplayName}`,
freeformTags: {
ociPublicServiceName: `${dev.ociPublicServiceName}`,
},
privateIpId: `${listPrivateIpsResponse.items[0].id}`,
};
const updatePublicIpRequest: core.requests.UpdatePublicIpRequest = {
publicIpId: `${dev.ociPublicIpId}`,
updatePublicIpDetails: updatePublicIpDetails,
};
const updatePublicIpResponse = await networkClient.updatePublicIp(
updatePublicIpRequest
);
const publicIpStatus = `${updatePublicIpResponse.publicIp.lifecycleState}`;
return (publicIpStatus
? `${publicIpStatus}`
: this.croak({ warning: `${publicIpStatus}`, stage: "updatePublicIpRequest" }));
} catch (error) {
return this.croak({ warning: `${error}`, stage: "updatePublicIpRequest" });
}
},
attachVnic: async function attachVnic({ ocid }: { ocid: string }): Promise<string> {
try {
// The Primary VNIC is allocated by the stack
// itself so we only need to worry about the
// other ones which varies depending on the
// shape of the instance.
// The Oracle Cloud Agent service should be enabled
// on the instance before running the stack to
// automatically configure the VNIC's once they attach.
// The Management Agent and OS Management Service Agent
// are also required for this to work. They will need
// to be enabled through the cloud portal during
// instance creation and in the stack's terraform file.
// Step One:
// is_management_disabled = "false"
// plugins_config {
// desired_state = "ENABLED"
// name = "OS Management Service Agent"
// }
// plugins_config {
// desired_state = "ENABLED"
// name = "Management Agent"
// }
// Step Two:
// systemctl enable --now ocid.service
// systemctl enable --now oracle-cloud-agent.service
// systemctl enable --now oracle-cloud-agent-updater.service
// Step Three:
// systemctl reboot
// Step Four:
// ip addr (verify if VNIC was configured).
// Note that if making a Custom Image it's
// recommended to manually shut down the
// instance first with systemctl poweroff
// to prevent systemd from desynchronizing
// with the Cloud Agent scripts.
const attachVnic2Details = {
createVnicDetails: {
assignPublicIp: true,
assignPrivateDnsRecord: true,
displayName: `${dev.secondaryVnicDisplayName}`,
hostnameLabel: `${dev.secondaryVnicHostnameLabel}`,
privateIp: `${secondaryPrivateIp}`,
skipSourceDestCheck: false,
subnetId: `${secondarySubnetId}`,
},
displayName: `${dev.secondaryVnicDisplayName}`,
instanceId: `${ocid}`,
};
const attachVnic2Request: core.requests.AttachVnicRequest = {
attachVnicDetails: attachVnic2Details,
};
let attachVnic2Response = await computeClient.attachVnic(
attachVnic2Request
);
let vnic2Status = `${attachVnic2Response.vnicAttachment.lifecycleState}`;
if (tertiaryPrivateIp) {
const attachVnic3Details = {
createVnicDetails: {
assignPublicIp: true,
assignPrivateDnsRecord: true,
displayName: `${dev.tertiaryVnicDisplayName}`,
hostnameLabel: `${dev.tertiaryVnicHostnameLabel}`,
privateIp: `${tertiaryPrivateIp}`,
skipSourceDestCheck: false,
subnetId: `${tertiarySubnetId}`,
},
displayName: `${dev.tertiaryVnicDisplayName}`,
instanceId: `${ocid}`,
};
const attachVnic3Request: core.requests.AttachVnicRequest = {
attachVnicDetails: attachVnic3Details,
};
// Give the previous instance modification
// some time to finish before attaching
// the next VNIC otherwise it will fail with
// "instance is currently being modified, try
// again later".
await this.sleep({ interval: 20000 });
let attachVnic3Response = await computeClient.attachVnic(
attachVnic3Request
);
let vnic3Status = `${attachVnic3Response.vnicAttachment.lifecycleState}`;
if (!quaternaryPrivateIp) {
return (vnic2Status === "ATTACHED" || vnic2Status === "ATTACHING" && vnic3Status === "ATTACHED" || vnic3Status === "ATTACHING" ? "ATTACHED" : this.croak({
warning: `[ "${vnic2Status}", "${vnic3Status}" ],`, stage: "attachVnicRequest"
}));
} else {
// Oracle seems to have some kind of obscure
// limitation on the number of VNIC's that can
// be attached to an instance. When attempting
// to attach more than 3 VNIC's, it quietly
// removes them contrary to what is described
// in the service limits about OCPU's and VNIC's.
const attachVnic4Details = {
createVnicDetails: {
assignPublicIp: true,
assignPrivateDnsRecord: true,
displayName: `${dev.quaternaryVnicDisplayName}`,
hostnameLabel: `${dev.quaternaryVnicHostnameLabel}`,
privateIp: `${quaternaryPrivateIp}`,
skipSourceDestCheck: false,
subnetId: `${quaternarySubnetId}`,
},
displayName: `${dev.quaternaryVnicDisplayName}`,
instanceId: `${ocid}`,
};
const attachVnic4Request: core.requests.AttachVnicRequest = {
attachVnicDetails: attachVnic4Details,
};
await this.sleep({ interval: 20000 });
let attachVnic4Response = await computeClient.attachVnic(
attachVnic4Request
);
let vnic4Status = `${attachVnic4Response.vnicAttachment.lifecycleState}`;
return (vnic2Status === "ATTACHED" || vnic2Status === "ATTACHING" && vnic3Status === "ATTACHED" || vnic3Status === "ATTACHING" && vnic4Status === "ATTACHED" || vnic4Status === "ATTACHING" ? "ATTACHED" : this.croak({
warning: `[ "${vnic2Status}", "${vnic3Status}", "${vnic4Status}" ],`, stage: "attachVnicRequest"
}));
}
}
return (vnic2Status === "ATTACHED" || vnic2Status === "ATTACHING" ? "ATTACHED" : this.croak({
warning: `[ "${vnic2Status}" ],`, stage: "attachVnicRequest"
}));
} catch (error) {
return this.croak({ warning: `${error}`, stage: "attachVnicRequest" });
}
},
terminateDevOpsServer: async function terminateDevOpsServer({
ocid,
}: {
ocid: string;
}): Promise<string> {
// For situations where you want to terminate
// the instance but retain the boot volume.
// Careful enabling this. You will need to
// keep an eye on Block Storage to ensure
// excessive volumes are not created if this
// is run instead of the "DESTROY" operations.
// This is intended more for stacks that
// target an UHP persistent volume boot source
// that do not need to generate a new boot
// volume each time whenever run.
try {
const terminateInstanceRequest: core.requests.TerminateInstanceRequest = {
instanceId: `${ocid}`,
preserveBootVolume: true,
};
await computeClient.terminateInstance(terminateInstanceRequest);
const getInstanceRequest: core.requests.GetInstanceRequest = {
instanceId: `${ocid}`,
};
await computeWaiter.forInstance(
getInstanceRequest,
core.models.Instance.LifecycleState.Terminated
);
const getInstanceResponse = await computeClient.getInstance(
getInstanceRequest
);
const instanceStatus = `${getInstanceResponse.instance.lifecycleState}`;
return (instanceStatus
? `${instanceStatus}`
: this.croak({ warning: `${instanceStatus}`, stage: "terminateInstanceRequest" }));
} catch (error) {
return this.croak({ warning: `${error}`, stage: "terminateInstanceRequest" });
}
},
destroyDevOpsServer: async function destroyDevOpsServer(): Promise<string> {
// Runs "DESTROY" on the DevOps stack and returns the status.
try {
const destroyJobDetails = {
stackId: `${dev.ociStackId}`,
displayName: `${dev.ociStackName}`,
operation: rm.models.Job.Operation.Destroy,
jobOperationDetails: {
operation: "DESTROY",
executionPlanStrategy:
rm.models.DestroyJobOperationDetails.ExecutionPlanStrategy.AutoApproved,
},
};
const destroyJobRequest: rm.requests.CreateJobRequest = {
createJobDetails: destroyJobDetails,
};
const createJobResponse = await jobClient.createJob(destroyJobRequest);
const jobStatus = `${createJobResponse.job.lifecycleState}`;
return (jobStatus
? `${jobStatus}`
: this.choke({ error: `${jobStatus}`, stage: "destroyJobRequest" }));
} catch (error) {
return this.choke({ error: `${error}`, stage: "destroyJobRequest" });
}
}
}