terraform { required_providers { coder = { source = "coder/coder" } } } locals { username = data.coder_workspace_owner.me.name user_id = data.coder_workspace_owner.me.id } data "coder_provisioner" "me" {} data "coder_workspace" "me" {} data "coder_workspace_owner" "me" {} variable "github_token" { description = "GitHub token for Copilot (optional)" type = string sensitive = true default = "" } resource "coder_agent" "main" { arch = data.coder_provisioner.me.arch os = "linux" startup_script = <<-EOT #!/bin/bash set +e # 初始化使用者目錄 if [ ! -f ~/.init_done ]; then cp -rT /etc/skel ~ 2>/dev/null || true touch ~/.init_done fi mkdir -p ~/.config ~/.cache ~/.local/share # 確保 code-server 運行中 # codercom/enterprise-base 已預裝 code-server # Coder agent 會自動管理 code-server 進程 EOT env = { GIT_AUTHOR_NAME = coalesce(data.coder_workspace_owner.me.full_name, data.coder_workspace_owner.me.name) GIT_AUTHOR_EMAIL = data.coder_workspace_owner.me.email GIT_COMMITTER_NAME = coalesce(data.coder_workspace_owner.me.full_name, data.coder_workspace_owner.me.name) GIT_COMMITTER_EMAIL = data.coder_workspace_owner.me.email GITHUB_TOKEN = var.github_token != "" ? var.github_token : "" } 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" script = "echo \"$$(cat /proc/loadavg | awk '{ print $$1 }') $$(nproc)\" | awk '{ printf \"%0.2f\", $$1/$$2 }'" interval = 60 timeout = 1 } metadata { display_name = "Swap Usage (Host)" key = "7_swap_host" script = "free -b | awk '/^Swap/ { printf(\"%.1f/%.1f\", $$3/1024.0/1024.0/1024.0, $$2/1024.0/1024.0/1024.0) }'" interval = 10 timeout = 1 } } # code-server 應用配置 resource "coder_app" "code_server" { count = data.coder_workspace.me.start_count agent_id = coder_agent.main.id slug = "code-server" display_name = "code-server" icon = "/icon/code.svg" url = "http://localhost:13337" subdomain = false share = "owner" healthcheck { url = "http://localhost:13337/health" interval = 3 threshold = 10 } } # JetBrains IDE 支援(可選) resource "coder_app" "jetbrains" { count = data.coder_workspace.me.start_count agent_id = coder_agent.main.id slug = "jetbrains" display_name = "JetBrains Toolbox" icon = "/icon/jetbrains.svg" url = "http://localhost:5037" subdomain = false share = "owner" }