1 | |
---|
2 | <?php |
---|
3 | |
---|
4 | define('OG_REST_URL', 'http://127.0.0.1:8888/'); |
---|
5 | |
---|
6 | define('GET', 1); |
---|
7 | define('POST', 2); |
---|
8 | define('CUSTOM', 3); |
---|
9 | |
---|
10 | define('OG_REST_CMD_CLIENTS', 'clients'); |
---|
11 | define('OG_REST_CMD_WOL', 'wol'); |
---|
12 | define('OG_REST_CMD_SESSION', 'session'); |
---|
13 | define('OG_REST_CMD_RUN', 'shell/run'); |
---|
14 | define('OG_REST_CMD_OUTPUT', 'shell/output'); |
---|
15 | define('OG_REST_CMD_POWEROFF', 'poweroff'); |
---|
16 | define('OG_REST_CMD_REBOOT', 'reboot'); |
---|
17 | define('OG_REST_CMD_STOP', 'stop'); |
---|
18 | define('OG_REST_CMD_REFRESH', 'refresh'); |
---|
19 | define('OG_REST_CMD_HARDWARE', 'hardware'); |
---|
20 | define('OG_REST_CMD_SOFTWARE', 'software'); |
---|
21 | define('OG_REST_CMD_CREATE_IMAGE', 'image/create'); |
---|
22 | define('OG_REST_CMD_RESTORE_IMAGE', 'image/restore'); |
---|
23 | |
---|
24 | define('OG_REST_PARAM_CLIENTS', 'clients'); |
---|
25 | define('OG_REST_PARAM_ADDR', 'addr'); |
---|
26 | define('OG_REST_PARAM_MAC', 'mac'); |
---|
27 | define('OG_REST_PARAM_DISK', 'disk'); |
---|
28 | define('OG_REST_PARAM_PART', 'partition'); |
---|
29 | define('OG_REST_PARAM_RUN', 'run'); |
---|
30 | define('OG_REST_PARAM_TYPE', 'type'); |
---|
31 | define('OG_REST_PARAM_STATE', 'state'); |
---|
32 | define('OG_REST_PARAM_NAME', 'name'); |
---|
33 | define('OG_REST_PARAM_REPOS', 'repository'); |
---|
34 | define('OG_REST_PARAM_ID', 'id'); |
---|
35 | define('OG_REST_PARAM_CODE', 'code'); |
---|
36 | define('OG_REST_PARAM_PROFILE', 'profile'); |
---|
37 | define('OG_REST_PARAM_TYPE', 'type'); |
---|
38 | |
---|
39 | $conf_file = parse_ini_file(__DIR__ . '/../../etc/ogAdmRepo.cfg'); |
---|
40 | define('OG_REST_API_TOKEN', 'Authorization: ' . $conf_file['ApiToken']); |
---|
41 | |
---|
42 | function common_request($command, $type, $data = null) { |
---|
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); |
---|
50 | curl_setopt($curl, CURLOPT_HTTPHEADER, array( |
---|
51 | OG_REST_API_TOKEN, |
---|
52 | )); |
---|
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) { |
---|
67 | syslog(LOG_ERR, 'error occured during curl exec. Additioanl info: ' . print_r($info, TRUE)); |
---|
68 | return 0; |
---|
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 | |
---|
79 | function 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 | |
---|
101 | function 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; |
---|
110 | $data = null; |
---|
111 | break; |
---|
112 | } |
---|
113 | |
---|
114 | $result = common_request(OG_REST_CMD_CLIENTS, $type, $data); |
---|
115 | |
---|
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 | } |
---|
122 | } |
---|
123 | |
---|
124 | return $trama_notificacion; |
---|
125 | } |
---|
126 | |
---|
127 | function 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 | |
---|
151 | function 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 | |
---|
165 | function 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 | |
---|
188 | function 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 | |
---|
211 | function 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 | |
---|
220 | function 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 | |
---|
229 | function 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 | |
---|
238 | function 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 | |
---|
247 | function 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 | |
---|
256 | function 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 | |
---|
265 | /* |
---|
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. |
---|
269 | * @warning Default options: does not verifying certificate, connection timeout 200 ms. |
---|
270 | * @Date 2015-10-14 |
---|
271 | */ |
---|
272 | function multiRequest($data, $options=array(CURLOPT_SSL_VERIFYHOST => false, CURLOPT_SSL_VERIFYPEER => false, CURLOPT_TIMEOUT_MS => 500)) { |
---|
273 | |
---|
274 | // array of curl handles |
---|
275 | $curly = array(); |
---|
276 | // Data to be returned (response data and code) |
---|
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; |
---|
290 | curl_setopt($curly[$id], CURLOPT_URL, $url); |
---|
291 | // HTTP headers? |
---|
292 | if (is_array($d) && !empty($d['header'])) { |
---|
293 | curl_setopt($curly[$id], CURLOPT_HTTPHEADER, $d['header']); |
---|
294 | } else { |
---|
295 | curl_setopt($curly[$id], CURLOPT_HEADER, 0); |
---|
296 | } |
---|
297 | curl_setopt($curly[$id], CURLOPT_RETURNTRANSFER, 1); |
---|
298 | |
---|
299 | // post? |
---|
300 | if (is_array($d)) { |
---|
301 | if (!empty($d['post'])) { |
---|
302 | curl_setopt($curly[$id], CURLOPT_POST, 1); |
---|
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 | |
---|
322 | // Get content and HTTP code, and remove handles |
---|
323 | foreach($curly as $id => $c) { |
---|
324 | $result[$id]['data'] = curl_multi_getcontent($c); |
---|
325 | $result[$id]['code'] = curl_getinfo($c, CURLINFO_HTTP_CODE); |
---|
326 | curl_multi_remove_handle($mh, $c); |
---|
327 | } |
---|
328 | |
---|
329 | // all done |
---|
330 | curl_multi_close($mh); |
---|
331 | |
---|
332 | return $result; |
---|
333 | } |
---|
334 | |
---|