85 lines
2.5 KiB
JavaScript
Executable File
85 lines
2.5 KiB
JavaScript
Executable File
#!/usr/bin/env node
|
|
|
|
import dotenv from 'dotenv'
|
|
import { join } from 'node:path'
|
|
dotenv.config({ path: join(import.meta.dirname, '../../.env') })
|
|
|
|
const apiV2Base = 'https://api.vultr.com/v2'
|
|
|
|
|
|
if (!process.env.VULTR_API_KEY) throw new Error('VULTR_API_KEY is missing in env');
|
|
|
|
|
|
|
|
async function deleteOrphanedLoadBalancers() {
|
|
console.log('getting load balancers')
|
|
const loadBalancersRes = await fetch(`${apiV2Base}/load-balancers`, {
|
|
method: 'GET',
|
|
headers: {
|
|
'authorization': `Bearer ${process.env.VULTR_API_KEY}`
|
|
}
|
|
})
|
|
const loadBalancerJson = await loadBalancersRes.json()
|
|
const orphanedLoadBalancers = loadBalancerJson.load_balancers.filter((lb) => (lb.instances.length === 0))
|
|
console.log(`found ${orphanedLoadBalancers.length} orphaned load balancers.`)
|
|
console.log('waiting 1 second')
|
|
await new Promise((resolve) => { setTimeout(resolve, 1000) })
|
|
|
|
for (const lb of orphanedLoadBalancers) {
|
|
console.log(`deleting load balancer ${lb.id}`)
|
|
|
|
const deleteLoadBalancerRes = await fetch(`https://api.vultr.com/v2/load-balancers/${lb.id}`, {
|
|
method: 'DELETE',
|
|
headers: {
|
|
'Authorization': `Bearer ${process.env.VULTR_API_KEY}`
|
|
}
|
|
})
|
|
|
|
console.log('waiting 1 second')
|
|
await new Promise((resolve) => { setTimeout(resolve, 1000) })
|
|
}
|
|
}
|
|
|
|
|
|
async function deleteOrphanedBlockStorage() {
|
|
console.log('getting block storage')
|
|
|
|
const blocksRes = await fetch(`${apiV2Base}/blocks`, {
|
|
method: 'GET',
|
|
headers: {
|
|
'authorization': `Bearer ${process.env.VULTR_API_KEY}`
|
|
}
|
|
})
|
|
const blocksJson = await blocksRes.json()
|
|
|
|
const orphanedBlocks = blocksJson.blocks.filter((b) => b.attached_to_instance === '')
|
|
console.log(`found ${orphanedBlocks.length} orphaned block storages`)
|
|
|
|
console.log('waiting 1 second')
|
|
await new Promise((resolve) => { setTimeout(resolve, 1000) })
|
|
for (const block of orphanedBlocks) {
|
|
console.log(`deleting block ${block.id}`)
|
|
|
|
const deleteBlocksRes = await fetch(`${apiV2Base}/blocks/${block.id}`, {
|
|
method: 'DELETE',
|
|
headers: {
|
|
'Authorization': `Bearer ${process.env.VULTR_API_KEY}`
|
|
}
|
|
})
|
|
|
|
console.log('waiting 1 second')
|
|
await new Promise((resolve) => { setTimeout(resolve, 1000) })
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
async function main() {
|
|
await deleteOrphanedLoadBalancers()
|
|
await deleteOrphanedBlockStorage()
|
|
}
|
|
|
|
main()
|