Created
February 5, 2016 17:38
-
-
Save DennyLoko/45dca76bb3013665f6a5 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/php | |
<?php | |
date_default_timezone_set('America/Sao_Paulo'); | |
/** | |
* Script responsible to handle communication with Akamai | |
* | |
* @author Danniel Magno | |
* @version 1.0 | |
*/ | |
class AkamaiTool { | |
private $user = ''; | |
private $pass = ''; | |
private $baseUrl = 'https://api.ccu.akamai.com/ccu/v2/'; | |
private $verbose = false; | |
private $curl; | |
public function __construct() | |
{ | |
// Instantiate the request | |
$this->createRequest(); | |
// Get the params | |
$args = getopt('hqs:w:p:v', ['help', 'viewqueue', 'jobstatus', 'watch', 'purge']); | |
if (isset($args['v'])) { | |
$this->verbose = true; | |
unset($args['v']); | |
} | |
// Check the values | |
if (count($args) === 0) { | |
echo 'Invalid option!' . PHP_EOL; | |
$this->showHelp(); | |
} elseif (isset($args['h']) || isset($args['help'])) { | |
$this->showHelp(); | |
} elseif (isset($args['q']) || isset($args['viewqueue'])) { | |
$this->viewQueue(); | |
} elseif (isset($args['s']) || isset($args['jobstatus'])) { | |
$jobId = (isset($args['s']) ? $args['s'] : $args['jobstatus']); | |
$this->viewJobStatus($jobId); | |
} elseif (isset($args['w']) || isset($args['watch'])) { | |
$jobId = (isset($args['w']) ? $args['w'] : $args['watch']); | |
$this->watchjob($jobId); | |
} elseif (isset($args['p']) || isset($args['purge'])) { | |
$object = (isset($args['p']) ? $args['p'] : $args['purge']); | |
$this->purgeObject($object); | |
} | |
} | |
public function __destruct() | |
{ | |
curl_close($this->curl); | |
} | |
private function createRequest() | |
{ | |
// Init cURL | |
$this->curl = curl_init(); | |
// Set the defaults | |
curl_setopt_array($this->curl, [ | |
CURLOPT_RETURNTRANSFER => true, | |
CURLOPT_HTTPHEADER => array("Content-Type: application/json"), | |
CURLOPT_USERPWD => $this->user . ':' . $this->pass, | |
CURLOPT_CONNECTTIMEOUT_MS => 5000, | |
]); | |
} | |
private function makeRequest($url, $postFields = []) | |
{ | |
// Set the url | |
curl_setopt($this->curl, CURLOPT_URL, $this->baseUrl . $url); | |
// We should send an POST? | |
if (!empty($postFields)) { | |
curl_setopt($this->curl, CURLOPT_POST, 1); | |
curl_setopt($this->curl, CURLOPT_POSTFIELDS, json_encode($postFields)); | |
} | |
// Do the request | |
$result = curl_exec($this->curl); | |
// Returns the result | |
return $result; | |
} | |
private function showHelp() | |
{ | |
echo 'Usage: akamai.php -q' . PHP_EOL; | |
echo ' akamai.php -s <jobId>' . PHP_EOL; | |
echo ' akamai.php -w <jobId>' . PHP_EOL; | |
echo ' akamai.php -p <object>' . PHP_EOL; | |
echo PHP_EOL; | |
echo ' -q, --viewqueue Show the queue length' . PHP_EOL; | |
echo ' -s, --jobstatus Show the status of a job' . PHP_EOL; | |
echo ' -w, --watch Watch a job, polling Akamai every 30s' . PHP_EOL; | |
echo ' -p, --purge Purge a single object' . PHP_EOL; | |
} | |
private function viewQueue() | |
{ | |
$queue = json_decode($this->makeRequest('queues/default')); | |
echo 'Length: ' . $queue->queueLength . PHP_EOL; | |
} | |
private function getJobStatus($jobId) | |
{ | |
return $this->makeRequest('purges/' . $jobId); | |
} | |
private function viewJobStatus($jobId) | |
{ | |
echo $this->getJobStatus($jobId); | |
} | |
private function purgeObject($object) | |
{ | |
// Check if the object has http/https prefix | |
if (preg_match('/^http(s)?\:\/\//', $object) == 0) { | |
echo 'The object must start with http:// or https://' . PHP_EOL; | |
} else { | |
$job = json_decode($this->makeRequest('queues/default', [ | |
'type' => 'arl', | |
'objects' => [$object], | |
])); | |
if ($job->httpStatus == 201) { | |
echo 'Purge queued!' . PHP_EOL; | |
echo 'Use "akamai.php -w ' . $job->purgeId . '" to watch the job.' . PHP_EOL; | |
} else { | |
echo 'Error while queuing the job!' . PHP_EOL; | |
echo $job->title . '(' . $job->detail . ')' . PHP_EOL; | |
} | |
} | |
} | |
private function watchJob($jobId) | |
{ | |
$repeat = false; | |
do { | |
// Get the job data | |
$job = $this->getJobStatus($jobId); | |
$jobObj = json_decode($job); | |
$jobStatus = strtolower($jobObj->purgeStatus); | |
if ($jobStatus === 'in-progress') { | |
echo 'Purge in progress...' . PHP_EOL; | |
$repeat = true; | |
sleep($jobObj->pingAfterSeconds / 2); | |
} elseif ($jobStatus === 'done') { | |
$submission = new DateTime($jobObj->submissionTime); | |
$completion = new DateTime($jobObj->completionTime); | |
$diff = $completion->diff($submission); | |
echo 'Job done!' . PHP_EOL; | |
echo 'Submission: ' . $submission->format('H:i:s d/m/Y') . ' | '; | |
echo 'Completion: ' . $completion->format('H:i:s d/m/Y') . ' | '; | |
echo 'Duration: ' . (($diff->format('%d') * 1440) + ($diff->format('%h') * 60) + $diff->format('%i')) . 'min and ' . $diff->format('%s') . 'secs' . PHP_EOL; | |
$repeat = false; | |
} elseif ($jobStatus === 'unknown') { | |
echo 'Job is in unknown status.' . PHP_EOL; | |
$repeat = false; | |
} else { | |
echo 'Unknown job status' . PHP_EOL; | |
echo $job; | |
} | |
} while ($repeat === true); | |
} | |
} | |
$akamai = new AkamaiTool($argv); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment