Skip to content

Commit f7ea016

Browse files
authored
Pass git configuration variables via terraform (#3034)
* Pass workspace owner email address to provisioner * Remove owner_email and owner_username fields from agent metadata * Add Git environment variables to example templates * Remove "owner_name" field from provisioner metadata, use username instead * Remove Git configuration from most templates, add documentation * Proofreading/typo fixes from @mafredri * Update example templates to latest version of terraform-provider-coder
1 parent b9847c1 commit f7ea016

File tree

18 files changed

+144
-120
lines changed

18 files changed

+144
-120
lines changed

agent/agent.go

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,6 @@ type Options struct {
5656

5757
type Metadata struct {
5858
WireguardAddresses []netaddr.IPPrefix `json:"addresses"`
59-
OwnerEmail string `json:"owner_email"`
60-
OwnerUsername string `json:"owner_username"`
6159
EnvironmentVariables map[string]string `json:"environment_variables"`
6260
StartupScript string `json:"startup_script"`
6361
Directory string `json:"directory"`
@@ -386,12 +384,6 @@ func (a *agent) createCommand(ctx context.Context, rawCommand string, env []stri
386384
// If using backslashes, it's unable to find the executable.
387385
unixExecutablePath := strings.ReplaceAll(executablePath, "\\", "/")
388386
cmd.Env = append(cmd.Env, fmt.Sprintf(`GIT_SSH_COMMAND=%s gitssh --`, unixExecutablePath))
389-
// These prevent the user from having to specify _anything_ to successfully commit.
390-
// Both author and committer must be set!
391-
cmd.Env = append(cmd.Env, fmt.Sprintf(`GIT_AUTHOR_EMAIL=%s`, metadata.OwnerEmail))
392-
cmd.Env = append(cmd.Env, fmt.Sprintf(`GIT_COMMITTER_EMAIL=%s`, metadata.OwnerEmail))
393-
cmd.Env = append(cmd.Env, fmt.Sprintf(`GIT_AUTHOR_NAME=%s`, metadata.OwnerUsername))
394-
cmd.Env = append(cmd.Env, fmt.Sprintf(`GIT_COMMITTER_NAME=%s`, metadata.OwnerUsername))
395387

396388
// Load environment variables passed via the agent.
397389
// These should override all variables we manually specify.

coderd/provisionerdaemons.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,7 @@ func (server *provisionerdServer) AcquireJob(ctx context.Context, _ *proto.Empty
256256
WorkspaceTransition: transition,
257257
WorkspaceName: workspace.Name,
258258
WorkspaceOwner: owner.Username,
259+
WorkspaceOwnerEmail: owner.Email,
259260
WorkspaceId: workspace.ID.String(),
260261
WorkspaceOwnerId: owner.ID.String(),
261262
},

coderd/workspaceagents.go

Lines changed: 0 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -129,38 +129,6 @@ func (api *API) workspaceAgentMetadata(rw http.ResponseWriter, r *http.Request)
129129
})
130130
return
131131
}
132-
resource, err := api.Database.GetWorkspaceResourceByID(r.Context(), workspaceAgent.ResourceID)
133-
if err != nil {
134-
httpapi.Write(rw, http.StatusInternalServerError, codersdk.Response{
135-
Message: "Internal error fetching workspace resources.",
136-
Detail: err.Error(),
137-
})
138-
return
139-
}
140-
build, err := api.Database.GetWorkspaceBuildByJobID(r.Context(), resource.JobID)
141-
if err != nil {
142-
httpapi.Write(rw, http.StatusInternalServerError, codersdk.Response{
143-
Message: "Internal error fetching workspace build.",
144-
Detail: err.Error(),
145-
})
146-
return
147-
}
148-
workspace, err := api.Database.GetWorkspaceByID(r.Context(), build.WorkspaceID)
149-
if err != nil {
150-
httpapi.Write(rw, http.StatusInternalServerError, codersdk.Response{
151-
Message: "Internal error fetching workspace.",
152-
Detail: err.Error(),
153-
})
154-
return
155-
}
156-
owner, err := api.Database.GetUserByID(r.Context(), workspace.OwnerID)
157-
if err != nil {
158-
httpapi.Write(rw, http.StatusInternalServerError, codersdk.Response{
159-
Message: "Internal error fetching workspace owner.",
160-
Detail: err.Error(),
161-
})
162-
return
163-
}
164132

165133
ipp, ok := netaddr.FromStdIPNet(&workspaceAgent.WireguardNodeIPv6.IPNet)
166134
if !ok {
@@ -173,8 +141,6 @@ func (api *API) workspaceAgentMetadata(rw http.ResponseWriter, r *http.Request)
173141

174142
httpapi.Write(rw, http.StatusOK, agent.Metadata{
175143
WireguardAddresses: []netaddr.IPPrefix{ipp},
176-
OwnerEmail: owner.Email,
177-
OwnerUsername: owner.Username,
178144
EnvironmentVariables: apiAgent.EnvironmentVariables,
179145
StartupScript: apiAgent.StartupScript,
180146
Directory: apiAgent.Directory,

docs/templates.md

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ code once you run `coder templates init` (new) or `coder templates pull`
4242
## Concepts in templates
4343

4444
While templates are written with standard Terraform, the
45-
[Coder Terraform Provider](https://registry.terraform.io/providers/coder/coder/latest/docs) is
45+
[Coder Terraform Provider](https://registry.terraform.io/providers/coder/coder/latest/docs) is
4646
used to define the workspace lifecycle and establish a connection from resources
4747
to Coder.
4848

@@ -51,7 +51,7 @@ template options, reference [Coder Terraform provider docs](https://registry.ter
5151

5252
### Resource
5353

54-
Resources in Coder are simply [Terraform resources](https://www.terraform.io/language/resources).
54+
Resources in Coder are simply [Terraform resources](https://www.terraform.io/language/resources).
5555
If a Coder agent is attached to a resource, users can connect directly to the resource over
5656
SSH or web apps.
5757

@@ -60,12 +60,12 @@ SSH or web apps.
6060
Once a Coder workspace is created, the Coder agent establishes a connection
6161
between a resource (docker_container) and Coder, so that a user can connect to
6262
their workspace from the web UI or CLI. A template can have multiple agents to
63-
allow users to connect to multiple resources in their workspace.
63+
allow users to connect to multiple resources in their workspace.
6464

6565
> Resources must download and start the Coder agent binary to connect to Coder.
6666
> This means the resource must be able to reach your Coder URL.
6767
68-
Use the Coder agent's init script to
68+
Use the Coder agent's init script to
6969

7070
```hcl
7171
data "coder_workspace" "me" {
@@ -90,6 +90,11 @@ resource "kubernetes_pod" "pod1" {
9090
}
9191
```
9292

93+
The `coder_agent` resource can be configured as described in the
94+
[documentation for the `coder` Terraform provider.](https://registry.terraform.io/providers/coder/coder/latest/docs/resources/agent)
95+
For example, you can use the `env` property to set environment variables that will be
96+
inherited by all child processes of the agent, including SSH sessions.
97+
9398
### Parameters
9499

95100
Templates often contain _parameters_. These are defined by `variable` blocks in
@@ -202,6 +207,31 @@ By default, all templates allow developers to connect over SSH and a web
202207
terminal. See [Configuring Web IDEs](./ides/configuring-web-ides.md) to
203208
learn how to give users access to additional web applications.
204209

210+
### Data source
211+
212+
When a workspace is being started or stopped, the `coder_workspace` data source provides
213+
some useful parameters. See the [documentation for the `coder` Terraform provider](https://registry.terraform.io/providers/coder/coder/latest/docs/data-sources/workspace)
214+
for more information.
215+
216+
For example, the [Docker quick-start template](https://github.com/coder/coder/tree/main/examples/templates/docker)
217+
sets a few environment variables based on the username and email address of the workspace's owner, so
218+
that you can make Git commits immediately without any manual configuration:
219+
220+
```tf
221+
resource "coder_agent" "dev" {
222+
# ...
223+
env = {
224+
GIT_AUTHOR_NAME = "${data.coder_workspace.me.owner}"
225+
GIT_COMMITTER_NAME = "${data.coder_workspace.me.owner}"
226+
GIT_AUTHOR_EMAIL = "${data.coder_workspace.me.owner_email}"
227+
GIT_COMMITTER_EMAIL = "${data.coder_workspace.me.owner_email}"
228+
}
229+
}
230+
```
231+
232+
You can add these environment variable definitions to your own templates, or customize them however
233+
you like.
234+
205235
## Creating & troubleshooting templates
206236

207237
You can use any Terraform resources or modules with Coder! When working on

examples/templates/aws-linux/main.tf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ terraform {
22
required_providers {
33
coder = {
44
source = "coder/coder"
5-
version = "0.3.4"
5+
version = "0.4.3"
66
}
77
}
88
}

examples/templates/aws-windows/main.tf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ terraform {
22
required_providers {
33
coder = {
44
source = "coder/coder"
5-
version = "0.3.4"
5+
version = "0.4.3"
66
}
77
}
88
}

examples/templates/do-linux/main.tf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ terraform {
22
required_providers {
33
coder = {
44
source = "coder/coder"
5-
version = "0.4.2"
5+
version = "0.4.3"
66
}
77
digitalocean = {
88
source = "digitalocean/digitalocean"

examples/templates/docker-code-server/main.tf

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ terraform {
22
required_providers {
33
coder = {
44
source = "coder/coder"
5-
version = "0.4.2"
5+
version = "0.4.3"
66
}
77
docker = {
88
source = "kreuzwerker/docker"
@@ -39,6 +39,17 @@ resource "coder_agent" "dev" {
3939
arch = var.docker_arch
4040
os = "linux"
4141
startup_script = "code-server --auth none"
42+
43+
# These environment variables allow you to make Git commits right away after creating a
44+
# workspace. Note that they take precedence over configuration defined in ~/.gitconfig!
45+
# You can remove this block if you'd prefer to configure Git manually or using
46+
# dotfiles. (see docs/dotfiles.md)
47+
env = {
48+
GIT_AUTHOR_NAME = "${data.coder_workspace.me.owner}"
49+
GIT_COMMITTER_NAME = "${data.coder_workspace.me.owner}"
50+
GIT_AUTHOR_EMAIL = "${data.coder_workspace.me.owner_email}"
51+
GIT_COMMITTER_EMAIL = "${data.coder_workspace.me.owner_email}"
52+
}
4253
}
4354

4455
resource "coder_app" "code-server" {

examples/templates/docker-image-builds/main.tf

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ terraform {
33
required_providers {
44
coder = {
55
source = "coder/coder"
6-
version = "0.4.2"
6+
version = "0.4.3"
77
}
88
docker = {
99
source = "kreuzwerker/docker"
@@ -107,7 +107,7 @@ resource "docker_container" "workspace" {
107107
# Hostname makes the shell more user friendly: coder@my-workspace:~$
108108
hostname = lower(data.coder_workspace.me.name)
109109
dns = ["1.1.1.1"]
110-
# Use the docker gateway if the access URL is 127.0.0.1
110+
# Use the docker gateway if the access URL is 127.0.0.1
111111
command = ["sh", "-c", replace(coder_agent.dev.init_script, "127.0.0.1", "host.docker.internal")]
112112
env = ["CODER_AGENT_TOKEN=${coder_agent.dev.token}"]
113113
host {

examples/templates/docker-with-dotfiles/main.tf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ terraform {
99
required_providers {
1010
coder = {
1111
source = "coder/coder"
12-
version = "0.4.2"
12+
version = "0.4.3"
1313
}
1414
docker = {
1515
source = "kreuzwerker/docker"

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy