Newer
Older
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
#!/bin/bash
API_BASE_URL=https://labguiws.foo.local/api/1.0
function getTokenJSON() {
echo `curl -s -k --max-time 10 -X POST -H "Content-Type: application/json" -d "{\"login\":\"guest\",\"pwd\":\"\"}" ${API_BASE_URL}/login`
}
function jsonValue() {
KEY=$1
num=$2
awk -F"[,:}]" '{for(i=1;i<=NF;i++){if($i~/'$KEY'\042/){print $(i+1)}}}' | tr -d '"' | sed -n ${num}p
}
stop() {
echo "STOP ESAME-GET-CONFIG"
#rm /local/esame-machine.conf
#rm /local/iptables
}
start() {
echo "START ESAME-GET-CONFIG"
TOKEN_JSON=$(getTokenJSON)
TOKEN=`echo ${TOKEN_JSON} | jsonValue token 1`
MACHINE_NAME=`hostname`
MACHINE_JSON=`/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`
MACHINE_CONFIGS_ID_PREV=''
if [[ -e /local/config_id ]]
then
MACHINE_CONFIGS_ID_PREV=`cat /local/config_id`
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"
# non sono riuscito a contattatare il ws
if [[ "$MACHINE_CONFIGS_ID" == "" ]]
then
echo "Errore: WS non raggiungibile o credenziali errate"
exit 1
fi
# 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
# get configuration file
HTTP_STATUS=`/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
exit 1
else
echo "Downloaded config file (http_status=$HTTP_STATUS)"
fi
# get ipconfig file
HTTP_STATUS=`/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
exit 1
else
echo "Downloaded iptables config file (http_status=$HTTP_STATUS)"
fi
# configuration profile is changed
if [[ "$MACHINE_CONFIGS_ID" != "$MACHINE_CONFIGS_ID_PREV" ]]
then
# update config_id file
echo ${MACHINE_CONFIGS_ID} > /local/config_id
# no exam, set normal target
if [[ "$MACHINE_CONFIGS_ID" -eq "0" ]]
then
# stop some services to revert system files
/usr/bin/systemctl stop esame-kioskmode
/usr/bin/systemctl stop esame-kdm-autologin
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!"
#/usr/bin/systemctl isolate graphical.target
/usr/bin/systemctl set-default graphical.target
reboot
else
echo "Exam!"
#/usr/bin/systemctl isolate esame.target
/usr/bin/systemctl set-default esame.target
reboot
fi
fi
if [[ "$MACHINE_CONFIGS_ID" -ne "0" ]]
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 services... "
# restart some exam services
echo -n "USB..."
/usr/bin/systemctl start esame-usb
echo " done."
echo -n "Firefox home..."
/usr/bin/systemctl start esame-firefox-home
echo " done."
echo
echo "All done."
fi
fi
/usr/bin/systemctl stop esame-get-config
}
args=("$@")
case "$1" in
'start')
start
;;
'stop')
stop
;;
'restart')
stop
start
;;
*)
echo "Usage: $0 { start | stop | restart }";
exit 1;
;;
esac
exit 0