diff options
author | Jonathan Bauer | 2018-12-12 14:27:04 +0100 |
---|---|---|
committer | Jonathan Bauer | 2018-12-12 14:27:04 +0100 |
commit | 227dc993f09592b57320130a0d6231fc2b60550b (patch) | |
tree | 4dc9057f7d5be4d02c28197725a39c7cd4ba2d9b /core/modules/run-virt | |
parent | [vmchooser] respect new urls params from resource_urls.conf (diff) | |
download | mltk-227dc993f09592b57320130a0d6231fc2b60550b.tar.gz mltk-227dc993f09592b57320130a0d6231fc2b60550b.tar.xz mltk-227dc993f09592b57320130a0d6231fc2b60550b.zip |
[run-virt] rework resource management
Diffstat (limited to 'core/modules/run-virt')
7 files changed, 73 insertions, 47 deletions
diff --git a/core/modules/run-virt/data/opt/openslx/vmchooser/run-virt-includes/download_vm_metadata.inc b/core/modules/run-virt/data/opt/openslx/vmchooser/run-virt-includes/download_vm_metadata.inc index df5a8367..8c680677 100644 --- a/core/modules/run-virt/data/opt/openslx/vmchooser/run-virt-includes/download_vm_metadata.inc +++ b/core/modules/run-virt/data/opt/openslx/vmchooser/run-virt-includes/download_vm_metadata.inc @@ -13,43 +13,35 @@ download_metadata() { EXIT_TYPE="internal" EXIT_REASON="Fehlendes Dienstprogramm 'wget'!" cleanexit 1 fi - if isempty SLX_VMCHOOSER_BASE_URL; then - writelog "SLX_VMCHOOSER_BASE_URL is not set! Was /opt/openslx/config sourced?" - EXIT_TYPE="internal" EXIT_REASON="msg.vmchooser.url-missing Keine URL zur Schnittstelle des bwLehrpool-Satelliten gefunden!" cleanexit 1 - fi - writelog "Detecting current/legacy mode..." + declare -rg TMPCONFIG="$CONFDIR/vmx" # Try new unified .tar.gz way TMPFILE="$TMPDIR/metadata.tgz" WLOG="$TMPDIR/wget-metadata.log" - curl -L -o "$TMPFILE" -w '\nretval:%{http_code}\n' --retry 3 --max-time 6 --retry-max-time 15 "${SLX_VMCHOOSER_BASE_URL}/lecture/${IMGUUID}/metadata" &> "$WLOG" + curl -L -o "$TMPFILE" -w '\nretval:%{http_code}\n' --retry 3 --max-time 6 --retry-max-time 15 "${url_lecture_metadata}" &> "$WLOG" ERRCODE=$( awk -F: '{ if ($1 == "retval") print $2 }' "$WLOG" ) RET=$? if [ "$RET" = 0 ] && [ -n "$ERRCODE" ] && [ "$ERRCODE" -ge 200 ] && [ "$ERRCODE" -lt 300 ]; then # Success, see if data is usable - mkdir -p "$TMPDIR/metadata" - if tar -C "$TMPDIR/metadata" -x -f "$TMPFILE" && [ -s "$TMPDIR/metadata/vmx" ]; then - declare -rg TMPCONFIG="$TMPDIR/metadata/vmx" - declare -rg CONFDIR="$TMPDIR/metadata" + if tar -C "$CONFDIR" -x -f "$TMPFILE" && [ -s "$CONFDIR/vmx" ]; then return 0 fi - rm -rf -- "$TMPDIR/metadata" "$TMPFILE" + rm -rf -- "$TMPFILE" writelog "Extracting metadata.tgz failed. Trying legacy mode." elif [ -n "$ERRCODE" ] && [ "$ERRCODE" -ge 500 ]; then slxlog --delete "run-virt-wget" "Downloading MetaData failed" "$WLOG" fi writelog "Metadata download failed, trying legacy approach..." - declare -rg TMPCONFIG="$TMPDIR/vmconfig.tmp" TRIES=0 while [ "$TRIES" -lt 3 ]; do TRIES=$(( TRIES + 1 )) writelog "wget try $TRIES" WLOG="$TMPDIR/wget-${TRIES}.log" - if wget -T 6 -O "$TMPCONFIG" "${SLX_VMCHOOSER_BASE_URL}/lecture/${IMGUUID}" &>"$WLOG"; then + if wget -T 6 -O "$TMPCONFIG" "${url_lecture_config}" &>"$WLOG"; then rm -f -- "$WLOG" - writelog "Downloaded VM description from '${SLX_VMCHOOSER_BASE_URL}/lecture/${IMGUUID}' successfully." + writelog "Downloaded VM description from '${url_lecture_config}' successfully." if [ -s "$TMPCONFIG" ]; then # Downloaded a non-zero VM description file, all good return 0 diff --git a/core/modules/run-virt/data/opt/openslx/vmchooser/run-virt-includes/get_xml_file_variables.inc b/core/modules/run-virt/data/opt/openslx/vmchooser/run-virt-includes/get_xml_file_variables.inc index 360cf7ed..7bf07458 100644 --- a/core/modules/run-virt/data/opt/openslx/vmchooser/run-virt-includes/get_xml_file_variables.inc +++ b/core/modules/run-virt/data/opt/openslx/vmchooser/run-virt-includes/get_xml_file_variables.inc @@ -17,6 +17,12 @@ parse_xml() { EXIT_TYPE="user" EXIT_REASON="Konnte keine ID für das ausgewählte Image ermitteln!" cleanexit 1 fi + # Set resource URLs for this lecture + [ -n "$url_lecture_config" ] && readonly url_lecture_config="${url_lecture_config//%UUID%/${IMGUUID}}" + [ -n "$url_lecture_metadata" ] && readonly url_lecture_metadata="${url_lecture_metadata//%UUID%/${IMGUUID}}" + [ -n "$url_lecture_netshares" ] && readonly url_lecture_netshares="${url_lecture_netshares//%UUID%/${IMGUUID}}" + [ -n "$url_lecture_runscript" ] && readonly url_lecture_runscript="${url_lecture_runscript//%UUID%/${IMGUUID}}" + # Relative / Absolute path to image declare -rg SRC_IMG_RELATIVE=$(get_xml "image_name") declare -g SRC_IMG_ABSOLUTE=$(get_xml "image_path") diff --git a/core/modules/run-virt/data/opt/openslx/vmchooser/run-virt-includes/init_core.inc b/core/modules/run-virt/data/opt/openslx/vmchooser/run-virt-includes/init_core.inc index 00211c94..b724a6f0 100644 --- a/core/modules/run-virt/data/opt/openslx/vmchooser/run-virt-includes/init_core.inc +++ b/core/modules/run-virt/data/opt/openslx/vmchooser/run-virt-includes/init_core.inc @@ -7,6 +7,7 @@ declare -rg VMCHOOSER_DIR="/opt/openslx/vmchooser" declare -rg VMCHOOSER_CONF_DIR="$VMCHOOSER_DIR/config" declare -rg USER="$(whoami)" declare -rg TMPDIR="/tmp/virt/${USER}/$$" +declare -rg CONFDIR="${TMPDIR}/metadata" # Check if the path to the logfile was given externally (by the wrapper) unset WRAPPED @@ -24,10 +25,11 @@ if notempty LOGFILE; then fi fi isset LOGFILE || declare -rg LOGFILE="/var/log/openslx/run-virt.${USER}.$$.log" +ln -sf "$LOGFILE" "/var/log/openslx/run-virt.latest.log" # Create temporary directory for current invocation -if check_dep mkdir && ! mkdir -p "$TMPDIR"; then - writelog "Could not create temporary directory '$TMPDIR' for session" +if check_dep mkdir && ! mkdir -p "$TMPDIR" "$CONFDIR"; then + writelog "Could not create temporary directories for session" EXIT_TYPE="internal" EXIT_REASON="Konnte kein Arbeitsverzeichnis für die VM-Sitzung anlegen." cleanexit 1 fi diff --git a/core/modules/run-virt/data/opt/openslx/vmchooser/run-virt-includes/load_configs.inc b/core/modules/run-virt/data/opt/openslx/vmchooser/run-virt-includes/load_configs.inc index bd205816..0f1fab39 100644 --- a/core/modules/run-virt/data/opt/openslx/vmchooser/run-virt-includes/load_configs.inc +++ b/core/modules/run-virt/data/opt/openslx/vmchooser/run-virt-includes/load_configs.inc @@ -2,6 +2,13 @@ # Include: Declaration of run-virt core variables # ################################################### ## Functions ## +check_base_url() { + if isempty SLX_VMCHOOSER_BASE_URL; then + writelog "SLX_VMCHOOSER_BASE_URL is not set! Was /opt/openslx/config sourced?" + EXIT_TYPE="internal" EXIT_REASON="msg.vmchooser.url-missing Keine URL zur Schnittstelle des bwLehrpool-Satelliten gefunden!" cleanexit 1 + fi + return 0 +} load_configs() { writelog "Loading configs..." @@ -13,6 +20,29 @@ load_configs() { # Load general openslx config $(safesource --exit "/opt/openslx/config") + + # Load config for resource URLs + $(safesource "$VMCHOOSER_CONF_DIR/resource_urls.conf") + if [ -z "$url_lecture_config" ]; then + writelog "URL for lecture config was not explicitly set, trying legacy method..." + check_base_url + url_lecture_config="${SLX_VMCHOOSER_BASE_URL}/lecture/%UUID%" + fi + if [ -z "$url_lecture_metadata" ]; then + writelog "URL for lecture metadata was not explicitly set, trying legacy method..." + check_base_url + url_lecture_metadata="${SLX_VMCHOOSER_BASE_URL}/lecture/%UUID%/metadata" + fi + if [ -z "$url_lecture_netshares" ]; then + writelog "URL for lecture netshares was not explicitly set, trying legacy method..." + check_base_url + url_lecture_netshares="${SLX_VMCHOOSER_BASE_URL}/lecture/%UUID%/netshares" + fi + if [ -z "$url_lecture_runscript" ]; then + writelog "URL for lecture runscript was not explicitly set, trying legacy method..." + check_base_url + url_lecture_runscript="${SLX_VMCHOOSER_BASE_URL}/lecture/%UUID%/runscript" + fi } ## MAIN ## diff --git a/core/modules/run-virt/data/opt/openslx/vmchooser/run-virt-includes/vmchooser_runvirt_functions.inc b/core/modules/run-virt/data/opt/openslx/vmchooser/run-virt-includes/vmchooser_runvirt_functions.inc index 54aceee5..b4f87d11 100644 --- a/core/modules/run-virt/data/opt/openslx/vmchooser/run-virt-includes/vmchooser_runvirt_functions.inc +++ b/core/modules/run-virt/data/opt/openslx/vmchooser/run-virt-includes/vmchooser_runvirt_functions.inc @@ -501,3 +501,22 @@ detect_cpu_flag() { done grep -m1 -qE '^flags\s*:.*\b('"${flags}"')\b' /proc/cpuinfo } + +# downloads the given URL to given file +download_file() { + [ $# -ne 2 ] && writelog "Usage: $0 <url> <path>." && return 1 + local _url="$1" + local _path="$2" + echo "Downloading '$_url' to '$_path'..." + if ! wget -T 6 -O "$_path" "$_url" 2> /dev/null >&2; then + writelog "Downloading '$_url' failed." + return 1 + fi + if [ ! -s "$_path" ]; then + # zero bytes, log and ignore + writelog "Downloaded resource from '$_url' has zero bytes." + return 1 + fi + return 0 +} + diff --git a/core/modules/run-virt/data/opt/openslx/vmchooser/run-virt.d/setup_virtual_floppy.inc b/core/modules/run-virt/data/opt/openslx/vmchooser/run-virt.d/setup_virtual_floppy.inc index 5729373a..10786257 100644 --- a/core/modules/run-virt/data/opt/openslx/vmchooser/run-virt.d/setup_virtual_floppy.inc +++ b/core/modules/run-virt/data/opt/openslx/vmchooser/run-virt.d/setup_virtual_floppy.inc @@ -2,34 +2,6 @@ # Include: Setup virtual floppy for drive b: # ############################################## ## Functions ## -# download the given resource for this lecture -# and makes it accessible globally through a -# variable of the same name. Given name will be -# put to lower case before wget'ing so its safe -# to use upper case variables... -load_dmsd_resource() { - [ $# -eq 0 ] && writelog "Missing argument for resource." && return - while [ $# -gt 0 ]; do - local RESOURCE="$1" - declare -g ${RESOURCE}="${TMPDIR}/${RESOURCE}" - local RESOURCE_URL="${SLX_VMCHOOSER_BASE_URL}/lecture/${IMGUUID}/$(echo $1 | awk '{print tolower($0)}')" - shift - echo "DOWNLOAD to ${!RESOURCE}" - if ! wget -T 6 -O "${!RESOURCE}" "${RESOURCE_URL}" 2> /dev/null >&2; then - writelog "Downloading runscript from '${RESOURCE_URL}' failed." - continue - fi - if [ ! -s "${!RESOURCE}" ]; then - # zero bytes, log and ignore - writelog "Downloaded resource from '${RESOURCE_URL}' has zero bytes." - unset "${RESOURCE}" - else - # there is stuff in it, make it readonly - readonly "${RESOURCE}" - fi - done - return 0 -} setup_virtual_floppy() { declare -rg SLX_FLOPPY_IMG="${TMPDIR}/floppy.img" declare -rg TMPHOME="${HOME}" @@ -88,7 +60,7 @@ setup_virtual_floppy() { # openslx.exe expects network shares line in the following format: # <path> <letter> <shortcut> <username> <password> # which is coincidentally the one we received from dmsd :) - if [ -f "$NETSHARES" ] || load_dmsd_resource NETSHARES; then + if [ -f "$NETSHARES" ] || download_file "$url_lecture_netshares" "$NETSHARES"; then if [ -s "${NETSHARES}" ]; then # try to detect if we are running windows and replace '/' with '\' # in an attempt to fix an improperly given samba path @@ -107,7 +79,7 @@ setup_virtual_floppy() { # Check downloaded runscript and handle options like # extension, visibility flag, ... # options are 'key=value' pairs separated with ';' - if [ -f "$RUNSCRIPT" ] || load_dmsd_resource RUNSCRIPT; then + if [ -f "$RUNSCRIPT" ] || download_file "$url_lecture_runscript" "$RUNSCRIPT"; then if [ -s "$RUNSCRIPT" ]; then IFS=$'\n' for OPT in $(head -n 1 "$RUNSCRIPT" | tr ";" "\n"); do diff --git a/core/modules/run-virt/data/opt/openslx/vmchooser/scripts/set-firewall b/core/modules/run-virt/data/opt/openslx/vmchooser/scripts/set-firewall index 2773150c..01c7472c 100644 --- a/core/modules/run-virt/data/opt/openslx/vmchooser/scripts/set-firewall +++ b/core/modules/run-virt/data/opt/openslx/vmchooser/scripts/set-firewall @@ -65,7 +65,12 @@ fi sort -u "${AUTORULES}" > "${RULES}" -wget -T 6 -O - "${SLX_VMCHOOSER_BASE_URL}/lecture/$1/netrules" >> "${RULES}" 2> "${AUTORULES}" +# determine the URL to download the netrules from +. /opt/openslx/vmchooser/config/resource_urls.conf +NETRULES_URL= +[ -n "$url_lecture_netrules" ] && NETRULES_URL="${url_lecture_netrules//%UUID%/${1}}" +[ -z "$NETRULES_URL" ] && NETRULES_URL="${SLX_VMCHOOSER_BASE_URL}/lecture/$1/netrules" +wget -T 6 -O - "${NETRULES_URL}" >> "${RULES}" 2> "${AUTORULES}" RET=$? if [ "$RET" != "0" ]; then |