-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
214 lines (185 loc) · 7.16 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
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 deleteEndpoint(workspaceName, resourceGroup, endpointName) {
/**
* 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 online-endpoint delete --name ${endpointName} --resource-group ${resourceGroup} --workspace-name ${workspaceName}`, [], options);
console.log("✅ Endpoint deleted. Output:", output);
return true;
} catch (error) {
console.log(
"❌ Endpoint not deleted, Error Occurred:", errorOutput || error.message
);
return false;
}
}
try {
const endpointName = core.getInput("endpoint_name");
const resourceGroup = core.getInput("resource_group");
const workspaceName = core.getInput("workspace_name");
// 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) {
console.log(`✅ Endpoint '${endpointName}' does not exist in resource group '${resourceGroup}' and workspace '${workspaceName}'.`);
// Exit 0
process.exit(0);
} else {
// Delete the endpoint
console.log(`🔹 Deleting endpoint '${endpointName}'...`);
const endpointDeleted = await deleteEndpoint(workspaceName, resourceGroup, endpointName);
if (endpointDeleted) {
console.log(`✅ Endpoint '${endpointName}' deleted successfully.`);
} else {
throw new Error(`Failed to delete endpoint '${endpointName}'.`);
}
}
} catch (error) {
console.log(error.message);
core.setFailed(`❌ Action failed: ${error.message}`);
}