From 9514dc1616d45bab43aaf66e85c08b8b7d2c79fa Mon Sep 17 00:00:00 2001 From: NichArchA82 <64152648+NichArchA82@users.noreply.github.com> Date: Sat, 7 Sep 2024 06:31:13 +0000 Subject: [PATCH] feat: using packer --- .../src/util/get-hetzner-images.js | 27 +++++++++++++++ command-handler/src/util/get-latest-tag.js | 19 +++++++++++ command-handler/src/util/hetzner-servers.js | 34 ++++++++++++++++--- 3 files changed, 76 insertions(+), 4 deletions(-) create mode 100644 command-handler/src/util/get-hetzner-images.js create mode 100644 command-handler/src/util/get-latest-tag.js diff --git a/command-handler/src/util/get-hetzner-images.js b/command-handler/src/util/get-hetzner-images.js new file mode 100644 index 0000000..c5d5b4f --- /dev/null +++ b/command-handler/src/util/get-hetzner-images.js @@ -0,0 +1,27 @@ +import axios from "axios" +import logger from "./logger.js" +import axiosError from "./axios-error-handler.js"; + +const log = logger(); + +export default async function getHetznerImages() { + const images = await axios.get(`https://api.hetzner.cloud/v1/images?type=snapshot`, { + headers: { + 'Authorization': `Bearer ${process.env.HETZNER_API_TOKEN}` + } + }) + .catch(error => { + log.error('Failed to get images from hetzner', axiosError(error)); + }); + + try { + if (!Array.isArray(images)) { + throw new Error('images is not an array'); + } + } catch (error) { + log.error({message: error.message, stack: error.stack}); + return null; + } + + return images; +} \ No newline at end of file diff --git a/command-handler/src/util/get-latest-tag.js b/command-handler/src/util/get-latest-tag.js new file mode 100644 index 0000000..ba93e77 --- /dev/null +++ b/command-handler/src/util/get-latest-tag.js @@ -0,0 +1,19 @@ +import axios from "axios" +import logger from "./logger.js" +import axiosError from "./axios-error-handler.js"; + +const log = logger(); + +export default async function getLatestTag() { + const tags = await axios.get(`https://api.github.com/repos/glueops/packer-cloud-developer-environments/tags`, { + }) + .catch(error => { + log.error('Failed to get tags from github', axiosError(error)); + }); + + if (tags.length > 0) { + return tags[0].name; + } else { + return null; + } +} \ No newline at end of file diff --git a/command-handler/src/util/hetzner-servers.js b/command-handler/src/util/hetzner-servers.js index 849b086..c54e6db 100644 --- a/command-handler/src/util/hetzner-servers.js +++ b/command-handler/src/util/hetzner-servers.js @@ -5,6 +5,8 @@ import formatUser from './format-user.js'; import getDevices from './get-devices-info.js'; import getServer from './get-servers.js'; import axiosError from './axios-error-handler.js'; +import getLatestTag from './get-latest-tag.js'; +import getHetznerImages from './get-hetzner-images.js'; import { uniqueNamesGenerator, colors, animals } from 'unique-names-generator'; const log = logger(); @@ -15,11 +17,9 @@ const delay = (ms) => { const region = "hel1"; const serverType = 'cx42'; -const image = 'debian-12'; const userData = ` #cloud-config runcmd: - - ['sh', '-c', 'curl -fsSL https://tailscale.com/install.sh | sh'] - ['tailscale', 'up', '--authkey=${process.env.TAILSCALE_AUTH_KEY}'] - ['tailscale', 'set', '--ssh'] - ['tailscale', 'set', '--accept-routes'] @@ -54,6 +54,32 @@ export default { } const userEmail = formatUser(info.user.profile.email); + + //get the hetzner images + const images = await getHetznerImages(); + + //get latest tag from github + const latestTag = await getLatestTag(); + + //return if it fails to get the images. + if (!images) { + app.client.chat.postEphemeral({ + channel: `${body.channel.id}`, + user: `${body.user.id}`, + text: `Failed to get image data` + }); + + return; + } + + const image = images.find(obj => obj.description === latestTag); + + //post a status message + app.client.chat.postEphemeral({ + channel: `${body.channel.id}`, + user: `${body.user.id}`, + text: `Creating the server with image: ${image} This will take about 4 minutes.` + }); //hetzner api to create the server try { @@ -91,8 +117,8 @@ export default { return; } - //set 2 minute delay - await delay(1000 * 60 * 2); + //set 4 minute delay + await delay(1000 * 60 * 4); //get servers and info from tailscale const { deviceId, deviceIP } = await getDevices(serverName);