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.
This commit is contained in:
Cian Johnston 2022-06-27 09:59:08 +01:00 committed by GitHub
parent f9d830a2b6
commit bbbd5241c3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 82 additions and 14 deletions

View File

@ -97,13 +97,16 @@ provision:
# Set up initial user
[ ! -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)
# Create an initial template
cd ${HOME}
echo code-server | coder templates init
cd ./docker-code-server
if [ $(arch) = "aarch64" ]; then
sed -i 's/arch.*=.*"amd64"/arch = "arm64"/' ./main.tf
temp_template_dir=$(mktemp -d)
echo code-server | coder templates init "${temp_template_dir}"
DOCKER_ARCH="amd64"
if [ "$(arch)" = "aarch64" ]; then
DOCKER_ARCH="arm64"
fi
coder templates create docker-code-server -y -d .
DOCKER_HOST=$(docker context inspect --format '{{.Endpoints.docker.Host}}')
printf 'docker_arch: "%s"\ndocker_host: "%s"\n' "${DOCKER_ARCH}" "${DOCKER_HOST}" | tee "${temp_template_dir}/params.yaml"
coder templates create "docker-code-server-${DOCKER_ARCH}" --directory "${temp_template_dir}" --parameter-file "${temp_template_dir}/params.yaml" --yes
rm -rfv "${temp_template_dir}"
probes:
- description: "docker to be installed"
script: |
@ -127,7 +130,7 @@ probes:
See "/var/log/cloud-init-output.log" in the guest.
message: |
All Done! Your Coder instance is accessible at http://localhost:3000
Username: "admin@coder.com"
Password: Run `LIMA_INSTANCE=coder lima cat /home/${USER}.linux/.config/coderv2/password` 🤫

View File

@ -8,4 +8,18 @@ tags: [local, docker]
## Getting started
Run `coder templates init` and select this template. Follow the instructions that appear.
Run `coder templates init` and select this template. Follow the instructions that appear.
## Supported Parameters
You can create a file containing parameters and pass the argument
`--parameter-file` to `coder templates create`.
See `params.sample.yaml` for more information.
This template has the following predefined parameters:
- `docker_host`: Path to (or address of) the Docker socket.
> You can determine the correct value for this by runnning
> `docker context ls`.
- `docker_arch`: Architecture of the host running Docker.
This can be `amd64`, `arm64`, or `armv7`.

View File

@ -11,14 +11,32 @@ terraform {
}
}
variable "docker_host" {
description = "Specify location of Docker socket (check `docker context ls` if you're not sure)"
sensitive = true
}
variable "docker_arch" {
description = "Specify architecture of docker host (amd64, arm64, or armv7)"
validation {
condition = contains(["amd64", "arm64", "armv7"], var.docker_arch)
error_message = "Value must be amd64, arm64, or armv7."
}
sensitive = true
}
provider "coder" {
}
provider "docker" {
host = var.docker_host
}
data "coder_workspace" "me" {
}
resource "coder_agent" "dev" {
arch = "amd64"
arch = var.docker_arch
os = "linux"
startup_script = "code-server --auth none"
}

View File

@ -0,0 +1,2 @@
docker_host: "unix:///var/run/docker.sock"
docker_arch: "amd64"

View File

@ -75,8 +75,8 @@ variable "docker_image" {
# The codercom/enterprise-* images are only built for amd64
default = "codercom/enterprise-base:ubuntu"
validation {
condition = contains(["codercom/enterprise-base:ubuntu", "codercom/enterprise-node:ubuntu",
"codercom/enterprise-intellij:ubuntu", "codercom/enterprise-golang:ubuntu"], var.docker_image)
condition = contains(["codercom/enterprise-base:ubuntu", "codercom/enterprise-node:ubuntu",
"codercom/enterprise-intellij:ubuntu", "codercom/enterprise-golang:ubuntu"], var.docker_image)
error_message = "Invalid Docker image!"
}

View File

@ -34,10 +34,12 @@ fi
# to kill both at the same time. For more details, see:
# https://stackoverflow.com/questions/3004811/how-do-you-run-multiple-programs-in-parallel-from-a-bash-script
(
SCRIPT_PID=$$
# If something goes wrong, just bail and tear everything down
# rather than leaving things in an inconsistent state.
trap 'kill -INT -$$' ERR
cdroot
CODERV2_HOST=http://127.0.0.1:3000 INSPECT_XSTATE=true yarn --cwd=./site dev || kill -INT -${SCRIPT_PID} &
go run -tags embed cmd/coder/main.go server --address 127.0.0.1:3000 --in-memory --tunnel || kill -INT -${SCRIPT_PID} &
CODER_HOST=http://127.0.0.1:3000 INSPECT_XSTATE=true yarn --cwd=./site dev || kill -INT -$$ &
go run -tags embed cmd/coder/main.go server --address 127.0.0.1:3000 --in-memory --tunnel || kill -INT -$$ &
echo '== Waiting for Coder to become ready'
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
# || true to always exit code 0. If this fails, whelp.
go run cmd/coder/main.go users create --email=member@coder.com --username=member --password="${CODER_DEV_ADMIN_PASSWORD}" ||
echo 'Failed to create regular user. To troubleshoot, try running this command manually.'
# If we have docker available, then let's try to create a template!
template_name=""
if docker info >/dev/null 2>&1; then
temp_template_dir=$(mktemp -d)
echo code-server | go run "${PROJECT_ROOT}/cmd/coder/main.go" templates init "${temp_template_dir}"
# shellcheck disable=SC1090
source <(go env | grep GOARCH)
DOCKER_HOST=$(docker context inspect --format '{{.Endpoints.docker.Host}}')
printf 'docker_arch: "%s"\ndocker_host: "%s"\n' "${GOARCH}" "${DOCKER_HOST}" | tee "${temp_template_dir}/params.yaml"
template_name="docker-${GOARCH}"
go run "${PROJECT_ROOT}/cmd/coder/main.go" templates create "${template_name}" --directory "${temp_template_dir}" --parameter-file "${temp_template_dir}/params.yaml" --yes
rm -rfv "${temp_template_dir}"
fi
log
log "======================================================================="
log "== =="
log "== Coder is now running in development mode. =="
log "== API : http://localhost:3000 =="
log "== Web UI: http://localhost:8080 =="
if [[ -n "${template_name}" ]]; then
log "== =="
log "== Docker template ${template_name} is ready to use! =="
log "== =="
fi
log "======================================================================="
log
# Wait for both frontend and backend to exit.
wait
)