Skip to content

Commit 22e5749

Browse files
authored
feat(coder-utils): nest scripts under module_directory/scripts (#871)
## Summary Move script files from the flat `${module_directory}` to a `scripts/` subdirectory, and prefix each script's filename with `${agent_name}-utils-` so multiple `coder-utils` instances can safely share a `module_directory`. Mirrors the layout #870 established for `logs/` and aligns with the Module Data Layout standard in `AGENTS.md` (#869). ## Changes - Compute `local.scripts_directory = "${var.module_directory}/scripts"` and use it for every `*.sh` path. - Script filenames are now `${agent_name}-utils-{pre_install,install,post_install,start}.sh` so two `coder-utils` instances don't collide on disk. - Pre-install and install `coder_script`s `mkdir -p` the `scripts/` sub-path before writing their `.sh`; post-install and start sync-depend on install, so the directory already exists by the time they run. - Update the `module_directory` description to call out the nested `scripts/` and `logs/` paths. - Add `test_scripts_nested_under_module_directory` asserting the new paths (including the `${agent_name}-utils-` prefix) and the `mkdir -p` in each script. - README: add a "Script file locations" section documenting the new layout. - Bump module version to `v1.3.0`. ## Breaking Changes Consumers reading `${module_directory}/install.sh` (and friends) directly must look under `${module_directory}/scripts/${agent_name}-utils-install.sh` instead. No in-repo consumers exist today. ## Validation - `terraform fmt -recursive` clean - `terraform validate` clean - `terraform test` → 16/16 pass (includes the new `test_scripts_nested_under_module_directory`) - `bun test main.test.ts` → 5/5 pass - `prettier --check` clean > 🤖 This PR was created with the help of Coder Agents, and needs a human review. 🧑‍💻
1 parent f3475c0 commit 22e5749

3 files changed

Lines changed: 72 additions & 8 deletions

File tree

registry/coder/modules/coder-utils/README.md

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ The Coder Utils module is a building block for modules that need to run multiple
2020
```tf
2121
module "coder_utils" {
2222
source = "registry.coder.com/coder/coder-utils/coder"
23-
version = "1.2.0"
23+
version = "1.3.0"
2424
2525
agent_id = coder_agent.main.id
2626
agent_name = "myagent"
@@ -70,7 +70,7 @@ By default each `coder_script` renders in the Coder UI as plain "Install Script"
7070
```tf
7171
module "coder_utils" {
7272
source = "registry.coder.com/coder/coder-utils/coder"
73-
version = "1.2.0"
73+
version = "1.3.0"
7474
7575
agent_id = coder_agent.main.id
7676
agent_name = "myagent"
@@ -94,3 +94,14 @@ The module writes each script's stdout+stderr to `${module_directory}/logs/`:
9494
- `start.log`
9595

9696
Each `coder_script` `mkdir -p`s this subdirectory before its `tee` runs, so the first script to execute creates it.
97+
98+
## Script file locations
99+
100+
The module materializes each script to `${module_directory}/scripts/` before running it:
101+
102+
- `${agent_name}-utils-pre_install.sh`
103+
- `${agent_name}-utils-install.sh`
104+
- `${agent_name}-utils-post_install.sh`
105+
- `${agent_name}-utils-start.sh`
106+
107+
The `${agent_name}-utils-` prefix namespaces files per-agent so multiple `coder-utils` instances can safely share a `module_directory`. The pre-install and install `coder_script`s `mkdir -p` this subdirectory; post-install and start sync-depend on install, so the directory already exists by the time they run.

registry/coder/modules/coder-utils/main.tf

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ variable "agent_name" {
5151

5252
variable "module_directory" {
5353
type = string
54-
description = "The module's working directory for the install/pre/post/start scripts this module writes. Logs land under a `logs/` subdirectory of this path."
54+
description = "The module's working directory. Scripts this module writes land under `scripts/` and their logs under `logs/` in this path."
5555
}
5656

5757
variable "display_name_prefix" {
@@ -77,17 +77,18 @@ locals {
7777
post_install_script_name = "${var.agent_name}-post_install_script"
7878
start_script_name = "${var.agent_name}-start_script"
7979

80-
pre_install_path = "${var.module_directory}/pre_install.sh"
81-
install_path = "${var.module_directory}/install.sh"
82-
post_install_path = "${var.module_directory}/post_install.sh"
83-
start_path = "${var.module_directory}/start.sh"
80+
pre_install_path = "${local.scripts_directory}/${var.agent_name}-utils-pre_install.sh"
81+
install_path = "${local.scripts_directory}/${var.agent_name}-utils-install.sh"
82+
post_install_path = "${local.scripts_directory}/${var.agent_name}-utils-post_install.sh"
83+
start_path = "${local.scripts_directory}/${var.agent_name}-utils-start.sh"
8484

8585
pre_install_log_path = "${local.log_directory}/pre_install.log"
8686
install_log_path = "${local.log_directory}/install.log"
8787
post_install_log_path = "${local.log_directory}/post_install.log"
8888
start_log_path = "${local.log_directory}/start.log"
8989

90-
log_directory = "${var.module_directory}/logs"
90+
scripts_directory = "${var.module_directory}/scripts"
91+
log_directory = "${var.module_directory}/logs"
9192

9293
install_sync_deps = var.pre_install_script != null ? local.pre_install_script_name : null
9394

@@ -112,6 +113,7 @@ resource "coder_script" "pre_install_script" {
112113
set -o pipefail
113114
114115
mkdir -p ${var.module_directory}
116+
mkdir -p ${local.scripts_directory}
115117
mkdir -p ${local.log_directory}
116118
117119
trap 'coder exp sync complete ${local.pre_install_script_name}' EXIT
@@ -135,6 +137,7 @@ resource "coder_script" "install_script" {
135137
set -o pipefail
136138
137139
mkdir -p ${var.module_directory}
140+
mkdir -p ${local.scripts_directory}
138141
mkdir -p ${local.log_directory}
139142
140143
trap 'coder exp sync complete ${local.install_script_name}' EXIT

registry/coder/modules/coder-utils/main.tftest.hcl

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -578,3 +578,53 @@ run "test_logs_nested_under_module_directory" {
578578
error_message = "install script must mkdir -p the logs/ sub-path"
579579
}
580580
}
581+
582+
# Scripts unconditionally land under ${module_directory}/scripts/. Each
583+
# script that materializes its own `.sh` file mkdirs that path first; the
584+
# first script to execute creates it for the rest.
585+
run "test_scripts_nested_under_module_directory" {
586+
command = plan
587+
588+
variables {
589+
agent_id = "test-agent-id"
590+
agent_name = "test-agent"
591+
module_directory = ".test-module"
592+
pre_install_script = "echo pre"
593+
install_script = "echo install"
594+
post_install_script = "echo post"
595+
start_script = "echo start"
596+
}
597+
598+
assert {
599+
condition = can(regex("> .test-module/scripts/test-agent-utils-pre_install.sh", coder_script.pre_install_script[0].script))
600+
error_message = "pre_install script must be written under module_directory/scripts"
601+
}
602+
603+
assert {
604+
condition = can(regex("> .test-module/scripts/test-agent-utils-install.sh", coder_script.install_script.script))
605+
error_message = "install script must be written under module_directory/scripts"
606+
}
607+
608+
assert {
609+
condition = can(regex("> .test-module/scripts/test-agent-utils-post_install.sh", coder_script.post_install_script[0].script))
610+
error_message = "post_install script must be written under module_directory/scripts"
611+
}
612+
613+
assert {
614+
condition = can(regex("> .test-module/scripts/test-agent-utils-start.sh", coder_script.start_script[0].script))
615+
error_message = "start script must be written under module_directory/scripts"
616+
}
617+
618+
# Only pre_install and install mkdir the scripts/ sub-path. post_install
619+
# and start sync-depend on install so the directory already exists by
620+
# the time they run.
621+
assert {
622+
condition = can(regex("mkdir -p .test-module/scripts", coder_script.pre_install_script[0].script))
623+
error_message = "pre_install script must mkdir -p the scripts/ sub-path"
624+
}
625+
626+
assert {
627+
condition = can(regex("mkdir -p .test-module/scripts", coder_script.install_script.script))
628+
error_message = "install script must mkdir -p the scripts/ sub-path"
629+
}
630+
}

0 commit comments

Comments
 (0)