source: admin/WebConsole/includes/restfunctions.php @ fc15dd1

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 fc15dd1 was b636e61, checked in by OpenGnSys Support Team <soporte-og@…>, 6 years ago

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

SocketHidra? crear imagen has been replaced by POST /imagen/create.

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