Newer
Older
API_BASE_URL=https://labmanager-api.edu-al.unipmn.it/api/1.0

Alberto LIVIO BECCARIA
committed
MAX_RETRY=2
RETRY_DELAY=2
function retry {
local n=1
local max=$MAX_RETRY
local delay=$RETRY_DELAY
while true; do
"$@"
if [[ $? == 0 ]]; then
break
else
if [[ $n -lt $max ]]; then
((n++))
echo "Failed. Attempt $n/$max..." >&2
sleep $delay;
else
echo "Command failed after $n attempts." >&2
exit 1
fi
fi
done
}

Alberto LIVIO BECCARIA
committed
# store the whole response with the status at the end
HTTP_RESPONSE=$(curl -s -k -w "HTTPSTATUS:%{http_code}" --max-time 10 -X POST -H "Content-Type: application/json" -d "{\"login\":\"guest\",\"pwd\":\"\"}" ${API_BASE_URL}/login)

Alberto LIVIO BECCARIA
committed
ret=$?
if [[ $ret != 0 ]]; then
return $ret
fi

Alberto LIVIO BECCARIA
committed
# extract the status
HTTP_STATUS=$(echo "$HTTP_RESPONSE" | tr -d '\n' | sed -e 's/.*HTTPSTATUS://')
# check the status

Alberto LIVIO BECCARIA
committed
if [[ $HTTP_STATUS -ne 200 ]]; then
return 1

Alberto LIVIO BECCARIA
committed
else

Alberto LIVIO BECCARIA
committed
# extract the body
HTTP_BODY=$(echo "$HTTP_RESPONSE" | sed -e 's/HTTPSTATUS\:.*//g')

Alberto LIVIO BECCARIA
committed
echo $HTTP_BODY
fi
}
function jsonValue() {
KEY=$1
num=$2
awk -F"[,:}]" '{for(i=1;i<=NF;i++){if($i~/\042'$KEY'\042/){print $(i+1)}}}' | tr -d '"' | sed -n ${num}p
}
stop() {
echo "STOP ESAME-GET-CONFIG"
}
getConfigVars() {

Alberto LIVIO BECCARIA
committed
echo "Authenticating to WS..."
TOKEN_JSON=`retry getTokenJSON`
if [[ "$TOKEN_JSON" == "" ]]; then
return 1
fi
TOKEN=`echo ${TOKEN_JSON} | jsonValue token 1`
MACHINE_NAME=`hostname`

Alberto LIVIO BECCARIA
committed
echo "Getting machine configuration..."
MACHINE_JSON=`retry /usr/bin/curl -s -k --max-time 10 -H "Authorization: Bearer ${TOKEN}" -X GET ${API_BASE_URL}/machines/name/${MACHINE_NAME}`
MACHINE_ID=`echo $MACHINE_JSON | jsonValue id 1`
MACHINE_CONFIGS_ID=`echo $MACHINE_JSON | jsonValue configs_id 1`
CONFIG_NAME=`echo $MACHINE_JSON | jsonValue name 2`

Alberto LIVIO BECCARIA
committed
if [[ "$CONFIG_NAME" == "" ]]; then
return 1
fi
CONFIG_NAME_PREV=''
if [[ -e /local/config_name ]]
CONFIG_NAME_PREV=`cat /local/config_name`
getConfigFile() {
# get configuration file

Alberto LIVIO BECCARIA
committed
if [[ "${MACHINE_ID}" == "" ]]; then
echo "Error downloading config file (machine_id empty, maybe it is not in LabManager's DB)"
return 1
fi
HTTP_STATUS=`retry /usr/bin/curl -s -k -w "%{http_code}" -o /local/esame-machine.conf --max-time 10 --header "Authorization: Bearer ${TOKEN}" -X GET ${API_BASE_URL}/machines/${MACHINE_ID}/configfile`
if [[ $HTTP_STATUS -ne 200 ]]
then
echo "Error downloading config file (http_status=$HTTP_STATUS)"

Alberto LIVIO BECCARIA
committed
return 1
else
echo "Downloaded config file (http_status=$HTTP_STATUS)"
fi
getIpConfigFile() {
# get ipconfig file

Alberto LIVIO BECCARIA
committed
HTTP_STATUS=`retry /usr/bin/curl -s -k -w "%{http_code}" -o /local/iptables --max-time 10 --header "Authorization: Bearer ${TOKEN}" -X GET ${API_BASE_URL}/machines/${MACHINE_ID}/ipconfigfile`
if [[ $HTTP_STATUS -ne 200 ]]
then
echo "Error downloading iptables config file (http_status=$HTTP_STATUS)"

Alberto LIVIO BECCARIA
committed
return 1
else
echo "Downloaded iptables config file (http_status=$HTTP_STATUS)"
fi
}
updateCurrentConfigId() {
# Update this machine's current config id
HTTP_STATUS=`/usr/bin/curl -s -k -w "%{http_code}" -o /dev/null --max-time 10 -H "Content-Type: application/json" -H "Authorization: Bearer ${TOKEN}" \
-X PUT -d "{\"current_configs_id\":\"$MACHINE_CONFIGS_ID\"}" ${API_BASE_URL}/machines/${MACHINE_ID}/currentconfig`
if [[ $HTTP_STATUS -ne 200 ]]
then
echo "Error updating current config (http_status=$HTTP_STATUS)"
exit 1
else
echo "Updated current config (http_status=$HTTP_STATUS)"
fi
}
start() {
echo "START ESAME-GET-CONFIG"

Alberto LIVIO BECCARIA
committed
ERROR=0
getConfigVars

Alberto LIVIO BECCARIA
committed
# non sono riuscito a contattare il ws
if [[ $? != 0 ]]; then
echo "Error: WS unreachable or wrong credentials"
ERROR=1
fi
#echo "TOKEN_JSON: $TOKEN_JSON"
#echo "TOKEN: $TOKEN"
#echo "MACHINE_NAME: $MACHINE_NAME"
#echo "MACHINE_JSON: $MACHINE_JSON"
#echo "MACHINE_ID: $MACHINE_ID"
#echo "MACHINE_CONFIGS_ID: $MACHINE_CONFIGS_ID"
#echo "MACHINE_CONFIGS_ID_PREV: $MACHINE_CONFIGS_ID_PREV"
echo "Configuration: $CONFIG_NAME"
# back config files up
cp -f /local/esame-machine.conf /local/esame-machine.conf.prev 2>&1
cp -f /local/iptables /local/iptables.prev 2>&1

Alberto LIVIO BECCARIA
committed
echo "Downloading machine config file..."
getConfigFile

Alberto LIVIO BECCARIA
committed
if [[ $? != 0 ]]; then
ERROR=1
fi
echo "Downloading machine iptables config file..."
getIpConfigFile

Alberto LIVIO BECCARIA
committed
if [[ $? != 0 ]]; then
ERROR=1
fi
if [[ $ERROR != 0 ]]; then
if [[ "$1" == "boot" ]]; then
echo "Fatal error(s): aborting esame and switching machine to normal state..."
rm /local/config_name
rm /local/esame-machine.conf 2>&1
rm /local/esame-machine.conf.prev 2>&1
rm /local/iptables 2>&1
rm /local/iptables.prev 2>&1
/usr/bin/systemctl isolate graphical
fi
fi
# configuration profile is changed
if [[ "$CONFIG_NAME" != "$CONFIG_NAME_PREV" ]]

Alberto LIVIO BECCARIA
committed
# stop the timer to avoid executing this script again before the boot
#/usr/bin/systemctl stop esame-apply-config.timer
# update config_name file
echo ${CONFIG_NAME} > /local/config_name
# no exam, set normal target
if [[ "$CONFIG_NAME" == "normale" ]]
then
# stop some services to revert system files
#/usr/bin/systemctl stop esame-kioskmode
#/usr/bin/systemctl stop esame-kdm-autologin
#/usr/bin/systemctl stop esame-virtualbox
rm /local/esame-machine.conf 2>&1
rm /local/esame-machine.conf.prev 2>&1
rm /local/iptables 2>&1
rm /local/iptables.prev 2>&1
echo "Normal!"
#systemctl isolate graphical && exit 0
#/usr/bin/systemctl set-default graphical.target
/usr/bin/systemctl reboot
#--force
else
echo "Exam!"
#systemctl isolate esame.target && exit 0
#/usr/bin/systemctl set-default esame.target
/usr/bin/systemctl reboot
#--force
# at boot time choose the correct target
if [[ "$1" == "boot" ]]
# we have just rebooted, update the current configuration via web service
updateCurrentConfigId
case "$CONFIG_NAME" in
'normale') # normal
systemctl isolate graphical.target && exit 0
;;
'kiosk-dir' | 'kiosk-dir-esami' | 'kiosk-escher' | 'kiosk-freebrowsing' | 'kiosk-teco') # kiosk mode
systemctl isolate esamekiosk.target && exit 0
;;
*) # all full desktop exams
systemctl isolate esame.target && exit 0
;;
esac
fi
fi
# Runtime parameters
if [[ "$CONFIG_NAME" != "normale" ]]
then
# check if parameters are changed
cmp --silent /local/esame-machine.conf /local/esame-machine.conf.prev
if [[ $? -ne 0 ]]
then
/usr/bin/systemctl daemon-reload
echo "Configuration parameters changed: restart some services... "
# restart some exam services
#echo -n "USB..."
#/usr/bin/systemctl restart esame-usb
#echo " done."
KIOSK_URL_CHANGED=$(diff /local/esame-machine.conf /local/esame-machine.conf.prev | grep ESAME_KIOSK_URL -c)
if [[ $KIOSK_URL_CHANGED -ne 0 ]]

Alberto LIVIO BECCARIA
committed
then
echo -n "Firefox Home..."
FIREFOX_PID=$(pgrep firefox)
if [[ "${FIREFOX_PID}" != "" ]]
then
FIREFOX_USER=$(ps -o user= -p ${FIREFOX_PID})
rm -f /local/${FIREFOX_USER}/.mozilla/firefox/44bu2eyb.default/lock
pkill firefox
sleep 2
/usr/bin/systemctl restart esame-firefox-home
echo " done."
# no way to restart firefox, I must miss something :(
#echo -n "Restarting Firefox..."
#su - ${FIREFOX_USER} -c 'nohup /usr/bin/firefox --display=:0.0 &'
#echo " done."
else
/usr/bin/systemctl restart esame-firefox-home
echo " done."
fi

Alberto LIVIO BECCARIA
committed
fi
echo
echo "All done."
fi
fi
'boot')
start "boot"
;;
stop
start
;;
echo "Usage: $0 { boot | start | stop | restart }";
exit 1;
;;