Skip to content

Commit f3475c0

Browse files
authored
feat(coder-utils): nest logs under module_directory/logs (#870)
1 parent 39f332f commit f3475c0

3 files changed

Lines changed: 79 additions & 15 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.1.0"
23+
version = "1.2.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.1.0"
73+
version = "1.2.0"
7474
7575
agent_id = coder_agent.main.id
7676
agent_name = "myagent"
@@ -83,3 +83,14 @@ module "coder_utils" {
8383
```
8484

8585
Both variables are optional. `display_name_prefix` defaults to `""` (no prefix), and `icon` defaults to `null` (use the Coder provider's default).
86+
87+
## Log file locations
88+
89+
The module writes each script's stdout+stderr to `${module_directory}/logs/`:
90+
91+
- `pre_install.log`
92+
- `install.log`
93+
- `post_install.log`
94+
- `start.log`
95+
96+
Each `coder_script` `mkdir -p`s this subdirectory before its `tee` runs, so the first script to execute creates it.

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

Lines changed: 9 additions & 5 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 scripts and logs."
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."
5555
}
5656

5757
variable "display_name_prefix" {
@@ -82,10 +82,12 @@ locals {
8282
post_install_path = "${var.module_directory}/post_install.sh"
8383
start_path = "${var.module_directory}/start.sh"
8484

85-
pre_install_log_path = "${var.module_directory}/pre_install.log"
86-
install_log_path = "${var.module_directory}/install.log"
87-
post_install_log_path = "${var.module_directory}/post_install.log"
88-
start_log_path = "${var.module_directory}/start.log"
85+
pre_install_log_path = "${local.log_directory}/pre_install.log"
86+
install_log_path = "${local.log_directory}/install.log"
87+
post_install_log_path = "${local.log_directory}/post_install.log"
88+
start_log_path = "${local.log_directory}/start.log"
89+
90+
log_directory = "${var.module_directory}/logs"
8991

9092
install_sync_deps = var.pre_install_script != null ? local.pre_install_script_name : null
9193

@@ -110,6 +112,7 @@ resource "coder_script" "pre_install_script" {
110112
set -o pipefail
111113
112114
mkdir -p ${var.module_directory}
115+
mkdir -p ${local.log_directory}
113116
114117
trap 'coder exp sync complete ${local.pre_install_script_name}' EXIT
115118
coder exp sync start ${local.pre_install_script_name}
@@ -132,6 +135,7 @@ resource "coder_script" "install_script" {
132135
set -o pipefail
133136
134137
mkdir -p ${var.module_directory}
138+
mkdir -p ${local.log_directory}
135139
136140
trap 'coder exp sync complete ${local.install_script_name}' EXIT
137141
%{if local.install_sync_deps != null~}

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

Lines changed: 57 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -510,22 +510,71 @@ run "test_scripts_tee_stdout_and_log_file" {
510510
}
511511

512512
assert {
513-
condition = can(regex("pre_install.sh 2>&1 \\| tee .*pre_install.log", coder_script.pre_install_script[0].script))
514-
error_message = "pre_install wrapper must tee combined output to the log file and stdout"
513+
condition = can(regex("pre_install.sh 2>&1 \\| tee .*logs/pre_install.log", coder_script.pre_install_script[0].script))
514+
error_message = "pre_install wrapper must tee combined output to the logs/ subdirectory"
515515
}
516516

517517
assert {
518-
condition = can(regex("install.sh 2>&1 \\| tee .*install.log", coder_script.install_script.script))
519-
error_message = "install wrapper must tee combined output to the log file and stdout"
518+
condition = can(regex("install.sh 2>&1 \\| tee .*logs/install.log", coder_script.install_script.script))
519+
error_message = "install wrapper must tee combined output to the logs/ subdirectory"
520520
}
521521

522522
assert {
523-
condition = can(regex("post_install.sh 2>&1 \\| tee .*post_install.log", coder_script.post_install_script[0].script))
524-
error_message = "post_install wrapper must tee combined output to the log file and stdout"
523+
condition = can(regex("post_install.sh 2>&1 \\| tee .*logs/post_install.log", coder_script.post_install_script[0].script))
524+
error_message = "post_install wrapper must tee combined output to the logs/ subdirectory"
525525
}
526526

527527
assert {
528-
condition = can(regex("start.sh 2>&1 \\| tee .*start.log", coder_script.start_script[0].script))
529-
error_message = "start wrapper must tee combined output to the log file and stdout"
528+
condition = can(regex("start.sh 2>&1 \\| tee .*logs/start.log", coder_script.start_script[0].script))
529+
error_message = "start wrapper must tee combined output to the logs/ subdirectory"
530+
}
531+
}
532+
533+
# Logs unconditionally land under ${module_directory}/logs/. Each script
534+
# mkdirs that path before tee runs so the first script to execute creates it.
535+
run "test_logs_nested_under_module_directory" {
536+
command = plan
537+
538+
variables {
539+
agent_id = "test-agent-id"
540+
agent_name = "test-agent"
541+
module_directory = ".test-module"
542+
pre_install_script = "echo pre"
543+
install_script = "echo install"
544+
post_install_script = "echo post"
545+
start_script = "echo start"
546+
}
547+
548+
assert {
549+
condition = can(regex("tee .test-module/logs/pre_install.log", coder_script.pre_install_script[0].script))
550+
error_message = "pre_install log must land under module_directory/logs"
551+
}
552+
553+
assert {
554+
condition = can(regex("tee .test-module/logs/install.log", coder_script.install_script.script))
555+
error_message = "install log must land under module_directory/logs"
556+
}
557+
558+
assert {
559+
condition = can(regex("tee .test-module/logs/post_install.log", coder_script.post_install_script[0].script))
560+
error_message = "post_install log must land under module_directory/logs"
561+
}
562+
563+
assert {
564+
condition = can(regex("tee .test-module/logs/start.log", coder_script.start_script[0].script))
565+
error_message = "start log must land under module_directory/logs"
566+
}
567+
568+
# Only pre_install and install mkdir the logs/ sub-path. post_install
569+
# and start sync-depend on install so the directory already exists by
570+
# the time they run.
571+
assert {
572+
condition = can(regex("mkdir -p .test-module/logs", coder_script.pre_install_script[0].script))
573+
error_message = "pre_install script must mkdir -p the logs/ sub-path"
574+
}
575+
576+
assert {
577+
condition = can(regex("mkdir -p .test-module/logs", coder_script.install_script.script))
578+
error_message = "install script must mkdir -p the logs/ sub-path"
530579
}
531580
}

0 commit comments

Comments
 (0)