Skip to content

Commit 967a4b0

Browse files
authored
feat: add example template using rich parameters (#9020)
1 parent 091c00b commit 967a4b0

File tree

3 files changed

+318
-0
lines changed

3 files changed

+318
-0
lines changed

examples/parameters/README.md

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
---
2+
name: Sample Template with Parameters
3+
description: Review the sample template and introduce parameters to your template
4+
tags: [local, docker, parameters]
5+
icon: /icon/docker.png
6+
---
7+
8+
# Overview
9+
10+
This Coder template presents various features of [rich parameters](https://coder.com/docs/v2/latest/templates/parameters), including types, validation constraints,
11+
mutability, ephemeral (one-time) parameters, etc.
12+
13+
## Development
14+
15+
Update the template and push it using the following command:
16+
17+
```bash
18+
./scripts/coder-dev.sh templates push examples-parameters -d examples/parameters --create
19+
```

examples/parameters/build/Dockerfile

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
FROM ubuntu
2+
3+
RUN apt-get update \
4+
&& apt-get install -y \
5+
curl \
6+
git \
7+
golang \
8+
sudo \
9+
vim \
10+
wget \
11+
&& rm -rf /var/lib/apt/lists/*
12+
13+
ARG USER=coder
14+
RUN useradd --groups sudo --no-create-home --shell /bin/bash ${USER} \
15+
&& echo "${USER} ALL=(ALL) NOPASSWD:ALL" >/etc/sudoers.d/${USER} \
16+
&& chmod 0440 /etc/sudoers.d/${USER}
17+
USER ${USER}
18+
WORKDIR /home/${USER}

examples/parameters/main.tf

+281
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,281 @@
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 = "~> 3.0.1"
10+
}
11+
}
12+
}
13+
14+
locals {
15+
username = data.coder_workspace.me.owner
16+
}
17+
18+
data "coder_provisioner" "me" {
19+
}
20+
21+
provider "docker" {
22+
}
23+
24+
data "coder_workspace" "me" {
25+
}
26+
27+
resource "coder_agent" "main" {
28+
arch = data.coder_provisioner.me.arch
29+
os = "linux"
30+
startup_script_timeout = 180
31+
startup_script = <<-EOT
32+
set -e
33+
34+
# install and start code-server
35+
curl -fsSL https://code-server.dev/install.sh | sh -s -- --method=standalone --prefix=/tmp/code-server --version 4.11.0
36+
/tmp/code-server/bin/code-server --auth none --port 13337 >/tmp/code-server.log 2>&1 &
37+
EOT
38+
}
39+
40+
resource "coder_app" "code-server" {
41+
agent_id = coder_agent.main.id
42+
slug = "code-server"
43+
display_name = "code-server"
44+
url = "http://localhost:13337/?folder=/home/${local.username}"
45+
icon = "/icon/code.svg"
46+
subdomain = false
47+
share = "owner"
48+
49+
healthcheck {
50+
url = "http://localhost:13337/healthz"
51+
interval = 5
52+
threshold = 6
53+
}
54+
}
55+
56+
resource "docker_volume" "home_volume" {
57+
name = "coder-${data.coder_workspace.me.id}-home"
58+
# Protect the volume from being deleted due to changes in attributes.
59+
lifecycle {
60+
ignore_changes = all
61+
}
62+
# Add labels in Docker to keep track of orphan resources.
63+
labels {
64+
label = "coder.owner"
65+
value = data.coder_workspace.me.owner
66+
}
67+
labels {
68+
label = "coder.owner_id"
69+
value = data.coder_workspace.me.owner_id
70+
}
71+
labels {
72+
label = "coder.workspace_id"
73+
value = data.coder_workspace.me.id
74+
}
75+
# This field becomes outdated if the workspace is renamed but can
76+
# be useful for debugging or cleaning out dangling volumes.
77+
labels {
78+
label = "coder.workspace_name_at_creation"
79+
value = data.coder_workspace.me.name
80+
}
81+
}
82+
83+
resource "docker_image" "main" {
84+
name = "coder-${data.coder_workspace.me.id}"
85+
build {
86+
context = "./build"
87+
build_args = {
88+
USER = local.username
89+
}
90+
}
91+
triggers = {
92+
dir_sha1 = sha1(join("", [for f in fileset(path.module, "build/*") : filesha1(f)]))
93+
}
94+
}
95+
96+
resource "docker_container" "workspace" {
97+
count = data.coder_workspace.me.start_count
98+
image = docker_image.main.name
99+
# Uses lower() to avoid Docker restriction on container names.
100+
name = "coder-${data.coder_workspace.me.owner}-${lower(data.coder_workspace.me.name)}"
101+
# Hostname makes the shell more user friendly: coder@my-workspace:~$
102+
hostname = data.coder_workspace.me.name
103+
# Use the docker gateway if the access URL is 127.0.0.1
104+
entrypoint = ["sh", "-c", replace(coder_agent.main.init_script, "/localhost|127\\.0\\.0\\.1/", "host.docker.internal")]
105+
env = ["CODER_AGENT_TOKEN=${coder_agent.main.token}"]
106+
host {
107+
host = "host.docker.internal"
108+
ip = "host-gateway"
109+
}
110+
volumes {
111+
container_path = "/home/${local.username}"
112+
volume_name = docker_volume.home_volume.name
113+
read_only = false
114+
}
115+
# Add labels in Docker to keep track of orphan resources.
116+
labels {
117+
label = "coder.owner"
118+
value = data.coder_workspace.me.owner
119+
}
120+
labels {
121+
label = "coder.owner_id"
122+
value = data.coder_workspace.me.owner_id
123+
}
124+
labels {
125+
label = "coder.workspace_id"
126+
value = data.coder_workspace.me.id
127+
}
128+
labels {
129+
label = "coder.workspace_name"
130+
value = data.coder_workspace.me.name
131+
}
132+
}
133+
134+
// Rich parameters
135+
// See: https://coder.com/docs/v2/latest/templates/parameters
136+
137+
data "coder_parameter" "project_id" {
138+
name = "project_id"
139+
display_name = "My Project ID"
140+
icon = "/emojis/1fab5.png"
141+
description = "Specify the project ID to deploy in workspace."
142+
default = "A1B2C3"
143+
mutable = true
144+
validation {
145+
regex = "^[A-Z0-9]+$"
146+
error = "Project ID is incorrect"
147+
}
148+
149+
order = 1
150+
}
151+
152+
data "coder_parameter" "region" {
153+
name = "region"
154+
display_name = "Region"
155+
icon = "/emojis/1f30e.png"
156+
description = "Select the region in which you would like to deploy your workspace."
157+
default = "eu-helsinki"
158+
option {
159+
icon = "/emojis/1f1fa-1f1f8.png"
160+
name = "Pittsburgh"
161+
description = "Pittsburgh is a city in the Commonwealth of Pennsylvania and the county seat of Allegheny County."
162+
value = "us-pittsburgh"
163+
}
164+
option {
165+
icon = "/emojis/1f1eb-1f1ee.png"
166+
name = "Helsinki"
167+
description = "Helsinki, the capital city of Finland, is renowned for its vibrant cultural scene, stunning waterfront architecture, and a harmonious blend of modernity and natural beauty."
168+
value = "eu-helsinki"
169+
}
170+
option {
171+
icon = "/emojis/1f1e6-1f1fa.png"
172+
name = "Sydney"
173+
description = "Sydney, the largest city in Australia, captivates with its iconic Sydney Opera House, picturesque harbor, and diverse neighborhoods, making it a captivating blend of urban sophistication and coastal charm."
174+
value = "ap-sydney"
175+
}
176+
177+
order = 1
178+
}
179+
180+
data "coder_parameter" "apps_dir" {
181+
name = "apps_dir"
182+
display_name = "Apps Directory"
183+
icon = "/emojis/1f9ba.png"
184+
type = "string"
185+
description = "Specify the directory to install project applications and tools."
186+
default = "/var/apps"
187+
188+
order = 2
189+
}
190+
191+
data "coder_parameter" "worker_instances" {
192+
name = "worker_instances"
193+
display_name = "Worker Instances"
194+
icon = "/emojis/2697.png"
195+
type = "number"
196+
description = "Specify the number of worker instances to spawn."
197+
default = "3"
198+
mutable = true
199+
validation {
200+
min = 3
201+
max = 12
202+
monotonic = "increasing"
203+
}
204+
order = 2
205+
}
206+
207+
data "coder_parameter" "security_groups" {
208+
name = "security_groups"
209+
display_name = "Security Groups"
210+
icon = "/emojis/26f4.png"
211+
type = "list(string)"
212+
description = "Select relevant security groups."
213+
mutable = true
214+
default = jsonencode([
215+
"Web Server Security Group",
216+
"Database Security Group",
217+
"Backend Security Group"
218+
])
219+
order = 2
220+
}
221+
222+
data "coder_parameter" "docker_image" {
223+
name = "docker_image"
224+
display_name = "Docker Image"
225+
mutable = true
226+
type = "string"
227+
description = "Docker image for the development container"
228+
default = "ghcr.io/coder/coder-preview:main"
229+
230+
order = 3
231+
}
232+
233+
data "coder_parameter" "command_line_args" {
234+
name = "command_line_args"
235+
display_name = "Extra command line args"
236+
type = "string"
237+
default = ""
238+
description = "Provide extra command line args for the startup script."
239+
mutable = true
240+
order = 80
241+
}
242+
243+
data "coder_parameter" "enable_monitoring" {
244+
name = "enable_monitoring"
245+
display_name = "Enable Workspace Monitoring"
246+
type = "bool"
247+
description = "This monitoring functionality empowers you to closely track the health and resource utilization of your instance in real-time."
248+
mutable = true
249+
order = 90
250+
}
251+
252+
// Build options (ephemeral parameters)
253+
// See: https://coder.com/docs/v2/latest/templates/parameters#ephemeral-parameters
254+
255+
data "coder_parameter" "pause-startup" {
256+
name = "pause-startup"
257+
display_name = "Pause startup script"
258+
type = "number"
259+
description = "Pause the startup script (seconds)"
260+
default = "1"
261+
mutable = true
262+
ephemeral = true
263+
validation {
264+
min = 0
265+
max = 300
266+
}
267+
268+
order = 4
269+
}
270+
271+
data "coder_parameter" "force-rebuild" {
272+
name = "force-rebuild"
273+
display_name = "Force rebuild project"
274+
type = "bool"
275+
description = "Rebuild the workspace project"
276+
default = "false"
277+
mutable = true
278+
ephemeral = true
279+
280+
order = 4
281+
}

0 commit comments

Comments
 (0)