From 06f7dd5dcd26aa4b56c994502d7dfea4f809ed43 Mon Sep 17 00:00:00 2001 From: Muhammad Atif Ali Date: Sun, 24 Sep 2023 14:11:04 +0300 Subject: [PATCH] add VS Code Web module (#25) --- vscode-web/README.md | 36 +++++++++++++++++++ vscode-web/main.tf | 82 ++++++++++++++++++++++++++++++++++++++++++++ vscode-web/run.sh | 15 ++++++++ 3 files changed, 133 insertions(+) create mode 100644 vscode-web/README.md create mode 100644 vscode-web/main.tf create mode 100644 vscode-web/run.sh diff --git a/vscode-web/README.md b/vscode-web/README.md new file mode 100644 index 0000000..c53e1d5 --- /dev/null +++ b/vscode-web/README.md @@ -0,0 +1,36 @@ +--- +display_name: vscode-web +description: VS Code Web - Visual Studio Code in the browser +icon: ../.icons/code.svg +maintainer_github: coder +verified: true +tags: [helper, ide, vscode, web] +--- + +# VS Code Web + +Automatically install [VS Code](https://code.visualstudio.com) in a workspace, create an app to access it via the dashboard. + +## Examples + +1. Install VS Code Web with default settings: + + ```hcl + module "vscode-web" { + source = "https://registry.coder.com/modules/vscode-web" + agent_id = coder_agent.example.id + accept_license = true + } + ``` + +2. Install VS Code Web with custom version and folder + + ```hcl + module "vscode-web" { + source = "https://registry.coder.com/modules/vscode-web" + agent_id = coder_agent.example.id + version = "1.82.0" + folder = "/home/coder/my-projet" + acept_license = true + } + ``` diff --git a/vscode-web/main.tf b/vscode-web/main.tf new file mode 100644 index 0000000..2bf6348 --- /dev/null +++ b/vscode-web/main.tf @@ -0,0 +1,82 @@ +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 "port" { + type = number + description = "The port to run VS Code Wbe on." + default = 13338 +} + +variable "folder" { + type = string + description = "The folder to open in VS Code Web." + default = "" +} + +variable "log_path" { + type = string + description = "The path to log." + default = "/tmp/vscode-web.log" +} + +variable "accept_license" { + type = bool + description = "Accept the VS Code license. https://code.visualstudio.com/license" + default = false + validation { + condition = var.accept_license == true + error_message = "You must accept the VS Code license agreement by setting accept_license=true." + } +} + +variable "version" { + type = string + description = "The version of VS Code to install." + default = "latest" + # add a validation block to validate the version is greater than or equal to 1.82.0 + validation { + condition = var.version >= "1.82.0" + error_message = "Version must be greater than or equal to 1.82.0" + } +} + +resource "coder_script" "vscode-web" { + agent_id = var.agent_id + display_name = "VS Code Web" + icon = "/icon/code.svg" + script = templatefile("${path.module}/run.sh", { + PORT : var.port, + LOG_PATH : var.log_path, + VERSION : var.version, + }) + run_on_start = true +} + +resource "coder_app" "vscode-web" { + agent_id = var.agent_id + slug = "vscode-web" + display_name = "VS Code Web" + url = "http://localhost:${var.port}/?folder=${var.folder}" + icon = "/icon/code.svg" + subdomain = true + share = "owner" + + healthcheck { + url = "http://localhost:${var.port}/healthz" + interval = 5 + threshold = 6 + } +} diff --git a/vscode-web/run.sh b/vscode-web/run.sh new file mode 100644 index 0000000..2e82307 --- /dev/null +++ b/vscode-web/run.sh @@ -0,0 +1,15 @@ +#!/usr/bin/env sh + +BOLD='\033[0;1m' +# check if +printf "$${BOLD}Installing VS Code!\n" +output=$(curl -L "https://update.code.visualstudio.com/${VERSION}/linux-deb-x64/stable" -o /tmp/code.deb && sudo dpkg -i /tmp/code.deb && sudo apt-get install -f -y) +if [ $? -ne 0 ]; then + echo "Failed to install VS Code: $output" + exit 1 +fi +printf "🥳 VS code has been installed.\n\n" + +echo "👷 Running code serve-web in the background..." +echo "Check logs at ${LOG_PATH}!" +code serve-web --port ${PORT} --without-connection-token --accept-server-license-terms >${LOG_PATH} 2>&1 &