[b636e61] | 1 | |
---|
[8c9fcb2] | 2 | <?php |
---|
[560455a] | 3 | |
---|
[c9459a1] | 4 | include_once (__DIR__ . '/../../etc/php-vars.php'); |
---|
[560455a] | 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'); |
---|
[251bb977] | 15 | define('OG_REST_CMD_POWEROFF', 'poweroff'); |
---|
[da462c8] | 16 | define('OG_REST_CMD_REBOOT', 'reboot'); |
---|
[dc82754] | 17 | define('OG_REST_CMD_STOP', 'stop'); |
---|
[c9cfd44] | 18 | define('OG_REST_CMD_REFRESH', 'refresh'); |
---|
[db537e7] | 19 | define('OG_REST_CMD_HARDWARE', 'hardware'); |
---|
[96cced9] | 20 | define('OG_REST_CMD_SOFTWARE', 'software'); |
---|
[b636e61] | 21 | define('OG_REST_CMD_CREATE_IMAGE', 'image/create'); |
---|
[d233fdb] | 22 | define('OG_REST_CMD_RESTORE_IMAGE', 'image/restore'); |
---|
[9381fdf] | 23 | define('OG_REST_CMD_SETUP', 'setup'); |
---|
[488b147] | 24 | define('OG_REST_CMD_CREATE_BASIC_IMAGE', 'image/create/basic'); |
---|
[45389c5] | 25 | define('OG_REST_CMD_CREATE_INCREMENTAL_IMAGE', 'image/create/incremental'); |
---|
[8068b82] | 26 | define('OG_REST_CMD_RESTORE_BASIC_IMAGE', 'image/restore/basic'); |
---|
[0ff87c2] | 27 | define('OG_REST_CMD_RESTORE_INCREMENTAL_IMAGE', 'image/restore/incremental'); |
---|
[1d380e5] | 28 | define('OG_REST_CMD_RUN_SCHEDULE', 'run/schedule'); |
---|
[560455a] | 29 | |
---|
| 30 | define('OG_REST_PARAM_CLIENTS', 'clients'); |
---|
| 31 | define('OG_REST_PARAM_ADDR', 'addr'); |
---|
| 32 | define('OG_REST_PARAM_MAC', 'mac'); |
---|
| 33 | define('OG_REST_PARAM_DISK', 'disk'); |
---|
| 34 | define('OG_REST_PARAM_PART', 'partition'); |
---|
| 35 | define('OG_REST_PARAM_RUN', 'run'); |
---|
| 36 | define('OG_REST_PARAM_TYPE', 'type'); |
---|
[e378505] | 37 | define('OG_REST_PARAM_STATE', 'state'); |
---|
[b636e61] | 38 | define('OG_REST_PARAM_NAME', 'name'); |
---|
| 39 | define('OG_REST_PARAM_REPOS', 'repository'); |
---|
| 40 | define('OG_REST_PARAM_ID', 'id'); |
---|
| 41 | define('OG_REST_PARAM_CODE', 'code'); |
---|
[d233fdb] | 42 | define('OG_REST_PARAM_PROFILE', 'profile'); |
---|
[c857bed] | 43 | define('OG_REST_PARAM_CACHE', 'cache'); |
---|
| 44 | define('OG_REST_PARAM_CACHE_SIZE', 'cache_size'); |
---|
| 45 | define('OG_REST_PARAM_FILE_SYSTEM', 'filesystem'); |
---|
| 46 | define('OG_REST_PARAM_SIZE', 'size'); |
---|
| 47 | define('OG_REST_PARAM_FORMAT', 'format'); |
---|
| 48 | define('OG_REST_PARAM_PARTITION_SETUP', 'partition_setup'); |
---|
[488b147] | 49 | define('OG_REST_PARAM_SYNC_PARAMS', 'sync_params'); |
---|
| 50 | define('OG_REST_PARAM_SYNC', 'sync'); |
---|
| 51 | define('OG_REST_PARAM_DIFF', 'diff'); |
---|
| 52 | define('OG_REST_PARAM_REMOVE', 'remove'); |
---|
| 53 | define('OG_REST_PARAM_COMPRESS', 'compress'); |
---|
| 54 | define('OG_REST_PARAM_CLEANUP', 'cleanup'); |
---|
| 55 | define('OG_REST_PARAM_CLEANUP_CACHE', 'cleanup_cache'); |
---|
| 56 | define('OG_REST_PARAM_REMOVE_DST', 'remove_dst'); |
---|
[8068b82] | 57 | define('OG_REST_PARAM_PATH', 'path'); |
---|
[45389c5] | 58 | define('OG_REST_PARAM_DIFF_ID', 'diff_id'); |
---|
| 59 | define('OG_REST_PARAM_DIFF_NAME', 'diff_name'); |
---|
[8068b82] | 60 | define('OG_REST_PARAM_METHOD', 'method'); |
---|
[e7ad0b8] | 61 | define('OG_REST_PARAM_ECHO', 'echo'); |
---|
[560455a] | 62 | |
---|
[436abc7] | 63 | $conf_file = parse_ini_file(__DIR__ . '/../../etc/ogAdmServer.cfg'); |
---|
| 64 | define('OG_REST_API_TOKEN', 'Authorization: ' . $conf_file['APITOKEN']); |
---|
[ef61154] | 65 | |
---|
[41ceac0] | 66 | function common_request($command, $type, $data = null) { |
---|
[560455a] | 67 | |
---|
| 68 | $json = json_encode($data); |
---|
| 69 | |
---|
| 70 | $service_url = OG_REST_URL.$command; |
---|
| 71 | |
---|
| 72 | $curl = curl_init($service_url); |
---|
| 73 | curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); |
---|
[ef61154] | 74 | curl_setopt($curl, CURLOPT_HTTPHEADER, array( |
---|
| 75 | OG_REST_API_TOKEN, |
---|
| 76 | )); |
---|
[c9459a1] | 77 | curl_setopt ($curl, CURLOPT_SSLCERT, __DIR__ . '/../../etc/ssl/WebConsole.crt.pem'); |
---|
| 78 | curl_setopt ($curl, CURLOPT_SSLKEY, __DIR__ . '/../../etc/ssl/WebConsole.key.pem'); |
---|
[560455a] | 79 | |
---|
| 80 | switch ($type) { |
---|
| 81 | default: |
---|
| 82 | case GET: |
---|
| 83 | break; |
---|
| 84 | case POST: |
---|
| 85 | curl_setopt($curl, CURLOPT_POST, true); |
---|
| 86 | curl_setopt($curl, CURLOPT_POSTFIELDS, $json); |
---|
| 87 | } |
---|
| 88 | |
---|
| 89 | $curl_response = curl_exec($curl); |
---|
| 90 | $info = curl_getinfo($curl); |
---|
| 91 | |
---|
| 92 | if ($curl_response === false || $info['http_code'] != 200) { |
---|
[4d7e35c] | 93 | syslog(LOG_ERR, 'error occured during curl exec. Additioanl info: ' . print_r($info, TRUE)); |
---|
[dc82754] | 94 | return 0; |
---|
[560455a] | 95 | } |
---|
| 96 | |
---|
| 97 | curl_close($curl); |
---|
| 98 | |
---|
| 99 | syslog(LOG_INFO, 'response '.$command.' ok!'); |
---|
| 100 | |
---|
| 101 | return json_decode($curl_response, true); |
---|
| 102 | } |
---|
| 103 | |
---|
| 104 | |
---|
| 105 | function shell($case, $string_ips, $command) { |
---|
| 106 | |
---|
| 107 | $ips = explode(';',$string_ips); |
---|
| 108 | |
---|
| 109 | switch ($case) { |
---|
[15243d4] | 110 | case 3: |
---|
| 111 | $command = substr($command, 4); |
---|
[e7ad0b8] | 112 | $data = array(OG_REST_PARAM_CLIENTS => $ips, |
---|
| 113 | OG_REST_PARAM_RUN => $command, |
---|
| 114 | OG_REST_PARAM_ECHO => false); |
---|
| 115 | $command = OG_REST_CMD_RUN; |
---|
| 116 | break; |
---|
[560455a] | 117 | case 1: |
---|
| 118 | $data = array(OG_REST_PARAM_CLIENTS => $ips, |
---|
[e7ad0b8] | 119 | OG_REST_PARAM_RUN => $command, |
---|
| 120 | OG_REST_PARAM_ECHO => true); |
---|
[560455a] | 121 | $command = OG_REST_CMD_RUN; |
---|
| 122 | break; |
---|
| 123 | default: |
---|
| 124 | case 2: |
---|
| 125 | $data = array(OG_REST_PARAM_CLIENTS => $ips); |
---|
| 126 | $command = OG_REST_CMD_OUTPUT; |
---|
| 127 | } |
---|
| 128 | |
---|
| 129 | $result = common_request($command, POST, |
---|
| 130 | $data)[OG_REST_PARAM_CLIENTS][0]['output']; |
---|
| 131 | |
---|
| 132 | return (is_null($result) ? '1' : $result); |
---|
| 133 | } |
---|
| 134 | |
---|
| 135 | function clients($case, $ips) { |
---|
| 136 | |
---|
| 137 | switch ($case) { |
---|
| 138 | case 1: |
---|
| 139 | $type = POST; |
---|
| 140 | $data = array(OG_REST_PARAM_CLIENTS => $ips); |
---|
| 141 | break; |
---|
| 142 | case 2: |
---|
| 143 | $type = GET; |
---|
[0b97d96] | 144 | $data = null; |
---|
[560455a] | 145 | break; |
---|
| 146 | } |
---|
| 147 | |
---|
| 148 | $result = common_request(OG_REST_CMD_CLIENTS, $type, $data); |
---|
| 149 | |
---|
[0b97d96] | 150 | $trama_notificacion = ""; |
---|
| 151 | if (isset($result[OG_REST_PARAM_CLIENTS])) { |
---|
| 152 | foreach ($result[OG_REST_PARAM_CLIENTS] as $client) { |
---|
| 153 | $trama_notificacion .= $client[OG_REST_PARAM_ADDR].'/'. |
---|
| 154 | $client[OG_REST_PARAM_STATE].';'; |
---|
| 155 | } |
---|
[560455a] | 156 | } |
---|
| 157 | |
---|
| 158 | return $trama_notificacion; |
---|
| 159 | } |
---|
| 160 | |
---|
| 161 | function wol($type_wol, $macs, $ips) { |
---|
| 162 | |
---|
| 163 | switch ($type_wol) { |
---|
| 164 | default: |
---|
| 165 | case 1: |
---|
| 166 | $wol = 'broadcast'; |
---|
| 167 | break; |
---|
| 168 | case 2: |
---|
| 169 | $wol = 'unicast'; |
---|
| 170 | } |
---|
| 171 | |
---|
| 172 | $clients = array(); |
---|
| 173 | |
---|
| 174 | for($i=0; $i<count($macs); $i++) { |
---|
| 175 | $clients[] = array(OG_REST_PARAM_ADDR => $ips[$i], |
---|
| 176 | OG_REST_PARAM_MAC => $macs[$i]); |
---|
| 177 | } |
---|
| 178 | |
---|
| 179 | $data = array(OG_REST_PARAM_TYPE => $wol, |
---|
| 180 | OG_REST_PARAM_CLIENTS => $clients); |
---|
| 181 | |
---|
| 182 | common_request(OG_REST_CMD_WOL, POST, $data); |
---|
| 183 | } |
---|
| 184 | |
---|
| 185 | function session($string_ips, $params) { |
---|
| 186 | |
---|
| 187 | preg_match_all('!\d{1}!', $params, $matches); |
---|
| 188 | |
---|
| 189 | $ips = explode(';',$string_ips); |
---|
| 190 | $disk = $matches[0][0]; |
---|
| 191 | $part = $matches[0][1]; |
---|
| 192 | |
---|
| 193 | $data = array(OG_REST_PARAM_CLIENTS => $ips, |
---|
| 194 | OG_REST_PARAM_DISK => $disk, OG_REST_PARAM_PART => $part); |
---|
| 195 | |
---|
| 196 | common_request(OG_REST_CMD_SESSION, POST, $data); |
---|
| 197 | } |
---|
| 198 | |
---|
[b636e61] | 199 | function create_image($string_ips, $params) { |
---|
| 200 | |
---|
| 201 | preg_match_all('/(?<=\=)(.*?)(?=\r)/', $params, $matches); |
---|
| 202 | |
---|
| 203 | $ips = explode(';',$string_ips); |
---|
| 204 | $disk = $matches[0][0]; |
---|
| 205 | $part = $matches[0][1]; |
---|
| 206 | $code = $matches[0][2]; |
---|
| 207 | $id = $matches[0][3]; |
---|
| 208 | $name = $matches[0][4]; |
---|
| 209 | $repos = $matches[0][5]; |
---|
| 210 | |
---|
| 211 | $data = array(OG_REST_PARAM_CLIENTS => $ips, |
---|
| 212 | OG_REST_PARAM_DISK => $disk, |
---|
| 213 | OG_REST_PARAM_PART => $part, |
---|
| 214 | OG_REST_PARAM_CODE => $code, |
---|
| 215 | OG_REST_PARAM_ID => $id, |
---|
| 216 | OG_REST_PARAM_NAME => $name, |
---|
| 217 | OG_REST_PARAM_REPOS => $repos); |
---|
| 218 | |
---|
| 219 | common_request(OG_REST_CMD_CREATE_IMAGE, POST, $data); |
---|
| 220 | } |
---|
| 221 | |
---|
[d233fdb] | 222 | function restore_image($string_ips, $params) { |
---|
| 223 | |
---|
| 224 | preg_match_all('/(?<=\=)(.*?)(?=\r)/', $params, $matches); |
---|
| 225 | |
---|
| 226 | $ips = explode(';',$string_ips); |
---|
| 227 | $disk = $matches[0][0]; |
---|
| 228 | $part = $matches[0][1]; |
---|
| 229 | $image_id = $matches[0][2]; |
---|
| 230 | $name = $matches[0][3]; |
---|
| 231 | $repos = $matches[0][4]; |
---|
| 232 | $profile = $matches[0][5]; |
---|
| 233 | $type = $matches[0][6]; |
---|
| 234 | |
---|
| 235 | $data = array(OG_REST_PARAM_DISK => $disk, OG_REST_PARAM_PART => $part, |
---|
[dcfa451] | 236 | OG_REST_PARAM_ID => $image_id, OG_REST_PARAM_NAME => $name, |
---|
[d233fdb] | 237 | OG_REST_PARAM_REPOS => $repos, |
---|
| 238 | OG_REST_PARAM_PROFILE => $profile, |
---|
| 239 | OG_REST_PARAM_TYPE => $type, |
---|
| 240 | OG_REST_PARAM_CLIENTS => $ips); |
---|
| 241 | |
---|
| 242 | common_request(OG_REST_CMD_RESTORE_IMAGE, POST, $data); |
---|
| 243 | } |
---|
| 244 | |
---|
[488b147] | 245 | function create_basic_image($string_ips, $params) { |
---|
| 246 | |
---|
| 247 | preg_match_all('/(?<=\=)[^\r]*(?=\r)?/', $params, $matches); |
---|
| 248 | |
---|
| 249 | $ips = explode(';',$string_ips); |
---|
| 250 | $disk = $matches[0][0]; |
---|
| 251 | $part = $matches[0][1]; |
---|
| 252 | $code = $matches[0][2]; |
---|
| 253 | $image_id = $matches[0][3]; |
---|
| 254 | $name = $matches[0][4]; |
---|
| 255 | $repos = $matches[0][5]; |
---|
| 256 | |
---|
| 257 | $sync = $matches[0][7]; // Syncronization method |
---|
| 258 | |
---|
| 259 | $diff = $matches[0][8]; // Send the whole file if there are differences |
---|
| 260 | $remove = $matches[0][9]; // Delete files at destination that are not at source |
---|
| 261 | $compress = $matches[0][10]; // Compress before sending |
---|
| 262 | |
---|
| 263 | $cleanup = $matches[0][11]; // Delete image before creating it |
---|
| 264 | $cache = $matches[0][12]; // Copy image to cache |
---|
| 265 | $cleanup_cache = $matches[0][13]; // Delete image from cache before copying |
---|
| 266 | $remove_dst = $matches[0][14]; // Dont delete files in destination |
---|
| 267 | |
---|
| 268 | $data = array(OG_REST_PARAM_CLIENTS => $ips, |
---|
| 269 | OG_REST_PARAM_DISK => $disk, |
---|
| 270 | OG_REST_PARAM_PART => $part, |
---|
| 271 | OG_REST_PARAM_CODE => $code, |
---|
| 272 | OG_REST_PARAM_ID => $image_id, |
---|
| 273 | OG_REST_PARAM_NAME => $name, |
---|
| 274 | OG_REST_PARAM_REPOS => $repos, |
---|
| 275 | OG_REST_PARAM_SYNC_PARAMS => array( |
---|
| 276 | OG_REST_PARAM_SYNC => $sync, |
---|
| 277 | OG_REST_PARAM_DIFF => $diff, |
---|
| 278 | OG_REST_PARAM_REMOVE => $remove, |
---|
| 279 | OG_REST_PARAM_COMPRESS => $compress, |
---|
| 280 | OG_REST_PARAM_CLEANUP => $cleanup, |
---|
| 281 | OG_REST_PARAM_CACHE => $cache, |
---|
| 282 | OG_REST_PARAM_CLEANUP_CACHE => $cleanup_cache, |
---|
| 283 | OG_REST_PARAM_REMOVE_DST => $remove_dst, |
---|
| 284 | ) |
---|
| 285 | ); |
---|
| 286 | |
---|
| 287 | common_request(OG_REST_CMD_CREATE_BASIC_IMAGE, POST, $data); |
---|
| 288 | } |
---|
| 289 | |
---|
[45389c5] | 290 | function create_incremental_image($string_ips, $params) { |
---|
| 291 | |
---|
| 292 | preg_match_all('/(?<=\=)[^\r]*(?=\r)?/', $params, $matches); |
---|
| 293 | |
---|
| 294 | $ips = explode(';',$string_ips); |
---|
| 295 | $disk = $matches[0][0]; |
---|
| 296 | $part = $matches[0][1]; |
---|
| 297 | $id = $matches[0][2]; |
---|
| 298 | $name = $matches[0][3]; |
---|
| 299 | $repos = $matches[0][4]; |
---|
| 300 | $diff_id = $matches[0][5]; |
---|
| 301 | $diff_name = $matches[0][6]; |
---|
| 302 | $path = $matches[0][7]; |
---|
| 303 | $sync = $matches[0][8]; |
---|
| 304 | $diff = $matches[0][9]; |
---|
| 305 | $remove = $matches[0][10]; |
---|
| 306 | $compress = $matches[0][11]; |
---|
| 307 | $cleanup = $matches[0][12]; |
---|
| 308 | $cache = $matches[0][13]; |
---|
| 309 | $cleanup_cache = $matches[0][14]; |
---|
| 310 | $remove_dst = $matches[0][15]; |
---|
| 311 | |
---|
| 312 | $data = array(OG_REST_PARAM_CLIENTS => $ips, |
---|
| 313 | OG_REST_PARAM_DISK => $disk, |
---|
| 314 | OG_REST_PARAM_PART => $part, |
---|
| 315 | OG_REST_PARAM_ID => $id, |
---|
| 316 | OG_REST_PARAM_NAME => $name, |
---|
| 317 | OG_REST_PARAM_REPOS => $repos, |
---|
| 318 | OG_REST_PARAM_SYNC_PARAMS => array( |
---|
| 319 | OG_REST_PARAM_SYNC => $sync, |
---|
| 320 | OG_REST_PARAM_PATH => $path, |
---|
| 321 | OG_REST_PARAM_DIFF => $diff, |
---|
| 322 | OG_REST_PARAM_DIFF_ID => $diff_id, |
---|
| 323 | OG_REST_PARAM_DIFF_NAME => $diff_name, |
---|
| 324 | OG_REST_PARAM_REMOVE => $remove, |
---|
| 325 | OG_REST_PARAM_COMPRESS => $compress, |
---|
| 326 | OG_REST_PARAM_CLEANUP => $cleanup, |
---|
| 327 | OG_REST_PARAM_CACHE => $cache, |
---|
| 328 | OG_REST_PARAM_CLEANUP_CACHE => $cleanup_cache, |
---|
| 329 | OG_REST_PARAM_REMOVE_DST => $remove_dst) |
---|
| 330 | ); |
---|
| 331 | |
---|
| 332 | common_request(OG_REST_CMD_CREATE_INCREMENTAL_IMAGE, POST, $data); |
---|
| 333 | } |
---|
| 334 | |
---|
[8068b82] | 335 | function restore_basic_image($string_ips, $params) { |
---|
| 336 | |
---|
| 337 | preg_match_all('/(?<=\=)[^\r]*(?=\r)?/', $params, $matches); |
---|
| 338 | |
---|
| 339 | $ips = explode(';',$string_ips); |
---|
| 340 | $disk = $matches[0][0]; |
---|
| 341 | $part = $matches[0][1]; |
---|
| 342 | $image_id = $matches[0][2]; |
---|
| 343 | $name = $matches[0][3]; |
---|
| 344 | $repos = $matches[0][4]; |
---|
| 345 | $profile = $matches[0][5]; |
---|
| 346 | |
---|
| 347 | $path = $matches[0][6]; |
---|
| 348 | $method = $matches[0][7]; |
---|
| 349 | $sync = $matches[0][8]; // Syncronization method |
---|
| 350 | |
---|
| 351 | $type = $matches[0][9]; |
---|
| 352 | |
---|
| 353 | $diff = $matches[0][10]; // Send the whole file if there are differences |
---|
| 354 | $remove = $matches[0][11]; // Delete files at destination that are not at source |
---|
| 355 | $compress = $matches[0][12]; // Compress before sending |
---|
| 356 | |
---|
| 357 | $cleanup = $matches[0][13]; // Delete image before creating it |
---|
| 358 | $cache = $matches[0][14]; // Copy image to cache |
---|
| 359 | $cleanup_cache = $matches[0][15]; // Delete image from cache before copying |
---|
| 360 | $remove_dst = $matches[0][16]; // Dont delete files in destination |
---|
| 361 | |
---|
| 362 | $data = array(OG_REST_PARAM_CLIENTS => $ips, |
---|
| 363 | OG_REST_PARAM_DISK => $disk, |
---|
| 364 | OG_REST_PARAM_PART => $part, |
---|
| 365 | OG_REST_PARAM_ID => $image_id, |
---|
| 366 | OG_REST_PARAM_NAME => $name, |
---|
| 367 | OG_REST_PARAM_REPOS => $repos, |
---|
| 368 | OG_REST_PARAM_PROFILE => $profile, |
---|
| 369 | OG_REST_PARAM_TYPE => $type, |
---|
| 370 | OG_REST_PARAM_SYNC_PARAMS => array( |
---|
| 371 | OG_REST_PARAM_PATH => $path, |
---|
| 372 | OG_REST_PARAM_METHOD => $method, |
---|
| 373 | OG_REST_PARAM_SYNC => $sync, |
---|
| 374 | OG_REST_PARAM_DIFF => $diff, |
---|
| 375 | OG_REST_PARAM_REMOVE => $remove, |
---|
| 376 | OG_REST_PARAM_COMPRESS => $compress, |
---|
| 377 | OG_REST_PARAM_CLEANUP => $cleanup, |
---|
| 378 | OG_REST_PARAM_CACHE => $cache, |
---|
| 379 | OG_REST_PARAM_CLEANUP_CACHE => $cleanup_cache, |
---|
| 380 | OG_REST_PARAM_REMOVE_DST => $remove_dst, |
---|
| 381 | ) |
---|
| 382 | ); |
---|
| 383 | |
---|
| 384 | common_request(OG_REST_CMD_RESTORE_BASIC_IMAGE, POST, $data); |
---|
| 385 | } |
---|
| 386 | |
---|
[0ff87c2] | 387 | function restore_incremental_image($string_ips, $params) { |
---|
| 388 | |
---|
| 389 | preg_match_all('/(?<=\=)[^\r]*(?=\r)?/', $params, $matches); |
---|
| 390 | |
---|
| 391 | $ips = explode(';',$string_ips); |
---|
| 392 | $disk = $matches[0][0]; |
---|
| 393 | $part = $matches[0][1]; |
---|
| 394 | $image_id = $matches[0][2]; |
---|
| 395 | $name = $matches[0][3]; |
---|
| 396 | $repos = $matches[0][4]; |
---|
| 397 | $profile = $matches[0][5]; |
---|
| 398 | $diff_id = $matches[0][6]; |
---|
| 399 | $diff_name = $matches[0][7]; |
---|
| 400 | $path = $matches[0][8]; |
---|
| 401 | $method = $matches[0][9]; |
---|
| 402 | $sync = $matches[0][10]; |
---|
| 403 | $type = $matches[0][11]; |
---|
| 404 | $diff = $matches[0][12]; |
---|
| 405 | $remove = $matches[0][13]; |
---|
| 406 | $compress = $matches[0][14]; |
---|
| 407 | $cleanup = $matches[0][15]; |
---|
| 408 | $cache = $matches[0][16]; |
---|
| 409 | $cleanup_cache = $matches[0][17]; |
---|
| 410 | $remove_dst = $matches[0][18]; |
---|
| 411 | |
---|
| 412 | $data = array(OG_REST_PARAM_CLIENTS => $ips, |
---|
| 413 | OG_REST_PARAM_DISK => $disk, |
---|
| 414 | OG_REST_PARAM_PART => $part, |
---|
| 415 | OG_REST_PARAM_ID => $image_id, |
---|
| 416 | OG_REST_PARAM_NAME => $name, |
---|
| 417 | OG_REST_PARAM_REPOS => $repos, |
---|
| 418 | OG_REST_PARAM_PROFILE => $profile, |
---|
| 419 | OG_REST_PARAM_TYPE => $type, |
---|
| 420 | OG_REST_PARAM_SYNC_PARAMS => array( |
---|
| 421 | OG_REST_PARAM_DIFF_ID => $diff_id, |
---|
| 422 | OG_REST_PARAM_DIFF_NAME => $diff_name, |
---|
| 423 | OG_REST_PARAM_PATH => $path, |
---|
| 424 | OG_REST_PARAM_METHOD => $method, |
---|
| 425 | OG_REST_PARAM_SYNC => $sync, |
---|
| 426 | OG_REST_PARAM_DIFF => $diff, |
---|
| 427 | OG_REST_PARAM_REMOVE => $remove, |
---|
| 428 | OG_REST_PARAM_COMPRESS => $compress, |
---|
| 429 | OG_REST_PARAM_CLEANUP => $cleanup, |
---|
| 430 | OG_REST_PARAM_CACHE => $cache, |
---|
| 431 | OG_REST_PARAM_CLEANUP_CACHE => $cleanup_cache, |
---|
| 432 | OG_REST_PARAM_REMOVE_DST => $remove_dst, |
---|
| 433 | ) |
---|
| 434 | ); |
---|
| 435 | |
---|
| 436 | common_request(OG_REST_CMD_RESTORE_INCREMENTAL_IMAGE, POST, $data); |
---|
| 437 | } |
---|
| 438 | |
---|
[251bb977] | 439 | function poweroff($string_ips) { |
---|
| 440 | |
---|
| 441 | $ips = explode(';',$string_ips); |
---|
| 442 | |
---|
| 443 | $data = array(OG_REST_PARAM_CLIENTS => $ips); |
---|
| 444 | |
---|
| 445 | common_request(OG_REST_CMD_POWEROFF, POST, $data); |
---|
| 446 | } |
---|
| 447 | |
---|
[da462c8] | 448 | function reboot($string_ips) { |
---|
| 449 | |
---|
| 450 | $ips = explode(';',$string_ips); |
---|
| 451 | |
---|
| 452 | $data = array(OG_REST_PARAM_CLIENTS => $ips); |
---|
| 453 | |
---|
| 454 | common_request(OG_REST_CMD_REBOOT, POST, $data); |
---|
| 455 | } |
---|
| 456 | |
---|
[dc82754] | 457 | function stop($string_ips) { |
---|
| 458 | |
---|
| 459 | $ips = explode(';',$string_ips); |
---|
| 460 | |
---|
| 461 | $data = array(OG_REST_PARAM_CLIENTS => $ips); |
---|
| 462 | |
---|
| 463 | common_request(OG_REST_CMD_STOP, POST, $data); |
---|
| 464 | } |
---|
| 465 | |
---|
[c9cfd44] | 466 | function refresh($string_ips) { |
---|
| 467 | |
---|
| 468 | $ips = explode(';',$string_ips); |
---|
| 469 | |
---|
| 470 | $data = array(OG_REST_PARAM_CLIENTS => $ips); |
---|
| 471 | |
---|
| 472 | common_request(OG_REST_CMD_REFRESH, POST, $data); |
---|
| 473 | } |
---|
| 474 | |
---|
[db537e7] | 475 | function hardware($string_ips) { |
---|
| 476 | |
---|
| 477 | $ips = explode(';',$string_ips); |
---|
| 478 | |
---|
| 479 | $data = array(OG_REST_PARAM_CLIENTS => $ips); |
---|
| 480 | |
---|
| 481 | common_request(OG_REST_CMD_HARDWARE, POST, $data); |
---|
| 482 | } |
---|
| 483 | |
---|
[de36dbe] | 484 | function software($string_ips, $params) { |
---|
| 485 | |
---|
| 486 | preg_match_all('/(?<=\=)(.*?)(?=\r)/', $params, $matches); |
---|
[96cced9] | 487 | |
---|
| 488 | $ips = explode(';',$string_ips); |
---|
[de36dbe] | 489 | $disk = $matches[0][0]; |
---|
| 490 | $part = $matches[0][1]; |
---|
[96cced9] | 491 | |
---|
[de36dbe] | 492 | $data = array(OG_REST_PARAM_CLIENTS => $ips, |
---|
| 493 | OG_REST_PARAM_DISK => $disk, |
---|
| 494 | OG_REST_PARAM_PART => $part); |
---|
[96cced9] | 495 | |
---|
| 496 | common_request(OG_REST_CMD_SOFTWARE, POST, $data); |
---|
| 497 | } |
---|
| 498 | |
---|
[c857bed] | 499 | function setup($string_ips, $params) { |
---|
| 500 | |
---|
| 501 | preg_match_all('/(?<=\=)(?!dis)(.*?)((?=\*)|(?=\r)|(?=\!)|(?=\%))/', |
---|
| 502 | $params, $matches); |
---|
| 503 | |
---|
| 504 | $ips = explode(';',$string_ips); |
---|
| 505 | $disk = $matches[0][0]; |
---|
| 506 | $cache = $matches[0][2]; |
---|
| 507 | $cache_size = $matches[0][3]; |
---|
| 508 | $partition_number = array(); |
---|
| 509 | $partition_code = array(); |
---|
| 510 | $file_system = array(); |
---|
| 511 | $part_size = array(); |
---|
| 512 | $format = array(); |
---|
| 513 | for ($x = 0; $x < 4; $x++) { |
---|
| 514 | $partition_number[$x] = $matches[0][4 + 5 * $x]; |
---|
| 515 | $partition_code[$x] = $matches[0][5 + 5 * $x]; |
---|
| 516 | $file_system[$x] = $matches[0][6 + 5 * $x]; |
---|
| 517 | $part_size[$x] = $matches[0][7 + 5 * $x]; |
---|
| 518 | $format[$x] = $matches[0][8 + 5 * $x]; |
---|
| 519 | } |
---|
| 520 | |
---|
| 521 | $data = array( |
---|
| 522 | OG_REST_PARAM_CLIENTS => $ips, |
---|
| 523 | OG_REST_PARAM_DISK => $disk, |
---|
| 524 | OG_REST_PARAM_CACHE => $cache, |
---|
| 525 | OG_REST_PARAM_CACHE_SIZE => $cache_size, |
---|
| 526 | OG_REST_PARAM_PARTITION_SETUP => array() |
---|
| 527 | ); |
---|
| 528 | |
---|
| 529 | for ($i = 0; $i < sizeof($partition_number); $i++) { |
---|
| 530 | $partition_setup = array( |
---|
| 531 | OG_REST_PARAM_PART => $partition_number[$i], |
---|
| 532 | OG_REST_PARAM_CODE => $partition_code[$i], |
---|
| 533 | OG_REST_PARAM_FILE_SYSTEM => $file_system[$i], |
---|
| 534 | OG_REST_PARAM_SIZE => $part_size[$i], |
---|
| 535 | OG_REST_PARAM_FORMAT => $format[$i] |
---|
| 536 | ); |
---|
| 537 | array_push($data[OG_REST_PARAM_PARTITION_SETUP], $partition_setup); |
---|
| 538 | } |
---|
| 539 | |
---|
| 540 | common_request(OG_REST_CMD_SETUP, POST, $data); |
---|
| 541 | } |
---|
| 542 | |
---|
[1d380e5] | 543 | function run_schedule($string_ips) { |
---|
| 544 | $ips = explode(';',$string_ips); |
---|
| 545 | $data = array(OG_REST_PARAM_CLIENTS => $ips); |
---|
| 546 | common_request(OG_REST_CMD_RUN_SCHEDULE, POST, $data); |
---|
| 547 | } |
---|
| 548 | |
---|
[8c9fcb2] | 549 | /* |
---|
[357352b] | 550 | * @function multiRequest. |
---|
| 551 | * @param URLs array (may include header and POST data), cURL options array. |
---|
| 552 | * @return Array of arrays with JSON requests and response codes. |
---|
[04319fb] | 553 | * @warning Default options: does not verifying certificate, connection timeout 200 ms. |
---|
[357352b] | 554 | * @Date 2015-10-14 |
---|
[8c9fcb2] | 555 | */ |
---|
[42d268a4] | 556 | function multiRequest($data, $options=array(CURLOPT_SSL_VERIFYHOST => false, CURLOPT_SSL_VERIFYPEER => false, CURLOPT_TIMEOUT_MS => 500)) { |
---|
[8c9fcb2] | 557 | |
---|
| 558 | // array of curl handles |
---|
| 559 | $curly = array(); |
---|
[357352b] | 560 | // Data to be returned (response data and code) |
---|
[8c9fcb2] | 561 | $result = array(); |
---|
| 562 | |
---|
| 563 | // multi handle |
---|
| 564 | $mh = curl_multi_init(); |
---|
| 565 | |
---|
| 566 | // loop through $data and create curl handles |
---|
| 567 | // then add them to the multi-handle |
---|
| 568 | foreach ($data as $id => $d) { |
---|
| 569 | |
---|
| 570 | |
---|
| 571 | $curly[$id] = curl_init(); |
---|
| 572 | |
---|
| 573 | $url = (is_array($d) && !empty($d['url'])) ? $d['url'] : $d; |
---|
[ba3c59d] | 574 | curl_setopt($curly[$id], CURLOPT_URL, $url); |
---|
[357352b] | 575 | // HTTP headers? |
---|
[69052958] | 576 | if (is_array($d) && !empty($d['header'])) { |
---|
[e46f7ce] | 577 | curl_setopt($curly[$id], CURLOPT_HTTPHEADER, $d['header']); |
---|
[69052958] | 578 | } else { |
---|
| 579 | curl_setopt($curly[$id], CURLOPT_HEADER, 0); |
---|
| 580 | } |
---|
[8c9fcb2] | 581 | curl_setopt($curly[$id], CURLOPT_RETURNTRANSFER, 1); |
---|
| 582 | |
---|
| 583 | // post? |
---|
| 584 | if (is_array($d)) { |
---|
| 585 | if (!empty($d['post'])) { |
---|
[ba3c59d] | 586 | curl_setopt($curly[$id], CURLOPT_POST, 1); |
---|
[8c9fcb2] | 587 | curl_setopt($curly[$id], CURLOPT_POSTFIELDS, $d['post']); |
---|
| 588 | } |
---|
| 589 | } |
---|
| 590 | |
---|
| 591 | // extra options? |
---|
| 592 | if (!empty($options)) { |
---|
| 593 | curl_setopt_array($curly[$id], $options); |
---|
| 594 | } |
---|
| 595 | |
---|
| 596 | curl_multi_add_handle($mh, $curly[$id]); |
---|
| 597 | } |
---|
| 598 | |
---|
| 599 | // execute the handles |
---|
| 600 | $running = null; |
---|
| 601 | do { |
---|
| 602 | curl_multi_exec($mh, $running); |
---|
| 603 | } while($running > 0); |
---|
| 604 | |
---|
| 605 | |
---|
[357352b] | 606 | // Get content and HTTP code, and remove handles |
---|
[8c9fcb2] | 607 | foreach($curly as $id => $c) { |
---|
[357352b] | 608 | $result[$id]['data'] = curl_multi_getcontent($c); |
---|
| 609 | $result[$id]['code'] = curl_getinfo($c, CURLINFO_HTTP_CODE); |
---|
[8c9fcb2] | 610 | curl_multi_remove_handle($mh, $c); |
---|
| 611 | } |
---|
| 612 | |
---|
| 613 | // all done |
---|
| 614 | curl_multi_close($mh); |
---|
| 615 | |
---|
| 616 | return $result; |
---|
| 617 | } |
---|
[b6ec162] | 618 | |
---|