-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.tf
More file actions
376 lines (331 loc) · 12.5 KB
/
main.tf
File metadata and controls
376 lines (331 loc) · 12.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
terraform {
required_providers {
coder = {
source = "coder/coder"
version = ">=2.4.0"
}
docker = {
source = "kreuzwerker/docker"
}
}
}
data "coder_provisioner" "me" {
}
data "coder_workspace" "me" {
}
data "coder_workspace_owner" "me" {}
locals {
coder_app_subdomain = false
coder_app_slug = "vscode-web"
is_template = data.coder_parameter.workspace_source.value == "template"
code_dir = local.is_template ? "code" : module.git_clone_source.folder_name
vscode_server_base_path = local.coder_app_subdomain ? "" : "/@${data.coder_workspace_owner.me.name}/${data.coder_workspace.me.name}/apps/${local.coder_app_slug}/"
vscode_server_args = "--server-data-dir /host/vscode/server ${local.vscode_server_base_path != "" ? format("%s %s","--server-base-path", local.vscode_server_base_path) : ""}"
devcontainer_flag_rebuid = data.coder_parameter.force_rebuild.value ? "--remove-existing-container" : ""
# git module has a bug that appens / infront of folder name if doesn't exist.
project_dir = local.is_template ? "$HOME/${local.code_dir}" : replace(module.git_clone_source.folder_name, "/^~/", "$HOME")
vscode_url_folder_arg = "/workspaces/${local.code_dir}"
devcontainer_poststart_script = join("\\n", [
"export VSCODE_CLI_USE_FILE_KEYCHAIN=1 && VSCODE_CLI_DISABLE_KEYCHAIN_ENCRYPT=1",
"sudo mkdir -p /host/vscode/server /workspaces && sudo chmod 777 -R /host",
"ln -fs /host/vscode ~/.vscode",
"ln -fs /parent/ssh ~/.ssh",
"sudo chown |dlr|(whoami) -R ~/.ssh /workspaces",
"export |dlr|(xargs < /parent/env) && if [ ! -d /workspaces/|dlr|GIT_PROJ_DIR ]; then ln -s |dlr|REMOTE_WS_DIR /workspaces/|dlr|GIT_PROJ_DIR; fi"
])
script_template_devc_up = <<EOF
if [ -f "${local.project_dir}/.devcontainer/devcontainer.json" ]; then
echo "devcontainer exists."
else
devcontainer templates apply --workspace-folder=${local.project_dir} ${try(data.coder_parameter.template_image[0].value, "")}
fi
EOF
devcontainer_templates = flatten([
for item in jsondecode(data.http.devcontainer_template_list.response_body): [
for imgVar in coalesce(item.imageVariant, ["null"]): {
id: item.id
name: imgVar == "null" ? item.id : "${item.id}:${imgVar}"
imageVariant: imgVar
template_args: imgVar == "null" ? "" : "--template-args '{ \"imageVariant\": \"${imgVar}\" }'"
}
]
])
}
variable "docker_config" {
type = string
description = "Docker config. Typically contains registry credential and other information"
default = ""
sensitive = true
}
variable "docker_socket" {
default = ""
description = "Docker socket URI"
type = string
}
provider "docker" {
host = var.docker_socket != "" ? var.docker_socket : null
ssh_opts = ["-o", "StrictHostKeyChecking=no", "-o", "UserKnownHostsFile=/dev/null"]
disable_docker_daemon_check = true
}
data "http" "devcontainer_template_list" {
url = "https://gist.githubusercontent.com/inabhi9/0473f0fce9d15576f1e97dae28d395d6/raw/output.json"
}
data "coder_parameter" "workspace_source" {
name = "Workspace Source"
type = "string"
mutable = false
order = 2
default = "git"
option {
name = "Git repository"
value = "git"
}
option {
name = "Template"
value = "template"
}
}
data "coder_parameter" "custom_repo_url" {
count = data.coder_parameter.workspace_source.value != "git" ? 0 : 1
name = "custom_repo"
display_name = "Git Repository URL"
order = 3
description = "Git Repository URL, see [awesome-devcontainers](https://github.com/manekinekko/awesome-devcontainers)."
mutable = false
}
data "coder_parameter" "devcontainer_repo_url" {
count = data.coder_parameter.workspace_source.value == "git" ? 1 : 0
name = "devcontainer_repo"
display_name = "Devcontainer repository URL"
order = 3
default = ""
description = "A separate repository to locate .devcontainer directory. When provided, it's cloned to temporary directory and provided during devcontainer build."
mutable = true
}
data coder_parameter "template_image" {
count = data.coder_parameter.workspace_source.value != "template" ? 0 : 1
name = "Template Image"
type = "string"
mutable = false
order = 3
form_type = "dropdown"
dynamic "option" {
for_each = local.devcontainer_templates
content {
name = option.value.name
value = "--template-id ghcr.io/devcontainers/templates/${option.value.id}:latest ${option.value.template_args}"
}
}
}
data "coder_parameter" "force_rebuild" {
name = "Force rebuild"
type = "bool"
description = "Rebuild the devcontainer rather than use the cached one."
mutable = true
default = false
ephemeral = true
}
module "git_clone_source" {
source = "registry.coder.com/coder/git-clone/coder"
agent_id = coder_agent.main.id
url = try(data.coder_parameter.custom_repo_url[0].value, "https://github.com/sarcasticadmin/empty-repo.git")
base_dir = local.is_template ? "/tmp" : "~"
# This ensures that the latest non-breaking version of the module gets
# downloaded, you can also pin the module version to prevent breaking
# changes in production.
version = "~> 1.0"
}
module "git_clone_devcontainer" {
version = "~> 1.0"
count = length(data.coder_parameter.devcontainer_repo_url) > 0 && data.coder_parameter.devcontainer_repo_url[0].value != "" ? 1 : 0
source = "registry.coder.com/coder/git-clone/coder"
agent_id = coder_agent.main.id
url = data.coder_parameter.devcontainer_repo_url[0].value
base_dir = "~"
post_clone_script = <<EOF
#!/bin/sh
cp -r ${module.git_clone_devcontainer[0].repo_dir}/.devcontainer ${module.git_clone_source.repo_dir}
git -C ${module.git_clone_source.repo_dir} config --replace-all core.excludesFile ${module.git_clone_source.repo_dir}/.git/.exclude
echo .devcontainer > ${module.git_clone_source.repo_dir}/.git/info/exclude
rm -fr ${module.git_clone_devcontainer[0].repo_dir}
EOF
}
module "devcontainers_cli" {
source = "registry.coder.com/coder/devcontainers-cli/coder"
version = "1.0.32"
agent_id = coder_agent.main.id
}
resource "coder_agent" "main" {
arch = "amd64"
os = "linux"
startup_script_behavior = "blocking"
startup_script = <<EOF
#!/bin/sh
set -e
sudo service docker start
git config --global user.name "${data.coder_workspace_owner.me.name}"
git config --global user.email "${data.coder_workspace_owner.me.email}"
mkdir -p ~/.ssh ~/parent ${local.project_dir}
echo "access_path: ${local.vscode_server_base_path}. Prodject dir: ${local.project_dir}, ${local.is_template}"
printf "Host *\n\tStrictHostKeyChecking no\n" > ~/.ssh/config
curl -s -H "Coder-Session-Token: $CODER_AGENT_TOKEN" $${CODER_AGENT_URL}api/v2/workspaceagents/me/gitsshkey \
| jq -r '.private_key' > ~/.ssh/id_ed25519 \
&& chmod 600 $HOME/.ssh/id_ed25519
while ! devcontainer --version > /dev/null || pgrep git > /dev/null || ! docker info > /dev/null
do
echo "Waiting for everything to be ready..."
sleep 2
done
${local.script_template_devc_up}
devcontainer up ${local.devcontainer_flag_rebuid} --workspace-folder=${local.project_dir} \
--mount=type=bind,source=/host,target=/host \
--mount=type=bind,source=$HOME/parent,target=/parent \
--mount=type=bind,source=$HOME/.ssh,target=/parent/ssh \
--mount=type=bind,source=$HOME/.gitconfig,target=/etc/gitconfig \
--additional-features='{"ghcr.io/inabhi9/devcontainer-features/msvscode-server:1.0.12": {"vscodeServerFlags": "${local.vscode_server_args}", "postContainerStartCommand": "${local.devcontainer_poststart_script}"}}' | tee $HOME/parent/result.json
# Workaround to open directory directly by vs code web
REMOTE_WS_DIR=$(cat $HOME/parent/result.json | jq -r .remoteWorkspaceFolder)
GIT_PROJ_DIR=${local.code_dir}
echo REMOTE_WS_DIR=$REMOTE_WS_DIR\\nGIT_PROJ_DIR=$GIT_PROJ_DIR > ~/parent/env
# Workaround for the first execution
devcontainer exec --workspace-folder=${local.project_dir} /usr/local/bin/vscode-server-entrypoint >/dev/null 2>&1 &
EOF
env = {
GIT_AUTHOR_NAME = "${data.coder_workspace_owner.me.name}"
GIT_COMMITTER_NAME = "${data.coder_workspace_owner.me.name}"
GIT_AUTHOR_EMAIL = "${data.coder_workspace_owner.me.email}"
GIT_COMMITTER_EMAIL = "${data.coder_workspace_owner.me.email}"
}
display_apps {
vscode = false
vscode_insiders = false
web_terminal = true
ssh_helper = false
port_forwarding_helper = false
}
}
resource "coder_app" "code_server" {
agent_id = coder_agent.main.id
slug = local.coder_app_slug
display_name = "VS Code Web"
icon = "/icon/code.svg"
url = "http://127.0.0.1:13338${local.vscode_server_base_path}?folder=${local.vscode_url_folder_arg}"
subdomain = local.coder_app_subdomain
share = "owner"
healthcheck {
url = "http://127.0.0.1:13338/healthz"
interval = 2
threshold = 100
}
}
resource "docker_volume" "workspaces" {
name = "coder-${data.coder_workspace.me.id}"
# Protect the volume from being deleted due to changes in attributes.
lifecycle {
ignore_changes = all
}
# Add labels in Docker to keep track of orphan resources.
labels {
label = "coder.owner"
value = data.coder_workspace_owner.me.name
}
labels {
label = "coder.owner_id"
value = data.coder_workspace_owner.me.id
}
labels {
label = "coder.workspace_id"
value = data.coder_workspace.me.id
}
# This field becomes outdated if the workspace is renamed but can
# be useful for debugging or cleaning out dangling volumes.
labels {
label = "coder.workspace_name_at_creation"
value = data.coder_workspace.me.name
}
}
resource "docker_volume" "docker_data_root" {
name = "coder-docker-data-${data.coder_workspace.me.id}"
# Protect the volume from being deleted due to changes in attributes.
lifecycle {
ignore_changes = all
}
# Add labels in Docker to keep track of orphan resources.
labels {
label = "coder.owner"
value = data.coder_workspace_owner.me.name
}
labels {
label = "coder.owner_id"
value = data.coder_workspace_owner.me.id
}
labels {
label = "coder.workspace_id"
value = data.coder_workspace.me.id
}
# This field becomes outdated if the workspace is renamed but can
# be useful for debugging or cleaning out dangling volumes.
labels {
label = "coder.workspace_name_at_creation"
value = data.coder_workspace.me.name
}
}
resource "docker_container" "workspace" {
runtime = "sysbox-runc"
count = data.coder_workspace.me.start_count
# Find the latest version here:
# https://github.com/coder/envbuilder/tags
image = "codercom/enterprise-node:ubuntu"
# Uses lower() to avoid Docker restriction on container names.
name = "coder-${data.coder_workspace_owner.me.name}-${lower(data.coder_workspace.me.name)}"
# Hostname makes the shell more user friendly: coder@my-workspace:~$
hostname = data.coder_workspace.me.name
# Use the docker gateway if the access URL is 127.0.0.1
entrypoint = ["sh", "-c", replace(coder_agent.main.init_script, "/localhost|127\\.0\\.0\\.1/", "host.docker.internal")]
env = [
"CODER_AGENT_TOKEN=${coder_agent.main.token}",
"GIT_URL=${try(data.coder_parameter.custom_repo_url[0].value, "")}",
"INIT_SCRIPT=${replace(coder_agent.main.init_script, "/localhost|127\\.0\\.0\\.1/", "host.docker.internal")}",
"DOCKER_CONFIG_JSON=${var.docker_config}"
]
host {
host = "host.docker.internal"
ip = "host-gateway"
}
volumes {
container_path = "/home/coder"
volume_name = docker_volume.workspaces.name
read_only = false
}
volumes {
container_path = "/var/lib/docker"
volume_name = docker_volume.docker_data_root.name
read_only = false
}
volumes {
container_path = "/host"
host_path = "/ddata/${lower(data.coder_workspace_owner.me.id)}"
}
capabilities {
# --cap-add=NET_ADMIN and --cap-add=NET_RAW are needed for iptables
# to forward vscode server port
add = ["NET_RAW", "NET_ADMIN"]
}
# Add labels in Docker to keep track of orphan resources.
labels {
label = "coder.owner"
value = data.coder_workspace_owner.me.name
}
labels {
label = "coder.owner_id"
value = data.coder_workspace_owner.me.id
}
labels {
label = "coder.workspace_id"
value = data.coder_workspace.me.id
}
labels {
label = "coder.workspace_name"
value = data.coder_workspace.me.name
}
}