Skip to content

Commit

Permalink
feat: using packer
Browse files Browse the repository at this point in the history
  • Loading branch information
NichArchA82 committed Sep 7, 2024
1 parent 3ec232b commit 9514dc1
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 4 deletions.
27 changes: 27 additions & 0 deletions command-handler/src/util/get-hetzner-images.js
Original file line number Diff line number Diff line change
@@ -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;
}
19 changes: 19 additions & 0 deletions command-handler/src/util/get-latest-tag.js
Original file line number Diff line number Diff line change
@@ -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;
}
}
34 changes: 30 additions & 4 deletions command-handler/src/util/hetzner-servers.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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']
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit 9514dc1

Please sign in to comment.