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