Skip to content
Open
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
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,5 @@ This template will do the following:
- Add volumes and firewall policy to the instance
- Ask the user if code-server should be installed and if so installs it

# Push
coder template push --variable "hcloud_token=token"
27 changes: 3 additions & 24 deletions cloud-config.yaml.tftpl
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ users:
- name: ${username}
sudo: ["ALL=(ALL) NOPASSWD:ALL"]
groups: sudo
shell: /bin/bash
shell: /usr/bin/bash
packages:
- git
- curl
Expand Down Expand Up @@ -42,28 +42,7 @@ 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: /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 }
- systemctl enable --now coder-agent
- resize2fs /dev/sdb
116 changes: 116 additions & 0 deletions coder.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
data "coder_parameter" "volume_size" {
name = "volume_size"
description = "Disk Size in GB"
default = 10
type = "number"
mutable = true
validation {
min = 10
max = 250
monotonic = "increasing"
}
}

data "coder_parameter" "code_server_extentions" {
name = "code-server extentions"
type = "list(string)"
description = "List of code-server extentions"
mutable = true
default = jsonencode([
"dracula-theme.theme-dracula",
"ms-toolsai.jupyter",
"redhat.vscode-yaml",
"redhat.vscode-xml",
"redhat.ansible",
"samuelcolvin.jinjahtml",
"PKief.material-icon-theme",
"hashicorp.terraform",
"hashicorp.hcl",
"GitLab.gitlab-workflow",
"scala-lang.scala",
"scalameta.metals"
])
}

data "coder_workspace" "me" {
}

data "coder_workspace_owner" "me" {
}

resource "coder_agent" "dev" {
arch = strcontains(module.hcloud_instance_type.value, "cax") ? "arm64" : "amd64" #change this!
os = "linux"

metadata {
display_name = "CPU Usage"
key = "cpu"
script = <<EOT
echo "$[100-$(vmstat 1 2|tail -1|awk '{print $15}')]"%
EOT
interval = 15
timeout = 5
}

metadata {
display_name = "Memory Usage"
key = "RAM"
script = <<EOT
free | awk '/^Mem/ { printf("%.0f%%", $2/$4 ) }'
EOT
interval = 15
timeout = 1
}

metadata {
display_name = "Load Average"
key = "load"
script = <<EOT
awk '{print $1}' /proc/loadavg
EOT
interval = 15
timeout = 1
}

metadata {
display_name = "Disk Usage /"
key = "disk-root"
script = <<EOT
df -h / | awk '{ print $5 }' | tail -1
EOT
interval = 15
timeout = 1
}

metadata {
display_name = "Disk Usage home"
key = "disk-home"
script = <<EOT
df -h /home/* | awk '{ print $5 }' | tail -1
EOT
interval = 15
timeout = 1
}

metadata {
display_name = "Process Count"
key = "process_count"
script = <<EOT
ps aux | wc -l
EOT
interval = 10
timeout = 1
}

metadata {
display_name = "Container Count"
key = "container_count"
script = <<EOT
[ -x "$(command -v docker)" ] && sudo docker ps | tail -n +2 | wc -l && exit 0
[ -x "$(command -v podman)" ] && podman ps | tail -n +2 | wc -l && exit 0
echo 0
EOT
interval = 15
timeout = 1
}
}
57 changes: 57 additions & 0 deletions hetzner.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
# 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_owner.me.name}-${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_owner.me.name}-${data.coder_workspace.me.name}-root"
server_type = module.hcloud_instance_type.value
location = module.hcloud_region.value
image = module.hcloud_os_type.value
ssh_keys = [hcloud_ssh_key.root.id]
user_data = templatefile("cloud-config.yaml.tftpl", {
username = data.coder_workspace_owner.me.name
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
})
}

resource "hcloud_volume" "root" {
name = "coder-${data.coder_workspace_owner.me.name}-${data.coder_workspace.me.name}-root"
format = "ext4"
size = data.coder_parameter.volume_size.value
location = module.hcloud_region.value
}

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_owner.me.name}-${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]
}
151 changes: 0 additions & 151 deletions main.tf

This file was deleted.

Loading