fp/services/actor/src/vultr.ts
CJ_Clippy cc0f0a33fa
Some checks failed
rssapp CI/CD / build (push) Successful in 2m2s
ci / test (push) Failing after 1m5s
ci / build (push) Has been cancelled
add rssapp gitea actions builder
2025-09-28 00:55:50 -08:00

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();
}