-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
549 lines (480 loc) · 19.7 KB
/
index.js
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
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
import * as core from "@actions/core";
import * as exec from "@actions/exec";
import fs from "fs";
async function checkIfEndpointExists(
endpointName, resourceGroup, workspaceName
) {
/**
* Check if the endpoint exists in the specified resource group and workspace.
* @param {string} endpointName - The name of the endpoint.
* @param {string} resourceGroup - The name of the resource group.
* @param {string} workspaceName - The name of the workspace.
* @return {boolean} - Returns true if the endpoint exists, false otherwise.
*/
let errorOutput = "";
let output = "";
try {
const options = {
listeners: {
stdout: (data) => {
output += data.toString();
},
stderr: (data) => {
errorOutput += data.toString();
}
},
silent: true
};
// Check if the endpoint exists
await exec.exec(`az ml online-endpoint show --name ${endpointName} --resource-group ${resourceGroup} --workspace-name ${workspaceName}`, [], options);
console.log("✅ Endpoint already exists. Output:", output);
return true; // If the command succeeds, the endpoint exists
} catch (error) {
return false; // If the command fails, the endpoint does not exist
}
}
async function checkIfResourceGroupExists(resourceGroup) {
/**
* Check if the resource group exists.
* @param {string} resourceGroup - The name of the resource group.
* @return {boolean} - Returns true if the resource group exists, false otherwise.
*/
let errorOutput = "";
let output = "";
try {
const options = {
listeners: {
stdout: (data) => {
output += data.toString();
},
stderr: (data) => {
errorOutput += data.toString();
}
},
silent: true
};
// Execute the Azure CLI command
await exec.exec(`az group show --name ${resourceGroup} --resource-group ${resourceGroup}`, [], options);
console.log("✅ Resource Group Found. Output:", output);
return true;
} catch (error) {
console.log(
"❌ Resource Group Not Found or Error Occurred:", errorOutput || error.message
);
return false; // Return false if the workspace does not exist
}
}
async function checkIfWorkspaceExists(workspaceName, resourceGroup) {
/**
* Check if the workspace exists in the specified resource group.
* @param {string} workspaceName - The name of the workspace.
* @param {string} resourceGroup - The name of the resource group.
* @return {boolean} - Returns true if the workspace exists, false otherwise.
*/
let errorOutput = "";
let output = "";
try {
const options = {
listeners: {
stdout: (data) => {
output += data.toString();
},
stderr: (data) => {
errorOutput += data.toString();
}
},
silent: true
};
// Check if the workspace exists
await exec.exec(`az ml workspace show --name ${workspaceName} --resource-group ${resourceGroup}`, [], options);
console.log("✅ Resource Group Found. Output:", output);
return true;
} catch (error) {
console.log(
"❌ Resource Group Not Found or Error Occurred:", errorOutput || error.message
);
return false;
}
}
async function checkIfRegistryExists(registryName, resourceGroup) {
/**
* Check if the registry exists in the specified resource group.
* @param {string} registryName - The name of the registry.
* @param {string} resourceGroup - The name of the resource group.
* @return {boolean} - Returns true if the registry exists, false otherwise.
*/
let errorOutput = "";
let output = "";
try {
const options = {
listeners: {
stdout: (data) => {
output += data.toString();
},
stderr: (data) => {
errorOutput += data.toString();
}
},
silent: true
};
// Check if the workspace exists
await exec.exec(`az ml registry show --name ${registryName} --resource-group ${resourceGroup}`, [], options);
console.log("✅ Registry Found. Output:", output);
return true;
} catch (error) {
console.log(
"❌ Registry Not Found or Error Occurred:", errorOutput || error.message
);
return false;
}
}
async function checkIfModelInRegistryExists(
modelName, modelVersion, registryName, resourceGroup
) {
/**
* Check if the model exists in the specified registry.
* @param {string} modelName - The name of the model.
* @param {string} modelVersion - The version of the model.
* @param {string} registryName - The name of the registry.
* @param {string} resourceGroup - The name of the resource group.
* @return {boolean} - Returns true if the model exists, false otherwise.
*/
let errorOutput = "";
let output = "";
try {
const options = {
listeners: {
stdout: (data) => {
output += data.toString();
},
stderr: (data) => {
errorOutput += data.toString();
}
},
silent: true
};
// Check if model exists in registry
await exec.exec(`az ml model show --name ${modelName} --version ${modelVersion} --registry-name ${registryName} --resource-group ${resourceGroup}`, [], options);
console.log("✅ Model Found. Output:", output);
return true;
} catch (error) {
console.log(
"❌ Model Not Found or Error Occurred:", errorOutput || error.message
);
return false;
}
}
async function checkIfModelInWorkspaceExists(
modelName, modelVersion, workspaceName, resourceGroup
) {
/**
* Check if the model exists in the specified workspace.
* @param {string} modelName - The name of the model.
* @param {string} modelVersion - The version of the model.
* @param {string} workspaceName - The name of the workspace.
* @param {string} resourceGroup - The name of the resource group.
* @return {boolean} - Returns true if the model exists, false otherwise.
*/
let errorOutput = "";
let output = "";
try {
const options = {
listeners: {
stdout: (data) => {
output += data.toString();
},
stderr: (data) => {
errorOutput += data.toString();
}
},
silent: true
};
// Check if model exists in registry
await exec.exec(`az ml model show --name ${modelName} --version ${modelVersion} --workspace-name ${workspaceName} --resource-group ${resourceGroup}`, [], options);
console.log("✅ Model Found. Output:", output);
return true;
} catch (error) {
console.log(
"❌ Model Not Found or Error Occurred:", errorOutput || error.message
);
return false;
}
}
function checkIfFileExists(filePath) {
/**
* Check if the specified file exists.
* @param {string} filePath - The path to the file.
* @return {Promise<boolean>} - Returns a promise that resolves to true if the file exists, false otherwise.
*/
return new Promise((resolve, reject) => {
fs.access(filePath,
fs.constants.F_OK,
(err) => {
if (err) {
console.error(`❌ File does not exist: ${filePath}`);
resolve(false);
} else {
console.log(`✅ File exists: ${filePath}`);
resolve(true);
}
}
);
}
);
}
async function createDeployment(
resourceGroup, workspaceName, deploymentYamlFilePath
) {
/**
* Create a deployment using the specified YAML file.
* @param {string} resourceGroup - The name of the resource group.
* @param {string} workspaceName - The name of the workspace.
* @param {string} deploymentYamlFilePath - The path to the deployment YAML file.
* @return {Promise<boolean>} - Returns a promise that resolves to true if the deployment is created successfully, false otherwise.
*/
let errorOutput = "";
let output = "";
try {
const options = {
listeners: {
stdout: (data) => {
output += data.toString();
},
stderr: (data) => {
errorOutput += data.toString();
}
},
silent: true
};
// Create the deployment
await exec.exec(`az ml online-deployment create --file ${deploymentYamlFilePath} --resource-group ${resourceGroup} --workspace-name ${workspaceName}`, [], options);
console.log("✅ Deployment created successfully. Output:", output);
return true;
} catch (error) {
console.log("❌ Deployment creation failed. Error:", errorOutput || error.message);
return false;
}
}
async function updateDeploymentTraffic(
resourceGroup, workspaceName, endpointName, deploymentName, traffic) {
/**
* Update the traffic for the specified deployment.
* @param {string} resourceGroup - The name of the resource group.
* @param {string} workspaceName - The name of the workspace.
* @param {string} endpointName - The name of the endpoint.
* @param {string} deploymentName - The name of the deployment.
* @param {string} traffic - The traffic configuration in JSON format.
* @return {Promise<boolean>} - Returns a promise that resolves to true if the traffic is updated successfully, false otherwise.
*/
let errorOutput = "";
let output = "";
try {
const options = {
listeners: {
stdout: (data) => {
output += data.toString();
},
stderr: (data) => {
errorOutput += data.toString();
}
},
silent: true
};
// Update the traffic
await exec.exec(`az ml online-endpoint update --name ${endpointName} --resource-group ${resourceGroup} --workspace-name ${workspaceName} --traffic "${deploymentName}"="${traffic}"`, [], options);
console.log("✅ Traffic updated successfully. Output:", output);
return true;
} catch (error) {
console.log("❌ Traffic update failed. Error:", errorOutput || error.message);
return false;
}
}
async function updateDeploymentMirrorTraffic(
resourceGroup, workspaceName, endpointName, deploymentName, traffic
) {
/**
* Update the mirror traffic for the specified deployment.
* @param {string} resourceGroup - The name of the resource group.
* @param {string} workspaceName - The name of the workspace.
* @param {string} endpointName - The name of the endpoint.
* @param {string} deploymentName - The name of the deployment.
* @param {string} traffic - The mirror traffic configuration in JSON format.
* @return {Promise<boolean>} - Returns a promise that resolves to true if the mirror traffic is updated successfully, false otherwise.
*/
let errorOutput = "";
let output = "";
try {
const options = {
listeners: {
stdout: (data) => {
output += data.toString();
},
stderr: (data) => {
errorOutput += data.toString();
}
},
silent: true
};
// Update the mirror traffic
await exec.exec(`az ml online-endpoint update --name ${endpointName} --resource-group ${resourceGroup} --workspace-name ${workspaceName} --mirror-traffic "${deploymentName}"="${traffic}"`, [], options);
console.log("✅ Mirror Traffic updated successfully. Output:", output);
return true;
} catch (error) {
console.log("❌ Mirror Traffic update failed. Error:", errorOutput || error.message);
return false;
}
}
try {
const endpointName = core.getInput("endpoint_name");
const resourceGroup = core.getInput("resource_group");
const workspaceName = core.getInput("workspace_name");
const registryName = core.getInput("registry_name");
const registryResourceGroup = core.getInput("registry_resource_group");
const modelName = core.getInput("model_name");
const modelVersion = core.getInput("model_version");
const traffic = core.getInput("traffic");
const deploymentYamlFilePath = core.getInput("deployment_yaml_file_path");
// Check if the required inputs are provided
if (!endpointName || endpointName === "") {
throw new Error("Endpoint name is required.");
}
if (!resourceGroup || resourceGroup === "") {
throw new Error("Resource group is required");
}
if (!workspaceName || workspaceName === "") {
throw new Error("Workspace name is required");
}
if (!modelName || modelName === "") {
throw new Error("Model name is required");
}
if (!modelVersion || modelVersion === "") {
throw new Error("Model version is required");
}
if (!traffic || traffic === "") {
throw new Error("Traffic is required");
}
// Check if deployment YAML file exists
if (!deploymentYamlFilePath || deploymentYamlFilePath === "") {
throw new Error("Deployment YAML file path is required.");
}
// Check if the resource group exists
console.log(`🔹 Checking if resource group '${resourceGroup}' exists...`)
;
const resourceGroupExists = await checkIfResourceGroupExists(resourceGroup);
if (!resourceGroupExists) {
throw new Error(`Resource group '${resourceGroup}' does not exist.`);
} else {
console.log(`✅ Resource group '${resourceGroup}' exists.`);
}
// Check if the workspace exists
console.log(`🔹 Checking if workspace '${workspaceName}' exists in resource group '${resourceGroup}'...`)
;
const workspaceExists = await checkIfWorkspaceExists(workspaceName, resourceGroup);
if (!workspaceExists) {
throw new Error(`Workspace '${workspaceName}' does not exist in resource group '${resourceGroup}'.`);
} else {
console.log(`✅ Workspace '${workspaceName}' exists in resource group '${resourceGroup}'.`);
}
// Check if endpoint exists
console.log(`🔹 Checking if endpoint '${endpointName}' exists...`);
const endpointExits = await checkIfEndpointExists(
endpointName, resourceGroup, workspaceName
);
if (!endpointExits) {
throw new Error(`Endpoint '${endpointName}' does not exist in resource group '${resourceGroup}' and workspace '${workspaceName}'.`);
} else {
console.log(`✅ Endpoint '${endpointName}' exists in resource group '${resourceGroup}' and workspace '${workspaceName}''${resourceGroup}'.`);
}
// Check if registry name is provided
if (registryName !== undefined && registryName !== "") {
// Check if the registry exists
console.log(`🔹 Checking if registry '${registryName}' exists in resource group '${registryResourceGroup}'...`);
const registryExists = await checkIfRegistryExists(registryName, registryResourceGroup);
if (!registryExists) {
throw new Error(`Registry '${registryName}' does not exist in resource group '${registryResourceGroup}'.`);
} else {
console.log(`✅ Registry '${registryName}' exists in resource group '${registryResourceGroup}'.`);
}
}
// Check if model exists in registry
if(registryName !== undefined && registryName !== "") {
console.log(`🔹 Checking if model '${modelName}' exists in registry '${registryName}'...`);
const modelInRegistryExists = await checkIfModelInRegistryExists(
modelName, modelVersion, registryName, registryResourceGroup
);
if (!modelInRegistryExists) {
throw new Error(`Model '${modelName}' does not exist in registry '${registryName}'.`);
} else {
console.log(`✅ Model '${modelName}' exists in registry '${registryName}'.`);
}
} else {
console.log(`🔹 Checking if model '${modelName}' exists in workspace '${workspaceName}'...`);
const modelInWorkspaceExists = await checkIfModelInWorkspaceExists(
modelName, modelVersion, workspaceName, resourceGroup
);
if (!modelInWorkspaceExists) {
throw new Error(`Model '${modelName}' does not exist in workspace '${workspaceName}'.`);
} else {
console.log(`✅ Model '${modelName}' exists in workspace '${workspaceName}'.`);
}
}
console.log(`🔹 Checking if deployment YAML file '${deploymentYamlFilePath}' exists...`);
// Check if the deployment YAML file exists
const deploymentYamlFileExists = await checkIfFileExists(deploymentYamlFilePath);
if (!deploymentYamlFileExists) {
throw new Error(`Deployment YAML file '${deploymentYamlFilePath}' does not exist.`);
} else {
console.log(`✅ Deployment YAML file '${deploymentYamlFilePath}' exists.`);
}
// Create deployment
console.log(`🔹 Creating deployment...`)
const deploymentCreated = await createDeployment(resourceGroup, workspaceName, deploymentYamlFilePath);
if (!deploymentCreated) {
throw new Error(`Deployment creation failed.`);
} else {
console.log(`✅ Deployment created successfully.`);
}
// Parse the traffic input
const trafficObj = JSON.parse(traffic);
// Get values for "mirror"
const trafficEntry = trafficObj["mirror"];
// Check if "mirror" is present in the traffic object
if (trafficEntry) {
// Iterate through the keys of the "mirror" object
for (const key in trafficEntry) {
console.log(`🔹 Updating deployment '${key}' with mirror traffic '${trafficObj[key]}%'...`);
// Update the mirror traffic
let updateMirrorTraffic = await updateDeploymentMirrorTraffic(
resourceGroup, workspaceName, endpointName, key, trafficEntry[key]
);
if (!updateMirrorTraffic) {
console.log(`❌ Mirror traffic for '${key}' update failed.`);
} else {
console.log(`✅ Mirror traffic for '${key}' updated successfully.`);
}
}
} else {
console.log("🔹 No mirror traffic specified.");
}
// Get the keys of the traffic object except "mirror"
const prodKeys = Object.keys(trafficObj).filter(key => key !== "mirror");
// Iterate through the keys of the traffic object and update each deployment
for (const key of prodKeys) {
console.log(`🔹 Updating deployment '${key}' with traffic '${trafficObj[key]}%'...`);
// Update the traffic for the deployment
let updateTraffic = await updateDeploymentTraffic(
resourceGroup, workspaceName, endpointName, key, trafficObj[key]
);
if (!updateTraffic) {
console.log(`❌ Traffic for deployment '${key}' update failed.`);
} else {
console.log(
`✅ Traffic for deployment '${key}' updated successfully.`
);
}
}
console.log("✅ Deployment traffic updated successfully.");
} catch (error) {
console.log(error.message);
core.setFailed(`❌ Action failed: ${error.message}`);
}