8000 add terraform modules · coder/coder@18a498d · GitHub
[go: up one dir, main page]

Skip to content

Commit 18a498d

Browse files
committed
add terraform modules
1 parent d3b4b78 commit 18a498d

File tree

4 files changed

+259
-7
lines changed

4 files changed

+259
-7
lines changed
Lines changed: 199 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,199 @@
1+
# Reusing template code
2+
3+
To reuse code across different Coder templates, such as common scripts or
4+
resource definitions, we suggest using
5+
[Terraform Modules](https://developer.hashicorp.com/terraform/language/modules).
6+
7+
You can store these modules externally from your Coder deployment, like in a git
8+
repository or a Terraform registry. This example shows how to reference a module
9+
from your template:
10+
11+
```hcl
12+
data "coder_workspace" "me" {}
13+
14+
module "coder-base" {
15+
source = "github.com/my-organization/coder-base"
16+
17+
# Modules take in variables and can provision infrastructure
18+
vpc_name = "devex-3"
19+
subnet_tags = { "name": data.coder_workspace.me.name }
20+
code_server_version = 4.14.1
21+
}
22+
23+
resource "coder_agent" "dev" {
24+
# Modules can provide outputs, such as helper scripts
25+
startup_script=<<EOF
26+
#!/bin/sh
27+
${module.coder-base.code_server_install_command}
28+
EOF
29+
}
30+
```
31+
32+
Learn more about
33+
[creating modules](https://developer.hashicorp.com/terraform/language/modules)
34+
and
35+
[module sources](https://developer.hashicorp.com/terraform/language/modules/sources)
36+
in the Terraform documentation.
37+
38+
## Coder modules
39+
40+
Coder publishes plenty of modules that can be used to simplify some common tasks
41+
across templates. Some of the modules we publish are,
42+
43+
1. [`code-server`](https://registry.coder.com/modules/code-server) and
44+
[`vscode-web`](https://registry.coder.com/modules/vscode-web)
45+
2. [`git-clone`](https://registry.coder.com/modules/git-clone)
46+
3. [`dotfiles`](https://registry.coder.com/modules/dotfiles)
47+
4. [`jetbrains-gateway`](https://registry.coder.com/modules/jetbrains-gateway)
48+
5. [`jfrog-oauth`](https://registry.coder.com/modules/jfrog-oauth) and
49+
[`jfrog-token`](https://registry.coder.com/modules/jfrog-token)
50+
6. [`vault-github`](https://registry.coder.com/modules/vault-github)
51+
52+
For a full list of available modules please check
53+
[Coder module registry](https://registry.coder.com/modules).
54+
55+
## Offline installations
56+
57+
In offline and restricted deploymnets, there are 2 ways to fetch modules.
58+
59+
1. Artifactory
60+
2. Private git repository
61+
62+
### Artifactory
63+
64+
Air gapped users can clone the [coder/modules](htpps://github.com/coder/modules)
65+
repo and publish a
66+
[local terraform module repository](https://jfrog.com/help/r/jfrog-artifactory-documentation/set-up-a-terraform-module/provider-registry)
67+
to resolve modules via [Artifactory](https://jfrog.com/artifactory/).
68+
69+
1. Create a local-terraform-repository with name `coder-modules-local`
70+
2. Create a virtual repository with name `tf`
71+
3. Follow the below instructions to publish coder modules to Artifactory
72+
73+
```shell
74+
git clone https://github.com/coder/modules
75+
cd modules
76+
jf tfc
77+
jf tf p --namespace="coder" --provider="coder" --tag="1.0.0"
78+
```
79+
80+
4. Generate a token with access to the `tf` repo and set an `ENV` variable
81+
`TF_TOKEN_example.jfrog.io="XXXXXXXXXXXXXXX"` on the Coder provisioner.
82+
5. Create a file `.terraformrc` with following content and mount at
83+
`/home/coder/.terraformrc` within the Coder provisioner.
84+
85+
```hcl
86+
provider_installation {
87+
direct {
88+
exclude = ["registry.terraform.io/*/*"]
89+
}
90+
network_mirror {
91+
url = "https://example.jfrog.io/artifactory/api/terraform/tf/providers/"
92+
}
93+
}
94+
```
95+
96+
6. Update module source as,
97+
98+
```hcl
99+
module "module-name" {
100+
source = "https://example.jfrog.io/tf__coder/module-name/coder"
101+
version = "1.0.0"
102+
agent_id = coder_agent.example.id
103+
...
104+
}
105+
```
106+
107+
> Do not forget to replace example.jfrog.io with your Artifactory URL
108+
109+
Based on the instructions
110+
[here](https://jfrog.com/blog/tour-terraform-registries-in-artifactory/).
111+
112+
#### Example template
113+
114+
We have an example template [here](../../examples/jfrog/remote/main.tf) that
115+
uses our [JFrog Docker](../../examples/jfrog/docker/main.tf) template as the
116+
underlying module.
117+
118+
### Private git repository
119+
120+
If you are importing a module from a private git repository, the Coder server or
121+
[provisioner](../admin/provisioners.md) needs git credentials. Since this token
122+
will only be used for cloning your repositories with modules, it is best to
123+
create a token with access limited to the repository and no extra permissions.
124+
In GitHub, you can generate a
125+
[fine-grained token](https://docs.github.com/en/rest/overview/permissions-required-for-fine-grained-personal-access-tokens?apiVersion=2022-11-28)
126+
with read only access to the necessary repos.
127+
128+
If you are running Coder on a VM, make sure that you have `git` installed and
129+
the `coder` user has access to the following files:
130+
131+
```shell
132+
# /home/coder/.gitconfig
133+
[credential]
134+
helper = store
135+
```
136+
137+
```shell
138+
# /home/coder/.git-credentials
139+
140+
# GitHub example:
141+
https://your-github-username:your-github-pat@github.com
142+
```
143+
144+
If you are running Coder on Docker or Kubernetes, `git` is pre-installed in the
145+
Coder image. However, you still need to mount credentials. This can be done via
146+
a Docker volume mount or Kubernetes secrets.
147+
148+
#### Passing git credentials in Kubernetes
149+
150+
First, create a `.gitconfig` and `.git-credentials` file on your local machine.
151+
You might want to do this in a temporary directory to avoid conflicting with
152+
your own git credentials.
153+
154+
Next, create the secret in Kubernetes. Be sure to do this in the same namespace
155+
that Coder is installed in.
156+
157+
```shell
158+
export NAMESPACE=coder
159+
kubectl apply -f - <<EOF
160+
apiVersion: v1
161+
kind: Secret
162+
metadata:
163+
name: git-secrets
164+
namespace: $NAMESPACE
165+
type: Opaque
166+
data:
167+
.gitconfig: $(cat .gitconfig | base64 | tr -d '\n')
168+
.git-credentials: $(cat .git-credentials | base64 | tr -d '\n')
169+
EOF
170+
```
171+
172+
Then, modify Coder's Helm values to mount the secret.
173+
174+
```yaml
175+
coder:
176+
volumes:
177+
- name: git-secrets
178+
secret:
179+
secretName: git-secrets
180+
volumeMounts:
181+
- name: git-secrets
182+
mountPath: "/home/coder/.gitconfig"
183+
subPath: .gitconfig
184+
readOnly: true
185+
- name: git-secrets
186+
mountPath: "/home/coder/.git-credentials"
187+
subPath: .git-credentials
188+
readOnly: true
189+
```
190+
191+
### Next up
192+
193+
Learn more about
194+
195+
- JFrog's Terraform Registry support
196+
[here](https://jfrog.com/help/r/jfrog-artifactory-documentation/terraform-registry).
197+
- Configuring the JFrog toolchain inside a workspace
198+
[here](../../integrations/jfrog-artifactory.md).
199+
- Coder Module Registry [here](https://registry.coder.com/modules)

docs/admin/templates/extending-templates/web-ides.md

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -299,6 +299,59 @@ module "airflow" {
299299

300300
![Airflow in Coder](../../../../images/airflow-port-forward.png)
301301

302+
## File Browser
303+
304+
To access the contents of a workspace directory in a browser, you can use File
305+
Browser. File Browser is a lightweight file manager that allows you to view and
306+
manipulate files in a web browser.
307+
308+
Show and manipulate the contents of the `/home/coder` directory in a browser.
309+
310+
```hcl
311+
resource "coder_agent" "coder" {
312+
os = "linux"
313+
arch = "amd64"
314+
dir = "/home/coder"
315+
startup_script = <<EOT
316+
#!/bin/bash
317+
318+
curl -fsSL https://raw.githubusercontent.com/filebrowser/get/master/get.sh | bash
319+
filebrowser --noauth --root /home/coder --port 13339 >/tmp/filebrowser.log 2>&1 &
320+
321+
EOT
322+
}
323+
324+
resource "coder_app" "filebrowser" {
325+
agent_id = coder_agent.coder.id
326+
display_name = "file browser"
327+
slug = "filebrowser"
328+
url = "http://localhost:13339"
329+
icon = "https://raw.githubusercontent.com/matifali/logos/main/database.svg"
330+
subdomain = true
331+
share = "owner"
332+
333+
healthcheck {
334+
url = "http://localhost:13339/healthz"
335+
interval = 3
336+
threshold = 10
337+
}
338+
}
339+
```
340+
341+
Or alternatively, you can use the
342+
[`filebrowser`](https://registry.coder.com/modules/filebrowser) module from the
343+
Coder registry:
344+
345+
```tf
346+
module "filebrowser" {
347+
source = "registry.coder.com/modules/filebrowser/coder"
348+
version = "1.0.8"
349+
agent_id = coder_agent.main.id
350+
}
351+
```
352+
353+
![File Browser](../images/file-browser.png)
354+
302355
## SSH Fallback
303356

304357
If you prefer to run web IDEs in localhost, you can port forward using

docs/manifest.json

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -291,14 +291,14 @@
291291
"path": "./admin/templates/extending-templates/variables.md"
292292
},
293293
{
294-
"title": "Web IDEs",
295-
"description": "Add and configure Web IDEs in your templates",
296-
"path": "./admin/templates/extending-templates/web-ides.md"
294+
"title": "Terraform Modules",
295+
"description": "Reuse terraform code across templates",
296+
"path": "./admin/templates/extending-templates/modules.md"
297297
},
298298
{
299-
"title": "Filebrowser",
300-
"description": "Add Filebrowser to your templates",
301-
"path": "./admin/templates/extending-templates/filebrowser.md"
299+
"title": "Web IDEs and Coder Apps",
300+
"description": "Add and configure Web IDEs in your templates as coder apps",
301+
"path": "./admin/templates/extending-templates/web-ides.md"
302302
}
303303
]
304304
},

docs/user-guides/workspace-access/filebrowser.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,5 @@
33
File Browser is a file manager for the web that can be used to upload, download,
44
and view files in your workspace. A template administrator can add it by
55
following the
6-
[Extending Templates](../../admin/templates/extending-templates/filebrowser.md)
6+
[Extending Templates](../../admin/templates/extending-templates/web-ides.md#file-browser)
77
guide. ![File Browser](../images/file-browser.png)

0 commit comments

Comments
 (0)
0