diff --git a/code-server/README.md b/code-server/README.md index c505e83..375d0e2 100644 --- a/code-server/README.md +++ b/code-server/README.md @@ -79,3 +79,30 @@ module "code-server" { extensions = ["dracula-theme.theme-dracula", "ms-azuretools.vscode-docker"] } ``` + +### Offline and Use Cached Modes + +By default the module looks for code-server at `/tmp/code-server` but this can be changed with `install_prefix`. + +Run an existing copy of code-server if found, otherwise download from GitHub: + +```tf +module "code-server" { + source = "registry.coder.com/modules/code-server/coder" + version = "1.0.8" + agent_id = coder_agent.example.id + use_cached = true + extensions = ["dracula-theme.theme-dracula", "ms-azuretools.vscode-docker"] +} +``` + +Just run code-server in the background, don't fetch it from GitHub: + +```tf +module "code-server" { + source = "registry.coder.com/modules/code-server/coder" + version = "1.0.8" + agent_id = coder_agent.example.id + offline = true +} +``` diff --git a/code-server/main.test.ts b/code-server/main.test.ts index daf3ac1..1d6da5e 100644 --- a/code-server/main.test.ts +++ b/code-server/main.test.ts @@ -1,5 +1,9 @@ import { describe, expect, it } from "bun:test"; -import { runTerraformInit, testRequiredVariables } from "../test"; +import { + runTerraformApply, + runTerraformInit, + testRequiredVariables, +} from "../test"; describe("code-server", async () => { await runTerraformInit(import.meta.dir); @@ -8,5 +12,27 @@ describe("code-server", async () => { agent_id: "foo", }); + it("use_cached and offline can not be used together", () => { + const t = async () => { + await runTerraformApply(import.meta.dir, { + agent_id: "foo", + use_cached: "true", + offline: "true", + }); + }; + expect(t).toThrow("Offline and Use Cached can not be used together"); + }); + + it("offline and extensions can not be used together", () => { + const t = async () => { + await runTerraformApply(import.meta.dir, { + agent_id: "foo", + offline: "true", + extensions: '["1", "2"]', + }); + }; + expect(t).toThrow("Offline mode does not allow extensions to be installed"); + }); + // More tests depend on shebang refactors }); diff --git a/code-server/main.tf b/code-server/main.tf index dc0e6bd..30b92bc 100644 --- a/code-server/main.tf +++ b/code-server/main.tf @@ -83,6 +83,18 @@ variable "order" { default = null } +variable "offline" { + type = bool + description = "Just run code-server in the background, don't fetch it from GitHub" + default = false +} + +variable "use_cached" { + type = bool + description = "Uses cached copy code-server in the background, otherwise fetched it from GitHub" + default = false +} + resource "coder_script" "code-server" { agent_id = var.agent_id display_name = "code-server" @@ -96,8 +108,22 @@ resource "coder_script" "code-server" { INSTALL_PREFIX : var.install_prefix, // This is necessary otherwise the quotes are stripped! SETTINGS : replace(jsonencode(var.settings), "\"", "\\\""), + OFFLINE : var.offline, + USE_CACHED : var.use_cached, }) run_on_start = true + + lifecycle { + precondition { + condition = !var.offline || length(var.extensions) == 0 + error_message = "Offline mode does not allow extensions to be installed" + } + + precondition { + condition = !var.offline || !var.use_cached + error_message = "Offline and Use Cached can not be used together" + } + } } resource "coder_app" "code-server" { diff --git a/code-server/run.sh b/code-server/run.sh index f6229f3..2444324 100755 --- a/code-server/run.sh +++ b/code-server/run.sh @@ -4,6 +4,34 @@ EXTENSIONS=("${EXTENSIONS}") BOLD='\033[0;1m' CODE='\033[36;40;1m' RESET='\033[0m' +CODE_SERVER="${INSTALL_PREFIX}/bin/code-server" + +function run_code_server() { + echo "👷 Running code-server in the background..." + echo "Check logs at ${LOG_PATH}!" + $CODE_SERVER --auth none --port "${PORT}" --app-name "${APP_NAME}" > "${LOG_PATH}" 2>&1 & +} + +# Check if the settings file exists... +if [ ! -f ~/.local/share/code-server/User/settings.json ]; then + echo "⚙️ Creating settings file..." + mkdir -p ~/.local/share/code-server/User + echo "${SETTINGS}" > ~/.local/share/code-server/User/settings.json +fi + +# Check if code-server is already installed for offline or cached mode +if [ -f "$CODE_SERVER" ]; then + if [ "${OFFLINE}" = true ] || [ "${USE_CACHED}" = true ]; then + echo "🥳 Found a copy of code-server" + run_code_server + exit 0 + fi +fi +# Offline mode always expects a copy of code-server to be present +if [ "${OFFLINE}" = true ]; then + echo "Failed to find a copy of code-server" + exit 1 +fi printf "$${BOLD}Installing code-server!\n" @@ -22,8 +50,6 @@ if [ $? -ne 0 ]; then fi printf "🥳 code-server has been installed in ${INSTALL_PREFIX}\n\n" -CODE_SERVER="${INSTALL_PREFIX}/bin/code-server" - # Install each extension... IFS=',' read -r -a EXTENSIONLIST <<< "$${EXTENSIONS}" for extension in "$${EXTENSIONLIST[@]}"; do @@ -38,13 +64,4 @@ for extension in "$${EXTENSIONLIST[@]}"; do fi done -# Check if the settings file exists... -if [ ! -f ~/.local/share/code-server/User/settings.json ]; then - echo "⚙️ Creating settings file..." - mkdir -p ~/.local/share/code-server/User - echo "${SETTINGS}" > ~/.local/share/code-server/User/settings.json -fi - -echo "👷 Running code-server in the background..." -echo "Check logs at ${LOG_PATH}!" -$CODE_SERVER --auth none --port ${PORT} --app-name "${APP_NAME}" > ${LOG_PATH} 2>&1 & +run_code_server