From 775d5c3dba5e8a71a09c4a21bd8b58333d1709cf Mon Sep 17 00:00:00 2001 From: NichArchA82 <64152648+NichArchA82@users.noreply.github.com> Date: Sat, 7 Sep 2024 18:51:57 +0000 Subject: [PATCH] feat: delimage command added --- command-handler/src/commands/delimage.js | 49 ++++++++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 command-handler/src/commands/delimage.js diff --git a/command-handler/src/commands/delimage.js b/command-handler/src/commands/delimage.js new file mode 100644 index 0000000..5d35960 --- /dev/null +++ b/command-handler/src/commands/delimage.js @@ -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` + }) + } +} \ No newline at end of file