
* add job for cleaning environments * Fix job for cleaning environment * Update .github/workflows/cleanEnvironments.js Co-authored-by: Mikail <6186720+NyanKiyoshi@users.noreply.github.com> --------- Co-authored-by: Mikail <6186720+NyanKiyoshi@users.noreply.github.com>
113 lines
3.4 KiB
JavaScript
113 lines
3.4 KiB
JavaScript
const { Command } = require("commander");
|
|
const fetch = require("node-fetch");
|
|
|
|
const program = new Command();
|
|
|
|
program
|
|
.name("cleanEnvironments")
|
|
.description("Clean environments")
|
|
.option("--token <token>", "token fo login to cloud")
|
|
.option("--snapshot <snapshot>", "snapshot to revert to")
|
|
.action(async options => {
|
|
const token = options.token;
|
|
const snapshot = options.snapshot;
|
|
const environmentsToClean = await getEnvironmentsForReleaseTesting(token);
|
|
environmentsToClean.forEach(environment => {
|
|
cleanEnvironment(environment, snapshot, token);
|
|
});
|
|
})
|
|
.parse();
|
|
|
|
async function getEnvironmentsForReleaseTesting(token) {
|
|
const environments = await getEnvironments(token);
|
|
const environmentsForReleaseTesting = environments.filter(environment => {
|
|
return (
|
|
environment.domain.match(/^v\d*.staging/) ||
|
|
environment.domain == "master.staging.saleor.cloud"
|
|
);
|
|
});
|
|
return environmentsForReleaseTesting;
|
|
}
|
|
|
|
async function getEnvironments(token) {
|
|
const response = await fetch(
|
|
`https://staging-cloud.saleor.io/api/organizations/saleor/environments/`,
|
|
{
|
|
method: "GET",
|
|
headers: {
|
|
Authorization: `Token ${token}`,
|
|
Accept: "application/json",
|
|
"Content-Type": "application/json;charset=UTF-8",
|
|
},
|
|
},
|
|
);
|
|
return await response.json();
|
|
}
|
|
|
|
async function cleanEnvironment(environment, snapshot, token) {
|
|
const response = await fetch(
|
|
`https://staging-cloud.saleor.io/api/organizations/saleor/environments/${environment.key}/restore/`,
|
|
{
|
|
method: "PUT",
|
|
body: JSON.stringify({ restore_from: snapshot }),
|
|
headers: {
|
|
Authorization: `Token ${token}`,
|
|
Accept: "application/json",
|
|
"Content-Type": "application/json;charset=UTF-8",
|
|
},
|
|
},
|
|
);
|
|
const responseInJson = await response.json();
|
|
if (
|
|
responseInJson.non_field_errors
|
|
? responseInJson.non_field_errors
|
|
: responseInJson.__all__
|
|
) {
|
|
console.warn(
|
|
`${environment.name}: ${
|
|
responseInJson.non_field_errors
|
|
? responseInJson.non_field_errors
|
|
: responseInJson.__all__
|
|
}`,
|
|
);
|
|
} else {
|
|
await waitUntilTaskInProgress(responseInJson.task_id, environment.name);
|
|
}
|
|
}
|
|
|
|
async function waitUntilTaskInProgress(taskId, environment) {
|
|
const throwErrorAfterTimeout = setTimeout(function () {
|
|
throw new Error("Environment didn't upgrade after 30 minutes");
|
|
}, 120000);
|
|
|
|
while (true) {
|
|
const response = await fetch(
|
|
`https://staging-cloud.saleor.io/api/service/task-status/${taskId}/`,
|
|
{
|
|
method: "GET",
|
|
headers: {
|
|
Accept: "application/json",
|
|
"Content-Type": "application/json;charset=UTF-8",
|
|
},
|
|
},
|
|
);
|
|
const responseInJson = await response.json();
|
|
switch (responseInJson.status) {
|
|
case "PENDING":
|
|
case "IN_PROGRESS":
|
|
await new Promise(resolve => setTimeout(resolve, 10000));
|
|
break;
|
|
case "SUCCEEDED":
|
|
console.log(
|
|
`${environment}: Job ended with status - ${responseInJson.status} - ${responseInJson.job_name}`,
|
|
);
|
|
clearTimeout(throwErrorAfterTimeout);
|
|
return responseInJson.status;
|
|
default:
|
|
clearTimeout(throwErrorAfterTimeout);
|
|
throw console.warn(
|
|
`${environment}: Job ended with status - ${responseInJson.status} - ${responseInJson.job_name}`,
|
|
);
|
|
}
|
|
}
|
|
}
|