Skip to content

Commit 033fac8

Browse files
committed
Download code server if app enabled
1 parent 68d7468 commit 033fac8

File tree

3 files changed

+69
-15
lines changed

3 files changed

+69
-15
lines changed

examples/builder/builder.go

Lines changed: 54 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,31 @@ var templates embed.FS
2020
type TemplateInput struct {
2121
TemplateName string
2222

23-
Kubernetes KubeOptions
23+
Kubernetes *KubeOptions
24+
Docker *DockerOptions
25+
}
26+
27+
func (in TemplateInput) build(tpl *template.Template) (tf file, readme ReadmeInput, err error) {
28+
inputs := 0
29+
if in.Kubernetes != nil {
30+
inputs = inputs + 1
31+
}
32+
if in.Docker != nil {
33+
inputs = inputs + 1
34+
}
35+
36+
if inputs != 1 {
37+
return file{}, ReadmeInput{}, xerrors.Errorf("expect only 1 input, got %d", inputs)
38+
}
39+
40+
switch {
41+
case in.Kubernetes != nil:
42+
return buildKube(tpl, *in.Kubernetes)
43+
case in.Docker != nil:
44+
return buildDocker(tpl, *in.Docker)
45+
default:
46+
return file{}, ReadmeInput{}, xerrors.Errorf("no input provided")
47+
}
2448
}
2549

2650
type ReadmeInput struct {
@@ -44,11 +68,12 @@ func buildTemplate(input TemplateInput) ([]byte, error) {
4468
var out bytes.Buffer
4569
tarWriter := tar.NewWriter(&out)
4670

47-
tf, readme, err := buildKube(tpl, input.Kubernetes)
71+
tf, readme, err := input.build(tpl)
4872
if err != nil {
4973
return nil, err
5074
}
5175

76+
// TODO: Should this just be in build?
5277
md, err := buildReadme(tpl, readme)
5378
if err != nil {
5479
return nil, err
@@ -90,6 +115,25 @@ func buildKube(tpl *template.Template, input KubeOptions) (file, ReadmeInput, er
90115
}, nil
91116
}
92117

118+
func buildDocker(tpl *template.Template, input DockerOptions) (file, ReadmeInput, error) {
119+
var out bytes.Buffer
120+
err := tpl.ExecuteTemplate(&out, "docker", input)
121+
if err != nil {
122+
return file{}, ReadmeInput{}, xerrors.Errorf("execute docker template: %w", err)
123+
}
124+
125+
return file{
126+
name: "main.tf",
127+
content: out.Bytes(),
128+
}, ReadmeInput{
129+
Platform: "Docker",
130+
Name: "Local Docker based template",
131+
Description: "Local development inside a docker container.",
132+
Tags: []string{"local", "docker"},
133+
Icon: "/icon/docker.png",
134+
}, nil
135+
}
136+
93137
func buildReadme(tpl *template.Template, input ReadmeInput) (file, error) {
94138
var out bytes.Buffer
95139
err := tpl.ExecuteTemplate(&out, "readme", input)
@@ -125,3 +169,11 @@ func writeFiles(w *tar.Writer, files ...file) error {
125169

126170
return nil
127171
}
172+
173+
// Move these to options.go
174+
type DockerOptions struct {
175+
Image string
176+
Env map[string]string
177+
HomeVolume bool
178+
Apps []string
179+
}

examples/builder/builder_internal_test.go

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,7 @@ import (
1010
func TestBuilder(t *testing.T) {
1111
out, err := buildTemplate(TemplateInput{
1212
TemplateName: "test",
13-
Kubernetes: KubeOptions{
14-
Os: "",
15-
Arch: "",
16-
Namespace: Variable{},
17-
Image: "",
18-
Resources: Resources{},
19-
Env: nil,
20-
HomePVC: false,
21-
},
13+
Docker: &DockerOptions{},
2214
})
2315
require.NoError(t, err)
2416
// This output is a tar file

examples/builder/templates/docker.tmpl

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
{{ define "docker" }}
2+
{{ $CodeServer := contains .Apps "code-server"}}
3+
24
terraform {
35
required_providers {
46
coder = {
@@ -32,8 +34,12 @@ resource "coder_agent" "main" {
3234
login_before_ready = false
3335
startup_script_timeout = 180
3436
startup_script = <<-EOT
35-
set -e
36-
code-server --auth none >/tmp/code-server.log 2>&1 &
37+
set -eux -o pipefail
38+
{{ if $CodeServer }}
39+
# install and start code-server
40+
curl -fsSL https://code-server.dev/install.sh | sh -s -- --method=standalone --prefix=/tmp/code-server --version 4.8.3
41+
/tmp/code-server/bin/code-server --auth none --port 13337 >/tmp/code-server.log 2>&1 &
42+
{{ end }}
3743
EOT
3844

3945
# These environment variables allow you to make Git commits right away after creating a
@@ -48,7 +54,7 @@ resource "coder_agent" "main" {
4854
}
4955
}
5056

51-
{{ if .Data.CodeServer.Enabled }}
57+
{{ if $CodeServer }}
5258
resource "coder_app" "code-server" {
5359
agent_id = coder_agent.main.id
5460
slug = "code-server"
@@ -66,6 +72,7 @@ resource "coder_app" "code-server" {
6672
}
6773
{{ end }}
6874

75+
{{ if .HomeVolume }}
6976
resource "docker_volume" "home_volume" {
7077
name = "coder-${data.coder_workspace.me.id}-home"
7178
# Protect the volume from being deleted due to changes in attributes.
@@ -92,10 +99,11 @@ resource "docker_volume" "home_volume" {
9299
value = data.coder_workspace.me.name
93100
}
94101
}
102+
{{ end }}
95103

96104
resource "docker_container" "workspace" {
97105
count = data.coder_workspace.me.start_count
98-
image = "{{ .Data.ImageURL }}"
106+
image = "{{ .Image }}"
99107
# Uses lower() to avoid Docker restriction on container names.
100108
name = "coder-${data.coder_workspace.me.owner}-${lower(data.coder_workspace.me.name)}"
101109
# Hostname makes the shell more user friendly: coder@my-workspace:~$
@@ -107,11 +115,13 @@ resource "docker_container" "workspace" {
107115
host = "host.docker.internal"
108116
ip = "host-gateway"
109117
}
118+
{{ if .HomeVolume }}
110119
volumes {
111120
contoainer_path = "/home/coder/"
112121
volume_name = docker_volume.home_volume.name
113122
read_only = false
114123
}
124+
{{ end }}
115125
# Add labels in Docker to keep track of orphan resources.
116126
labels {
117127
label = "coder.owner"

0 commit comments

Comments
 (0)