Skip to content

Commit

Permalink
example: hetzner
Browse files Browse the repository at this point in the history
  • Loading branch information
thdxr committed Jul 29, 2024
1 parent eabf056 commit da84268
Show file tree
Hide file tree
Showing 6 changed files with 88 additions and 0 deletions.
3 changes: 3 additions & 0 deletions examples/hetzner/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# sst
.sst
key_rsa
Binary file added examples/hetzner/bun.lockb
Binary file not shown.
7 changes: 7 additions & 0 deletions examples/hetzner/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"name": "base-ts",
"version": "0.0.0",
"dependencies": {
"sst": "^0.1.54"
}
}
8 changes: 8 additions & 0 deletions examples/hetzner/sst-env.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/* tslint:disable */
/* eslint-disable */
import "sst"
declare module "sst" {
export interface Resource {
}
}
export {}
69 changes: 69 additions & 0 deletions examples/hetzner/sst.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/// <reference path="./.sst/platform/config.d.ts" />

import { writeFileSync } from "fs";
import { resolve } from "path";

export default $config({
app(input) {
return {
name: "hetzner",
removal: input?.stage === "production" ? "retain" : "remove",
home: "aws",
providers: { hcloud: true, tls: true, docker: true },
};
},
async run() {
const privateKey = new tls.PrivateKey("PrivateKey", {
algorithm: "RSA",
rsaBits: 4096,
});
const publicKey = new hcloud.SshKey("PublicKey", {
publicKey: privateKey.publicKeyOpenssh,
});
const server = new hcloud.Server("Server", {
image: "debian-12",
serverType: "cx11",
sshKeys: [publicKey.id],
userData: [
`#!/bin/bash`,
`apt-get update`,
`apt-get install -y docker.io apparmor`,
`systemctl enable --now docker`,
`usermod -aG docker debian`,
].join("\n"),
});

const keyPath = privateKey.privateKeyOpenssh.apply((key) => {
const path = "key_rsa";
writeFileSync(path, key, { mode: 0o600 });
return resolve(path);
});

const dockerProvider = new docker.Provider("DockerProvider", {
host: $interpolate`ssh://root@${server.ipv4Address}`,
sshOpts: ["-i", keyPath, "-o", "StrictHostKeyChecking=no"],
});

const nginx = new docker.Container(
"Nginx",
{
image: "nginx:latest",
ports: [
{
internal: 80,
external: 80,
},
],
restart: "always",
},
{
provider: dockerProvider,
dependsOn: [server],
},
);

return {
url: $interpolate`http://${server.ipv4Address}`,
};
},
});
1 change: 1 addition & 0 deletions examples/hetzner/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{}

0 comments on commit da84268

Please sign in to comment.