mirror of
https://github.com/KevinMidboe/schleppe-ha-project.git
synced 2026-02-13 19:19:08 +00:00
defines network, subnets, cloudflare dns & floatingip
This commit is contained in:
@@ -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;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user