simplify: just add pre/post install script variables inline in run.sh
Remove all the coder exp sync machinery, separate script resources, locals, and outputs. Simply pass the scripts as template variables into run.sh and eval them before/after the nvm install.
This commit is contained in:
parent
79cef2ecfc
commit
8bd24f8b44
@ -41,7 +41,7 @@ module "nodejs" {
|
||||
|
||||
## Pre and Post Install Scripts
|
||||
|
||||
Use `pre_install_script` and `post_install_script` to run custom scripts before and after Node.js installation. These use `coder exp sync` for reliable script ordering, making them useful for dependency coordination between modules.
|
||||
Use `pre_install_script` and `post_install_script` to run custom scripts before and after Node.js installation.
|
||||
|
||||
```tf
|
||||
module "nodejs" {
|
||||
@ -55,8 +55,6 @@ module "nodejs" {
|
||||
}
|
||||
```
|
||||
|
||||
The module exports sync script names (`pre_install_script_name`, `install_script_name`, `post_install_script_name`) that other modules can use with `coder exp sync want` to coordinate execution order.
|
||||
|
||||
## Full example
|
||||
|
||||
A example with all available options:
|
||||
|
||||
@ -1,5 +1,5 @@
|
||||
import { describe, expect, it } from "bun:test";
|
||||
import { runTerraformInit, testRequiredVariables, runTerraformApply } from "~test";
|
||||
import { describe } from "bun:test";
|
||||
import { runTerraformInit, testRequiredVariables } from "~test";
|
||||
|
||||
describe("nodejs", async () => {
|
||||
await runTerraformInit(import.meta.dir);
|
||||
@ -7,20 +7,4 @@ describe("nodejs", async () => {
|
||||
testRequiredVariables(import.meta.dir, {
|
||||
agent_id: "foo",
|
||||
});
|
||||
|
||||
it("accepts pre_install_script and post_install_script", async () => {
|
||||
const state = await runTerraformApply(import.meta.dir, {
|
||||
agent_id: "foo",
|
||||
pre_install_script: "echo pre",
|
||||
post_install_script: "echo post",
|
||||
});
|
||||
expect(state).toBeDefined();
|
||||
});
|
||||
|
||||
it("works without pre/post install scripts", async () => {
|
||||
const state = await runTerraformApply(import.meta.dir, {
|
||||
agent_id: "foo",
|
||||
});
|
||||
expect(state).toBeDefined();
|
||||
});
|
||||
});
|
||||
|
||||
@ -40,7 +40,7 @@ variable "default_node_version" {
|
||||
|
||||
variable "pre_install_script" {
|
||||
type = string
|
||||
description = "Custom script to run before installing Node.js. Can be used for dependency ordering between modules."
|
||||
description = "Custom script to run before installing Node.js."
|
||||
default = null
|
||||
}
|
||||
|
||||
@ -50,102 +50,17 @@ variable "post_install_script" {
|
||||
default = null
|
||||
}
|
||||
|
||||
locals {
|
||||
encoded_pre_install_script = var.pre_install_script != null ? base64encode(var.pre_install_script) : ""
|
||||
encoded_post_install_script = var.post_install_script != null ? base64encode(var.post_install_script) : ""
|
||||
|
||||
module_dir_path = "$HOME/.nodejs-module"
|
||||
|
||||
pre_install_script_name = "nodejs-pre_install_script"
|
||||
install_script_name = "nodejs-install_script"
|
||||
post_install_script_name = "nodejs-post_install_script"
|
||||
}
|
||||
|
||||
resource "coder_script" "nodejs_pre_install" {
|
||||
count = var.pre_install_script != null ? 1 : 0
|
||||
agent_id = var.agent_id
|
||||
display_name = "Node.js: Pre-Install"
|
||||
run_on_start = true
|
||||
script = <<-EOT
|
||||
#!/bin/bash
|
||||
set -o errexit
|
||||
set -o pipefail
|
||||
|
||||
mkdir -p ${local.module_dir_path}
|
||||
|
||||
trap 'coder exp sync complete ${local.pre_install_script_name}' EXIT
|
||||
coder exp sync start ${local.pre_install_script_name}
|
||||
|
||||
echo -n '${local.encoded_pre_install_script}' | base64 -d > ${local.module_dir_path}/pre_install.sh
|
||||
chmod +x ${local.module_dir_path}/pre_install.sh
|
||||
|
||||
${local.module_dir_path}/pre_install.sh 2>&1
|
||||
EOT
|
||||
}
|
||||
|
||||
resource "coder_script" "nodejs" {
|
||||
agent_id = var.agent_id
|
||||
display_name = "Node.js: Install"
|
||||
run_on_start = true
|
||||
script = <<-EOT
|
||||
#!/bin/bash
|
||||
set -o errexit
|
||||
set -o pipefail
|
||||
|
||||
mkdir -p ${local.module_dir_path}
|
||||
|
||||
trap 'coder exp sync complete ${local.install_script_name}' EXIT
|
||||
%{if var.pre_install_script != null~}
|
||||
coder exp sync want ${local.install_script_name} ${local.pre_install_script_name}
|
||||
%{endif~}
|
||||
coder exp sync start ${local.install_script_name}
|
||||
|
||||
echo -n '${base64encode(templatefile("${path.module}/run.sh", {
|
||||
NVM_VERSION = var.nvm_version,
|
||||
INSTALL_PREFIX = var.nvm_install_prefix,
|
||||
NODE_VERSIONS = join(",", var.node_versions),
|
||||
DEFAULT = var.default_node_version,
|
||||
}))}' | base64 -d > ${local.module_dir_path}/install.sh
|
||||
chmod +x ${local.module_dir_path}/install.sh
|
||||
|
||||
${local.module_dir_path}/install.sh 2>&1
|
||||
EOT
|
||||
|
||||
display_name = "Node.js:"
|
||||
script = templatefile("${path.module}/run.sh", {
|
||||
NVM_VERSION : var.nvm_version,
|
||||
INSTALL_PREFIX : var.nvm_install_prefix,
|
||||
NODE_VERSIONS : join(",", var.node_versions),
|
||||
DEFAULT : var.default_node_version,
|
||||
PRE_INSTALL_SCRIPT : var.pre_install_script != null ? var.pre_install_script : "",
|
||||
POST_INSTALL_SCRIPT : var.post_install_script != null ? var.post_install_script : "",
|
||||
})
|
||||
run_on_start = true
|
||||
start_blocks_login = true
|
||||
}
|
||||
|
||||
resource "coder_script" "nodejs_post_install" {
|
||||
count = var.post_install_script != null ? 1 : 0
|
||||
agent_id = var.agent_id
|
||||
display_name = "Node.js: Post-Install"
|
||||
run_on_start = true
|
||||
script = <<-EOT
|
||||
#!/bin/bash
|
||||
set -o errexit
|
||||
set -o pipefail
|
||||
|
||||
trap 'coder exp sync complete ${local.post_install_script_name}' EXIT
|
||||
coder exp sync want ${local.post_install_script_name} ${local.install_script_name}
|
||||
coder exp sync start ${local.post_install_script_name}
|
||||
|
||||
echo -n '${local.encoded_post_install_script}' | base64 -d > ${local.module_dir_path}/post_install.sh
|
||||
chmod +x ${local.module_dir_path}/post_install.sh
|
||||
|
||||
${local.module_dir_path}/post_install.sh 2>&1
|
||||
EOT
|
||||
}
|
||||
|
||||
output "pre_install_script_name" {
|
||||
description = "The name of the pre-install script for sync."
|
||||
value = local.pre_install_script_name
|
||||
}
|
||||
|
||||
output "install_script_name" {
|
||||
description = "The name of the install script for sync."
|
||||
value = local.install_script_name
|
||||
}
|
||||
|
||||
output "post_install_script_name" {
|
||||
description = "The name of the post-install script for sync."
|
||||
value = local.post_install_script_name
|
||||
}
|
||||
|
||||
@ -4,10 +4,17 @@ NVM_VERSION='${NVM_VERSION}'
|
||||
NODE_VERSIONS='${NODE_VERSIONS}'
|
||||
INSTALL_PREFIX='${INSTALL_PREFIX}'
|
||||
DEFAULT='${DEFAULT}'
|
||||
PRE_INSTALL_SCRIPT='${PRE_INSTALL_SCRIPT}'
|
||||
POST_INSTALL_SCRIPT='${POST_INSTALL_SCRIPT}'
|
||||
BOLD='\033[0;1m'
|
||||
CODE='\033[36;40;1m'
|
||||
RESET='\033[0m'
|
||||
|
||||
if [ -n "$${PRE_INSTALL_SCRIPT}" ]; then
|
||||
printf "$${BOLD}Running pre-install script...$${RESET}\n"
|
||||
eval "$${PRE_INSTALL_SCRIPT}"
|
||||
fi
|
||||
|
||||
printf "$${BOLD}Installing nvm!$${RESET}\n"
|
||||
|
||||
export NVM_DIR="$HOME/$${INSTALL_PREFIX}/nvm"
|
||||
@ -51,3 +58,8 @@ if [ -n "$${DEFAULT}" ]; then
|
||||
printf "🛠️ Setting default node version $${CODE}$DEFAULT$${RESET}...\n"
|
||||
output=$(nvm alias default $DEFAULT 2>&1)
|
||||
fi
|
||||
|
||||
if [ -n "$${POST_INSTALL_SCRIPT}" ]; then
|
||||
printf "$${BOLD}Running post-install script...$${RESET}\n"
|
||||
eval "$${POST_INSTALL_SCRIPT}"
|
||||
fi
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user