Skip to content

Commit

Permalink
Merge pull request #150 from Barts-Life-Science/deploy-custom-vms
Browse files Browse the repository at this point in the history
Deploy custom vms
  • Loading branch information
BIOKU-BH authored Sep 17, 2024
2 parents e6dc4a4 + 671ae1b commit 9304426
Show file tree
Hide file tree
Showing 8 changed files with 105 additions and 80 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,10 @@ custom:
install_ui: true
conda_config: false
# For information on using custom images, see README.me in the guacamole/user-resources folder
# "Custom Image From Gallery":
# source_image_name: your-image
# install_ui: true
# conda_config: true
"Custom Ubuntu 22.04 LTS":
source_image_name: imgdef-linux-dsvm-rpython
install_ui: true
conda_config: true

credentials:
- name: azure_tenant_id
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
"title": "Linux image",
"description": "Select Linux image to use for VM",
"enum": [
"Custom Ubuntu 22.04 LTS",
"Ubuntu 22.04 LTS"
]
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ apt:
deb [trusted=yes] $PRIMARY $RELEASE main restricted universe multiverse
deb [trusted=yes] $PRIMARY $RELEASE-updates main restricted universe multiverse
deb [trusted=yes] $SECURITY $RELEASE main restricted universe multiverse
deb [signed-by=/etc/apt/trusted.gpg.d/microsoft.gpg] ${nexus_proxy_url}/repository/microsoft-apt/ubuntu/${apt_sku}/prod $RELEASE main
deb [signed-by=/etc/apt/trusted.gpg.d/microsoft.gpg] ${nexus_proxy_url}/repository/microsoft-apt/ubuntu/__VERSION_ID__/prod $RELEASE main
deb [signed-by=/etc/apt/trusted.gpg.d/microsoft.gpg] ${nexus_proxy_url}/repository/microsoft-apt/repos/edge stable main
deb [signed-by=/etc/apt/trusted.gpg.d/microsoft.gpg] ${nexus_proxy_url}/repository/microsoft-apt/repos/vscode stable main
deb [signed-by=/etc/apt/trusted.gpg.d/microsoft.gpg] ${nexus_proxy_url}/repository/microsoft-apt/repos/azure-cli $RELEASE main
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,6 @@ data "template_file" "vm_config" {
NEXUS_PROXY_URL = local.nexus_proxy_url
CONDA_CONFIG = local.selected_image.conda_config ? 1 : 0
VM_USER = random_string.username.result
APT_SKU = replace(local.apt_sku, ".", "")
}
}

Expand All @@ -133,7 +132,6 @@ data "template_file" "apt_sources_config" {
template = file("${path.module}/apt_sources_config.yml")
vars = {
nexus_proxy_url = local.nexus_proxy_url
apt_sku = local.apt_sku
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,7 @@ locals {
# selected_image_source_refs is an array to enable easy use of a dynamic block
selected_image_source_refs = lookup(local.selected_image, "source_image_reference", null) == null ? [] : [local.selected_image.source_image_reference]
selected_image_source_id = lookup(local.selected_image, "source_image_name", null) == null ? null : "${var.image_gallery_id}/images/${local.selected_image.source_image_name}"
apt_sku = local.selected_image_source_refs[0]["apt_sku"]
# apt_sku = local.selected_image_source_refs[0]["apt_sku"]
# apt_sku = length(local.selected_image_source_refs) > 0 ? local.selected_image_source_refs[0]["apt_sku"] : "unknown_sku"

}
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,13 @@ set -o nounset
# set -o xtrace

# Remove apt sources not included in sources.list file
echo "init_vm.sh: APT sources"
sudo rm -f /etc/apt/sources.list.d/*

# shellcheck disable=SC1091
. /etc/os-release
sed -i "s%__VERSION_ID__%$VERSION_ID%" /etc/apt/sources.list

# Update apt packages from configured Nexus sources
echo "init_vm.sh: START"
sudo apt update || true
Expand All @@ -35,7 +40,75 @@ sudo -u "${VM_USER}" -i bash -c 'echo xset -dpms >> ~/.xsession'
sudo systemctl enable xrdp
sudo service xrdp restart

# Prevent screen timeout
echo "init_vm.sh: Preventing Timeout"
sudo mkdir -p /home/"${VM_USER}"/.config/xfce4/xfconf/xfce-perchannel-xml
sudo touch /home/"${VM_USER}"/.config/xfce4/xfconf/xfce-perchannel-xml/xfce4-screensaver.xml
sudo chmod 664 /home/"${VM_USER}"/.config/xfce4/xfconf/xfce-perchannel-xml/xfce4-screensaver.xml
sudo tee /home/"${VM_USER}"/.config/xfce4/xfconf/xfce-perchannel-xml/xfce4-screensaver.xml << END
<?xml version="1.0" encoding="UTF-8"?>
<channel name="xfce4-screensaver" version="1.0">
<property name="saver" type="empty">
<property name="mode" type="int" value="0"/>
<property name="enabled" type="bool" value="false"/>
</property>
<property name="lock" type="empty">
<property name="enabled" type="bool" value="false"/>
</property>
</ channel>
END
sudo chown -Rf "${VM_USER}":"${VM_USER}" /home/"${VM_USER}"/.config

# Fix for blank screen on DSVM (/sh -> /bash due to conflict with profile.d scripts)
sudo sed -i 's|!/bin/sh|!/bin/bash|g' /etc/xrdp/startwm.sh

if [ "${SHARED_STORAGE_ACCESS}" -eq 1 ]; then
# Install required packages
echo "init_vm.sh: Shared storage"
sudo apt-get install autofs -y

# Pass in required variables
storageAccountName="${STORAGE_ACCOUNT_NAME}"
storageAccountKey="${STORAGE_ACCOUNT_KEY}"
httpEndpoint="${HTTP_ENDPOINT}"
fileShareName="${FILESHARE_NAME}"
# Configure for permanent mount instead of autofs
mntRoot="/shared-storage"
credentialRoot="/etc/smbcredentials"

# mntPath="$mntRoot/$fileShareName"
# shellcheck disable=SC2308
smbPath=$(echo "$httpEndpoint" | cut -c7-"$(expr length "$httpEndpoint")")$fileShareName
smbCredentialFile="$credentialRoot/$storageAccountName.cred"

# Create required file paths
# sudo mkdir -p "$mntPath"
sudo mkdir -p $credentialRoot
sudo mkdir -p $mntRoot

### Auto FS to persist storage
# Create credential file
if [ ! -f "$smbCredentialFile" ]; then
echo "username=$storageAccountName" | sudo tee "$smbCredentialFile" > /dev/null
echo "password=$storageAccountKey" | sudo tee -a "$smbCredentialFile" > /dev/null
else
echo "The credential file $smbCredentialFile already exists, and was not modified."
fi

# Change permissions on the credential file so only root can read or modify the password file.
sudo chmod 600 "$smbCredentialFile"

echo "$smbPath $mntRoot cifs rw,vers=default,dir_mode=0777,file_mode=0777,uid=1000,gid=1000,credentials=$smbCredentialFile 0 0" | sudo tee -a /etc/fstab >/dev/null
sudo mount $mntRoot
fi

# set +o errexit
# set +o pipefail
# set +o nounset
set -o xtrace

## Python 3.8 and Jupyter
echo "init_vm.sh: Jupyter, Edge"
sudo apt install -y jupyter-notebook microsoft-edge-dev

## VS Code
Expand All @@ -47,7 +120,7 @@ echo "init_vm.sh: Folders"
sudo mkdir -p /opt/vscode/user-data
sudo mkdir -p /opt/vscode/extensions

# echo "init_vm.sh: azure-cli"
echo "init_vm.sh: azure-cli"
sudo apt install azure-cli -y

# TODO: need to look at proxy extentions
Expand Down Expand Up @@ -83,58 +156,19 @@ sudo apt install -y r-base

# RStudio Desktop
echo "init_vm.sh: RStudio"
wget "${NEXUS_PROXY_URL}"/repository/r-studio-download/electron/jammy/amd64/rstudio-2023.12.1-402-amd64.deb -P /tmp/2204
wget "${NEXUS_PROXY_URL}"/repository/r-studio-download/electron/focal/amd64/rstudio-2023.12.1-402-amd64.deb -P /tmp/2004
sudo gdebi --non-interactive /tmp/"${APT_SKU}"/rstudio-2023.12.1-402-amd64.deb

# Fix for blank screen on DSVM (/sh -> /bash due to conflict with profile.d scripts)
sudo sed -i 's|!/bin/sh|!/bin/bash|g' /etc/xrdp/startwm.sh

if [ "${SHARED_STORAGE_ACCESS}" -eq 1 ]; then
# Install required packages
sudo apt-get install autofs -y

# Pass in required variables
storageAccountName="${STORAGE_ACCOUNT_NAME}"
storageAccountKey="${STORAGE_ACCOUNT_KEY}"
httpEndpoint="${HTTP_ENDPOINT}"
fileShareName="${FILESHARE_NAME}"
# Configure for permanent mount instead of autofs
mntRoot="/shared-storage"
credentialRoot="/etc/smbcredentials"

# mntPath="$mntRoot/$fileShareName"
# shellcheck disable=SC2308
smbPath=$(echo "$httpEndpoint" | cut -c7-"$(expr length "$httpEndpoint")")$fileShareName
smbCredentialFile="$credentialRoot/$storageAccountName.cred"

# Create required file paths
# sudo mkdir -p "$mntPath"
sudo mkdir -p $credentialRoot
sudo mkdir -p $mntRoot

### Auto FS to persist storage
# Create credential file
if [ ! -f "$smbCredentialFile" ]; then
echo "username=$storageAccountName" | sudo tee "$smbCredentialFile" > /dev/null
echo "password=$storageAccountKey" | sudo tee -a "$smbCredentialFile" > /dev/null
else
echo "The credential file $smbCredentialFile already exists, and was not modified."
fi

# Change permissions on the credential file so only root can read or modify the password file.
sudo chmod 600 "$smbCredentialFile"

echo "$smbPath $mntRoot cifs rw,vers=default,dir_mode=0777,file_mode=0777,uid=1000,gid=1000,credentials=$smbCredentialFile 0 0" | sudo tee -a /etc/fstab >/dev/null
sudo mount $mntRoot
fi
wget "${NEXUS_PROXY_URL}"/repository/r-studio-download/electron/jammy/amd64/rstudio-2023.12.1-402-amd64.deb -P /tmp/
# wget "${NEXUS_PROXY_URL}"/repository/r-studio-download/electron/focal/amd64/rstudio-2023.12.1-402-amd64.deb -P /tmp/
sudo gdebi --non-interactive /tmp/rstudio-2023.12.1-402-amd64.deb

### Anaconda Config
if [ "${CONDA_CONFIG}" -eq 1 ]; then
echo "init_vm.sh: Anaconda"
export PATH="/anaconda/condabin":$PATH
export PATH="/anaconda/bin":$PATH
export PATH="/anaconda/envs/py38_default/bin":$PATH
if [ -d "/anaconda" ]; then
export PATH="/anaconda/condabin:/anaconda/bin:$/anaconda/envs/py38_default/bin":$PATH
fi
if [ -d "/opt/anaconda" ]; then
export PATH="/opt/anaconda/condabin:/opt/anaconda/bin":$PATH
fi
conda config --add channels "${NEXUS_PROXY_URL}"/repository/conda-mirror/main/ --system
conda config --add channels "${NEXUS_PROXY_URL}"/repository/conda-repo/main/ --system
conda config --remove channels defaults --system
Expand All @@ -160,24 +194,9 @@ sudo sed -i -e 's/Terminal=true/Terminal=false/g' /usr/share/applications/jupyte
# Default Browser
sudo update-alternatives --config x-www-browser

# Prevent screen timeout
echo "init_vm.sh: Preventing Timeout"
sudo mkdir -p /home/"${VM_USER}"/.config/xfce4/xfconf/xfce-perchannel-xml
sudo touch /home/"${VM_USER}"/.config/xfce4/xfconf/xfce-perchannel-xml/xfce4-screensaver.xml
sudo chmod 664 /home/"${VM_USER}"/.config/xfce4/xfconf/xfce-perchannel-xml/xfce4-screensaver.xml
sudo tee /home/"${VM_USER}"/.config/xfce4/xfconf/xfce-perchannel-xml/xfce4-screensaver.xml << END
<?xml version="1.0" encoding="UTF-8"?>
<channel name="xfce4-screensaver" version="1.0">
<property name="saver" type="empty">
<property name="mode" type="int" value="0"/>
<property name="enabled" type="bool" value="false"/>
</property>
<property name="lock" type="empty">
<property name="enabled" type="bool" value="false"/>
</property>
</ channel>
END
sudo chown -Rf "${VM_USER}":"${VM_USER}" /home/"${VM_USER}"/.config
echo "init_vm.sh: environment"
echo "export NEXUS_PROXY_URL=${NEXUS_PROXY_URL}" > /etc/profile.d/99-sde-environment.sh
env | sort

## Cleanup
echo "init_vm.sh: Cleanup"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,12 @@ custom:
version: latest
conda_config: true
# For information on using custom images, see README.me in the guacamole/user-resources folder
# "Custom Image From Gallery":
# source_image_name: your-image
# conda_config: true
"Custom Server 2019 Data Science VM":
source_image_name: imgdef-windows11-dsvm-rpython
conda_config: true
"Custom Windows 10":
source_image_name: imgdef-windows10-dsvm-rpython
conda_config: true

credentials:
- name: azure_tenant_id
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,11 @@
"title": "Windows image",
"description": "Select Windows image to use for VM",
"enum": [
"Custom Server 2019 Data Science VM",
"Custom Windows 10",
"Server 2019 Data Science VM",
"Windows 10",
"Windows 11",
"Server 2019 Data Science VM"
"Windows 11"
]
},
"vm_size": {
Expand Down

0 comments on commit 9304426

Please sign in to comment.