summaryrefslogtreecommitdiffstats
path: root/core/modules/run-virt/data/opt/openslx/scripts/vmchooser-xml_filter
blob: 2d2df3042e23d19ec97e857f703de68f776ba18c (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#!/bin/bash
# Probably needs bash somewhere
# -----------------------------------------------------------------------------
#
# Copyright (c) 2007..2018 bwLehrpool-Projektteam
#
# This program/file is free software distributed under the GPL version 2.
# See https://www.gnu.org/licenses/old-licenses/gpl-2.0-standalone.html
#
# If you have any feedback please consult https://bwlehrpool.de and
# send your feedback to support@bwlehrpool.de.
#
# General information about bwLehrpool can be found at https://bwlehrpool.de
#
# -----------------------------------------------------------------------------
# xmlfilter.sh
#    - This script is invoked by the vmchooser tool. It simply filters xml-
#      files (taking the path to these files in $1). You might modify it in any
#      way to match your needs, e.g. ask some database instead. You can re-
#      implement it in any other programming language too. You simply have to
#      return a list of proper xml files to be interpreted by the vmchooser
#      binary). Please check for vmchooser.sh too ...
# -----------------------------------------------------------------------------

# This script .
#
# currently:
#     - filter for slxgrp (which comes from /etc/machine-setup)
#

# include default directories
. /opt/openslx/config

if [ -f "${OPENSLX_DEFAULT_CONFDIR}/plugins/vmchooser/vmchooser.conf" ]; then
	. "${OPENSLX_DEFAULT_CONFDIR}/plugins/vmchooser/vmchooser.conf"
fi

function handlePersistentVM() {
	if grep --extended-regexp -q "<persistent param=\"(|.+:)$USER(|:.+)\"" "$1"; then
		# If this virtual machine is useable as persistent version for current
		# user we provide an additional persistent version.
		# TODO: This evil black magic needs to be rewritten some day (by someone who understands it)
		local originalImagePath=$(grep -io '<image_name param=.*"' "$1" | sed -e "s/&.*;/; /g" | awk -F '"' '{ print $2 }')
		local imageName=$(basename "$originalImagePath")
		eval export SLX_VM_PERSISTENT_PATH="${SLX_VM_PERSISTENT_PATH}" && \
			local imagePath="${SLX_VM_PERSISTENT_PATH}${imageName}"
		local persistentConfigVersionFilePath="$(mktemp --directory)/$(basename "$1")"
		local vmchooserFilePath="$(dirname "$0")/vmchooser-clc"
		if [ ! -f "$vmchooserFilePath" ]; then
			vmchooserFilePath='/opt/openslx/scripts/vmchooser-clc'
		fi
		"$vmchooserFilePath" "$1" "$persistentConfigVersionFilePath" \
			--create-persistent-config "$imagePath" && \
			echo "$persistentConfigVersionFilePath"
	fi
}
	
for FILE in $(find -L "$1" -iname "*.xml"); do
	# filter all xmls which aren't set active
	grep -i -q "<active param=.*true.*" "$FILE" || continue
	# HACK: filter all virtualbox images, as vbox is not ready yet
	grep -i -q "<virtualmachine param=.*virtualbox.*" "$FILE" && continue
	if [ -n "${SLX_VM_POOL_FILTER}" ]; then
		# filter all xmls with pool-param not equal to vmchooser::env
		if [ $(grep "<pools param=\"${SLX_VM_POOL_FILTER}\"" "$FILE" | wc -l) -eq 1 ]; then
			handlePersistentVM "$FILE"
			echo "$FILE"
		fi
	else
		handlePersistentVM "$FILE"
		# if there is no pool set, just take all available xmls
		echo "$FILE"
	fi
done