-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathmain.tf
152 lines (130 loc) · 4.35 KB
/
main.tf
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
terraform {
required_providers {
coder = {
source = "coder/coder"
version = "0.4.2"
}
docker = {
source = "kreuzwerker/docker"
version = "~> 2.16.0"
}
}
}
# Admin parameters
variable "step1_docker_host_warning" {
description = <<-EOF
Is Docker running on the Coder host?
This template will use the Docker socket present on
the Coder host, which is not necessarily your local machine.
You can specify a different host in the template file and
surpress this warning.
EOF
validation {
condition = contains(["Continue using /var/run/docker.sock on the Coder host"], var.step1_docker_host_warning)
error_message = "Cancelling template create."
}
sensitive = true
}
variable "step2_arch" {
description = "arch: What architecture is your Docker host on?"
validation {
condition = contains(["amd64", "arm64", "armv7"], var.step2_arch)
error_message = "Value must be amd64, arm64, or armv7."
}
sensitive = true
}
variable "step3_OS" {
description = <<-EOF
What operating system is your Coder host on?
EOF
validation {
condition = contains(["MacOS", "Windows", "Linux"], var.step3_OS)
error_message = "Value must be MacOS, Windows, or Linux."
}
sensitive = true
}
# https://ppswi.us/noVNC/app/images/icons/novnc-192x192.png
provider "docker" {
host = var.step3_OS == "Windows" ? "npipe:////.//pipe//docker_engine" : "unix:///var/run/docker.sock"
}
provider "coder" {
}
data "coder_workspace" "me" {
}
# Matlab
resource "coder_app" "novnc" {
agent_id = coder_agent.dev.id
name = "Matlab"
icon = "https://img.icons8.com/nolan/344/matlab.png"
url = "http://localhost:8888/@${data.coder_workspace.me.owner}/${data.coder_workspace.me.name}/apps/Matlab"
}
# code-server
resource "coder_app" "code-server" {
agent_id = coder_agent.dev.id
name = "code-server"
icon = "https://cdn.icon-icons.com/icons2/2107/PNG/512/file_type_vscode_icon_130084.png"
url = "http://localhost:13337"
relative_path = true
}
resource "coder_agent" "dev" {
arch = var.step2_arch
os = "linux"
startup_script = <<EOT
#!/bin/bash
set -euo pipefail
# start code-server
code-server --auth none --port 13337 &
# start Matlab
MWI_BASE_URL="/@${data.coder_workspace.me.owner}/${data.coder_workspace.me.name}/apps/Matlab" matlab-proxy-app &
EOT
}
variable "docker_image" {
description = "What Docker image would you like to use for your workspace?"
default = "matlab-base"
# List of images available for the user to choose from.
# Delete this condition to give users free text input.
validation {
condition = contains(["matlab-base"], var.docker_image)
error_message = "Invalid Docker image!"
}
# Prevents admin errors when the image is not found
validation {
condition = fileexists("images/${var.docker_image}.Dockerfile")
error_message = "Invalid Docker image. The file does not exist in the images directory."
}
}
resource "docker_volume" "home_volume" {
name = "coder-${data.coder_workspace.me.owner}-${lower(data.coder_workspace.me.name)}-root"
}
resource "docker_image" "coder_image" {
name = "coder-base-${data.coder_workspace.me.owner}-${lower(data.coder_workspace.me.name)}"
build {
path = "./images/"
dockerfile = "${var.docker_image}.Dockerfile"
tag = ["coder-${var.docker_image}:v0.3"]
}
# Keep alive for other workspaces to use upon deletion
keep_locally = true
}
resource "docker_container" "workspace" {
count = data.coder_workspace.me.start_count
image = docker_image.coder_image.latest
# Uses lower() to avoid Docker restriction on container names.
name = "coder-${data.coder_workspace.me.owner}-${lower(data.coder_workspace.me.name)}"
# Hostname makes the shell more user friendly: coder@my-workspace:~$
hostname = lower(data.coder_workspace.me.name)
dns = ["1.1.1.1"]
# Use the docker gateway if the access URL is 127.0.0.1
entrypoint = ["sh", "-c", replace(coder_agent.dev.init_script, "127.0.0.1", "host.docker.internal")]
# command = ["/bin/sh", "-ec", "sleep infinity"]
env = ["CODER_AGENT_TOKEN=${coder_agent.dev.token}"]
host {
host = "host.docker.internal"
ip = "host-gateway"
}
volumes {
container_path = "/home/coder/"
volume_name = docker_volume.home_volume.name
read_only = false
}
}