From 8d8374937459ee7058c2acf532bbb5d3e69bfade Mon Sep 17 00:00:00 2001 From: Charlie Voiselle <464492+angrycub@users.noreply.github.com> Date: Mon, 19 May 2025 21:07:16 -0400 Subject: [PATCH 01/11] add subdomain plumbing --- registry/coder/modules/kasmvnc/README.md | 3 ++- registry/coder/modules/kasmvnc/main.tf | 20 ++++++++++++++------ 2 files changed, 16 insertions(+), 7 deletions(-) diff --git a/registry/coder/modules/kasmvnc/README.md b/registry/coder/modules/kasmvnc/README.md index df548d1c..bed11e98 100644 --- a/registry/coder/modules/kasmvnc/README.md +++ b/registry/coder/modules/kasmvnc/README.md @@ -15,9 +15,10 @@ Automatically install [KasmVNC](https://kasmweb.com/kasmvnc) in a workspace, and module "kasmvnc" { count = data.coder_workspace.me.start_count source = "registry.coder.com/coder/kasmvnc/coder" - version = "1.0.23" + version = "1.0.24" agent_id = coder_agent.example.id desktop_environment = "xfce" + subdomain = true } ``` diff --git a/registry/coder/modules/kasmvnc/main.tf b/registry/coder/modules/kasmvnc/main.tf index 4265f3c7..1b64bf7a 100644 --- a/registry/coder/modules/kasmvnc/main.tf +++ b/registry/coder/modules/kasmvnc/main.tf @@ -29,22 +29,29 @@ variable "kasm_version" { variable "desktop_environment" { type = string description = "Specifies the desktop environment of the workspace. This should be pre-installed on the workspace." + validation { condition = contains(["xfce", "kde", "gnome", "lxde", "lxqt"], var.desktop_environment) error_message = "Invalid desktop environment. Please specify a valid desktop environment." } } +variable "subdomain" { + type = bool + default = true + description = "Is subdomain sharing enabled in your cluster?" +} + resource "coder_script" "kasm_vnc" { agent_id = var.agent_id display_name = "KasmVNC" icon = "/icon/kasmvnc.svg" - script = templatefile("${path.module}/run.sh", { - PORT : var.port, - DESKTOP_ENVIRONMENT : var.desktop_environment, - KASM_VERSION : var.kasm_version - }) run_on_start = true + script = templatefile("${path.module}/run.sh", { + PORT = var.port, + DESKTOP_ENVIRONMENT = var.desktop_environment, + KASM_VERSION = var.kasm_version + }) } resource "coder_app" "kasm_vnc" { @@ -53,8 +60,9 @@ resource "coder_app" "kasm_vnc" { display_name = "kasmVNC" url = "http://localhost:${var.port}" icon = "/icon/kasmvnc.svg" - subdomain = true + subdomain = var.subdomain share = "owner" + healthcheck { url = "http://localhost:${var.port}/app" interval = 5 From b89bb084160c12f10e8ebd21ed7b42b070784e83 Mon Sep 17 00:00:00 2001 From: Charlie Voiselle <464492+angrycub@users.noreply.github.com> Date: Thu, 22 May 2025 14:02:26 -0400 Subject: [PATCH 02/11] add the bounce page --- registry/coder/modules/kasmvnc/path_vnc.html | 81 ++++++++++++++++++++ 1 file changed, 81 insertions(+) create mode 100644 registry/coder/modules/kasmvnc/path_vnc.html diff --git a/registry/coder/modules/kasmvnc/path_vnc.html b/registry/coder/modules/kasmvnc/path_vnc.html new file mode 100644 index 00000000..849ec777 --- /dev/null +++ b/registry/coder/modules/kasmvnc/path_vnc.html @@ -0,0 +1,81 @@ + + +
++ This application is being served via path sharing. + If you are not redirected, check the + Javascript console in your browser's developer tools + for more information. +
+ + + From bb711d09aaaf82de20e11c56d71cfdbb091849fd Mon Sep 17 00:00:00 2001 From: Charlie Voiselle <464492+angrycub@users.noreply.github.com> Date: Thu, 22 May 2025 14:09:12 -0400 Subject: [PATCH 03/11] wire up bounce page to run script --- registry/coder/modules/kasmvnc/main.tf | 4 +- registry/coder/modules/kasmvnc/run.sh | 59 ++++++++++++++++++++++++++ 2 files changed, 62 insertions(+), 1 deletion(-) diff --git a/registry/coder/modules/kasmvnc/main.tf b/registry/coder/modules/kasmvnc/main.tf index 1b64bf7a..211f63e9 100644 --- a/registry/coder/modules/kasmvnc/main.tf +++ b/registry/coder/modules/kasmvnc/main.tf @@ -47,10 +47,12 @@ resource "coder_script" "kasm_vnc" { display_name = "KasmVNC" icon = "/icon/kasmvnc.svg" run_on_start = true - script = templatefile("${path.module}/run.sh", { + script = templatefile("${path.module}/run.sh", { PORT = var.port, DESKTOP_ENVIRONMENT = var.desktop_environment, KASM_VERSION = var.kasm_version + SUBDOMAIN = tostring(var.subdomain) + PATH_VNC_HTML = var.subdomain ? "" : "${path.module}/path_vnc.html" }) } diff --git a/registry/coder/modules/kasmvnc/run.sh b/registry/coder/modules/kasmvnc/run.sh index c285b050..ea8cd056 100644 --- a/registry/coder/modules/kasmvnc/run.sh +++ b/registry/coder/modules/kasmvnc/run.sh @@ -2,6 +2,7 @@ # Exit on error, undefined variables, and pipe failures set -euo pipefail +error() { printf "💀 ERROR: %s\n" "$@"; exit 1; } # Function to check if vncserver is already installed check_installed() { @@ -220,6 +221,64 @@ EOF # and does not listen publicly echo -e "password\npassword\n" | vncpasswd -wo -u "$USER" +get_http_dir() { + # determine the served file path + # Start with the default + httpd_directory="/usr/share/kasmvnc/www" + + # Check the system configuration path + if [[ -e /etc/kasmvnc/kasmvnc.yaml ]]; then + d=($(grep -E "^\s*httpd_directory:.*$" /etc/kasmvnc/kasmvnc.yaml)) + # If this grep is successful, it will return: + # httpd_directory: /usr/share/kasmvnc/www + if [[ $${#d[@]} -eq 2 && -d "$${d[1]}" ]]; then + httpd_directory="$${d[1]}" + fi + fi + + # Check the home directory for overriding values + if [[ -e "$HOME/.vnc/kasmvnc.yaml" ]]; then + d=($(grep -E "^\s*httpd_directory:.*$" /etc/kasmvnc/kasmvnc.yaml)) + if [[ $${#d[@]} -eq 2 && -d "$${d[1]}" ]]; then + httpd_directory="$${d[1]}" + fi + fi + echo $httpd_directory +} + +fix_server_index_file(){ + local fname=$${FUNCNAME[0]} # gets current function name + if [[ $# -ne 1 ]]; then + error "$fname requires exactly 1 parameter:\n\tpath to KasmVNC httpd_directory" + fi + local httpdir="$1" + if [[ ! -d "$httpdir" ]]; then + error "$fname: $httpdir is not a directory" + fi + pushd "$httpdir" > /dev/null + + cat <<'EOH' > /tmp/path_vnc.html +${PATH_VNC_HTML} +EOH + $SUDO mv /tmp/path_vnc.html . + # check for the switcheroo + if [[ -f "index.html" && -L "vnc.html" ]]; then + $SUDO mv $httpdir/index.html $httpdir/vnc.html + fi + $SUDO ln -s -f path_vnc.html index.html + popd > /dev/null +} + +patch_kasm_http_files(){ + homedir=$(get_http_dir) + fix_server_index_file "$homedir" +} + +if [[ "${SUBDOMAIN}" == "false" ]]; then + info "🩹 Patching up webserver files to support path-sharing..." + patch_kasm_http_files +fi + # Start the server printf "🚀 Starting KasmVNC server...\n" vncserver -select-de "${DESKTOP_ENVIRONMENT}" -disableBasicAuth > /tmp/kasmvncserver.log 2>&1 & From ccd4b0c29c29d8f0ac15db06b015f42e3ee373a0 Mon Sep 17 00:00:00 2001 From: Charlie Voiselle <464492+angrycub@users.noreply.github.com> Date: Thu, 22 May 2025 14:10:10 -0400 Subject: [PATCH 04/11] drive-by: make empty value obvious --- registry/coder/modules/kasmvnc/run.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/registry/coder/modules/kasmvnc/run.sh b/registry/coder/modules/kasmvnc/run.sh index ea8cd056..97fd0f82 100644 --- a/registry/coder/modules/kasmvnc/run.sh +++ b/registry/coder/modules/kasmvnc/run.sh @@ -189,7 +189,7 @@ if command -v sudo &> /dev/null && sudo -n true 2> /dev/null; then SUDO=sudo else kasm_config_file="$HOME/.vnc/kasmvnc.yaml" - SUDO= + SUDO="" echo "WARNING: Sudo access not available, using user config dir!" From 0f41a422ec94a920b269e8e3ac512220bcfbdc40 Mon Sep 17 00:00:00 2001 From: Charlie Voiselle <464492+angrycub@users.noreply.github.com> Date: Thu, 22 May 2025 14:10:38 -0400 Subject: [PATCH 05/11] update default kasmvnc version --- registry/coder/modules/kasmvnc/main.tf | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/registry/coder/modules/kasmvnc/main.tf b/registry/coder/modules/kasmvnc/main.tf index 211f63e9..fdfc3ce0 100644 --- a/registry/coder/modules/kasmvnc/main.tf +++ b/registry/coder/modules/kasmvnc/main.tf @@ -23,7 +23,7 @@ variable "port" { variable "kasm_version" { type = string description = "Version of KasmVNC to install." - default = "1.3.2" + default = "1.3.4" } variable "desktop_environment" { From bec8d0bf029ddaab34b2a4bc42fc57b795d0918c Mon Sep 17 00:00:00 2001 From: Charlie Voiselle <464492+angrycub@users.noreply.github.com> Date: Thu, 22 May 2025 14:12:02 -0400 Subject: [PATCH 06/11] emit kasmvnc logs to the workspace build logs on startup failure --- registry/coder/modules/kasmvnc/main.tf | 4 ++-- registry/coder/modules/kasmvnc/run.sh | 11 +++++++++-- 2 files changed, 11 insertions(+), 4 deletions(-) diff --git a/registry/coder/modules/kasmvnc/main.tf b/registry/coder/modules/kasmvnc/main.tf index fdfc3ce0..685a09ad 100644 --- a/registry/coder/modules/kasmvnc/main.tf +++ b/registry/coder/modules/kasmvnc/main.tf @@ -23,7 +23,7 @@ variable "port" { variable "kasm_version" { type = string description = "Version of KasmVNC to install." - default = "1.3.4" + default = "1.3.2" } variable "desktop_environment" { @@ -52,7 +52,7 @@ resource "coder_script" "kasm_vnc" { DESKTOP_ENVIRONMENT = var.desktop_environment, KASM_VERSION = var.kasm_version SUBDOMAIN = tostring(var.subdomain) - PATH_VNC_HTML = var.subdomain ? "" : "${path.module}/path_vnc.html" + PATH_VNC_HTML = var.subdomain ? "" : file("${path.module}/path_vnc.html") }) } diff --git a/registry/coder/modules/kasmvnc/run.sh b/registry/coder/modules/kasmvnc/run.sh index 97fd0f82..55ec3b56 100644 --- a/registry/coder/modules/kasmvnc/run.sh +++ b/registry/coder/modules/kasmvnc/run.sh @@ -2,6 +2,7 @@ # Exit on error, undefined variables, and pipe failures set -euo pipefail + error() { printf "💀 ERROR: %s\n" "$@"; exit 1; } # Function to check if vncserver is already installed @@ -275,7 +276,7 @@ patch_kasm_http_files(){ } if [[ "${SUBDOMAIN}" == "false" ]]; then - info "🩹 Patching up webserver files to support path-sharing..." + echo "🩹 Patching up webserver files to support path-sharing..." patch_kasm_http_files fi @@ -288,7 +289,13 @@ pid=$! sleep 5 grep -v '^[[:space:]]*$' /tmp/kasmvncserver.log | tail -n 10 if ps -p $pid | grep -q "^$pid"; then - echo "ERROR: Failed to start KasmVNC server. Check full logs at /tmp/kasmvncserver.log" + echo "ERROR: Failed to start KasmVNC server. Printing logs from /tmp/kasmvncserver.log" + if [[ -f /tmp/kasmvncserver.log ]]; then + echo "Full logs:" + cat /tmp/kasmvncserver.log + else + echo "ERROR: Log file not found: /tmp/kasmvncserver.log" + fi exit 1 fi printf "🚀 KasmVNC server started successfully!\n" From e3840c434e18d71769cd3539f6e7069f7a2e19d6 Mon Sep 17 00:00:00 2001 From: Charlie Voiselle <464492+angrycub@users.noreply.github.com> Date: Thu, 22 May 2025 17:10:15 -0400 Subject: [PATCH 07/11] add log output on server failure --- registry/coder/modules/kasmvnc/run.sh | 23 ++++++++++++----------- 1 file changed, 12 insertions(+), 11 deletions(-) diff --git a/registry/coder/modules/kasmvnc/run.sh b/registry/coder/modules/kasmvnc/run.sh index 55ec3b56..d5955ea1 100644 --- a/registry/coder/modules/kasmvnc/run.sh +++ b/registry/coder/modules/kasmvnc/run.sh @@ -280,22 +280,23 @@ if [[ "${SUBDOMAIN}" == "false" ]]; then patch_kasm_http_files fi +VNC_LOG="/tmp/kasmvncserver.log" # Start the server printf "🚀 Starting KasmVNC server...\n" -vncserver -select-de "${DESKTOP_ENVIRONMENT}" -disableBasicAuth > /tmp/kasmvncserver.log 2>&1 & -pid=$! - -# Wait for server to start -sleep 5 -grep -v '^[[:space:]]*$' /tmp/kasmvncserver.log | tail -n 10 -if ps -p $pid | grep -q "^$pid"; then - echo "ERROR: Failed to start KasmVNC server. Printing logs from /tmp/kasmvncserver.log" - if [[ -f /tmp/kasmvncserver.log ]]; then +set +e +# shellcheck disable=SC2086 +vncserver -select-de "${DESKTOP_ENVIRONMENT}" -disableBasicAuth > "$VNC_LOG" 2>&1 +RETVAL=$? +set -e +if [[ $RETVAL -ne 0 ]]; then + echo "ERROR: Failed to start KasmVNC server. Return code: $RETVAL" + if [[ -f "$VNC_LOG" ]]; then echo "Full logs:" - cat /tmp/kasmvncserver.log + cat "$VNC_LOG" else - echo "ERROR: Log file not found: /tmp/kasmvncserver.log" + echo "ERROR: Log file not found: $VNC_LOG" fi exit 1 fi + printf "🚀 KasmVNC server started successfully!\n" From 64b282f33e4cd3c4286c13fcc9906bd46236c0aa Mon Sep 17 00:00:00 2001 From: Charlie Voiselle <464492+angrycub@users.noreply.github.com> Date: Thu, 22 May 2025 17:15:13 -0400 Subject: [PATCH 08/11] run kasmvnc listener on localhost --- registry/coder/modules/kasmvnc/run.sh | 1 + 1 file changed, 1 insertion(+) diff --git a/registry/coder/modules/kasmvnc/run.sh b/registry/coder/modules/kasmvnc/run.sh index d5955ea1..5ba4a27b 100644 --- a/registry/coder/modules/kasmvnc/run.sh +++ b/registry/coder/modules/kasmvnc/run.sh @@ -208,6 +208,7 @@ echo "Writing KasmVNC config to $kasm_config_file" $SUDO tee "$kasm_config_file" > /dev/null << EOF network: protocol: http + interface: 127.0.0.1 websocket_port: ${PORT} ssl: require_ssl: false From db65a2b1c0b47624598021e692bc7e681a6a8824 Mon Sep 17 00:00:00 2001 From: Charlie Voiselle <464492+angrycub@users.noreply.github.com> Date: Thu, 22 May 2025 17:28:03 -0400 Subject: [PATCH 09/11] fix brand name --- registry/coder/modules/kasmvnc/main.tf | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/registry/coder/modules/kasmvnc/main.tf b/registry/coder/modules/kasmvnc/main.tf index 685a09ad..28f3a21d 100644 --- a/registry/coder/modules/kasmvnc/main.tf +++ b/registry/coder/modules/kasmvnc/main.tf @@ -59,7 +59,7 @@ resource "coder_script" "kasm_vnc" { resource "coder_app" "kasm_vnc" { agent_id = var.agent_id slug = "kasm-vnc" - display_name = "kasmVNC" + display_name = "KasmVNC" url = "http://localhost:${var.port}" icon = "/icon/kasmvnc.svg" subdomain = var.subdomain From 4fd38f3801e27aa465b985c0c66418d8c090aca3 Mon Sep 17 00:00:00 2001 From: Charlie Voiselle <464492+angrycub@users.noreply.github.com> Date: Thu, 22 May 2025 17:59:26 -0400 Subject: [PATCH 10/11] remove shellcheck disable --- registry/coder/modules/kasmvnc/run.sh | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/registry/coder/modules/kasmvnc/run.sh b/registry/coder/modules/kasmvnc/run.sh index 5ba4a27b..67a8a310 100644 --- a/registry/coder/modules/kasmvnc/run.sh +++ b/registry/coder/modules/kasmvnc/run.sh @@ -284,11 +284,12 @@ fi VNC_LOG="/tmp/kasmvncserver.log" # Start the server printf "🚀 Starting KasmVNC server...\n" + set +e -# shellcheck disable=SC2086 vncserver -select-de "${DESKTOP_ENVIRONMENT}" -disableBasicAuth > "$VNC_LOG" 2>&1 RETVAL=$? set -e + if [[ $RETVAL -ne 0 ]]; then echo "ERROR: Failed to start KasmVNC server. Return code: $RETVAL" if [[ -f "$VNC_LOG" ]]; then From e9fac913ad4c6cd61cb1d75139d0aed7331612ce Mon Sep 17 00:00:00 2001 From: Charlie Voiselle <464492+angrycub@users.noreply.github.com> Date: Fri, 23 May 2025 09:27:20 -0400 Subject: [PATCH 11/11] bump module minor version Co-authored-by: Atif Ali