source: admin/WebConsole/includes/restfunctions.php @ 57200ac

918-git-images-111dconfigfileconfigure-oglivegit-imageslgromero-new-oglivemainmaint-cronmount-efivarfsmultivmmultivm-ogboot-installerogClonningEngineogboot-installer-jenkinsoglive-ipv6test-python-scriptsticket-301ticket-50ticket-50-oldticket-577ticket-585ticket-611ticket-612ticket-693ticket-700ubu24tplunification2use-local-agent-oglivevarios-instalacion
Last change on this file since 57200ac was d233fdb, checked in by OpenGnSys Support Team <soporte-og@…>, 6 years ago

#915: Adapt web to use new image/restore cmd in REST API

SocketHidra? restaurar imagen has been replaced by POST image/restore.

  • Property mode set to 100644
File size: 7.9 KB
RevLine 
[b636e61]1
[8c9fcb2]2<?php
[560455a]3
4define('OG_REST_URL', 'http://127.0.0.1:8888/');
5
6define('GET', 1);
7define('POST', 2);
8define('CUSTOM', 3);
9
10define('OG_REST_CMD_CLIENTS', 'clients');
11define('OG_REST_CMD_WOL', 'wol');
12define('OG_REST_CMD_SESSION', 'session');
13define('OG_REST_CMD_RUN', 'shell/run');
14define('OG_REST_CMD_OUTPUT', 'shell/output');
[251bb977]15define('OG_REST_CMD_POWEROFF', 'poweroff');
[da462c8]16define('OG_REST_CMD_REBOOT', 'reboot');
[dc82754]17define('OG_REST_CMD_STOP', 'stop');
[c9cfd44]18define('OG_REST_CMD_REFRESH', 'refresh');
[db537e7]19define('OG_REST_CMD_HARDWARE', 'hardware');
[96cced9]20define('OG_REST_CMD_SOFTWARE', 'software');
[b636e61]21define('OG_REST_CMD_CREATE_IMAGE', 'image/create');
[d233fdb]22define('OG_REST_CMD_RESTORE_IMAGE', 'image/restore');
[560455a]23
24define('OG_REST_PARAM_CLIENTS', 'clients');
25define('OG_REST_PARAM_ADDR', 'addr');
26define('OG_REST_PARAM_MAC', 'mac');
27define('OG_REST_PARAM_DISK', 'disk');
28define('OG_REST_PARAM_PART', 'partition');
29define('OG_REST_PARAM_RUN', 'run');
30define('OG_REST_PARAM_TYPE', 'type');
[e378505]31define('OG_REST_PARAM_STATE', 'state');
[b636e61]32define('OG_REST_PARAM_NAME', 'name');
33define('OG_REST_PARAM_REPOS', 'repository');
34define('OG_REST_PARAM_ID', 'id');
35define('OG_REST_PARAM_CODE', 'code');
[d233fdb]36define('OG_REST_PARAM_PROFILE', 'profile');
37define('OG_REST_PARAM_TYPE', 'type');
[560455a]38
[31c92b6]39$conf_file = parse_ini_file(__DIR__ . '/../../etc/ogAdmRepo.cfg');
[ef61154]40define('OG_REST_API_TOKEN', 'Authorization: ' . $conf_file['ApiToken']);
41
[41ceac0]42function common_request($command, $type, $data = null) {
[560455a]43
44        $json = json_encode($data);
45
46        $service_url = OG_REST_URL.$command;
47
48        $curl = curl_init($service_url);
49        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
[ef61154]50        curl_setopt($curl, CURLOPT_HTTPHEADER, array(
51                OG_REST_API_TOKEN,
52        ));
[560455a]53
54        switch ($type) {
55                default:
56                case GET:
57                        break;
58                case POST:
59                        curl_setopt($curl, CURLOPT_POST, true);
60                        curl_setopt($curl, CURLOPT_POSTFIELDS, $json);
61        }
62
63        $curl_response = curl_exec($curl);
64        $info = curl_getinfo($curl);
65
66        if ($curl_response === false || $info['http_code'] != 200) {
[4d7e35c]67                syslog(LOG_ERR, 'error occured during curl exec. Additioanl info: ' . print_r($info, TRUE));
[dc82754]68                return 0;
[560455a]69        }
70
71        curl_close($curl);
72
73        syslog(LOG_INFO, 'response '.$command.' ok!');
74
75        return json_decode($curl_response, true);
76}
77
78
79function shell($case, $string_ips, $command) {
80
81        $ips = explode(';',$string_ips);
82
83        switch ($case) {
84                case 1:
85                        $data = array(OG_REST_PARAM_CLIENTS => $ips,
86                                OG_REST_PARAM_RUN => $command);
87                        $command = OG_REST_CMD_RUN;
88                        break;
89                default:
90                case 2:
91                        $data = array(OG_REST_PARAM_CLIENTS => $ips);
92                        $command = OG_REST_CMD_OUTPUT;
93        }
94
95        $result = common_request($command, POST,
96                $data)[OG_REST_PARAM_CLIENTS][0]['output'];
97
98        return (is_null($result) ? '1' : $result);
99}
100
101function clients($case, $ips) {
102
103        switch ($case) {
104                case 1:
105                        $type = POST;
106                        $data = array(OG_REST_PARAM_CLIENTS => $ips);
107                        break;
108                case 2:
109                        $type = GET;
[0b97d96]110                        $data = null;
[560455a]111                        break;
112        }
113
114        $result = common_request(OG_REST_CMD_CLIENTS, $type, $data);
115
[0b97d96]116        $trama_notificacion = "";
117        if (isset($result[OG_REST_PARAM_CLIENTS])) {
118                foreach ($result[OG_REST_PARAM_CLIENTS] as $client) {
119                        $trama_notificacion .= $client[OG_REST_PARAM_ADDR].'/'.
120                                $client[OG_REST_PARAM_STATE].';';
121                }
[560455a]122        }
123
124        return $trama_notificacion;
125}
126
127function wol($type_wol, $macs, $ips) {
128
129        switch ($type_wol) {
130                default:
131                case 1:
132                        $wol = 'broadcast';
133                        break;
134                case 2:
135                        $wol = 'unicast';
136        }
137
138        $clients = array();
139
140        for($i=0; $i<count($macs); $i++) {
141                $clients[] = array(OG_REST_PARAM_ADDR => $ips[$i],
142                        OG_REST_PARAM_MAC => $macs[$i]);
143        }
144
145        $data = array(OG_REST_PARAM_TYPE => $wol,
146                OG_REST_PARAM_CLIENTS => $clients);
147
148        common_request(OG_REST_CMD_WOL, POST, $data);
149}
150
151function session($string_ips, $params) {
152
153        preg_match_all('!\d{1}!', $params, $matches);
154
155        $ips = explode(';',$string_ips);
156        $disk = $matches[0][0];
157        $part = $matches[0][1];
158
159        $data = array(OG_REST_PARAM_CLIENTS => $ips,
160                OG_REST_PARAM_DISK => $disk, OG_REST_PARAM_PART => $part);
161
162        common_request(OG_REST_CMD_SESSION, POST, $data);
163}
164
[b636e61]165function create_image($string_ips, $params) {
166
167        preg_match_all('/(?<=\=)(.*?)(?=\r)/', $params, $matches);
168
169        $ips = explode(';',$string_ips);
170        $disk = $matches[0][0];
171        $part = $matches[0][1];
172        $code = $matches[0][2];
173        $id = $matches[0][3];
174        $name = $matches[0][4];
175        $repos = $matches[0][5];
176
177        $data = array(OG_REST_PARAM_CLIENTS => $ips,
178                OG_REST_PARAM_DISK => $disk,
179                OG_REST_PARAM_PART => $part,
180                OG_REST_PARAM_CODE => $code,
181                OG_REST_PARAM_ID => $id,
182                OG_REST_PARAM_NAME => $name,
183                OG_REST_PARAM_REPOS => $repos);
184
185        common_request(OG_REST_CMD_CREATE_IMAGE, POST, $data);
186}
187
[d233fdb]188function restore_image($string_ips, $params) {
189
190        preg_match_all('/(?<=\=)(.*?)(?=\r)/', $params, $matches);
191
192        $ips = explode(';',$string_ips);
193        $disk = $matches[0][0];
194        $part = $matches[0][1];
195        $image_id = $matches[0][2];
196        $name = $matches[0][3];
197        $repos = $matches[0][4];
198        $profile = $matches[0][5];
199        $type = $matches[0][6];
200
201        $data = array(OG_REST_PARAM_DISK => $disk, OG_REST_PARAM_PART => $part,
202                OG_REST_PARAM_IMAGE_ID => $image_id, OG_REST_PARAM_NAME => $name,
203                OG_REST_PARAM_REPOS => $repos,
204                OG_REST_PARAM_PROFILE => $profile,
205                OG_REST_PARAM_TYPE => $type,
206                OG_REST_PARAM_CLIENTS => $ips);
207
208        common_request(OG_REST_CMD_RESTORE_IMAGE, POST, $data);
209}
210
[251bb977]211function poweroff($string_ips) {
212
213        $ips = explode(';',$string_ips);
214
215        $data = array(OG_REST_PARAM_CLIENTS => $ips);
216
217        common_request(OG_REST_CMD_POWEROFF, POST, $data);
218}
219
[da462c8]220function reboot($string_ips) {
221
222        $ips = explode(';',$string_ips);
223
224        $data = array(OG_REST_PARAM_CLIENTS => $ips);
225
226        common_request(OG_REST_CMD_REBOOT, POST, $data);
227}
228
[dc82754]229function stop($string_ips) {
230
231        $ips = explode(';',$string_ips);
232
233        $data = array(OG_REST_PARAM_CLIENTS => $ips);
234
235        common_request(OG_REST_CMD_STOP, POST, $data);
236}
237
[c9cfd44]238function refresh($string_ips) {
239
240        $ips = explode(';',$string_ips);
241
242        $data = array(OG_REST_PARAM_CLIENTS => $ips);
243
244        common_request(OG_REST_CMD_REFRESH, POST, $data);
245}
246
[db537e7]247function hardware($string_ips) {
248
249        $ips = explode(';',$string_ips);
250
251        $data = array(OG_REST_PARAM_CLIENTS => $ips);
252
253        common_request(OG_REST_CMD_HARDWARE, POST, $data);
254}
255
[96cced9]256function software($string_ips) {
257
258        $ips = explode(';',$string_ips);
259
260        $data = array(OG_REST_PARAM_CLIENTS => $ips);
261
262        common_request(OG_REST_CMD_SOFTWARE, POST, $data);
263}
264
[8c9fcb2]265/*
[357352b]266 * @function multiRequest.
267 * @param    URLs array (may include header and POST data), cURL options array.
268 * @return   Array of arrays with JSON requests and response codes.
[04319fb]269 * @warning  Default options: does not verifying certificate, connection timeout 200 ms.
[357352b]270 * @Date     2015-10-14
[8c9fcb2]271 */
[42d268a4]272function multiRequest($data, $options=array(CURLOPT_SSL_VERIFYHOST => false, CURLOPT_SSL_VERIFYPEER => false, CURLOPT_TIMEOUT_MS => 500)) {
[8c9fcb2]273 
274  // array of curl handles
275  $curly = array();
[357352b]276  // Data to be returned (response data and code)
[8c9fcb2]277  $result = array();
278 
279  // multi handle
280  $mh = curl_multi_init();
281 
282  // loop through $data and create curl handles
283  // then add them to the multi-handle
284  foreach ($data as $id => $d) {
285 
286
287    $curly[$id] = curl_init();
288 
289    $url = (is_array($d) && !empty($d['url'])) ? $d['url'] : $d;
[ba3c59d]290    curl_setopt($curly[$id], CURLOPT_URL, $url);
[357352b]291    // HTTP headers?
[69052958]292    if (is_array($d) && !empty($d['header'])) {
[e46f7ce]293       curl_setopt($curly[$id], CURLOPT_HTTPHEADER, $d['header']);
[69052958]294    } else {
295       curl_setopt($curly[$id], CURLOPT_HEADER, 0);
296    }
[8c9fcb2]297    curl_setopt($curly[$id], CURLOPT_RETURNTRANSFER, 1);
298 
299    // post?
300    if (is_array($d)) {
301      if (!empty($d['post'])) {
[ba3c59d]302        curl_setopt($curly[$id], CURLOPT_POST, 1);
[8c9fcb2]303        curl_setopt($curly[$id], CURLOPT_POSTFIELDS, $d['post']);
304      }
305    }
306
307    // extra options?
308    if (!empty($options)) {
309      curl_setopt_array($curly[$id], $options);
310    }
311 
312    curl_multi_add_handle($mh, $curly[$id]);
313  }
314 
315  // execute the handles
316  $running = null;
317  do {
318    curl_multi_exec($mh, $running);
319  } while($running > 0);
320 
321 
[357352b]322  // Get content and HTTP code, and remove handles
[8c9fcb2]323  foreach($curly as $id => $c) {
[357352b]324    $result[$id]['data'] = curl_multi_getcontent($c);
325    $result[$id]['code'] = curl_getinfo($c, CURLINFO_HTTP_CODE);
[8c9fcb2]326    curl_multi_remove_handle($mh, $c);
327  }
328
329 // all done
330  curl_multi_close($mh);
331 
332  return $result;
333}
[b6ec162]334
Note: See TracBrowser for help on using the repository browser.