Skip to content

develop.sh: attempt to create a Docker template automatically #2627

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Jun 27, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 10 additions & 7 deletions examples/lima/coder.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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: |
Expand All @@ -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` 🤫

Expand Down
16 changes: 15 additions & 1 deletion examples/templates/docker-code-server/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`.
20 changes: 19 additions & 1 deletion examples/templates/docker-code-server/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
Expand Down
2 changes: 2 additions & 0 deletions examples/templates/docker-code-server/params.sample.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
docker_host: "unix:///var/run/docker.sock"
docker_arch: "amd64"
4 changes: 2 additions & 2 deletions examples/templates/docker/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -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!"
}

Expand Down
37 changes: 34 additions & 3 deletions scripts/develop.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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 -$$ &
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

did this var change?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As far as I can tell it was always CODER_HOST and not CODERV2_HOST in webpack.dev.ts

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'
Expand All @@ -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
)