47 lines
1.2 KiB
TypeScript
47 lines
1.2 KiB
TypeScript
import { readFile } from 'fs/promises';
|
|
import nunjucks from 'nunjucks';
|
|
import { env } from './env.js';
|
|
|
|
nunjucks.configure({ autoescape: true });
|
|
|
|
export async function createVultrInstance(): Promise<any> {
|
|
// Load cloud-init template
|
|
const template = await readFile('./user-data.j2', 'utf-8');
|
|
|
|
const hostname = `gitea-runner-${Date.now()}`
|
|
|
|
// Render template with the runner token
|
|
const userData = nunjucks.renderString(template, {
|
|
GITEA_RUNNER_REGISTRATION_TOKEN: env.GITEA_RUNNER_REGISTRATION_TOKEN,
|
|
hostname,
|
|
});
|
|
|
|
const body = {
|
|
region: 'ord',
|
|
plan: env.VPS_SPEC,
|
|
os_id: 2284, // Ubuntu 22.04 x64
|
|
user_data: Buffer.from(userData).toString('base64'),
|
|
label: hostname,
|
|
hostname,
|
|
sshkey_id: null,
|
|
enable_ipv6: true,
|
|
enable_private_network: true,
|
|
};
|
|
|
|
const res = await fetch('https://api.vultr.com/v2/instances', {
|
|
method: 'POST',
|
|
headers: {
|
|
'Authorization': `Bearer ${env.VULTR_API_KEY}`,
|
|
'Content-Type': 'application/json',
|
|
},
|
|
body: JSON.stringify(body),
|
|
});
|
|
|
|
if (!res.ok) {
|
|
const text = await res.text();
|
|
throw new Error(`Failed to create VPS: ${res.status} ${text}`);
|
|
}
|
|
|
|
return res.json();
|
|
}
|