From 8a9482690f7254263549ed7fe696bcc8ad37f9d7 Mon Sep 17 00:00:00 2001 From: FlorianGareis Date: Sat, 17 Feb 2024 18:36:19 +0000 Subject: [PATCH] Add initial version of the node-via-nvm module --- .icons/node.svg | 1 + node-via-nvm/README.md | 58 +++++++++++++++++++++++++++++++++++++++ node-via-nvm/main.test.ts | 12 ++++++++ node-via-nvm/main.tf | 50 +++++++++++++++++++++++++++++++++ node-via-nvm/run.sh | 41 +++++++++++++++++++++++++++ 5 files changed, 162 insertions(+) create mode 100644 .icons/node.svg create mode 100644 node-via-nvm/README.md create mode 100644 node-via-nvm/main.test.ts create mode 100644 node-via-nvm/main.tf create mode 100755 node-via-nvm/run.sh diff --git a/.icons/node.svg b/.icons/node.svg new file mode 100644 index 0000000..e33a588 --- /dev/null +++ b/.icons/node.svg @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/node-via-nvm/README.md b/node-via-nvm/README.md new file mode 100644 index 0000000..8c50287 --- /dev/null +++ b/node-via-nvm/README.md @@ -0,0 +1,58 @@ +--- +display_name: node-via-nvm +description: Install node via nvm +icon: ../.icons/node.svg +maintainer_github: coder +verified: true +tags: [node, nvm, nodejs] +--- + +# node-via-nvm + +Automatically installs the latest version of [node](https://github.com/nodejs/node) versions via [nvm](https://github.com/nvm-sh/nvm). It can also install multiple versions of node and set a default version. + +```tf +module "node-via-nvm" { + source = "registry.coder.com/modules/node-via-nvm/coder" + version = "1.0.2" + agent_id = coder_agent.example.id +} +``` + + +### Install multiple versions + +This installs multiple versions of node + +```tf +module "node-via-nvm" { + source = "registry.coder.com/modules/node-via-nvm/coder" + version = "1.0.2" + agent_id = coder_agent.example.id + node_versions = [ + "18" + "20" + "node" + ] + default_node_version = "20" +} +``` + +### Full example + +A example with all available options: +```tf +module "node-via-nvm" { + source = "registry.coder.com/modules/node-via-nvm/coder" + version = "1.0.2" + agent_id = coder_agent.example.id + nvm_version = "v0.39.7" + nvm_install_prefix = "/opt/nvm" + node_versions = [ + "16" + "18" + "node" + ] + default_node_version = "16" +} +``` diff --git a/node-via-nvm/main.test.ts b/node-via-nvm/main.test.ts new file mode 100644 index 0000000..daf3ac1 --- /dev/null +++ b/node-via-nvm/main.test.ts @@ -0,0 +1,12 @@ +import { describe, expect, it } from "bun:test"; +import { runTerraformInit, testRequiredVariables } from "../test"; + +describe("code-server", async () => { + await runTerraformInit(import.meta.dir); + + testRequiredVariables(import.meta.dir, { + agent_id: "foo", + }); + + // More tests depend on shebang refactors +}); diff --git a/node-via-nvm/main.tf b/node-via-nvm/main.tf new file mode 100644 index 0000000..51fd192 --- /dev/null +++ b/node-via-nvm/main.tf @@ -0,0 +1,50 @@ +terraform { + required_version = ">= 1.0" + + required_providers { + coder = { + source = "coder/coder" + version = ">= 0.12" + } + } +} + +variable "agent_id" { + type = string + description = "The ID of a Coder agent." +} + +variable "nvm_version" { + type = string + description = "The version of nvm to install." + default = "master" +} + +variable "nvm_install_prefix" { + type = string + description = "The prefix to install nvm to." + default = "$HOME/.nvm" +} + +variable "node_versions" { + type = list(string) + description = "A list of node versions to install." + default = ["node"] +} + +variable "default_node_version" { + type = string + description = "The default node version" + default = "node" +} + +resource "coder_script" "node" { + agent_id = var.agent_id + 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, + }) + run_on_start = true +} diff --git a/node-via-nvm/run.sh b/node-via-nvm/run.sh new file mode 100755 index 0000000..1dc3ce9 --- /dev/null +++ b/node-via-nvm/run.sh @@ -0,0 +1,41 @@ +#!/usr/bin/env bash + +NODE_VERSIONS=("${NODE_VERSIONS}") +BOLD='\033[0;1m' +CODE='\033[36;40;1m' +RESET='\033[0m' + +printf "${BOLD}Installing nvm!${RESET}\n" + +output=$(curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/${NVM_VERSION}/install.sh | NVM_DIR=${INSTALL_PREFIX} bash) +if [ $? -ne 0 ]; then + echo "Failed to install nvm: $output" + exit 1 +fi +printf "🥳 nvm has been installed\n\n" + +NVM="${INSTALL_PREFIX}/nvm.sh" + +# Set up nvm in the current shell session +export NVM_DIR="$([ -z "${XDG_CONFIG_HOME-}" ] && printf %s "${HOME}/.nvm" || printf %s "${XDG_CONFIG_HOME}/nvm")" +[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh" + +# Install each node version... +IFS=',' read -r -a VERSIONLIST <<< "${NODE_VERSIONS}" +for version in "${VERSIONLIST[@]}"; do + if [ -z "$version" ]; then + continue + fi + printf "🛠️ Installing node version ${CODE}$version${RESET}...\n" + output=$(nvm install "$version") + if [ $? -ne 0 ]; then + echo "Failed to install version: $version: $output" + exit 1 + fi +done + +# Set default if provided +if [ -n "${DEFAULT}" ]; then + printf "🛠️ Setting default node version ${CODE}$DEFAULT${RESET}...\n" + output=$(nvm alias default $DEFAULT) +fi