## Summary Threads the deployment's configured module registry (`CODER_TEMPLATE_BUILDER_REGISTRY_URL`) into template-builder **base** rendering, so a base that embeds a catalog module (e.g. the quickstart base's `git-clone`) resolves its `source` through a registry mirror the same way wizard-composed modules already do. Previously base-embedded module sources hardcoded `registry.coder.com`, so on a mirror/private-registry deployment a base-embedded module reached the public registry while wizard modules used the mirror. Implements [DOCS-610](https://linear.app/codercom/issue/DOCS-610). ## What changed * `BaseRenderContext.RegistryBase` (new field), mirroring `ModuleRenderContext.RegistryBase`. `Compose` threads the deployment registry through `renderBase` into the base render context, and the four hardcoded base module sources (`quickstart` git-clone, `azure-linux`/`gcp-linux`/`gcp-windows` region modules) now use `{{ .RegistryBase }}`. Because the value defaults back to `registry.coder.com`, rendered sources stay byte-identical; the only golden change is the removal of the now-stale quickstart "follow-up" note. * **Boot-time validation.** `codersdk.ValidateTemplateBuilderRegistryURL` runs in `DeploymentValues.Validate()`, gated on the builder being enabled, matching the chat-hook precedent. A malformed value fails at server start naming the option, instead of surfacing as a per-request error to a template author who cannot fix it. It uses `net/url` in authority form to reject a scheme, path, query, fragment, trailing slash, or credentials and accept a bare `host[:port]`; it does not canonicalize the host. * `Compose` **trusts the validated value**, defaulting an unset one and defensively rejecting a malformed one for direct callers that bypass boot. ## Tests * `codersdk`: validator accept/reject matrix (incl. credentials-without-echo) and a `DeploymentValues.Validate` boundary test with a disabled-skips-validation case. * `templatebuilder`: a base honors the mirror and falls back to the default when unset, plus a defensive-reject case; existing compose tests updated to bare-host registry values. <details><summary>Design decision log</summary> This PR is a deliberately minimal reimplementation of the [DOCS-610](https://linear.app/codercom/issue/DOCS-610/template-builder-base-embedded-modules-should-honor-the-deployment) scope, started fresh from `main` rather than carrying forward an earlier, larger draft (coder/coder#28480). Key decisions: * **Scope kept to the ticket.** The ticket's fix is three bullets: add a registry field to `BaseRenderContext`, thread the deployment `RegistryURL` through `Compose` -> `renderBase`, and use it in base module sources. Independent hardening explored in the earlier draft was dropped as out of scope: `terraform-svchost`-based IDN/punycode/port canonicalization, a rendered-HCL backstop with a 500 mapping, and HCL module-source extraction helpers. Telemetry misclassification of mirror modules is tracked separately ([DOCS-712](https://linear.app/codercom/issue/DOCS-712/telemetry-misclassifies-registry-mirror-modules-as-non-coder)) and is not addressed here. * **Default lives in the render layer.** `templatebuilder.DefaultRegistryBase` holds the fallback host; `codersdk` keeps its own option-default literal. An earlier version exported the constant from `codersdk`, which only emitted a dead `typesGenerated.ts` entry that nothing in the frontend consumed, so it was moved out. The `codersdk` validator is a function, so it generates no TS type. * **Validate at the config boundary, fail fast.** The repo convention is to hard-fail structurally invalid config in `DeploymentValues.Validate()` (access URL, OAuth lifetimes, chat hook URL) and to warn only for functional-but-risky settings. A malformed registry host is structurally invalid, so it fails at boot. This also puts the error in front of the admin who set it, rather than a template author hitting a per-request error they cannot fix. * `net/url`**, not a regex.** The codebase validates identifiers/names with regexes but host/URL-shaped config with `url.Parse` (chat hook URL, OAuth redirect URIs, AI provider base URL). This value is host-shaped, so it follows that convention. `url.Parse("//" + v)` with `u.Host == v` rejects a scheme/path/query/fragment/trailing-slash in one check. </details> > This PR was created with AI assistance (Coder Agents).
154 lines
3.9 KiB
Cheetah
154 lines
3.9 KiB
Cheetah
terraform {
|
|
required_providers {
|
|
coder = {
|
|
source = "coder/coder"
|
|
}
|
|
google = {
|
|
source = "hashicorp/google"
|
|
}
|
|
}
|
|
}
|
|
|
|
provider "coder" {}
|
|
|
|
variable "project_id" {
|
|
description = "Which Google Compute Project should your workspace live in?"
|
|
default = {{ .Variables.project_id }}
|
|
}
|
|
|
|
# See https://registry.coder.com/modules/coder/gcp-region
|
|
module "gcp_region" {
|
|
source = "{{ .RegistryBase }}/coder/gcp-region/coder"
|
|
|
|
# This ensures that the latest non-breaking version of the module gets downloaded, you can also pin the module version to prevent breaking changes in production.
|
|
version = "~> 1.0"
|
|
|
|
regions = ["us", "europe"]
|
|
default = "us-central1-a"
|
|
}
|
|
|
|
provider "google" {
|
|
zone = module.gcp_region.value
|
|
project = var.project_id
|
|
}
|
|
|
|
data "google_compute_default_service_account" "default" {}
|
|
|
|
data "coder_workspace" "me" {}
|
|
data "coder_workspace_owner" "me" {}
|
|
|
|
resource "google_compute_disk" "root" {
|
|
name = "coder-${data.coder_workspace.me.id}-root"
|
|
type = "pd-ssd"
|
|
zone = module.gcp_region.value
|
|
image = "debian-cloud/debian-11"
|
|
lifecycle {
|
|
ignore_changes = [name, image]
|
|
}
|
|
}
|
|
|
|
resource "coder_agent" "main" {
|
|
auth = "google-instance-identity"
|
|
arch = "amd64"
|
|
os = "linux"
|
|
startup_script = <<-EOT
|
|
set -e
|
|
|
|
# Add any commands that should be executed at workspace startup (e.g install requirements, start a program, etc) here
|
|
EOT
|
|
|
|
metadata {
|
|
key = "cpu"
|
|
display_name = "CPU Usage"
|
|
interval = 5
|
|
timeout = 5
|
|
script = <<-EOT
|
|
#!/bin/bash
|
|
set -e
|
|
top -bn1 | grep "Cpu(s)" | awk '{print $2 + $4 "%"}'
|
|
EOT
|
|
}
|
|
metadata {
|
|
key = "memory"
|
|
display_name = "Memory Usage"
|
|
interval = 5
|
|
timeout = 5
|
|
script = <<-EOT
|
|
#!/bin/bash
|
|
set -e
|
|
free -m | awk 'NR==2{printf "%.2f%%\t", $3*100/$2 }'
|
|
EOT
|
|
}
|
|
metadata {
|
|
key = "disk"
|
|
display_name = "Disk Usage"
|
|
interval = 600 # every 10 minutes
|
|
timeout = 30 # df can take a while on large filesystems
|
|
script = <<-EOT
|
|
#!/bin/bash
|
|
set -e
|
|
df /home/coder | awk '$NF=="/"{printf "%s", $5}'
|
|
EOT
|
|
}
|
|
}
|
|
|
|
resource "google_compute_instance" "dev" {
|
|
zone = module.gcp_region.value
|
|
count = data.coder_workspace.me.start_count
|
|
name = "coder-${lower(data.coder_workspace_owner.me.name)}-${lower(data.coder_workspace.me.name)}-root"
|
|
machine_type = "e2-medium"
|
|
network_interface {
|
|
network = "default"
|
|
access_config {
|
|
// Ephemeral public IP
|
|
}
|
|
}
|
|
boot_disk {
|
|
auto_delete = false
|
|
source = google_compute_disk.root.name
|
|
}
|
|
service_account {
|
|
email = data.google_compute_default_service_account.default.email
|
|
scopes = ["cloud-platform"]
|
|
}
|
|
# The startup script runs as root with no $HOME environment set up, so instead of directly
|
|
# running the agent init script, create a user (with a homedir, default shell and sudo
|
|
# permissions) and execute the init script as that user.
|
|
metadata_startup_script = <<EOMETA
|
|
#!/usr/bin/env sh
|
|
set -eux
|
|
|
|
# If user does not exist, create it and set up passwordless sudo
|
|
if ! id -u "${local.linux_user}" >/dev/null 2>&1; then
|
|
useradd -m -s /bin/bash "${local.linux_user}"
|
|
echo "${local.linux_user} ALL=(ALL) NOPASSWD:ALL" > /etc/sudoers.d/coder-user
|
|
fi
|
|
|
|
exec sudo -u "${local.linux_user}" sh -c '${coder_agent.main.init_script}'
|
|
EOMETA
|
|
}
|
|
|
|
locals {
|
|
# Ensure Coder username is a valid Linux username
|
|
linux_user = lower(substr(data.coder_workspace_owner.me.name, 0, 32))
|
|
}
|
|
|
|
resource "coder_metadata" "workspace_info" {
|
|
count = data.coder_workspace.me.start_count
|
|
resource_id = google_compute_instance.dev[0].id
|
|
|
|
item {
|
|
key = "type"
|
|
value = google_compute_instance.dev[0].machine_type
|
|
}
|
|
}
|
|
|
|
resource "coder_metadata" "home_info" {
|
|
resource_id = google_compute_disk.root.id
|
|
|
|
item {
|
|
key = "size"
|
|
value = "${google_compute_disk.root.size} GiB"
|
|
}
|
|
}
|