From 054cd415cf4cc4f95146a1e0a145dee6c26768d8 Mon Sep 17 00:00:00 2001 From: Karolina Rakoczy Date: Thu, 13 Apr 2023 14:44:22 +0200 Subject: [PATCH] Add job for cleaning environments (#3359) * 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> --- .github/workflows/clean-envs.yml | 33 ++++++++ .github/workflows/cleanEnvironments.js | 113 +++++++++++++++++++++++++ .github/workflows/tests-nightly.yml | 2 +- 3 files changed, 147 insertions(+), 1 deletion(-) create mode 100644 .github/workflows/clean-envs.yml create mode 100644 .github/workflows/cleanEnvironments.js diff --git a/.github/workflows/clean-envs.yml b/.github/workflows/clean-envs.yml new file mode 100644 index 000000000..42dbd817d --- /dev/null +++ b/.github/workflows/clean-envs.yml @@ -0,0 +1,33 @@ +name: Clean environments +on: + schedule: + - cron: "0 0 * * 0" + workflow_dispatch: + branches: ["main"] + +jobs: + cleanEnvironments: + runs-on: ubuntu-latest + env: + TOKEN: ${{ secrets.CLOUD_ACCESS_TOKEN }} + SNAPSHOT: 9oUFxT7f + steps: + - name: Checkout + uses: actions/checkout@v3 + + - name: Setup Node + uses: actions/setup-node@v3 + with: + node-version-file: ".nvmrc" + + - name: Install dependencies + run: | + cd .github/workflows + npm ci + + - name: clean environments + id: clean-environments + run: | + node .github/workflows/cleanEnvironments.js \ + --token "$TOKEN" \ + --snapshot "$SNAPSHOT" \ No newline at end of file diff --git a/.github/workflows/cleanEnvironments.js b/.github/workflows/cleanEnvironments.js new file mode 100644 index 000000000..1e7d3a85a --- /dev/null +++ b/.github/workflows/cleanEnvironments.js @@ -0,0 +1,113 @@ +const { Command } = require("commander"); +const fetch = require("node-fetch"); + +const program = new Command(); + +program + .name("cleanEnvironments") + .description("Clean environments") + .option("--token ", "token fo login to cloud") + .option("--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}`, + ); + } + } +} diff --git a/.github/workflows/tests-nightly.yml b/.github/workflows/tests-nightly.yml index c49ea406b..78abd6a4b 100644 --- a/.github/workflows/tests-nightly.yml +++ b/.github/workflows/tests-nightly.yml @@ -75,7 +75,7 @@ jobs: env: CI: true SALEOR_CLI_ENV: staging - run: npx saleor backup restore 9S4PckyI --skip-webhooks-update + run: npx saleor backup restore W5yhqHkw --skip-webhooks-update - name: Notify Slack if: ${{ failure() }}