|
| 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) |
0 commit comments