1 | <?php
|
---|
2 | // ********************************************************************
|
---|
3 | // Author: Juan Manuel Bardallo (UHU), Natalia Serrano (Qindel)
|
---|
4 | // Description: Updates UDS with the number of available computers depending on OG's calendars and other circumstances
|
---|
5 | // *********************************************************************
|
---|
6 |
|
---|
7 | include_once("./controlacceso.php");
|
---|
8 | include_once("./includes/CreaComando.php");
|
---|
9 | include_once("./clases/AdoPhp.php");
|
---|
10 |
|
---|
11 | $tz = 'Europe/Madrid';
|
---|
12 | $OG_REST_URL = 'https://og.example.com/opengnsys/rest/';
|
---|
13 | $UDS_REST_URL = 'https://uds.example.com/uds/rest/';
|
---|
14 | $UDS_AUTHENTICATOR = 'Local users';
|
---|
15 | $UDS_USER = 'user';
|
---|
16 | $UDS_PASS = 'pass';
|
---|
17 |
|
---|
18 | /**
|
---|
19 | * uds_getHeaders ($auth_token, $scrambler)
|
---|
20 | * @brief Builds set of authentication headers from a couple of UDS params
|
---|
21 | * @param string authentication token
|
---|
22 | * @param string scrambler
|
---|
23 | * @return array headers
|
---|
24 | */
|
---|
25 | function uds_getHeaders ($auth_token, $scrambler) {
|
---|
26 | $headers = [
|
---|
27 | 'X-Auth-Token: ' . $auth_token, // Authentication token to access REST API of UDS -- MUST be present in all requests except login request
|
---|
28 | 'Content-Type: ' . 'application/json',
|
---|
29 | 'User-Agent: ' . 'UDS Test Linux Client 1.0', // important to include "Linux" on UA to allow UDS to know OS. If not present, UDS will not know wich OS is connecting, and will not send the correct services
|
---|
30 | 'Scrambler: ' . $scrambler, // This header is a cryptographic key -- MUST BE INCLUDED in all requests except login request
|
---|
31 | ];
|
---|
32 | return $headers;
|
---|
33 | }
|
---|
34 |
|
---|
35 | /**
|
---|
36 | * db_fetch_apikey()
|
---|
37 | * @brief Retrieves API key for the first user (assumed admin) from the OG database
|
---|
38 | * @return string api key
|
---|
39 | */
|
---|
40 | function db_fetch_apikey() {
|
---|
41 | global $cnx;
|
---|
42 | $cmd = CreaComando ($cnx);
|
---|
43 | if (!$cmd) { die ('ACCESS_ERROR'); }
|
---|
44 |
|
---|
45 | $cmd->texto = 'SELECT apikey FROM usuarios WHERE idusuario=1';
|
---|
46 | $rs = new Recordset;
|
---|
47 | $rs->Comando = &$cmd;
|
---|
48 |
|
---|
49 | if (!$rs->Abrir()) return (null);
|
---|
50 |
|
---|
51 | $rs->Primero();
|
---|
52 | if ($rs->EOF) { return (null); }
|
---|
53 |
|
---|
54 | $k = $rs->campos['apikey'];
|
---|
55 | $rs->Cerrar();
|
---|
56 | return $k;
|
---|
57 | }
|
---|
58 |
|
---|
59 | /**
|
---|
60 | * db_fetchCals()
|
---|
61 | * @brief Retrieves calendars from the database
|
---|
62 | * @return array calendars
|
---|
63 | */
|
---|
64 | function db_fetchCals() {
|
---|
65 | global $cnx;
|
---|
66 |
|
---|
67 | $tbl = array();
|
---|
68 | $cmd = CreaComando ($cnx);
|
---|
69 | if (!$cmd) { die ('ACCESS_ERROR'); }
|
---|
70 |
|
---|
71 | $cmd->texto = 'SELECT idcalendario, description, json_text FROM calendarios';
|
---|
72 | $rs = new Recordset;
|
---|
73 | $rs->Comando = &$cmd;
|
---|
74 |
|
---|
75 | if (!$rs->Abrir()) return (false);
|
---|
76 |
|
---|
77 | $rs->Primero();
|
---|
78 | if ($rs->EOF) { return ($tbl); }
|
---|
79 |
|
---|
80 | $id = $rs->campos['idcalendario'];
|
---|
81 | $desc = $rs->campos['description'];
|
---|
82 | $txt = $rs->campos['json_text'];
|
---|
83 | $tbl[$id] = array (
|
---|
84 | 'id' => $id,
|
---|
85 | 'desc' => $desc,
|
---|
86 | 'json' => json_decode($txt),
|
---|
87 | );
|
---|
88 |
|
---|
89 | while (1) {
|
---|
90 | $rs->Siguiente();
|
---|
91 | if ($rs->EOF) { break; }
|
---|
92 |
|
---|
93 | $id = $rs->campos['idcalendario'];
|
---|
94 | $desc = $rs->campos['description'];
|
---|
95 | $txt = $rs->campos['json_text'];
|
---|
96 | $tbl[$id] = array (
|
---|
97 | 'id' => $id,
|
---|
98 | 'desc' => $desc,
|
---|
99 | 'json' => json_decode($txt),
|
---|
100 | );
|
---|
101 | }
|
---|
102 |
|
---|
103 | $rs->Cerrar();
|
---|
104 | return $tbl;
|
---|
105 | }
|
---|
106 |
|
---|
107 | /**
|
---|
108 | * uds_login ($auth, $username, $password)
|
---|
109 | * @brief Log into UDS
|
---|
110 | * @param string UDS authenticator
|
---|
111 | * @param string user
|
---|
112 | * @param string password
|
---|
113 | * @return object response from the server, contains result, token, version and scrambler
|
---|
114 | */
|
---|
115 | function uds_login ($auth, $username, $password) {
|
---|
116 | global $UDS_REST_URL;
|
---|
117 |
|
---|
118 | $parameters = [
|
---|
119 | 'auth' => $auth,
|
---|
120 | 'username' => $username,
|
---|
121 | 'password' => $password
|
---|
122 | ];
|
---|
123 |
|
---|
124 | $payload = json_encode ($parameters);
|
---|
125 |
|
---|
126 | $ch = curl_init();
|
---|
127 | curl_setopt ($ch, CURLOPT_URL, $UDS_REST_URL."auth/login");
|
---|
128 | curl_setopt ($ch, CURLOPT_POST, 1);
|
---|
129 | curl_setopt ($ch, CURLOPT_POSTFIELDS, $payload);
|
---|
130 | curl_setopt ($ch, CURLOPT_HTTPHEADER, array ('Content-Type:application/json'));
|
---|
131 | curl_setopt ($ch, CURLOPT_RETURNTRANSFER, true);
|
---|
132 |
|
---|
133 | // In real life you should use something like:
|
---|
134 | // curl_setopt ($ch, CURLOPT_POSTFIELDS, http_build_query (array ('postvar1' => 'value1')));
|
---|
135 |
|
---|
136 | $server_output = json_decode (curl_exec ($ch));
|
---|
137 | curl_close ($ch);
|
---|
138 |
|
---|
139 | return $server_output;
|
---|
140 | }
|
---|
141 |
|
---|
142 | /**
|
---|
143 | * uds_getServiceInfo ($headers, $providerId, $serviceId)
|
---|
144 | * @brief Get service information from UDS
|
---|
145 | * @param array UDS headers
|
---|
146 | * @param string provider id
|
---|
147 | * @param string service id
|
---|
148 | * @return object response from the server
|
---|
149 | */
|
---|
150 | function uds_getServiceInfo ($headers, $providerId, $serviceId) {
|
---|
151 | global $UDS_REST_URL;
|
---|
152 |
|
---|
153 | $ch = curl_init();
|
---|
154 | curl_setopt ($ch, CURLOPT_URL, $UDS_REST_URL."providers/".$providerId."/services/".$serviceId);
|
---|
155 | curl_setopt ($ch, CURLOPT_RETURNTRANSFER, true);
|
---|
156 | curl_setopt ($ch, CURLOPT_HTTPHEADER, $headers);
|
---|
157 |
|
---|
158 | $server_output = json_decode (curl_exec ($ch));
|
---|
159 | print_r (curl_error ($ch));
|
---|
160 | curl_close ($ch);
|
---|
161 |
|
---|
162 | return $server_output;
|
---|
163 | }
|
---|
164 |
|
---|
165 | /**
|
---|
166 | * uds_getAllServicePools ($headers)
|
---|
167 | * @brief Get all service pools from UDS
|
---|
168 | * @param array UDS headers
|
---|
169 | * @return object response from the server
|
---|
170 | */
|
---|
171 | function uds_getAllServicePools ($headers) {
|
---|
172 | global $UDS_REST_URL;
|
---|
173 |
|
---|
174 | $ch = curl_init();
|
---|
175 | curl_setopt ($ch, CURLOPT_URL, $UDS_REST_URL."servicespools/overview");
|
---|
176 | curl_setopt ($ch, CURLOPT_RETURNTRANSFER, true);
|
---|
177 | curl_setopt ($ch, CURLOPT_HTTPHEADER, $headers);
|
---|
178 |
|
---|
179 | $server_output = json_decode (curl_exec ($ch));
|
---|
180 | curl_close ($ch);
|
---|
181 |
|
---|
182 | return $server_output;
|
---|
183 | }
|
---|
184 |
|
---|
185 | /**
|
---|
186 | * uds_setServicePool ($headers, $servicePool)
|
---|
187 | * @brief Writes service pool information to UDS
|
---|
188 | * @param array UDS headers
|
---|
189 | * @param object service pool details
|
---|
190 | * @return object response from the server
|
---|
191 | */
|
---|
192 | function uds_setServicePool ($headers, $servicePool) {
|
---|
193 | global $UDS_REST_URL;
|
---|
194 |
|
---|
195 | $payload = json_encode ($servicePool);
|
---|
196 |
|
---|
197 | $ch = curl_init();
|
---|
198 | curl_setopt ($ch, CURLOPT_URL, $UDS_REST_URL."servicespools/".$servicePool->id);
|
---|
199 | curl_setopt ($ch, CURLOPT_POST, 1);
|
---|
200 | curl_setopt ($ch, CURLOPT_CUSTOMREQUEST, "PUT");
|
---|
201 | curl_setopt ($ch, CURLOPT_POSTFIELDS, $payload);
|
---|
202 | curl_setopt ($ch, CURLOPT_RETURNTRANSFER, true);
|
---|
203 | curl_setopt ($ch, CURLOPT_HTTPHEADER, $headers);
|
---|
204 |
|
---|
205 | $server_output = curl_exec ($ch);
|
---|
206 | curl_close ($ch);
|
---|
207 |
|
---|
208 | return json_decode ($server_output);
|
---|
209 | }
|
---|
210 |
|
---|
211 | /**
|
---|
212 | * og_getAula ($idCentro, $idAula)
|
---|
213 | * @brief Gets lab information from OG
|
---|
214 | * @param int building id
|
---|
215 | * @param int lab id
|
---|
216 | * @return object response from the server
|
---|
217 | */
|
---|
218 | function og_getAula ($idCentro, $idAula) {
|
---|
219 | global $OG_REST_URL;
|
---|
220 | global $OG_REST_AUTH;
|
---|
221 |
|
---|
222 | $result = null;
|
---|
223 | $url = $OG_REST_URL."ous/".$idCentro."/labs/".$idAula;
|
---|
224 |
|
---|
225 | $ch = curl_init();
|
---|
226 | curl_setopt ($ch, CURLOPT_URL, $url);
|
---|
227 | curl_setopt ($ch, CURLOPT_SSL_VERIFYHOST, 0);
|
---|
228 | curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER, 0);
|
---|
229 | curl_setopt ($ch, CURLOPT_RETURNTRANSFER, true);
|
---|
230 | curl_setopt ($ch, CURLOPT_HTTPHEADER, [
|
---|
231 | 'Accept: application/json',
|
---|
232 | 'Authorization: '.$OG_REST_AUTH
|
---|
233 | ]);
|
---|
234 |
|
---|
235 |
|
---|
236 | $server_output = json_decode (curl_exec ($ch));
|
---|
237 | $curl_errno = curl_errno ($ch);
|
---|
238 | $curl_error = curl_error ($ch);
|
---|
239 | if ($curl_errno) {
|
---|
240 | printf ("error (%s) errno (%d)\n", $curl_error, $curl_errno);
|
---|
241 | return null;
|
---|
242 | }
|
---|
243 |
|
---|
244 | $code = curl_getinfo ($ch, CURLINFO_RESPONSE_CODE);
|
---|
245 | curl_close ($ch);
|
---|
246 |
|
---|
247 | if ($code <= 201) {
|
---|
248 | $result = $server_output;
|
---|
249 | }
|
---|
250 |
|
---|
251 | return $result;
|
---|
252 | }
|
---|
253 |
|
---|
254 | /**
|
---|
255 | * og_getClientDiskConfig ($idCentro, $idAula, $idCliente)
|
---|
256 | * @brief Gets information about disks of a client from OG
|
---|
257 | * @param int building id
|
---|
258 | * @param int lab id
|
---|
259 | * @param int client id
|
---|
260 | * @return object response from the server
|
---|
261 | */
|
---|
262 | function og_getClientDiskConfig ($idCentro, $idAula, $idCliente) {
|
---|
263 | global $OG_REST_URL;
|
---|
264 | global $OG_REST_AUTH;
|
---|
265 |
|
---|
266 | $result = null;
|
---|
267 | $url = $OG_REST_URL."ous/".$idCentro."/labs/".$idAula."/clients/".$idCliente."/diskcfg";
|
---|
268 |
|
---|
269 | $ch = curl_init();
|
---|
270 | curl_setopt ($ch, CURLOPT_URL, $url);
|
---|
271 | curl_setopt ($ch, CURLOPT_SSL_VERIFYHOST, 0);
|
---|
272 | curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER, 0);
|
---|
273 | curl_setopt ($ch, CURLOPT_RETURNTRANSFER, true);
|
---|
274 | curl_setopt ($ch, CURLOPT_HTTPHEADER, [
|
---|
275 | 'Accept: application/json',
|
---|
276 | 'Authorization: '.$OG_REST_AUTH
|
---|
277 | ]);
|
---|
278 |
|
---|
279 | $server_output = json_decode (curl_exec ($ch));
|
---|
280 | $curl_errno = curl_errno ($ch);
|
---|
281 | $curl_error = curl_error ($ch);
|
---|
282 | if ($curl_errno) {
|
---|
283 | printf ("error (%s) errno (%d)\n", $curl_error, $curl_errno);
|
---|
284 | return null;
|
---|
285 | }
|
---|
286 |
|
---|
287 | $code = curl_getinfo ($ch, CURLINFO_RESPONSE_CODE);
|
---|
288 | curl_close ($ch);
|
---|
289 |
|
---|
290 | if ($code <= 201) {
|
---|
291 | $result = $server_output;
|
---|
292 | }
|
---|
293 |
|
---|
294 | return $result;
|
---|
295 | }
|
---|
296 |
|
---|
297 | /**
|
---|
298 | * og_aula_is_remote ($cal_id)
|
---|
299 | * @brief Checks whether lab is remote right now
|
---|
300 | * @param int calendar id
|
---|
301 | * @return bool whether lab is remote (true) or not (false)
|
---|
302 | */
|
---|
303 | function og_aula_is_remote ($cal_id) {
|
---|
304 | global $cals, $tz;
|
---|
305 | $ruleset = $cals[$cal_id]['json']->remotepc_calendar->ruleset; //print_r($ruleset);
|
---|
306 |
|
---|
307 | $dt = new DateTime ('now', new DateTimeZone ($tz));
|
---|
308 | $dow = strtolower ($dt->format ('D')); // textual representation of a day, three letters, 'Mon' through 'Sun'
|
---|
309 | $hour = $dt->format ('G'); // 24-hour without leading zeros
|
---|
310 | $ts = $dt->format ('U'); // unix epoch
|
---|
311 |
|
---|
312 | // primero recorremos todas las reglas mirando solo las que tienen remote=false (ie. presencial)
|
---|
313 | // si estamos fuera de todos estos rangos, es que estamos en remoto: return 1
|
---|
314 | // si estamos dentro de alguno de ellos, es que estamos en presencial: continuamos
|
---|
315 | $presencial = 0;
|
---|
316 | foreach ($ruleset as $r) {
|
---|
317 | if (array_key_exists ('remote', $r) and $r->remote) { continue; }
|
---|
318 | if (!array_key_exists ($dow, $r) or !$r->$dow) { continue; }
|
---|
319 | if ($hour < $r->from_hr or $hour > $r->to_hr) { continue; }
|
---|
320 | $presencial = 1; break;
|
---|
321 | }
|
---|
322 |
|
---|
323 | if (0 == $presencial) {
|
---|
324 | return 1;
|
---|
325 | }
|
---|
326 |
|
---|
327 | // si llegamos aqui, es que estamos en uno de los rangos de presencial, pero puede haber excepciones
|
---|
328 | // recorremos todas las reglas mirando las que tienen remote=true
|
---|
329 | // si estamos en alguno de esos rangos, return 1
|
---|
330 | foreach ($ruleset as $r) {
|
---|
331 | if (!array_key_exists ('remote', $r) or !$r->remote) { continue; }
|
---|
332 | if ($ts >= $r->from_ts and $ts <= $r->to_ts) { return 1; }
|
---|
333 | }
|
---|
334 |
|
---|
335 | // si llegamos aqui es que estamos dentro de presencial y fuera de las excepciones: return 0
|
---|
336 | return 0;
|
---|
337 | }
|
---|
338 |
|
---|
339 | function og_ordenador_cumple_criterios ($client) {
|
---|
340 | if ($client->status == 'oglive' || $client->status == 'linux' || $client->status == 'windows') {
|
---|
341 | return 1;
|
---|
342 | }
|
---|
343 |
|
---|
344 | return 0;
|
---|
345 | }
|
---|
346 | // {"remotepc_calendar":{"timezone":"Europe/Madrid","ruleset":[
|
---|
347 | // {"remote":false,"mon":true,"tue":true,"wed":true,"thu":true,"fri":true,"from_hr":"8","to_hr":"17"},
|
---|
348 | // {"remote":true,"reason":"exc","from_ts":1709074800,"to_ts":1706569199}
|
---|
349 | // ]}}
|
---|
350 |
|
---|
351 | /**
|
---|
352 | * og_sondeoAula ($idCentro, $idAula, $idImagen)
|
---|
353 | * @brief Gets how many clients are available for remotepc from OG
|
---|
354 | * @param int building id
|
---|
355 | * @param int lab id
|
---|
356 | * @param int image id
|
---|
357 | * @return int number of clients available for remotepc
|
---|
358 | */
|
---|
359 | function og_sondeoAula ($idCentro, $idAula, $idImagen) {
|
---|
360 | global $OG_REST_URL;
|
---|
361 | global $OG_REST_AUTH;
|
---|
362 |
|
---|
363 | $aula = og_getAula ($idCentro, $idAula);
|
---|
364 | if (null === $aula || $aula->inremotepc != 1) { return 0; }
|
---|
365 |
|
---|
366 | if (!og_aula_is_remote ($aula->idcalendario)) {
|
---|
367 | return 0;
|
---|
368 | }
|
---|
369 |
|
---|
370 | $url = $OG_REST_URL."ous/".$idCentro."/labs/".$idAula."/clients/status";
|
---|
371 | $ch = curl_init();
|
---|
372 | curl_setopt ($ch, CURLOPT_URL, $url);
|
---|
373 | curl_setopt ($ch, CURLOPT_SSL_VERIFYHOST, 0);
|
---|
374 | curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER, 0);
|
---|
375 | curl_setopt ($ch, CURLOPT_RETURNTRANSFER, true);
|
---|
376 | curl_setopt ($ch, CURLOPT_HTTPHEADER, [
|
---|
377 | 'Accept: application/json',
|
---|
378 | 'Authorization: '.$OG_REST_AUTH
|
---|
379 | ]);
|
---|
380 |
|
---|
381 | $server_output = json_decode (curl_exec ($ch));
|
---|
382 | $curl_errno = curl_errno ($ch);
|
---|
383 | $curl_error = curl_error ($ch);
|
---|
384 | if ($curl_errno) {
|
---|
385 | printf ("error (%s) errno (%d)\n", $curl_error, $curl_errno);
|
---|
386 | return null;
|
---|
387 | }
|
---|
388 | $code = curl_getinfo ($ch, CURLINFO_RESPONSE_CODE);
|
---|
389 | curl_close ($ch);
|
---|
390 |
|
---|
391 | if ($code > 201) { return 0; }
|
---|
392 |
|
---|
393 | $clientsOn = 0;
|
---|
394 | foreach ($server_output as $client) {
|
---|
395 | if (!og_ordenador_cumple_criterios ($client)) { continue; }
|
---|
396 |
|
---|
397 | $clientDisk = og_getClientDiskConfig ($idCentro, $idAula, $client->id);
|
---|
398 | if (null === $clientDisk) { continue; }
|
---|
399 |
|
---|
400 | for ($p = 0; $p < count ($clientDisk->diskcfg); $p++) {
|
---|
401 | $part = $clientDisk->diskcfg[$p];
|
---|
402 | if (isset ($part->image) && $part->image->id == $idImagen) {
|
---|
403 | $clientsOn++;
|
---|
404 | break;
|
---|
405 | }
|
---|
406 | }
|
---|
407 | }
|
---|
408 |
|
---|
409 | return $clientsOn;
|
---|
410 | }
|
---|
411 |
|
---|
412 |
|
---|
413 |
|
---|
414 | $OG_REST_AUTH = db_fetch_apikey();
|
---|
415 | if (null == $OG_REST_AUTH) {
|
---|
416 | print ("failed to fetch API key from the database\n");
|
---|
417 | exit (1);
|
---|
418 | }
|
---|
419 |
|
---|
420 | $cals = db_fetchCals(); //print_r($cals);
|
---|
421 |
|
---|
422 | $server_output = uds_login ($UDS_AUTHENTICATOR, $UDS_USER, $UDS_PASS);
|
---|
423 | if ($server_output->result != "ok") {
|
---|
424 | print_r (curl_error ($ch));
|
---|
425 | exit (1);
|
---|
426 | }
|
---|
427 |
|
---|
428 | $headers = uds_getHeaders ($server_output->token, $server_output->scrambler);
|
---|
429 | $servicePools = uds_getAllServicePools ($headers);
|
---|
430 |
|
---|
431 | foreach ($servicePools as $servicePool) {
|
---|
432 | $providerId = $servicePool->provider_id;
|
---|
433 | $serviceId = $servicePool->service_id;
|
---|
434 |
|
---|
435 | $service = uds_getServiceInfo ($headers, $providerId, $serviceId);
|
---|
436 | if ($service === null) {
|
---|
437 | print ("service with providerId ($providerId) serviceId ($serviceId) is null\n");
|
---|
438 | continue;
|
---|
439 | }
|
---|
440 |
|
---|
441 | // Conectar con opengnsys para ver cuantos pcs hay disponibles y enviar dicho parametro
|
---|
442 | $max_srvs = og_sondeoAula ($service->ou, $service->lab, $service->image);
|
---|
443 | if (null === $max_srvs) {
|
---|
444 | print ("og_sondeoAula failed\n");
|
---|
445 | continue;
|
---|
446 | }
|
---|
447 | $servicePool->osmanager_id = null;
|
---|
448 | $servicePool->max_srvs = $max_srvs;
|
---|
449 | $servicePool->initial_srvs = $servicePool->max_srvs;
|
---|
450 | $servicePool->cache_l1_srvs = 0;
|
---|
451 |
|
---|
452 | $servicePool->visible = ($servicePool->max_srvs > 0);
|
---|
453 |
|
---|
454 | /*
|
---|
455 | if ($servicePool->initial_srvs > $servicePool->max_srvs){
|
---|
456 | $servicePool->initial_srvs = $servicePool->max_srvs;
|
---|
457 | }
|
---|
458 | /**/
|
---|
459 | $sp = uds_setServicePool ($headers, $servicePool);
|
---|
460 | if (null === $sp) {
|
---|
461 | print ("uds_setServicePool failed\n");
|
---|
462 | continue;
|
---|
463 | }
|
---|
464 | printf ("%s: ou:%d lab:%d imagen:%d max_srvs:%d visible:%d\n", $sp->name, $service->ou, $service->lab, $service->image, $sp->max_srvs, $sp->visible);
|
---|
465 | }
|
---|