|
| 1 | +terraform { |
| 2 | + required_providers { |
| 3 | + coder = { |
| 4 | + source = "coder/coder" |
| 5 | + version = "0.4.1" |
| 6 | + } |
| 7 | + docker = { |
| 8 | + source = "kreuzwerker/docker" |
| 9 | + version = "~> 2.16.0" |
| 10 | + } |
| 11 | + } |
| 12 | +} |
| 13 | + |
| 14 | +# Admin parameters |
| 15 | + |
| 16 | +# Comment this out if you are specifying a different docker |
| 17 | +# host on the "docker" provider below. |
| 18 | +variable "step1_docker_host_warning" { |
| 19 | + description = <<-EOF |
| 20 | + This template will use the Docker socket present on |
| 21 | + the Coder host, which is not necessarily your local machine. |
| 22 | +
|
| 23 | + You can specify a different host in the template file and |
| 24 | + surpress this warning. |
| 25 | + EOF |
| 26 | + validation { |
| 27 | + condition = contains(["Continue using /var/run/docker.sock on the Coder host"], var.step1_docker_host_warning) |
| 28 | + error_message = "Cancelling template create." |
| 29 | + } |
| 30 | + |
| 31 | + sensitive = true |
| 32 | +} |
| 33 | +variable "step2_arch" { |
| 34 | + description = <<-EOF |
| 35 | + arch: What architecture is your Docker host on? |
| 36 | +
|
| 37 | + note: codercom/enterprise-* images are only built for amd64 |
| 38 | + EOF |
| 39 | + |
| 40 | + validation { |
| 41 | + condition = contains(["amd64", "arm64", "armv7"], var.step2_arch) |
| 42 | + error_message = "Value must be amd64, arm64, or armv7." |
| 43 | + } |
| 44 | + sensitive = true |
| 45 | +} |
| 46 | +variable "step3_OS" { |
| 47 | + description = <<-EOF |
| 48 | + What operating system is your Coder host on? |
| 49 | + EOF |
| 50 | + |
| 51 | + validation { |
| 52 | + condition = contains(["MacOS", "Windows", "Linux"], var.step3_OS) |
| 53 | + error_message = "Value must be MacOS, Windows, or Linux." |
| 54 | + } |
| 55 | + sensitive = true |
| 56 | +} |
| 57 | + |
| 58 | +provider "docker" { |
| 59 | + host = var.step3_OS == "Windows" ? "npipe:////.//pipe//docker_engine" : "unix:///var/run/docker.sock" |
| 60 | +} |
| 61 | + |
| 62 | +provider "coder" { |
| 63 | +} |
| 64 | + |
| 65 | +data "coder_workspace" "me" { |
| 66 | +} |
| 67 | + |
| 68 | +variable "docker_image" { |
| 69 | + description = "Which Docker image would you like to use for your workspace?" |
| 70 | + # The codercom/enterprise-* images are only built for amd64 |
| 71 | + default = "codercom/enterprise-base:ubuntu" |
| 72 | + validation { |
| 73 | + condition = contains(["codercom/enterprise-base:ubuntu", "codercom/enterprise-node:ubuntu", "codercom/enterprise-intellij:ubuntu"], var.docker_image) |
| 74 | + error_message = "Invalid Docker image!" |
| 75 | + } |
| 76 | +} |
| 77 | + |
| 78 | +variable "dotfiles_uri" { |
| 79 | + description = <<-EOF |
| 80 | + Dotfiles repo URI (optional) |
| 81 | +
|
| 82 | + see https://dotfiles.github.io |
| 83 | + EOF |
| 84 | + # The codercom/enterprise-* images are only built for amd64 |
| 85 | + default = "" |
| 86 | +} |
| 87 | + |
| 88 | +resource "coder_agent" "dev" { |
| 89 | + arch = var.step2_arch |
| 90 | + os = "linux" |
| 91 | + startup_script = var.dotfiles_uri != "" ? "/tmp/tmp.coder*/coder dotfiles -y ${var.dotfiles_uri}" : null |
| 92 | +} |
| 93 | + |
| 94 | +resource "docker_volume" "home_volume" { |
| 95 | + name = "coder-${data.coder_workspace.me.owner}-${data.coder_workspace.me.name}-root" |
| 96 | +} |
| 97 | + |
| 98 | +resource "docker_container" "workspace" { |
| 99 | + count = data.coder_workspace.me.start_count |
| 100 | + image = var.docker_image |
| 101 | + # Uses lower() to avoid Docker restriction on container names. |
| 102 | + name = "coder-${data.coder_workspace.me.owner}-${lower(data.coder_workspace.me.name)}" |
| 103 | + # Hostname makes the shell more user friendly: coder@my-workspace:~$ |
| 104 | + hostname = lower(data.coder_workspace.me.name) |
| 105 | + dns = ["1.1.1.1"] |
| 106 | + # Use the docker gateway if the access URL is 127.0.0.1 |
| 107 | + command = ["sh", "-c", replace(coder_agent.dev.init_script, "127.0.0.1", "host.docker.internal")] |
| 108 | + env = ["CODER_AGENT_TOKEN=${coder_agent.dev.token}"] |
| 109 | + host { |
| 110 | + host = "host.docker.internal" |
| 111 | + ip = "host-gateway" |
| 112 | + } |
| 113 | + volumes { |
| 114 | + container_path = "/home/coder/" |
| 115 | + volume_name = docker_volume.home_volume.name |
| 116 | + read_only = false |
| 117 | + } |
| 118 | +} |
0 commit comments