-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
295 lines (257 loc) · 10.8 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
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 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 traffic = core.getInput("traffic");
// 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");
}
// 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}'.`);
}
if (traffic !== undefined && traffic !== "") {
console.log(`🔹 Updating traffic for endpoint '${endpointName}'...`);
// 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}`);
}