Skip to content

Commit

Permalink
fix: refactor hetzner code
Browse files Browse the repository at this point in the history
  • Loading branch information
NichArchA82 committed Sep 28, 2024
1 parent 568abb1 commit d527ae7
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 22 deletions.
5 changes: 2 additions & 3 deletions bot/example.env
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
SIGNING_SECRET=<SLACK Bot signing Secret>
BOT_TOKEN=<Slack Bot Token>
APP_TOKEN=<Slack Bot App Token>
BOLT_PORT=3000
SERVER_PORT=5000
HETZNER_API_TOKEN=<Hetzner API Token>
TAILSCALE_API_TOKEN=<Tailscale API Token>
TAILSCALE_AUTH_KEY=<Tailscale Auth Key>
TAILSCALE_TAILNET_NAME=<Tailscale Tailnet Name>
TAILSCALE_TAILNET_NAME=<Tailscale Tailnet Name>
HETZNER_API_TOKEN=<Hetzner API Token>
4 changes: 2 additions & 2 deletions command-handler/src/util/get-servers.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import logger from './logger.js';

const log = logger();

export default async function getServer() {
const data = await axios.get('https://api.hetzner.cloud/v1/servers', {
export default async function getServer(serverId = "") {
const data = await axios.get(`https://api.hetzner.cloud/v1/servers/${serverId}`, {
headers: {
'Authorization': `Bearer ${process.env.HETZNER_API_TOKEN}`
}
Expand Down
59 changes: 42 additions & 17 deletions command-handler/src/util/hetzner-servers.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,9 @@ export default {
});

//hetzner api to create the server
let serverRes;
try {
await axios.post('https://api.hetzner.cloud/v1/servers',
serverRes = await axios.post('https://api.hetzner.cloud/v1/servers',
{
"automount": false,
"image": imageID,
Expand Down Expand Up @@ -97,18 +98,44 @@ export default {
return;
}

const maxRetries = 20;
let maxRetries = 20;
let attempts;

for (attempts = 1; attempts <= maxRetries; attempts++) {
//set tag in tailscale
//wait 30 seconds
await delay(1000 * 30);

const server = await getServer(serverRes.data.server.id);

if (server.data.server.status === 'running') {
break;
} else {
log.info(`Attempt ${attempts} Failed. Backing off for 30 seconds`);
}
}

if (attempts === maxRetries) {
try {
//wait 30 seconds
await delay(1000 * 30);
throw new Error(`Failed to initialize server in hetzner after ${attempts} retries`);
} catch (error) {
log.error({message: error.message, stack: error.stack});
app.client.chat.postEphemeral({
channel: `${body.channel.id}`,
user: `${body.user.id}`,
text: `Failed to initialize server in Hetzner`
});
return;
}
}

maxRetries = 4;
for (attempts = 1; attempts <= maxRetries; attempts++) {
//wait 30 seconds
await delay(1000 * 30);
try {
//get servers and info from tailscale
const { deviceId } = await getDevices(serverName);

await axios.post(`https://api.tailscale.com/api/v2/device/${deviceId}/tags`,
{
"tags": [
Expand All @@ -120,25 +147,23 @@ export default {
'Content-Type': 'application/json'
}
});

//break out of the function if successful
break;
} catch (error) {
log.info(`Attempt ${attempts} Failed. Backing off for 30 seconds`)
}
log.info(`Attempt ${attempts} Failed. Backing off for 30 seconds`)
}
}

if (attempts === maxRetries) {
try {
throw new Error(`Failed to set tags in tailscale after ${attempts} retries`);
} catch (error) {
log.error({message: error.message, stack: error.stack});
app.client.chat.postEphemeral({
channel: `${body.channel.id}`,
user: `${body.user.id}`,
text: `Failed to set tags in tailscale`
});
return;
log.error({message: error.message, stack: error.stack});
app.client.chat.postEphemeral({
channel: `${body.channel.id}`,
user: `${body.user.id}`,
text: `Failed to set tags in tailscale`
});
return;
}
}

Expand Down

0 comments on commit d527ae7

Please sign in to comment.