Skip to content
Snippets Groups Projects
Commit bb2798af authored by Alberto LIVIO BECCARIA's avatar Alberto LIVIO BECCARIA
Browse files

Boots in normal mode if labmanager-api fails on boot (no action if the PC is up and running

parent eaace5a3
No related branches found
No related tags found
No related merge requests found
#!/bin/bash
#!/usr/bin/env -S bash
API_BASE_URL=https://labmanager-api.edu-al.unipmn.it/api/1.0
MAX_RETRY=5
......@@ -25,8 +25,9 @@ retry() {
local delay=$RETRY_DELAY
while true; do
"$@"
if [[ $? == 0 ]]; then
break
ret=$?
if [[ $ret == 0 ]]; then
return $ret
else
if [[ $n -lt $max ]]; then
((n++))
......@@ -34,7 +35,7 @@ retry() {
sleep $delay;
else
echo "Command failed after $n attempts." >&2
exit 1
return $ret
fi
fi
done
......@@ -52,8 +53,7 @@ getTokenJSON() {
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)
ret=$?
if [[ $ret != 0 ]]; then
echo "Invalid authentication request!"
if [[ $ret -ne 0 ]]; then
return $ret
fi
......@@ -62,7 +62,6 @@ getTokenJSON() {
# check the status
if [[ $HTTP_STATUS -ne 200 ]]; then
echo "Authentication failed!"
return 1
else
# extract the body
......@@ -94,6 +93,7 @@ getConfigVars() {
echo "Authenticating to WS..."
TOKEN_JSON=`retry getTokenJSON`
if [[ "$TOKEN_JSON" == "" ]]; then
echo "Authentication failed!"
return 2
fi
TOKEN=`echo ${TOKEN_JSON} | jsonValue token 1`
......@@ -132,13 +132,13 @@ getConfigFile() {
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)"
retry /usr/bin/curl -s -k -o /local/esame-machine.conf --max-time 10 --header "Authorization: Bearer ${TOKEN}" -X GET ${API_BASE_URL}/machines/${MACHINE_ID}/configfile
ret=$?
if [[ $ret -ne 0 ]]; then
echo "Error downloading config file (ret=$ret)"
return 1
else
echo "Downloaded config file (http_status=$HTTP_STATUS)"
echo "Downloaded config file (ret=$ret)"
fi
}
......@@ -146,13 +146,13 @@ getConfigFile() {
getIpConfigFile() {
echo "Downloading machine iptables config file..."
# get ipconfig file
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)"
retry /usr/bin/curl -s -k -o /local/iptables --max-time 10 --header "Authorization: Bearer ${TOKEN}" -X GET ${API_BASE_URL}/machines/${MACHINE_ID}/ipconfigfile
ret=$?
if [[ $ret -ne 0 ]]; then
echo "Error downloading iptables config file (ret=$ret)"
return 1
else
echo "Downloaded iptables config file (http_status=$HTTP_STATUS)"
echo "Downloaded iptables config file (ret=$ret)"
fi
}
......@@ -176,7 +176,9 @@ getConfig() {
while [[ $? != 0 ]]; do
echo "Error fetching machine info!"
echo "If this is a new machine, did you add it into labmanager-admin?"
sleep 30;
# to repeat until successful, just comment next line
return 1
sleep 30
getConfigVars
done
......@@ -229,7 +231,7 @@ start() {
rm /local/iptables >/dev/null 2>&1
rm /local/iptables.prev >/dev/null 2>&1
rm /local/mk-homedir >/dev/null 2>&1
/usr/bin/systemctl start graphical && exit 0
/usr/bin/systemctl start graphical.target && exit 0
fi
echo "Fatal error(s): missing or wrong web service reply, abort."
exit 1
......@@ -264,7 +266,7 @@ start() {
echo "Exam ID: ${EXAM_ID}"
fi
if [ "${ESAME_TARGET}" != "" ]; then
echo "Systemd Target:: ${ESAME_TARGET}"
echo "Systemd Target: ${ESAME_TARGET}"
fi
fi
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment