From 9de08270494dde3fedc4187553571acdabcc31a0 Mon Sep 17 00:00:00 2001 From: Jess Divers Date: Wed, 23 Oct 2024 11:16:59 -0700 Subject: [PATCH] feat: delete old on create new --- modules/swapAliasIndexGenerator.js | 87 +++++++++++++++++++----------- 1 file changed, 55 insertions(+), 32 deletions(-) diff --git a/modules/swapAliasIndexGenerator.js b/modules/swapAliasIndexGenerator.js index f67b43c1b..3864c1acd 100644 --- a/modules/swapAliasIndexGenerator.js +++ b/modules/swapAliasIndexGenerator.js @@ -5,49 +5,72 @@ consola.level = 5 export default function () { this.nuxt.hook("generate:done", async () => { - consola.debug("In generate done hook swap alias") - consola.debug(this.nuxt.options.publicRuntimeConfig.esTempIndex) - consola.debug(this.nuxt.options.publicRuntimeConfig.esIndex) - const response = await fetch( - `${this.nuxt.options.publicRuntimeConfig.esURL}/_aliases`, - { - headers: { - Authorization: `ApiKey ${this.nuxt.options.privateRuntimeConfig.esWriteKey}`, - "Content-Type": "application/json", - }, + consola.debug("In generate done hook, starting alias swap and index cleanup") + + const publicConfig = this.nuxt.options.publicRuntimeConfig + const privateConfig = this.nuxt.options.privateRuntimeConfig + const esUrl = publicConfig.esURL + const esAlias = publicConfig.esIndex + const tempIndex = publicConfig.esTempIndex + const headers = { + Authorization: `ApiKey ${privateConfig.esWriteKey}`, + "Content-Type": "application/json", + } + + // Step 1: Fetch current alias indices + let indicesToDelete = [] + try { + const aliasResponse = await fetch(`${esUrl}/_alias/${esAlias}`, { + headers, + method: "GET", + }) + const aliasData = await aliasResponse.json() + indicesToDelete = Object.keys(aliasData) // Extract the indices associated with the alias + consola.debug("Indices associated with alias:", indicesToDelete) + } catch (err) { + consola.error("Error fetching alias indices:", err) + throw err + } + + // Step 2: Update the alias to point to the new index + try { + const aliasUpdateResponse = await fetch(`${esUrl}/_aliases`, { + headers, method: "POST", body: JSON.stringify({ actions: [ { - remove: { - index: "*", - alias: this.nuxt.options.publicRuntimeConfig - .esIndex, - }, + remove: { index: "*", alias: esAlias }, }, { - add: { - indices: [ - this.nuxt.options.publicRuntimeConfig - .esTempIndex, - ], - alias: this.nuxt.options.publicRuntimeConfig - .esIndex, - }, + add: { indices: [tempIndex], alias: esAlias }, }, ], }), - } - ) - const body = await response.text() + }) + const aliasUpdateBody = await aliasUpdateResponse.text() + const aliasUpdateJson = JSON.parse(aliasUpdateBody) + consola.debug("Alias updated:", JSON.stringify(aliasUpdateJson)) + } catch (err) { + consola.error("Error updating alias:", err) + throw err + } + + // Step 3: Delete old indices try { - let testJson = JSON.parse(body) + for (const index of indicesToDelete) { + if (index === tempIndex) continue // Skip the new temp index - consola.debug("Alias updated :" + JSON.stringify(testJson)) + consola.debug(`Deleting index: ${index}`) + const deleteResponse = await fetch(`${esUrl}/${index}`, { + headers, + method: "DELETE", + }) + const deleteBody = await deleteResponse.text() + consola.debug(`Deleted index ${index}: ${deleteBody}`) + } } catch (err) { - consola.error("Error:", err) - consola.error("Response body:", body) - throw err + consola.error("Error deleting old indices:", err) } }) -} +} \ No newline at end of file