From ad58a76ef794cfaa8e47470f4d83dd9e01fbdd79 Mon Sep 17 00:00:00 2001 From: Michael Smith Date: Wed, 16 Apr 2025 15:26:50 +0000 Subject: [PATCH] chore: add sample module files --- .sample/modules/README.md | 72 +++++++++++++++++++++++++ .sample/modules/main.tf | 108 ++++++++++++++++++++++++++++++++++++++ .sample/modules/run.sh | 26 +++++++++ 3 files changed, 206 insertions(+) create mode 100644 .sample/modules/README.md create mode 100644 .sample/modules/main.tf create mode 100644 .sample/modules/run.sh diff --git a/.sample/modules/README.md b/.sample/modules/README.md new file mode 100644 index 00000000..e2fb4151 --- /dev/null +++ b/.sample/modules/README.md @@ -0,0 +1,72 @@ +--- +display_name: MODULE_NAME +description: Describe what this module does +icon: ../.icons/.svg +maintainer_github: GITHUB_USERNAME +verified: false +tags: [helper] +--- + +# MODULE_NAME + + + +```tf +module "MODULE_NAME" { + count = data.coder_workspace.me.start_count + source = "registry.coder.com/modules/MODULE_NAME/coder" + version = "1.0.2" +} +``` + + + +## Examples + +### Example 1 + +Install the Dracula theme from [OpenVSX](https://open-vsx.org/): + +```tf +module "MODULE_NAME" { + count = data.coder_workspace.me.start_count + source = "registry.coder.com/modules/MODULE_NAME/coder" + version = "1.0.2" + agent_id = coder_agent.example.id + extensions = [ + "dracula-theme.theme-dracula" + ] +} +``` + +Enter the `.` into the extensions array and code-server will automatically install on start. + +### Example 2 + +Configure VS Code's [settings.json](https://code.visualstudio.com/docs/getstarted/settings#_settingsjson) file: + +```tf +module "MODULE_NAME" { + count = data.coder_workspace.me.start_count + source = "registry.coder.com/modules/MODULE_NAME/coder" + version = "1.0.2" + agent_id = coder_agent.example.id + extensions = [ "dracula-theme.theme-dracula" ] + settings = { + "workbench.colorTheme" = "Dracula" + } +} +``` + +### Example 3 + +Run code-server in the background, don't fetch it from GitHub: + +```tf +module "MODULE_NAME" { + source = "registry.coder.com/modules/MODULE_NAME/coder" + version = "1.0.2" + agent_id = coder_agent.example.id + offline = true +} +``` diff --git a/.sample/modules/main.tf b/.sample/modules/main.tf new file mode 100644 index 00000000..910320e3 --- /dev/null +++ b/.sample/modules/main.tf @@ -0,0 +1,108 @@ +terraform { + required_version = ">= 1.0" + + required_providers { + coder = { + source = "coder/coder" + version = ">= 0.17" + } + } +} + +locals { + # A built-in icon like "/icon/code.svg" or a full URL of icon + icon_url = "https://raw.githubusercontent.com/coder/coder/main/site/static/icon/code.svg" + # a map of all possible values + options = { + "Option 1" = { + "name" = "Option 1", + "value" = "1" + "icon" = "/emojis/1.png" + } + "Option 2" = { + "name" = "Option 2", + "value" = "2" + "icon" = "/emojis/2.png" + } + } +} + +# Add required variables for your modules and remove any unneeded variables +variable "agent_id" { + type = string + description = "The ID of a Coder agent." +} + +variable "log_path" { + type = string + description = "The path to log MODULE_NAME to." + default = "/tmp/MODULE_NAME.log" +} + +variable "port" { + type = number + description = "The port to run MODULE_NAME on." + default = 19999 +} + +variable "mutable" { + type = bool + description = "Whether the parameter is mutable." + default = true +} + +variable "order" { + type = number + description = "The order determines the position of app in the UI presentation. The lowest order is shown first and apps with equal order are sorted by name (ascending order)." + default = null +} +# Add other variables here + + +resource "coder_script" "MODULE_NAME" { + agent_id = var.agent_id + display_name = "MODULE_NAME" + icon = local.icon_url + script = templatefile("${path.module}/run.sh", { + LOG_PATH : var.log_path, + }) + run_on_start = true + run_on_stop = false +} + +resource "coder_app" "MODULE_NAME" { + agent_id = var.agent_id + slug = "MODULE_NAME" + display_name = "MODULE_NAME" + url = "http://localhost:${var.port}" + icon = local.icon_url + subdomain = false + share = "owner" + order = var.order + + # Remove if the app does not have a healthcheck endpoint + healthcheck { + url = "http://localhost:${var.port}/healthz" + interval = 5 + threshold = 6 + } +} + +data "coder_parameter" "MODULE_NAME" { + type = "list(string)" + name = "MODULE_NAME" + display_name = "MODULE_NAME" + icon = local.icon_url + mutable = var.mutable + default = local.options["Option 1"]["value"] + + dynamic "option" { + for_each = local.options + content { + icon = option.value.icon + name = option.value.name + value = option.value.value + } + } +} + diff --git a/.sample/modules/run.sh b/.sample/modules/run.sh new file mode 100644 index 00000000..f50f6baa --- /dev/null +++ b/.sample/modules/run.sh @@ -0,0 +1,26 @@ +#!/usr/bin/env sh + +# Convert templated variables to shell variables +# shellcheck disable=SC2269 +LOG_PATH=${LOG_PATH} + +# shellcheck disable=SC2034 +BOLD='\033[0;1m' + +# shellcheck disable=SC2059 +printf "$${BOLD}Installing MODULE_NAME ...\n\n" + +# Add code here +# Use varibles from the templatefile function in main.tf +# e.g. LOG_PATH, PORT, etc. + +printf "🥳 Installation comlete!\n\n" + +printf "👷 Starting MODULE_NAME in background...\n\n" +# Start the app in here +# 1. Use & to run it in background +# 2. redirct stdout and stderr to log files + +./app > "$${LOG_PATH}" 2>&1 & + +printf "check logs at %s\n\n" "$${LOG_PATH}"