Skip to content

Commit

Permalink
feat: delete old on create new
Browse files Browse the repository at this point in the history
  • Loading branch information
Jess Divers committed Oct 23, 2024
1 parent 0337bb9 commit 9de0827
Showing 1 changed file with 55 additions and 32 deletions.
87 changes: 55 additions & 32 deletions modules/swapAliasIndexGenerator.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
})
}
}

0 comments on commit 9de0827

Please sign in to comment.