Skip to content

Instantly share code, notes, and snippets.

@sreya
Created April 11, 2024 16:53
Show Gist options
  • Save sreya/4766fa3d0e97023d9725712bb4f65703 to your computer and use it in GitHub Desktop.
Save sreya/4766fa3d0e97023d9725712bb4f65703 to your computer and use it in GitHub Desktop.
terraform {
required_providers {
coder = {
source = "coder/coder"
}
}
}
provider "local" {}
locals {
username = data.coder_workspace.me.owner
}
resource "null_resource" "systemd_service" {
count = "${data.coder_workspace.me.start_count}"
provisioner "local-exec" {
command = <<-EOT
#!/bin/bash
mkdir -p ~/.config/systemd/user/
cat <<EOF > ~/.config/systemd/user/coder.service
[Unit]
Description=Coder
After=network.target
[Service]
Type=simple
ExecStart=/usr/bin/coder agent
Restart=always
RestartSec=3
Environment="CODER_AGENT_TOKEN=${coder_agent.main.token}"
Environment="CODER_AGENT_URL=${data.coder_workspace.me.access_url}"
Environment="CODER_AGENT_AUTH=token"
[Install]
WantedBy=default.target
EOF
systemctl --user daemon-reload
systemctl --user start coder.service
EOT
interpreter = ["bash", "-c"]
}
}
resource "null_resource" "stop_systemd_service" {
count = "${data.coder_workspace.me.start_count}" == 0 ? 1 : 0
# This resource runs only during Terraform destroy
provisioner "local-exec" {
command = "systemctl --user stop coder.service"
}
}
data "coder_provisioner" "me" {
}
data "coder_workspace" "me" {
}
resource "coder_agent" "main" {
arch = data.coder_provisioner.me.arch
os = "linux"
startup_script = <<-EOT
set -e
# install and start code-server
curl -fsSL https://code-server.dev/install.sh | sh -s -- --method=standalone --prefix=/tmp/code-server --version 4.19.1
/tmp/code-server/bin/code-server --auth none --port 13337 >/tmp/code-server.log 2>&1 &
EOT
# These environment variables allow you to make Git commits right away after creating a
# workspace. Note that they take precedence over configuration defined in ~/.gitconfig!
# You can remove this block if you'd prefer to configure Git manually or using
# dotfiles. (see docs/dotfiles.md)
env = {
GIT_AUTHOR_NAME = coalesce(data.coder_workspace.me.owner_name, data.coder_workspace.me.owner)
GIT_AUTHOR_EMAIL = "${data.coder_workspace.me.owner_email}"
GIT_COMMITTER_NAME = coalesce(data.coder_workspace.me.owner_name, data.coder_workspace.me.owner)
GIT_COMMITTER_EMAIL = "${data.coder_workspace.me.owner_email}"
}
# The following metadata blocks are optional. They are used to display
# information about your workspace in the dashboard. You can remove them
# if you don't want to display any information.
# For basic resources, you can use the `coder stat` command.
# If you need more control, you can write your own script.
metadata {
display_name = "CPU Usage"
key = "0_cpu_usage"
script = "coder stat cpu"
interval = 10
timeout = 1
}
metadata {
display_name = "RAM Usage"
key = "1_ram_usage"
script = "coder stat mem"
interval = 10
timeout = 1
}
metadata {
display_name = "Home Disk"
key = "3_home_disk"
script = "coder stat disk --path $${HOME}"
interval = 60
timeout = 1
}
metadata {
display_name = "CPU Usage (Host)"
key = "4_cpu_usage_host"
script = "coder stat cpu --host"
interval = 10
timeout = 1
}
metadata {
display_name = "Memory Usage (Host)"
key = "5_mem_usage_host"
script = "coder stat mem --host"
interval = 10
timeout = 1
}
metadata {
display_name = "Load Average (Host)"
key = "6_load_host"
# get load avg scaled by number of cores
script = <<EOT
echo "`cat /proc/loadavg | awk '{ print $1 }'` `nproc`" | awk '{ printf "%0.2f", $1/$2 }'
EOT
interval = 60
timeout = 1
}
metadata {
display_name = "Swap Usage (Host)"
key = "7_swap_host"
script = <<EOT
free -b | awk '/^Swap/ { printf("%.1f/%.1f", $3/1024.0/1024.0/1024.0, $2/1024.0/1024.0/1024.0) }'
EOT
interval = 10
timeout = 1
}
}
resource "coder_app" "code-server" {
agent_id = coder_agent.main.id
slug = "code-server"
display_name = "code-server"
url = "http://localhost:13337/?folder=/home/${local.username}"
icon = "/icon/code.svg"
subdomain = false
share = "owner"
healthcheck {
url = "http://localhost:13337/healthz"
interval = 5
threshold = 6
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment