Skip to content

Commit 8353aa4

Browse files
mtojekpull[bot]
authored andcommitted
docs: describe dynamic options and locals (#9429)
1 parent a6d7d8b commit 8353aa4

File tree

3 files changed

+199
-0
lines changed

3 files changed

+199
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
---
2+
name: Sample Template with Dynamic Parameter Options
3+
description: Review the sample template and introduce dynamic parameter options to your template
4+
tags: [local, docker, parameters]
5+
icon: /icon/docker.png
6+
---
7+
8+
# Overview
9+
10+
This Coder template presents use of [dynamic](https://developer.hashicorp.com/terraform/language/expressions/dynamic-blocks) [parameter options](https://coder.com/docs/v2/latest/templates/parameters#options) and Terraform [locals](https://developer.hashicorp.com/terraform/language/values/locals).
11+
12+
## Use case
13+
14+
The Coder template makes use of Docker containers to provide workspace users with programming language SDKs like Go, Java, and more.
15+
The template administrator wants to make sure that only certain versions of these programming environments are available,
16+
without allowing users to manually change them.
17+
18+
Workspace users should simply choose the programming environment they prefer. When the template admin upgrades SDK versions,
19+
during the next workspace update the chosen environment will automatically be upgraded to the latest version without causing any disruption or prompts.
20+
21+
The references to Docker images are represented by Terraform variables, and they're passed via the configuration file like this:
22+
23+
```yaml
24+
go_image: "bitnami/golang:1.20-debian-11"
25+
java_image: "bitnami/java:1.8-debian-11
26+
```
27+
28+
The template admin needs to update image references, publish a new version of the template, and then either handle workspace updates on their own or let workspace users take care of it.
29+
30+
## Development
31+
32+
Update the template and push it using the following command:
33+
34+
```bash
35+
./scripts/coder-dev.sh templates push examples-parameters-dynamic-options \
36+
-d examples/parameters-dynamic-options \
37+
--variables-file examples/parameters-dynamic-options/variables.yml \
38+
--create \
39+
-y
40+
```
+157
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,157 @@
1+
terraform {
2+
required_providers {
3+
coder = {
4+
source = "coder/coder"
5+
version = "0.11.1"
6+
}
7+
docker = {
8+
source = "kreuzwerker/docker"
9+
version = "~> 2.22"
10+
}
11+
}
12+
}
13+
14+
variable "go_image" {
15+
description = "Go SDK image reference"
16+
type = string
17+
}
18+
19+
variable "java_image" {
20+
description = "Java image reference."
21+
type = string
22+
}
23+
24+
locals {
25+
username = data.coder_workspace.me.owner
26+
27+
images = {
28+
"go" = var.go_image,
29+
"java" = var.java_image,
30+
}
31+
}
32+
33+
data "coder_provisioner" "me" {
34+
}
35+
36+
data "coder_workspace" "me" {
37+
}
38+
39+
data "coder_parameter" "container_image" {
40+
name = "container_image"
41+
display_name = "Workspace container image"
42+
description = "Select the container image for your development environment."
43+
default = "java"
44+
mutable = true
45+
46+
dynamic "option" {
47+
for_each = [for k in keys(local.images) : { name = k, value = lower(k) }]
48+
content {
49+
name = option.value.name
50+
value = option.value.value
51+
}
52+
}
53+
}
54+
55+
resource "coder_agent" "main" {
56+
arch = data.coder_provisioner.me.arch
57+
os = "linux"
58+
startup_script = <<EOF
59+
#!/bin/sh
60+
# install and start code-server
61+
curl -fsSL https://code-server.dev/install.sh | sh -s -- --version 4.8.3
62+
code-server --auth none --port 13337
63+
EOF
64+
65+
env = {
66+
GIT_AUTHOR_NAME = "${data.coder_workspace.me.owner}"
67+
GIT_COMMITTER_NAME = "${data.coder_workspace.me.owner}"
68+
GIT_AUTHOR_EMAIL = "${data.coder_workspace.me.owner_email}"
69+
GIT_COMMITTER_EMAIL = "${data.coder_workspace.me.owner_email}"
70+
}
71+
}
72+
73+
resource "coder_app" "code-server" {
74+
agent_id = coder_agent.main.id
75+
slug = "code-server"
76+
display_name = "code-server"
77+
url = "http://localhost:13337/?folder=/home/${local.username}"
78+
icon = "/icon/code.svg"
79+
subdomain = false
80+
share = "owner"
81+
82+
healthcheck {
83+
url = "http://localhost:13337/healthz"
84+
interval = 5
85+
threshold = 6
86+
}
87+
}
88+
89+
resource "docker_volume" "home_volume" {
90+
name = "coder-${data.coder_workspace.me.id}-home"
91+
lifecycle {
92+
ignore_changes = all
93+
}
94+
labels {
95+
label = "coder.owner"
96+
value = data.coder_workspace.me.owner
97+
}
98+
labels {
99+
label = "coder.owner_id"
100+
value = data.coder_workspace.me.owner_id
101+
}
102+
labels {
103+
label = "coder.workspace_id"
104+
value = data.coder_workspace.me.id
105+
}
106+
labels {
107+
label = "coder.workspace_name_at_creation"
108+
value = data.coder_workspace.me.name
109+
}
110+
}
111+
112+
resource "coder_metadata" "home_info" {
113+
resource_id = docker_volume.home_volume.id
114+
115+
item {
116+
key = "size"
117+
value = "5 GiB"
118+
}
119+
}
120+
121+
resource "docker_container" "workspace" {
122+
count = data.coder_workspace.me.start_count
123+
image = local.images[data.coder_parameter.container_image.value]
124+
name = "coder-${data.coder_workspace.me.owner}-${lower(data.coder_workspace.me.name)}"
125+
hostname = data.coder_workspace.me.name
126+
entrypoint = ["sh", "-c", replace(coder_agent.main.init_script, "/localhost|127\\.0\\.0\\.1/", "host.docker.internal")]
127+
env = [
128+
"CODER_AGENT_TOKEN=${coder_agent.main.token}",
129+
"CODER_PARAMETER_CONTAINER_IMAGE=${local.images[data.coder_parameter.container_image.value]}"
130+
]
131+
host {
132+
host = "host.docker.internal"
133+
ip = "host-gateway"
134+
}
135+
volumes {
136+
container_path = "/home/${local.username}"
137+
volume_name = docker_volume.home_volume.name
138+
read_only = false
139+
}
140+
141+
labels {
142+
label = "coder.owner"
143+
value = data.coder_workspace.me.owner
144+
}
145+
labels {
146+
label = "coder.owner_id"
147+
value = data.coder_workspace.me.owner_id
148+
}
149+
labels {
150+
label = "coder.workspace_id"
151+
value = data.coder_workspace.me.id
152+
}
153+
labels {
154+
label = "coder.workspace_name"
155+
value = data.coder_workspace.me.name
156+
}
157+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
go_image: "bitnami/golang:1.20-debian-11"
2+
java_image: "bitnami/java:1.8-debian-11"

0 commit comments

Comments
 (0)