diff --git a/registry/coder/modules/kasmvnc/README.md b/registry/coder/modules/kasmvnc/README.md index 0e6d24c..e071867 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.1.0" 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 4265f3c..28f3a21 100644 --- a/registry/coder/modules/kasmvnc/main.tf +++ b/registry/coder/modules/kasmvnc/main.tf @@ -29,32 +29,42 @@ 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" + run_on_start = true script = templatefile("${path.module}/run.sh", { - PORT : var.port, - DESKTOP_ENVIRONMENT : var.desktop_environment, - KASM_VERSION : var.kasm_version + PORT = var.port, + DESKTOP_ENVIRONMENT = var.desktop_environment, + KASM_VERSION = var.kasm_version + SUBDOMAIN = tostring(var.subdomain) + PATH_VNC_HTML = var.subdomain ? "" : file("${path.module}/path_vnc.html") }) - run_on_start = true } 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 = true + subdomain = var.subdomain share = "owner" + healthcheck { url = "http://localhost:${var.port}/app" interval = 5 diff --git a/registry/coder/modules/kasmvnc/path_vnc.html b/registry/coder/modules/kasmvnc/path_vnc.html new file mode 100644 index 0000000..849ec77 --- /dev/null +++ b/registry/coder/modules/kasmvnc/path_vnc.html @@ -0,0 +1,81 @@ + + + + Path-Sharing Bounce Page + + + + +

Path-Sharing Bounce Page

+

+ 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. +

+ + + diff --git a/registry/coder/modules/kasmvnc/run.sh b/registry/coder/modules/kasmvnc/run.sh index c285b05..67a8a31 100644 --- a/registry/coder/modules/kasmvnc/run.sh +++ b/registry/coder/modules/kasmvnc/run.sh @@ -3,6 +3,8 @@ # 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() { if command -v vncserver &> /dev/null; then @@ -188,7 +190,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!" @@ -206,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 @@ -220,16 +223,82 @@ 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 + echo "🩹 Patching up webserver files to support path-sharing..." + 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. Check full logs at /tmp/kasmvncserver.log" + +set +e +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 "$VNC_LOG" + else + echo "ERROR: Log file not found: $VNC_LOG" + fi exit 1 fi + printf "🚀 KasmVNC server started successfully!\n"