Skip to content

Commit

Permalink
feat: delimage command added
Browse files Browse the repository at this point in the history
  • Loading branch information
NichArchA82 committed Sep 7, 2024
1 parent 1dd476d commit 775d5c3
Showing 1 changed file with 49 additions and 0 deletions.
49 changes: 49 additions & 0 deletions command-handler/src/commands/delimage.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import axios from "axios"
import logger from "../util/logger.js"
import getLatestTag from "../util/get-latest-tag.js"
import getHetznerImages from "../util/get-hetzner-images.js"

const log = logger();

export default {
description: 'deletes outdated images from hetzner',

run: async ({ response }) => {

//get github tags
const latestTag = await getLatestTag();

//get hetzner images
const images = await getHetznerImages();

//return if no images
if (!images) {
app.client.chat.postEphemeral({
channel: `${message.channel}`,
user: `${message.user}`,
text: `Failed to get image data`
});

return;
}

//get the image to delete
for (const image of images) {
if (image.description !== latestTag) {
//delete the image that doesn't have the latest tag
await axios.delete(`https://api.hetzner.cloud/v1/images/${image.id}`, {
headers: {
'Authorization': `Bearer ${process.env.HETZNER_API_TOKEN}`
}
})
.catch(error => {
log.error('Failed to delete image from hetzner', axiosError(error));
});
}
}

response({
text: `cleanup complete`
})
}
}

0 comments on commit 775d5c3

Please sign in to comment.