// All rights reserved. // Redistribution and use in source and binary forms, with or without // modification, are permitted provided that the following conditions // are met: // 1. Redistributions of source code must retain the above copyright // notice, this list of conditions and the following disclaimer. // 2. Redistributions in binary form must reproduce the above copyright // notice, this list of conditions and the following disclaimer in the // documentation and/or other materials provided with the distribution. // THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND // ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE // ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE // FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL // DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS // OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) // HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT // LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY // OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF // SUCH DAMAGE. require_once 'Log.php'; function cleanup() { global $logger; $logger->close(); } function print_header($title) { ?> <?= $title ?> /dev/null 2>&1 &"); } function get_PI_REST($url, $p = false, $decode = true, $timeout = 300) { $cookie = false; $error = ''; if (DEBUG >= 5) { echo "

calling $url with params:

"; var_dump($p); } $i = 0; $res = false; while ($i < REST_RETRIES) { $res = fetch_url($url, $cookie, $error, $p, $timeout); if ($res) { break; } sleep(3); ++$i; } if ($res) { if ($decode) { $json = json_decode($res, true); return $json; } else { return $res; } } return false; } function apicGetTicket($host, $user, $pass, &$error) { $cookie = false; $url = 'https://' . $host . '/api/v1/ticket'; $p = array(); $p['post'] = json_encode(array('username' => $user, 'password' => $pass)); $p['header']['Content-Type'] = 'application/json'; $i = 0; $res = false; while ($i < REST_RETRIES) { $res = fetch_url($url, $cookie, $error, $p); if ($res) { break; } sleep(REST_RETRY_INTERVAL); ++$i; } if ($res) { $json = json_decode($res, true); return $json['response']['serviceTicket']; } return false; } function apicAddDevice($host, $ticket, $projid, $device, $sn, $pid, &$msg, $config = null, $image = null) { $cookie = false; $error = ''; $url = 'https://' . $host . '/api/v1/pnp-project/' . $projid . '/device'; $p = array(); $params = array(); $params['hostName'] = $device; $params['serialNumber'] = $sn; $params['platformId'] = $pid; if ($config) { $params['configId'] = $config; } if ($image) { $params['imageId'] = $image; } $p['post'] = json_encode($params); $p['header']['Content-Type'] = 'application/json'; $p['header']['X-Auth-Token'] = $ticket; $i = 0; $res = false; while ($i < REST_RETRIES) { $res = fetch_url($url, $cookie, $error, $p); if ($res) { break; } sleep(REST_RETRY_INTERVAL); ++$i; } if ($res) { $json = json_decode($res, true); return apicGetTaskResult($host, $ticket, $json['response']['taskId'], $msg); } $msg = $error; return false; } function apicGetTaskResult($host, $ticket, $task, &$msg, $timeout = 60) { $cookie = false; $error = ''; $url = 'https://' . $host . '/api/v1/task/' . $task; $p = array(); $p['header']['X-Auth-Token'] = $ticket; $done = false; $j = 0; while ($j < $timeout) { $i = 0; $res = false; while ($i < REST_RETRIES) { $res = fetch_url($url, $cookie, $error, $p); if ($res) { break; } sleep(REST_RETRY_INTERVAL); ++$i; } if ($res) { $json = json_decode($res, true); if (isset($json['response']['isError']) && $json['response']['isError'] === true) { $msg = $json['response']['failureReason']; return false; } if (isset($json['response']['endTime']) && $json['response']['endTime'] >= $json['response']['startTime']) { return true; } // This allows the timeout to be specified in seconds. sleep(1); ++$j; } } if ($error != '') { $msg = $error; } else { $msg = 'Failed to get result from APIC-EM'; } return false; } function apicGetProject($host, $ticket, $proj, &$error) { $cookie = false; $url = 'https://' . $host . '/api/v1/pnp-project/count'; $p = array(); $p['header']['X-Auth-Token'] = $ticket; $i = 0; $res = false; while ($i < REST_RETRIES) { $res = fetch_url($url, $cookie, $error, $p); if ($res) { break; } sleep(REST_RETRY_INTERVAL); ++$i; } if ($res) { $json = json_decode($res, true); $count = $json['response']; $url = 'https://' . $host . '/api/v1/pnp-project'; $offset = 1; $limit = 500; do { $p['get']['offset'] = $offset; $p['get']['limit'] = $limit; $i = 0; $res = false; while ($i < REST_RETRIES) { $res = fetch_url($url, $cookie, $error, $p); if ($res) { break; } sleep(REST_RETRY_INTERVAL); ++$i; } if ($res) { $json = json_decode($res, true); foreach ($json['response'] as $s) { if ($s['siteName'] == $proj) { return $s; } } } $offset += $limit; } while ($offset + $limit < $count); } return false; } function apicUpdateProject($host, $ticket, $projid, $proj, $ip, &$msg, $path = '/') { $cookie = false; $error = ''; $url = 'https://' . $host . '/api/v1/pnp-project'; $p = array(); $params = array(); $params['id'] = $projid; $params['siteName'] = $proj; $params['tftpServer'] = $ip; $params['tftpPath'] = $path; $p['put'] = json_encode($params); $p['header']['Content-Type'] = 'application/json'; $p['header']['X-Auth-Token'] = $ticket; $i = 0; $res = false; while ($i < REST_RETRIES) { $res = fetch_url($url, $cookie, $error, $p); if ($res) { break; } sleep(REST_RETRY_INTERVAL); ++$i; } if ($res) { $json = json_decode($res, true); return apicGetTaskResult($host, $ticket, $json['response']['taskId'], $msg); } $msg = $error; return false; } function fetch_url($url, &$cookie, &$error, $p = false, $timeout = REST_TIMEOUT) { $ch = curl_init(); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0); curl_setopt($ch, CURLOPT_TIMEOUT, $timeout); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); if (DEBUG > 5) { curl_setopt($ch, CURLOPT_HEADER, true); curl_setopt($ch, CURLINFO_HEADER_OUT, true); curl_setopt($ch, CURLOPT_VERBOSE, true); } else { curl_setopt($ch, CURLOPT_HEADER, false); } if (isset($p['get'])) { $url .= '?' . http_build_query($p['get']); } if (isset($p['post'])) { curl_setopt($ch, CURLOPT_POST, count($p['post'])); curl_setopt($ch, CURLOPT_POSTFIELDS, $p['post']); } if (isset($p['put'])) { curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PUT'); $p['header']['Content-Length'] = strlen($p['put']); curl_setopt($ch, CURLOPT_POSTFIELDS, $p['put']); } if (isset($p['header'])) { foreach ($p['header'] as $name => $value) { $header[] = $name . ': ' . $value; } curl_setopt($ch, CURLOPT_HTTPHEADER, $header); } if (isset($p['cookie'])) { curl_setopt($ch, CURLOPT_COOKIE, $p['cookie']); } if (isset($p['authentication'])) { curl_setopt($ch, CURLOPT_USERPWD, $p['authentication']['user'] . ':' . $p['authentication']['password']); } curl_setopt($ch, CURLOPT_URL, $url); $response = curl_exec($ch); $httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE); $error = 'cURL error num: ' . curl_errno($ch) . ' => ' . curl_error($ch); if (DEBUG >= 5) { echo '

Request:

'; var_dump(curl_getinfo($ch)); echo '

Response:

'; var_dump($response); echo curl_error($ch) . "\n"; } if (empty($response)) { if (DEBUG > 0) { echo "WARNING: empty response from URL
\n"; curl_close($ch); return false; } } else { if (empty($httpcode)) { if (DEBUG > 0) { echo "WARNING: no HTTP code was returned\n$response\n"; curl_close($ch); return false; } } elseif ($httpcode < 200 || $httpcode > 299) { if (DEBUG > 0) { echo "ERROR: the server returned http code: $httpcode\n$response\n"; } if (DEBUG > 1) { echo '

Request:

'; var_dump(curl_getinfo($ch)); echo '

Response:

'; var_dump($response); } curl_close($ch); $cookie = false; $error = $httpcode; return false; } else { curl_close($ch); return $response; } } } ?>