From be89a0e2c5acd93118de0de9702fe8c6f3658667 Mon Sep 17 00:00:00 2001 From: ntimo Date: Fri, 15 Apr 2022 20:16:46 +0000 Subject: [PATCH 1/4] example: added hetzner cloud workspace --- examples/hetzner-linux/README.md | 5 + .../hetzner-linux/cloud-config.yaml.tftpl | 46 +++++++ examples/hetzner-linux/main.tf | 120 ++++++++++++++++++ 3 files changed, 171 insertions(+) create mode 100644 examples/hetzner-linux/README.md create mode 100644 examples/hetzner-linux/cloud-config.yaml.tftpl create mode 100644 examples/hetzner-linux/main.tf diff --git a/examples/hetzner-linux/README.md b/examples/hetzner-linux/README.md new file mode 100644 index 0000000000000..9ec74231df71b --- /dev/null +++ b/examples/hetzner-linux/README.md @@ -0,0 +1,5 @@ +--- +name: Develop in Linux on Hetzner Cloud +description: Get started with Linux development on Hetzner Cloud. +tags: [cloud, hetzner] +--- diff --git a/examples/hetzner-linux/cloud-config.yaml.tftpl b/examples/hetzner-linux/cloud-config.yaml.tftpl new file mode 100644 index 0000000000000..bb8a047b367ee --- /dev/null +++ b/examples/hetzner-linux/cloud-config.yaml.tftpl @@ -0,0 +1,46 @@ +#cloud-config +users: + - name: ${username} + sudo: ["ALL=(ALL) NOPASSWD:ALL"] + groups: sudo + shell: /bin/bash +packages: + - git +mounts: + - [ + "${volume_path}", + "/home/${username}", + ext4, + "discard,defaults", + ] +write_files: + - path: /opt/coder/init + permissions: "0755" + encoding: b64 + content: ${init_script} + - path: /etc/systemd/system/coder-agent.service + permissions: "0644" + content: | + [Unit] + Description=Coder Agent + After=network-online.target + Wants=network-online.target + + [Service] + User=${username} + ExecStart=/opt/coder/init + Environment=CODER_AGENT_TOKEN=${coder_agent_token} + Restart=always + RestartSec=10 + TimeoutStopSec=90 + KillMode=process + + OOMScoreAdjust=-900 + SyslogIdentifier=coder-agent + + [Install] + WantedBy=multi-user.target +runcmd: + - chown ${username}:${username} /home/${username} + - systemctl enable coder-agent + - systemctl start coder-agent diff --git a/examples/hetzner-linux/main.tf b/examples/hetzner-linux/main.tf new file mode 100644 index 0000000000000..5c705a01e07c1 --- /dev/null +++ b/examples/hetzner-linux/main.tf @@ -0,0 +1,120 @@ +terraform { + required_providers { + coder = { + source = "coder/coder" + version = "0.4.2" + } + hcloud = { + source = "hetznercloud/hcloud" + version = "1.33.2" + } + } +} + +provider "hcloud" { + token = var.hcloud_token +} + +provider "coder" { +} + +variable "hcloud_token" { + description = < Date: Thu, 9 Jun 2022 19:30:09 +0000 Subject: [PATCH 2/4] example: added Code-Server installation to hetzner template --- .../hetzner-linux/cloud-config.yaml.tftpl | 26 +++++++++++++++++++ examples/hetzner-linux/main.tf | 23 ++++++++++++++-- 2 files changed, 47 insertions(+), 2 deletions(-) diff --git a/examples/hetzner-linux/cloud-config.yaml.tftpl b/examples/hetzner-linux/cloud-config.yaml.tftpl index bb8a047b367ee..ea1fc553328c7 100644 --- a/examples/hetzner-linux/cloud-config.yaml.tftpl +++ b/examples/hetzner-linux/cloud-config.yaml.tftpl @@ -6,6 +6,8 @@ users: shell: /bin/bash packages: - git + - curl + - jq mounts: - [ "${volume_path}", @@ -40,7 +42,31 @@ write_files: [Install] WantedBy=multi-user.target +%{ if code_server_setup ~} + - path: /tmp/install_code_server.sh + permissions: "0777" + content: | + #!/bin/bash + CODE_SERVER_DOWNLOAD_URL=$(curl -sL https://api.github.com/repos/coder/code-server/releases/latest | jq -r '.assets[].browser_download_url' | grep "amd64.deb") + curl -fL $CODE_SERVER_DOWNLOAD_URL -o /tmp/code_server.deb + dpkg -i /tmp/code_server.deb + systemctl enable --now code-server@${username} + rm /tmp/code_server.deb + - path: /tmp/install_code_server.sh + permissions: "0777" + content: | + - path: /home/${username}/.config/code-server/config.yaml + permissions: "0644" + content: | + bind-addr: 127.0.0.1:8080 + auth: none + cert: false +%{ endif ~} runcmd: - chown ${username}:${username} /home/${username} - systemctl enable coder-agent - systemctl start coder-agent +%{ if code_server_setup ~} + - /tmp/install_code_server.sh + - rm /tmp/install_code_server.sh +%{ endif } diff --git a/examples/hetzner-linux/main.tf b/examples/hetzner-linux/main.tf index 5c705a01e07c1..5e2ddfbb06430 100644 --- a/examples/hetzner-linux/main.tf +++ b/examples/hetzner-linux/main.tf @@ -65,6 +65,15 @@ variable "volume_size" { } } +variable "code_server" { + description = "Should Code Server be installed?" + default = "true" + validation { + condition = contains(["true","false"], var.code_server) + error_message = "Your answer can only be yes or no!" + } +} + data "coder_workspace" "me" { } @@ -73,17 +82,27 @@ resource "coder_agent" "dev" { os = "linux" } +resource "coder_app" "code-server" { + count = var.code_server ? 1 : 0 + agent_id = coder_agent.dev.id + name = "code-server" + icon = "https://cdn.icon-icons.com/icons2/2107/PNG/512/file_type_vscode_icon_130084.png" + url = "http://localhost:8080" + relative_path = true +} + resource "hcloud_server" "root" { - count = data.coder_workspace.me.start_count + count = data.coder_workspace.me.start_count name = "coder-${data.coder_workspace.me.owner}-${data.coder_workspace.me.name}-root" server_type = var.instance_type location = var.instance_location image = var.instance_os - user_data = templatefile("cloud-config.yaml.tftpl", { + user_data = templatefile("cloud-config.yaml.tftpl", { username = data.coder_workspace.me.owner volume_path = "/dev/disk/by-id/scsi-0HC_Volume_${hcloud_volume.root.id}" init_script = base64encode(coder_agent.dev.init_script) coder_agent_token = coder_agent.dev.token + code_server_setup = var.code_server }) } From 87a4e4880328dc8ed574d615f73c01da51241002 Mon Sep 17 00:00:00 2001 From: ntimo Date: Thu, 9 Jun 2022 19:52:23 +0000 Subject: [PATCH 3/4] example: remove fedora from hetzner cloud template --- examples/hetzner-linux/main.tf | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/hetzner-linux/main.tf b/examples/hetzner-linux/main.tf index 5e2ddfbb06430..1ac8407b63822 100644 --- a/examples/hetzner-linux/main.tf +++ b/examples/hetzner-linux/main.tf @@ -51,7 +51,7 @@ variable "instance_os" { description = "Which operating system should your workspace use?" default = "ubuntu-20.04" validation { - condition = contains(["ubuntu-22.04","ubuntu-20.04", "ubuntu-18.04", "debian-11", "debian-10", "fedora-35"], var.instance_os) + condition = contains(["ubuntu-22.04","ubuntu-20.04", "ubuntu-18.04", "debian-11", "debian-10"], var.instance_os) error_message = "Invalid OS!" } } From db360b6c846a7cf0764743a0177ad8dee191efbc Mon Sep 17 00:00:00 2001 From: ntimo Date: Thu, 9 Jun 2022 21:25:54 +0000 Subject: [PATCH 4/4] example: generate dummy ssh private key for hetzner cloud --- examples/hetzner-linux/main.tf | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/examples/hetzner-linux/main.tf b/examples/hetzner-linux/main.tf index 1ac8407b63822..b426427be8d19 100644 --- a/examples/hetzner-linux/main.tf +++ b/examples/hetzner-linux/main.tf @@ -91,12 +91,24 @@ resource "coder_app" "code-server" { relative_path = true } +# Generate a dummy ssh key that is not accessible so Hetzner cloud does not spam the admin with emails. +resource "tls_private_key" "rsa_4096" { + algorithm = "RSA" + rsa_bits = 4096 +} + +resource "hcloud_ssh_key" "root" { + name = "coder-${data.coder_workspace.me.owner}-${data.coder_workspace.me.name}-root" + public_key = tls_private_key.rsa_4096.public_key_openssh +} + resource "hcloud_server" "root" { count = data.coder_workspace.me.start_count name = "coder-${data.coder_workspace.me.owner}-${data.coder_workspace.me.name}-root" server_type = var.instance_type location = var.instance_location image = var.instance_os + ssh_keys = [hcloud_ssh_key.root.id] user_data = templatefile("cloud-config.yaml.tftpl", { username = data.coder_workspace.me.owner volume_path = "/dev/disk/by-id/scsi-0HC_Volume_${hcloud_volume.root.id}"