Refactor KasmVNC installation script logic

- Simplify and modularize the installation process
- Add mapping for different Linux distributions and architectures
- Validate the desktop environment to ensure compatibility
pull/250/head
Muhammad Atif Ali 10 months ago
parent de525a3f3e
commit 6017b05bc1

@ -9,7 +9,7 @@ tags: [helper, vnc, desktop]
# KasmVNC # KasmVNC
Automatically install [KasmVNC](https://kasmweb.com/kasmvnc) in a workspace, and create an app to access it via the dashboard. Add latest version of KasmVNC with [`xfce`](https://xfce.org/) desktop environment. Automatically install [KasmVNC](https://kasmweb.com/kasmvnc) in a workspace, and create an app to access it via the dashboard.
```tf ```tf
module "kasmvnc" { module "kasmvnc" {
@ -19,4 +19,4 @@ module "kasmvnc" {
} }
``` ```
> **Note:** This module only works on debian-based workspaces. It is recommended to use an image with a desktop environment pre-installed to speed up the installation process. > **Note:** This module only works on workspaces with a pre-installed desktop environment.

@ -26,10 +26,13 @@ variable "kasm_version" {
default = "1.3.1" default = "1.3.1"
} }
variable "wait_for_script" { variable "desktop_environment" {
type = string type = string
description = "The script to wait for before running the KasmVNC script." description = "Specifies the desktop environment of the workspace. This should be pre-installed on the workspace."
default = "" validation {
condition = contains(["xfce", "kde", "gnome", "lxde", "lxqt", "xfce"], var.desktop_environment)
error_message = "Invalid desktop environment. Please specify a valid desktop environment."
}
} }
resource "coder_script" "kasm_vnc" { resource "coder_script" "kasm_vnc" {
@ -38,7 +41,7 @@ resource "coder_script" "kasm_vnc" {
icon = "/icon/kasmvnc.svg" icon = "/icon/kasmvnc.svg"
script = templatefile("${path.module}/run.sh", { script = templatefile("${path.module}/run.sh", {
PORT : var.port, PORT : var.port,
WAIT_FOR_SCRIPT : var.wait_for_script, DESKTOP_ENVIRONMENT : var.desktop_environment,
VERSION : var.kasm_version VERSION : var.kasm_version
}) })
run_on_start = true run_on_start = true

@ -1,38 +1,137 @@
#!/usr/bin/env bash #!/usr/bin/env bash
# check if there is a WAIT_FOR_SCRIPT env variable and if so, wait for it to be available #!/bin/bash
# Wait for the startup script to complete # Function to check if vncserver is already installed
if [ -n "${WAIT_FOR_SCRIPT}" ]; then check_installed() {
# This assumes that the script will create a file called /tmp/.coder-${WAIT_FOR_SCRIPT}.done if command -v vncserver &> /dev/null; then
while [ ! -f /tmp/.coder-${WAIT_FOR_SCRIPT}.done ]; do echo "vncserver is already installed."
sleep 1 return 0 # Don't exit, just indicate it's installed
done else
fi return 1 # Indicates not installed
fi
}
# Function to install kasmvncserver for debian-based distros
install_deb() {
local url=$1
wget $url -O /tmp/kasmvncserver.deb
sudo apt-get install ./kasmvncserver_*.deb -y
sudo adduser $USER ssl-cert
rm /tmp/kasmvncserver.deb
}
# Function to install kasmvncserver for Oracle 8
install_rpm_oracle8() {
local url=$1
wget $url -O /tmp/kasmvncserver.rpm
sudo dnf config-manager --set-enabled ol8_codeready_builder
sudo dnf install oracle-epel-release-el8 -y
sudo dnf localinstall ./kasmvncserver_*.rpm -y
sudo usermod -a -G kasmvnc-cert $USER
rm /tmp/kasmvncserver.rpm
}
# Function to install kasmvncserver for CentOS 7
install_rpm_centos7() {
local url=$1
wget $url -O /tmp/kasmvncserver.rpm
sudo yum install epel-release -y
sudo yum install ./kasmvncserver_*.rpm -y
sudo usermod -a -G kasmvnc-cert $USER
rm /tmp/kasmvncserver.rpm
}
PACKAGES="xfce4 xfce4-goodies libdatetime-perl" # Function to install kasmvncserver for rpm-based distros
# Check if desktop environment is installed install_rpm() {
if ! dpkg -s $PACKAGES &> /dev/null; then local url=$1
sudo apt-get update wget $url -O /tmp/kasmvncserver.rpm
DEBIAN_FRONTEND=noninteractive sudo apt-get install -y $PACKAGES --no-install-recommends --no-install-suggests sudo rpm -i /tmp/kasmvncserver.rpm
rm /tmp/kasmvncserver.rpm
}
# Function to install kasmvncserver for Alpine Linux
install_alpine() {
local url=$1
wget $url -O /tmp/kasmvncserver.tgz
tar -xzf /tmp/kasmvncserver.tgz -C /usr/local/bin/
rm /tmp/kasmvncserver.tgz
}
# Detect system information
distro=$(grep "^ID=" /etc/os-release | awk -F= '{print $2}')
version=$(grep "^VERSION_ID=" /etc/os-release | awk -F= '{print $2}' | tr -d '"')
arch=$(uname -m)
echo "Detected Distribution: $distro"
echo "Detected Version: $version"
echo "Detected Architecture: $arch"
# Map arch to package arch
if [[ "$arch" == "x86_64" ]]; then
arch="x86_64"
elif [[ "$arch" == "aarch64" || "$arch" == "arm64" ]]; then
arch="aarch64"
else else
echo "$PACKAGES are already installed." echo "Unsupported architecture: $arch"
exit 1
fi fi
# Check if vncserver is installed # Check if vncserver is installed, and install if not
if ! dpkg -s kasmvncserver &> /dev/null; then if ! check_installed; then
DISTRO=$(lsb_release -c -s) case $distro in
ARCH=$(dpkg --print-architecture) ubuntu | debian | kali)
wget -q https://github.com/kasmtech/KasmVNC/releases/download/v${VERSION}/kasmvncserver_$${DISTRO}_${VERSION}_$${ARCH}.deb -O /tmp/kasmvncserver.deb case $version in
sudo apt-get install -y /tmp/kasmvncserver.deb "18.04")
printf "🥳 KasmVNC v${VERSION} has been successfully installed!\n\n" install_deb "https://github.com/kasmtech/KasmVNC/releases/download/v${VERSION}/kasmvncserver_bionic_${VERSION}_$${arch}.deb"
sudo rm -f /tmp/kasmvncserver.deb ;;
"20.04")
install_deb "https://github.com/kasmtech/KasmVNC/releases/download/v${VERSION}/kasmvncserver_focal_${VERSION}_$${arch}.deb"
;;
"22.04")
install_deb "https://github.com/kasmtech/KasmVNC/releases/download/v${VERSION}/kasmvncserver_jammy_${VERSION}_$${arch}.deb"
;;
*)
echo "Unsupported Ubuntu/Debian/Kali version: $${version}"
exit 1
;;
esac
;;
oracle)
if [[ "$version" == "8" ]]; then
install_rpm_oracle8 "https://github.com/kasmtech/KasmVNC/releases/download/v${VERSION}/kasmvncserver_oracle_8_${VERSION}_$${arch}.rpm"
else
echo "Unsupported Oracle version: $${version}"
exit 1
fi
;;
centos)
if [[ "$version" == "7" ]]; then
install_rpm_centos7 "https://github.com/kasmtech/KasmVNC/releases/download/v${VERSION}/kasmvncserver_centos_core_${VERSION}_$${arch}.rpm"
else
install_rpm "https://github.com/kasmtech/KasmVNC/releases/download/v${VERSION}/kasmvncserver_centos_core_${VERSION}_$${arch}.rpm"
fi
;;
alpine)
if [[ "$version" == "3.17" || "$version" == "3.18" || "$version" == "3.19" ]]; then
install_alpine "https://github.com/kasmtech/KasmVNC/releases/download/v${VERSION}/kasmvnc.alpine_$${version}_$${arch}.tgz"
else
echo "Unsupported Alpine version: $${version}"
exit 1
fi
;;
fedora | opensuse)
install_rpm "https://github.com/kasmtech/KasmVNC/releases/download/v${VERSION}/kasmvncserver_$${distro}_$${version}_${VERSION}_$${arch}.rpm"
;;
*)
echo "Unsupported distribution: $${distro}"
exit 1
;;
esac
else else
echo "KasmVNC is already installed." echo "vncserver already installed. Skipping installation."
fi fi
sudo addgroup $USER ssl-cert
# Coder port-forwarding from dashboard only supports HTTP # Coder port-forwarding from dashboard only supports HTTP
sudo bash -c "cat > /etc/kasmvnc/kasmvnc.yaml <<EOF sudo bash -c "cat > /etc/kasmvnc/kasmvnc.yaml <<EOF
network: network:
@ -46,9 +145,9 @@ EOF"
# This password is not used since we start the server without auth. # This password is not used since we start the server without auth.
# The server is protected via the Coder session token / tunnel # The server is protected via the Coder session token / tunnel
# and does not listen publicly on the VM # and does not listen publicly
echo -e "password\npassword\n" | vncpasswd -wo -u $USER echo -e "password\npassword\n" | vncpasswd -wo -u $USER
# Start the server # Start the server
printf "🚀 Starting KasmVNC server...\n" printf "🚀 Starting KasmVNC server...\n"
sudo -u $USER bash -c 'vncserver -select-de xfce -disableBasicAuth' > /tmp/kassmvncserver.log 2>&1 & sudo -u $USER bash -c "vncserver -select-de ${DESKTOP_ENVIRONMENT} -disableBasicAuth" > /tmp/kasmvncserver.log 2>&1 &

Loading…
Cancel
Save