Skip to content

Commit bbbd524

Browse files
authored
develop.sh: attempt to create a Docker template automatically (#2627)
This commit makes the following changes: - Adds two variables docker_host and docker_arch to the example docker-code-server template - Adds an example params.yaml to docker-code-server and updates the README.md to reference these parameters - scripts/develop.sh will now attempt to create a template using docker-code-server with the appropriate parameters for the environment - Updated Lima example to make use of the template parameters for docker-code-server Additional drive-bys: - webpack.dev.ts references CODER_HOST and not CODERV2_HOST; updated develop.sh accordingly - develop.sh should now terminate child processes upon error.
1 parent f9d830a commit bbbd524

File tree

6 files changed

+82
-14
lines changed

6 files changed

+82
-14
lines changed

examples/lima/coder.yaml

+10-7
Original file line numberDiff line numberDiff line change
@@ -97,13 +97,16 @@ provision:
9797
# Set up initial user
9898
[ ! -e ~/.config/coderv2/session ] && coder login http://localhost:3000 --username admin --email admin@coder.com --password $(< /dev/urandom tr -dc _A-Z-a-z-0-9 | head -c8 | tee ${HOME}/.config/coderv2/password)
9999
# Create an initial template
100-
cd ${HOME}
101-
echo code-server | coder templates init
102-
cd ./docker-code-server
103-
if [ $(arch) = "aarch64" ]; then
104-
sed -i 's/arch.*=.*"amd64"/arch = "arm64"/' ./main.tf
100+
temp_template_dir=$(mktemp -d)
101+
echo code-server | coder templates init "${temp_template_dir}"
102+
DOCKER_ARCH="amd64"
103+
if [ "$(arch)" = "aarch64" ]; then
104+
DOCKER_ARCH="arm64"
105105
fi
106-
coder templates create docker-code-server -y -d .
106+
DOCKER_HOST=$(docker context inspect --format '{{.Endpoints.docker.Host}}')
107+
printf 'docker_arch: "%s"\ndocker_host: "%s"\n' "${DOCKER_ARCH}" "${DOCKER_HOST}" | tee "${temp_template_dir}/params.yaml"
108+
coder templates create "docker-code-server-${DOCKER_ARCH}" --directory "${temp_template_dir}" --parameter-file "${temp_template_dir}/params.yaml" --yes
109+
rm -rfv "${temp_template_dir}"
107110
probes:
108111
- description: "docker to be installed"
109112
script: |
@@ -127,7 +130,7 @@ probes:
127130
See "/var/log/cloud-init-output.log" in the guest.
128131
message: |
129132
All Done! Your Coder instance is accessible at http://localhost:3000
130-
133+
131134
Username: "admin@coder.com"
132135
Password: Run `LIMA_INSTANCE=coder lima cat /home/${USER}.linux/.config/coderv2/password` 🤫
133136

examples/templates/docker-code-server/README.md

+15-1
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,18 @@ tags: [local, docker]
88

99
## Getting started
1010

11-
Run `coder templates init` and select this template. Follow the instructions that appear.
11+
Run `coder templates init` and select this template. Follow the instructions that appear.
12+
13+
## Supported Parameters
14+
15+
You can create a file containing parameters and pass the argument
16+
`--parameter-file` to `coder templates create`.
17+
See `params.sample.yaml` for more information.
18+
19+
This template has the following predefined parameters:
20+
21+
- `docker_host`: Path to (or address of) the Docker socket.
22+
> You can determine the correct value for this by runnning
23+
> `docker context ls`.
24+
- `docker_arch`: Architecture of the host running Docker.
25+
This can be `amd64`, `arm64`, or `armv7`.

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

+19-1
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,32 @@ terraform {
1111
}
1212
}
1313

14+
variable "docker_host" {
15+
description = "Specify location of Docker socket (check `docker context ls` if you're not sure)"
16+
sensitive = true
17+
}
18+
19+
variable "docker_arch" {
20+
description = "Specify architecture of docker host (amd64, arm64, or armv7)"
21+
validation {
22+
condition = contains(["amd64", "arm64", "armv7"], var.docker_arch)
23+
error_message = "Value must be amd64, arm64, or armv7."
24+
}
25+
sensitive = true
26+
}
27+
1428
provider "coder" {
1529
}
1630

31+
provider "docker" {
32+
host = var.docker_host
33+
}
34+
1735
data "coder_workspace" "me" {
1836
}
1937

2038
resource "coder_agent" "dev" {
21-
arch = "amd64"
39+
arch = var.docker_arch
2240
os = "linux"
2341
startup_script = "code-server --auth none"
2442
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
docker_host: "unix:///var/run/docker.sock"
2+
docker_arch: "amd64"

examples/templates/docker/main.tf

+2-2
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,8 @@ variable "docker_image" {
7575
# The codercom/enterprise-* images are only built for amd64
7676
default = "codercom/enterprise-base:ubuntu"
7777
validation {
78-
condition = contains(["codercom/enterprise-base:ubuntu", "codercom/enterprise-node:ubuntu",
79-
"codercom/enterprise-intellij:ubuntu", "codercom/enterprise-golang:ubuntu"], var.docker_image)
78+
condition = contains(["codercom/enterprise-base:ubuntu", "codercom/enterprise-node:ubuntu",
79+
"codercom/enterprise-intellij:ubuntu", "codercom/enterprise-golang:ubuntu"], var.docker_image)
8080
error_message = "Invalid Docker image!"
8181
}
8282

scripts/develop.sh

+34-3
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,12 @@ fi
3434
# to kill both at the same time. For more details, see:
3535
# https://stackoverflow.com/questions/3004811/how-do-you-run-multiple-programs-in-parallel-from-a-bash-script
3636
(
37-
SCRIPT_PID=$$
37+
# If something goes wrong, just bail and tear everything down
38+
# rather than leaving things in an inconsistent state.
39+
trap 'kill -INT -$$' ERR
3840
cdroot
39-
CODERV2_HOST=http://127.0.0.1:3000 INSPECT_XSTATE=true yarn --cwd=./site dev || kill -INT -${SCRIPT_PID} &
40-
go run -tags embed cmd/coder/main.go server --address 127.0.0.1:3000 --in-memory --tunnel || kill -INT -${SCRIPT_PID} &
41+
CODER_HOST=http://127.0.0.1:3000 INSPECT_XSTATE=true yarn --cwd=./site dev || kill -INT -$$ &
42+
go run -tags embed cmd/coder/main.go server --address 127.0.0.1:3000 --in-memory --tunnel || kill -INT -$$ &
4143

4244
echo '== Waiting for Coder to become ready'
4345
timeout 60s bash -c 'until curl -s --fail http://localhost:3000 > /dev/null 2>&1; do sleep 0.5; done'
@@ -49,5 +51,34 @@ fi
4951
# || true to always exit code 0. If this fails, whelp.
5052
go run cmd/coder/main.go users create --email=member@coder.com --username=member --password="${CODER_DEV_ADMIN_PASSWORD}" ||
5153
echo 'Failed to create regular user. To troubleshoot, try running this command manually.'
54+
55+
# If we have docker available, then let's try to create a template!
56+
template_name=""
57+
if docker info >/dev/null 2>&1; then
58+
temp_template_dir=$(mktemp -d)
59+
echo code-server | go run "${PROJECT_ROOT}/cmd/coder/main.go" templates init "${temp_template_dir}"
60+
# shellcheck disable=SC1090
61+
source <(go env | grep GOARCH)
62+
DOCKER_HOST=$(docker context inspect --format '{{.Endpoints.docker.Host}}')
63+
printf 'docker_arch: "%s"\ndocker_host: "%s"\n' "${GOARCH}" "${DOCKER_HOST}" | tee "${temp_template_dir}/params.yaml"
64+
template_name="docker-${GOARCH}"
65+
go run "${PROJECT_ROOT}/cmd/coder/main.go" templates create "${template_name}" --directory "${temp_template_dir}" --parameter-file "${temp_template_dir}/params.yaml" --yes
66+
rm -rfv "${temp_template_dir}"
67+
fi
68+
69+
log
70+
log "======================================================================="
71+
log "== =="
72+
log "== Coder is now running in development mode. =="
73+
log "== API : http://localhost:3000 =="
74+
log "== Web UI: http://localhost:8080 =="
75+
if [[ -n "${template_name}" ]]; then
76+
log "== =="
77+
log "== Docker template ${template_name} is ready to use! =="
78+
log "== =="
79+
fi
80+
log "======================================================================="
81+
log
82+
# Wait for both frontend and backend to exit.
5283
wait
5384
)

0 commit comments

Comments
 (0)