Thanks to visit codestin.com
Credit goes to github.com

Skip to content

example: added hetzner cloud workspace #1884

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions examples/hetzner-linux/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
name: Develop in Linux on Hetzner Cloud
description: Get started with Linux development on Hetzner Cloud.
tags: [cloud, hetzner]
---
72 changes: 72 additions & 0 deletions examples/hetzner-linux/cloud-config.yaml.tftpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
#cloud-config
users:
- name: ${username}
sudo: ["ALL=(ALL) NOPASSWD:ALL"]
groups: sudo
shell: /bin/bash
packages:
- git
- curl
- jq
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
%{ 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 }
151 changes: 151 additions & 0 deletions examples/hetzner-linux/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
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 = <<EOF
Coder requires a Hetzner Cloud token to provision workspaces.
EOF
sensitive = true
validation {
condition = length(var.hcloud_token) == 64
error_message = "Please provide a valid Hetzner Cloud API token."
}
}

variable "instance_location" {
description = "What region should your workspace live in?"
default = "nbg1"
validation {
condition = contains(["nbg1", "fsn1", "hel1"], var.instance_location)
error_message = "Invalid zone!"
}
}

variable "instance_type" {
description = "What instance type should your workspace use?"
default = "cx11"
validation {
condition = contains(["cx11", "cx21", "cx31", "cx41", "cx51"], var.instance_type)
error_message = "Invalid instance type!"
}
}

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"], var.instance_os)
error_message = "Invalid OS!"
}
}

variable "volume_size" {
description = "How much storage space do you need?"
default = "50"
validation {
condition = contains(["50","100","150"], var.volume_size)
error_message = "Invalid 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" {
}

resource "coder_agent" "dev" {
arch = "amd64"
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
}

# 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}"
init_script = base64encode(coder_agent.dev.init_script)
coder_agent_token = coder_agent.dev.token
code_server_setup = var.code_server
})
}

resource "hcloud_volume" "root" {
name = "coder-${data.coder_workspace.me.owner}-${data.coder_workspace.me.name}-root"
size = var.volume_size
format = "ext4"
location = var.instance_location
}

resource "hcloud_volume_attachment" "root" {
count = data.coder_workspace.me.start_count
volume_id = hcloud_volume.root.id
server_id = hcloud_server.root[count.index].id
automount = false
}

resource "hcloud_firewall" "root" {
name = "coder-${data.coder_workspace.me.owner}-${data.coder_workspace.me.name}-root"
rule {
direction = "in"
protocol = "icmp"
source_ips = [
"0.0.0.0/0",
"::/0"
]
}
}

resource "hcloud_firewall_attachment" "root_fw_attach" {
count = data.coder_workspace.me.start_count
firewall_id = hcloud_firewall.root.id
server_ids = [hcloud_server.root[count.index].id]
}