From f4e7dcf79e9ae21e177111149cd654be31df1083 Mon Sep 17 00:00:00 2001 From: Your Name Date: Thu, 19 Sep 2024 18:27:13 +0200 Subject: [PATCH] feat: catch buckets events --- packages/common/config/load-config.js | 4 + packages/common/package.json | 1 + .../common/utils/flatten-aggregate-error.js | 6 +- .../contrib/deploy-sidecars/kontinuous-ui.js | 38 ------ .../deploy-sidecars/kontinuous-ui.js | 109 ++++++++++++++++++ plugins/kontinuous-ui/lib/supabase.js | 7 ++ .../pre-deploy/01-deployment_event.js | 36 +++--- yarn.lock | 10 ++ 8 files changed, 153 insertions(+), 58 deletions(-) delete mode 100644 plugins/contrib/deploy-sidecars/kontinuous-ui.js create mode 100644 plugins/kontinuous-ui/deploy-sidecars/kontinuous-ui.js create mode 100644 plugins/kontinuous-ui/lib/supabase.js diff --git a/packages/common/config/load-config.js b/packages/common/config/load-config.js index ff3606079f..9d2bbedb32 100644 --- a/packages/common/config/load-config.js +++ b/packages/common/config/load-config.js @@ -1,6 +1,7 @@ const os = require("os") const path = require("path") const { mkdtemp } = require("fs/promises") +const { v4: uuidv4 } = require("uuid") const { satisfies } = require("compare-versions") const fs = require("fs-extra") @@ -241,6 +242,9 @@ const loadConfig = async ( env: "KS_FORCE_NEW_DEPLOY", envParser: envParserYaml, }, + pipelineUUID: { + defaultFunction: () => uuidv4(), + }, pipelineId: { defaultFunction: (config) => { const { diff --git a/packages/common/package.json b/packages/common/package.json index 5dad39450d..c92806bd02 100644 --- a/packages/common/package.json +++ b/packages/common/package.json @@ -31,6 +31,7 @@ "slugify": "^1.6.5", "sonic-boom": "^3.0.0", "tiged": "^2.12.4", + "uuid": "^10.0.0", "which": "^3.0.0", "yaml": "^2.3.1", "zx": "^7.1.1" diff --git a/packages/common/utils/flatten-aggregate-error.js b/packages/common/utils/flatten-aggregate-error.js index 242a682882..f1342746b6 100644 --- a/packages/common/utils/flatten-aggregate-error.js +++ b/packages/common/utils/flatten-aggregate-error.js @@ -1,7 +1,8 @@ const indent = require("./indent") -module.exports = (aggregateError) => - new Error( +module.exports = (aggregateError) => { + console.log("aggregateError", aggregateError) + return new Error( `${aggregateError.name} ${aggregateError.message}: \n${indent( aggregateError.errors .map((error) => `${error.stack.toString()}`) @@ -9,3 +10,4 @@ module.exports = (aggregateError) => 2 )}` ) +} diff --git a/plugins/contrib/deploy-sidecars/kontinuous-ui.js b/plugins/contrib/deploy-sidecars/kontinuous-ui.js deleted file mode 100644 index 758b4e8232..0000000000 --- a/plugins/contrib/deploy-sidecars/kontinuous-ui.js +++ /dev/null @@ -1,38 +0,0 @@ -module.exports = async (options, { logger, utils, dryRun, ctx }) => { - if (dryRun) { - return - } - - const eventsBucket = ctx.require("eventsBucket") - const abortController = ctx.require("abortController") - - const waitingFor = [] - eventsBucket.on("resource:created", ({ manifests }) => { - waitingFor.push( - new Promise(async (resolve, reject) => { - try { - // TODO push events - resolve(e) - } catch (e) { - reject(e) - } - // await - }) - ) - }) - - return new Promise(async (res, rej) => { - while (true) { - if (abortController.signal.aborted) { - try { - await Promise.all(...waitingFor) - res() - } catch (err) { - rej(err) - } - return - } - await sleep(1) - } - }) -} diff --git a/plugins/kontinuous-ui/deploy-sidecars/kontinuous-ui.js b/plugins/kontinuous-ui/deploy-sidecars/kontinuous-ui.js new file mode 100644 index 0000000000..7d2efa2358 --- /dev/null +++ b/plugins/kontinuous-ui/deploy-sidecars/kontinuous-ui.js @@ -0,0 +1,109 @@ +const { setTimeout: sleep } = require("timers/promises") +const supabase = require("../lib/supabase") + +module.exports = async (_options, { config, dryRun, ctx }) => { + if (dryRun) { + return + } + + const { + gitSha, + projectName, + environment, + gitBranch, + repositoryName, + gitRepositoryUrl, + pipelineUUID, + } = config + + const values = { + uuid: pipelineUUID, + commit_hash: gitSha, + project: projectName, + environment, + branch: gitBranch, + repository: repositoryName, + repository_url: gitRepositoryUrl, + } + + const eventsBucket = ctx.require("eventsBucket") + // const abortController = ctx.require("abortController") + + // resource:waiting + // resource:failed + // resource:ready + // resource:closed + + async function insertValues(data) { + const { error } = await supabase.from("deployments_logs").insert([data]) + if (error) throw error + } + + const waitingFor = [] + eventsBucket.on("resource:waiting", () => { + waitingFor.push( + new Promise(async (resolve, reject) => { + try { + const valuesToInsert = { ...values, status: "waiting" } + await insertValues(valuesToInsert) + resolve() + } catch (e) { + reject(e) + } + // await + }) + ) + }) + + eventsBucket.on("resource:ready", () => { + waitingFor.push( + new Promise(async (resolve, reject) => { + try { + const valuesToInsert = { ...values, status: "ready" } + await insertValues(valuesToInsert) + resolve() + } catch (e) { + reject(e) + } + // await + }) + ) + }) + + eventsBucket.on("resource:failed", () => { + waitingFor.push( + new Promise(async (resolve, reject) => { + try { + const valuesToInsert = { ...values, status: "failed" } + await insertValues(valuesToInsert) + resolve() + } catch (e) { + reject(e) + } + // await + }) + ) + }) + + let finished + eventsBucket.on("deploy-with:finish", () => { + finished = true + }) + + return new Promise(async (res, rej) => { + while (true) { + if (finished) { + console.log("WAITING FOR...") + try { + await Promise.all(waitingFor) + res() + } catch (err) { + console.log("ERROR", err) + rej(err) + } + return + } + await sleep(1) + } + }) +} diff --git a/plugins/kontinuous-ui/lib/supabase.js b/plugins/kontinuous-ui/lib/supabase.js new file mode 100644 index 0000000000..223f85ac61 --- /dev/null +++ b/plugins/kontinuous-ui/lib/supabase.js @@ -0,0 +1,7 @@ +const { createClient } = require("@supabase/supabase-js") + +const { SUPABASE_URL: supabaseUrl, SUPABASE_KEY: supabaseKey } = process.env + +const supabase = createClient(supabaseUrl, supabaseKey) + +module.exports = supabase diff --git a/plugins/kontinuous-ui/pre-deploy/01-deployment_event.js b/plugins/kontinuous-ui/pre-deploy/01-deployment_event.js index 7b8021912d..a3a9f04c76 100644 --- a/plugins/kontinuous-ui/pre-deploy/01-deployment_event.js +++ b/plugins/kontinuous-ui/pre-deploy/01-deployment_event.js @@ -1,30 +1,30 @@ -const { createClient } = require("@supabase/supabase-js") - -module.exports = async (_manifests, _options, { config, ctx }) => { - const processEnv = ctx.get("env") || process.env - const { SUPABASE_URL: supabaseUrl, SUPABASE_KEY: supabaseKey } = processEnv +const supabase = require("../lib/supabase") +module.exports = async (_manifests, _options, { config, logger }) => { const { gitSha, projectName, environment, - refLabelValue, + gitBranch, repositoryName, - actionCommandName, + gitRepositoryUrl, + pipelineUUID, } = config - const supabase = createClient(supabaseUrl, supabaseKey) + const values = { + uuid: pipelineUUID, + status: "pre-deploy", + commit_hash: gitSha, + project: projectName, + environment, + branch: gitBranch, + repository: repositoryName, + repository_url: gitRepositoryUrl, + } - const { error } = await supabase.from("deployments_logs").insert([ - { - status: actionCommandName, - commit_hash: gitSha, - project: projectName, - environment, - branch: refLabelValue, - repository: repositoryName, - }, - ]) + const { error } = await supabase.from("deployments_logs").insert([values]) if (error) throw error + + logger.info(values, "FINISHED") } diff --git a/yarn.lock b/yarn.lock index 3f48b7185e..4ff49c7535 100644 --- a/yarn.lock +++ b/yarn.lock @@ -15698,6 +15698,15 @@ __metadata: languageName: node linkType: hard +"uuid@npm:^10.0.0": + version: 10.0.0 + resolution: "uuid@npm:10.0.0" + bin: + uuid: dist/bin/uuid + checksum: 9ea91ef753d88b03746de0a22192251c355bbe595d9cb3a67520aacf693c793df7e85c4ad0378aecb133c372a5e7bb7ec0f3b14db477ab7e3b6ff29792e047b9 + languageName: node + linkType: hard + "uuid@npm:^9.0.0": version: 9.0.0 resolution: "uuid@npm:9.0.0" @@ -16253,6 +16262,7 @@ __metadata: slugify: "npm:^1.6.5" sonic-boom: "npm:^3.0.0" tiged: "npm:^2.12.4" + uuid: "npm:^10.0.0" which: "npm:^3.0.0" yaml: "npm:^2.3.1" zx: "npm:^7.1.1"