defines network, subnets, cloudflare dns & floatingip

This commit is contained in:
2025-12-31 14:46:53 +01:00
parent 2bb876904f
commit e65aead5f0
8 changed files with 312 additions and 84 deletions

View File

@@ -1,51 +1,86 @@
import * as pulumi from "@pulumi/pulumi";
import * as hcloud from "@pulumi/hcloud";
import * as random from "@pulumi/random";
import { config } from './config';
import { getCheapestServerType } from './utils';
import { config } from "./config";
import { getCheapestServerType, topicedLabel } from "./utils";
import { VmSize, OS, ServerLocations } from "./types";
// “Tag” servers using labels. Hetzner firewalls can target servers by label selectors. :contentReference[oaicite:2]{index=2}
const serverLabels = {
app: "demo",
role: "web",
env: pulumi.getStack(),
managed: "pulumi",
};
const sshPublicKey = config.require("sshPublicKey");
const sshKey = new hcloud.SshKey("ssh-key", {
name: `pulumi-${pulumi.getStack()}-ssh`,
publicKey: sshPublicKey,
});
const sshKey = new hcloud.SshKey("ssh-key", {
name: `pulumi-${pulumi.getStack()}-ssh`,
publicKey: sshPublicKey,
});
const serverName = (name: string, location: string) => {
if (name.includes("-")) {
const [n, id] = name.split("-");
return `${n}-${location}-${id}`;
}
return `${name}-${location}`;
};
export function server(
name: string,
size: VmSize,
os: OS = OS.debian,
location: ServerLocations,
network: hcloud.NetworkSubnet
network: hcloud.NetworkSubnet,
ipv4: boolean = false,
): hcloud.Server {
const ceap = getCheapestServerType('eu');
const extraLabel = topicedLabel(name)
name = serverName(name, location);
const networkId = network.networkId.apply((id) => String(id).split("-")[0]);
const hexId = new random.RandomId(`${name}-${location}`, {
byteLength: 2, // 2 bytes = 4 hex characters
});
name = `${name}-${location}`
return new hcloud.Server(name, {
const server = new hcloud.Server(
name,
image: os,
serverType: ceap,
location,
backups: false,
publicNets: [{
ipv4Enabled: false,
ipv6Enabled: true,
}],
networks: [network],
sshKeys: [sshKey.name],
labels: serverLabels
})
{
name,
image: os,
serverType: size,
location,
backups: false,
publicNets: [
{
ipv4Enabled: ipv4,
ipv6Enabled: true,
},
],
networks: [
{
networkId: networkId.apply((nid) => Number(nid)),
},
],
sshKeys: [sshKey.name],
labels: {
...serverLabels,
...extraLabel
},
},
{ dependsOn: [network] },
);
const serverNet = new hcloud.ServerNetwork(
`${name}-servernet-${location}`,
{
serverId: server.id.apply((id) => Number(id)),
subnetId: network.id,
},
{
dependsOn: [network, server],
parent: server,
deleteBeforeReplace: true,
ignoreChanges: [ 'serverId', 'ip', 'aliasIps', 'networkId', 'subnetId' ]
},
);
return server;
}