|
| 1 | +terraform { |
| 2 | + required_providers { |
| 3 | + coder = { |
| 4 | + source = "coder/coder" |
| 5 | + version = "0.11.0" |
| 6 | + } |
| 7 | + docker = { |
| 8 | + source = "kreuzwerker/docker" |
| 9 | + version = "3.0.2" |
| 10 | + } |
| 11 | + } |
| 12 | +} |
| 13 | + |
| 14 | +data "coder_provisioner" "me" { |
| 15 | +} |
| 16 | + |
| 17 | +provider "docker" { |
| 18 | +} |
| 19 | + |
| 20 | +data "coder_workspace" "me" { |
| 21 | +} |
| 22 | + |
| 23 | +resource "coder_agent" "main" { |
| 24 | + arch = data.coder_provisioner.me.arch |
| 25 | + os = "linux" |
| 26 | + startup_script_timeout = 180 |
| 27 | + startup_script = <<-EOT |
| 28 | + set -e |
| 29 | +
|
| 30 | + # install and start code-server |
| 31 | + curl -fsSL https://code-server.dev/install.sh | sh -s -- --method=standalone --prefix=/tmp/code-server --version 4.11.0 |
| 32 | + /tmp/code-server/bin/code-server --auth none --port 13337 >/tmp/code-server.log 2>&1 & |
| 33 | + EOT |
| 34 | + dir = "/worskpaces" |
| 35 | + |
| 36 | + # These environment variables allow you to make Git commits right away after creating a |
| 37 | + # workspace. Note that they take precedence over configuration defined in ~/.gitconfig! |
| 38 | + # You can remove this block if you'd prefer to configure Git manually or using |
| 39 | + # dotfiles. (see docs/dotfiles.md) |
| 40 | + env = { |
| 41 | + GIT_AUTHOR_NAME = "${data.coder_workspace.me.owner}" |
| 42 | + GIT_COMMITTER_NAME = "${data.coder_workspace.me.owner}" |
| 43 | + GIT_AUTHOR_EMAIL = "${data.coder_workspace.me.owner_email}" |
| 44 | + GIT_COMMITTER_EMAIL = "${data.coder_workspace.me.owner_email}" |
| 45 | + } |
| 46 | + |
| 47 | + # The following metadata blocks are optional. They are used to display |
| 48 | + # information about your workspace in the dashboard. You can remove them |
| 49 | + # if you don't want to display any information. |
| 50 | + # For basic resources, you can use the `coder stat` command. |
| 51 | + # If you need more control, you can write your own script. |
| 52 | + metadata { |
| 53 | + display_name = "CPU Usage" |
| 54 | + key = "0_cpu_usage" |
| 55 | + script = "coder stat cpu" |
| 56 | + interval = 10 |
| 57 | + timeout = 1 |
| 58 | + } |
| 59 | + |
| 60 | + metadata { |
| 61 | + display_name = "RAM Usage" |
| 62 | + key = "1_ram_usage" |
| 63 | + script = "coder stat mem" |
| 64 | + interval = 10 |
| 65 | + timeout = 1 |
| 66 | + } |
| 67 | + |
| 68 | + metadata { |
| 69 | + display_name = "Home Disk" |
| 70 | + key = "3_home_disk" |
| 71 | + script = "coder stat disk --path $HOME" |
| 72 | + interval = 60 |
| 73 | + timeout = 1 |
| 74 | + } |
| 75 | + |
| 76 | + metadata { |
| 77 | + display_name = "CPU Usage (Host)" |
| 78 | + key = "4_cpu_usage_host" |
| 79 | + script = "coder stat cpu --host" |
| 80 | + interval = 10 |
| 81 | + timeout = 1 |
| 82 | + } |
| 83 | + |
| 84 | + metadata { |
| 85 | + display_name = "Memory Usage (Host)" |
| 86 | + key = "5_mem_usage_host" |
| 87 | + script = "coder stat mem --host" |
| 88 | + interval = 10 |
| 89 | + timeout = 1 |
| 90 | + } |
| 91 | + |
| 92 | + metadata { |
| 93 | + display_name = "Load Average (Host)" |
| 94 | + key = "6_load_host" |
| 95 | + # get load avg scaled by number of cores |
| 96 | + script = <<EOT |
| 97 | + echo "`cat /proc/loadavg | awk '{ print $1 }'` `nproc`" | awk '{ printf "%0.2f", $1/$2 }' |
| 98 | + EOT |
| 99 | + interval = 60 |
| 100 | + timeout = 1 |
| 101 | + } |
| 102 | + |
| 103 | + metadata { |
| 104 | + display_name = "Swap Usage (Host)" |
| 105 | + key = "7_swap_host" |
| 106 | + script = <<EOT |
| 107 | + free -b | awk '/^Swap/ { printf("%.1f/%.1f", $3/1024.0/1024.0/1024.0, $2/1024.0/1024.0/1024.0) }' |
| 108 | + EOT |
| 109 | + interval = 10 |
| 110 | + timeout = 1 |
| 111 | + } |
| 112 | +} |
| 113 | + |
| 114 | +resource "coder_app" "code-server" { |
| 115 | + agent_id = coder_agent.main.id |
| 116 | + slug = "code-server" |
| 117 | + display_name = "code-server" |
| 118 | + url = "http://localhost:13337/?folder=/workspaces" |
| 119 | + icon = "/icon/code.svg" |
| 120 | + subdomain = false |
| 121 | + share = "owner" |
| 122 | + |
| 123 | + healthcheck { |
| 124 | + url = "http://localhost:13337/healthz" |
| 125 | + interval = 5 |
| 126 | + threshold = 6 |
| 127 | + } |
| 128 | +} |
| 129 | + |
| 130 | + |
| 131 | +resource "docker_volume" "workspaces" { |
| 132 | + name = "coder-${data.coder_workspace.me.id}" |
| 133 | + # Protect the volume from being deleted due to changes in attributes. |
| 134 | + lifecycle { |
| 135 | + ignore_changes = all |
| 136 | + } |
| 137 | + # Add labels in Docker to keep track of orphan resources. |
| 138 | + labels { |
| 139 | + label = "coder.owner" |
| 140 | + value = data.coder_workspace.me.owner |
| 141 | + } |
| 142 | + labels { |
| 143 | + label = "coder.owner_id" |
| 144 | + value = data.coder_workspace.me.owner_id |
| 145 | + } |
| 146 | + labels { |
| 147 | + label = "coder.workspace_id" |
| 148 | + value = data.coder_workspace.me.id |
| 149 | + } |
| 150 | + # This field becomes outdated if the workspace is renamed but can |
| 151 | + # be useful for debugging or cleaning out dangling volumes. |
| 152 | + labels { |
| 153 | + label = "coder.workspace_name_at_creation" |
| 154 | + value = data.coder_workspace.me.name |
| 155 | + } |
| 156 | +} |
| 157 | + |
| 158 | +data "coder_parameter" "repo" { |
| 159 | + name = "repo" |
| 160 | + display_name = "Repository (auto)" |
| 161 | + order = 1 |
| 162 | + description = "Select a repository to automatically clone and start working with a devcontainer." |
| 163 | + mutable = true |
| 164 | + option { |
| 165 | + name = "vercel/next.js" |
| 166 | + description = "The React Framework" |
| 167 | + value = "https://github.com/vercel/next.js" |
| 168 | + } |
| 169 | + option { |
| 170 | + name = "home-assistant/core" |
| 171 | + description = "🏡 Open source home automation that puts local control and privacy first." |
| 172 | + value = "https://github.com/home-assistant/core" |
| 173 | + } |
| 174 | + option { |
| 175 | + name = "discourse/discourse" |
| 176 | + description = "A platform for community discussion. Free, open, simple." |
| 177 | + value = "https://github.com/discourse/discourse" |
| 178 | + } |
| 179 | + option { |
| 180 | + name = "denoland/deno" |
| 181 | + description = "A modern runtime for JavaScript and TypeScript." |
| 182 | + value = "https://github.com/denoland/deno" |
| 183 | + } |
| 184 | + option { |
| 185 | + name = "microsoft/vscode" |
| 186 | + icon = "/icon/code.svg" |
| 187 | + description = "Code editing. Redefined." |
| 188 | + value = "https://github.com/microsoft/vscode" |
| 189 | + } |
| 190 | + option { |
| 191 | + name = "Custom" |
| 192 | + icon = "/emojis/1f5c3.png" |
| 193 | + description = "Specify a custom repo URL below" |
| 194 | + value = "custom" |
| 195 | + } |
| 196 | +} |
| 197 | + |
| 198 | +data "coder_parameter" "custom_repo_url" { |
| 199 | + name = "custom_repo" |
| 200 | + display_name = "Repository URL (custom)" |
| 201 | + order = 2 |
| 202 | + default = "" |
| 203 | + description = "Optionally enter a custom repository URL, see [awesome-devcontainers](https://github.com/manekinekko/awesome-devcontainers)." |
| 204 | + mutable = true |
| 205 | +} |
| 206 | + |
| 207 | +resource "docker_container" "workspace" { |
| 208 | + count = data.coder_workspace.me.start_count |
| 209 | + image = "ghcr.io/coder/envbuilder:0.1.3" |
| 210 | + # Uses lower() to avoid Docker restriction on container names. |
| 211 | + name = "coder-${data.coder_workspace.me.owner}-${lower(data.coder_workspace.me.name)}" |
| 212 | + # Hostname makes the shell more user friendly: coder@my-workspace:~$ |
| 213 | + hostname = data.coder_workspace.me.name |
| 214 | + # Use the docker gateway if the access URL is 127.0.0.1 |
| 215 | + env = [ |
| 216 | + "CODER_AGENT_TOKEN=${coder_agent.main.token}", |
| 217 | + "CODER_AGENT_URL=${replace(data.coder_workspace.me.access_url, "/localhost|127\\.0\\.0\\.1/", "host.docker.internal")}", |
| 218 | + "GIT_URL=${data.coder_parameter.repo.value == "custom" ? data.coder_parameter.custom_repo_url.value : data.coder_parameter.repo.value}", |
| 219 | + "INIT_SCRIPT=${replace(coder_agent.main.init_script, "/localhost|127\\.0\\.0\\.1/", "host.docker.internal")}", |
| 220 | + "FALLBACK_IMAGE=codercom/enterprise-base:ubuntu" # This image runs if builds fail |
| 221 | + ] |
| 222 | + host { |
| 223 | + host = "host.docker.internal" |
| 224 | + ip = "host-gateway" |
| 225 | + } |
| 226 | + volumes { |
| 227 | + container_path = "/workspaces" |
| 228 | + volume_name = docker_volume.workspaces.name |
| 229 | + read_only = false |
| 230 | + } |
| 231 | + # Add labels in Docker to keep track of orphan resources. |
| 232 | + labels { |
| 233 | + label = "coder.owner" |
| 234 | + value = data.coder_workspace.me.owner |
| 235 | + } |
| 236 | + labels { |
| 237 | + label = "coder.owner_id" |
| 238 | + value = data.coder_workspace.me.owner_id |
| 239 | + } |
| 240 | + labels { |
| 241 | + label = "coder.workspace_id" |
| 242 | + value = data.coder_workspace.me.id |
| 243 | + } |
| 244 | + labels { |
| 245 | + label = "coder.workspace_name" |
| 246 | + value = data.coder_workspace.me.name |
| 247 | + } |
| 248 | +} |
0 commit comments