2019-03-05 15:28:29 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @file
|
|
|
|
* Implements bootstrap functions.
|
|
|
|
*/
|
|
|
|
|
|
|
|
// Include addon functions only if its an admin request.
|
|
|
|
if (COCKPIT_ADMIN && !COCKPIT_API_REQUEST) {
|
|
|
|
// Extend addon functions.
|
|
|
|
$this->module('drone')->extend([
|
|
|
|
'fetchDeploys' => function ($limit = 50) {
|
2019-03-06 12:26:09 +01:00
|
|
|
$settings = $this->app->storage->findOne('drone/settings') ?? FALSE;
|
2019-03-05 15:28:29 +01:00
|
|
|
|
|
|
|
if (!$settings || !isset($settings['url'],
|
|
|
|
$settings['owner'],
|
|
|
|
$settings['project'],
|
2019-03-05 15:52:53 +01:00
|
|
|
$settings['target'],
|
2019-03-06 12:26:09 +01:00
|
|
|
$settings['token']) || !$settings['active']) {
|
2019-03-05 15:28:29 +01:00
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
|
|
|
// $url = $settings['api_url'] . '/sites/' . $settings['site_id'] . '/deploys' . '?access_token=' . $settings['access_token'];
|
|
|
|
$url = trim($settings['url'], "/") . '/api/repos/' . $settings['owner'] . '/' . $settings['project'] . '/builds';
|
|
|
|
$branch = $settings['branch'] ? $settings['branch'] : 'master';
|
|
|
|
|
|
|
|
$headers = [
|
|
|
|
'Content-Type: application/json',
|
2019-03-05 16:45:04 +01:00
|
|
|
'Accept: application/json',
|
|
|
|
'Authorization: Bearer ' . $settings['token']
|
2019-03-05 15:28:29 +01:00
|
|
|
];
|
|
|
|
|
|
|
|
$ch = curl_init($url);
|
|
|
|
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
|
|
|
|
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
|
|
|
|
curl_setopt($ch, CURLOPT_TIMEOUT, 5);
|
|
|
|
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'GET');
|
|
|
|
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
|
|
|
|
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
|
|
|
|
|
|
|
|
$deploys = [];
|
|
|
|
$unfiltered_deploys = curl_exec($ch);
|
|
|
|
curl_close($ch);
|
|
|
|
$unfiltered_deploys = json_decode($unfiltered_deploys);
|
|
|
|
$build = $settings['build'];
|
|
|
|
if ($unfiltered_deploys && is_array($unfiltered_deploys)) {
|
|
|
|
foreach($unfiltered_deploys as $deploy) {
|
2019-03-05 15:52:53 +01:00
|
|
|
if ($deploy->{target} == $branch && ($deploy->event == 'push' || ($deploy->event == 'promote' && $deploy->deploy_to == $settings['target']))) {
|
2019-03-05 15:28:29 +01:00
|
|
|
// find latest successful build
|
|
|
|
if (!$build) {
|
|
|
|
if ($deploy->event == 'push' && $deploy->status == 'success') {
|
|
|
|
$build = $deploy->number;
|
|
|
|
} else {
|
|
|
|
$build = $deploy->parent;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$limit--;
|
|
|
|
if ($limit>0) {
|
|
|
|
$deploys[] = $deploy;
|
2019-03-05 16:45:04 +01:00
|
|
|
}
|
|
|
|
}
|
2019-03-05 15:28:29 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Parse dates and check if any deploy is on building status.
|
|
|
|
$building = false;
|
|
|
|
foreach ($deploys as $idx => $deploy) {
|
|
|
|
$deploys[$idx]->building = false;
|
|
|
|
if (!in_array($deploy->status, ['success', 'failure'])) {
|
|
|
|
$building = true;
|
|
|
|
$deploys[$idx]->building = true;
|
|
|
|
}
|
|
|
|
$deploys[$idx]->created_at = date('Y-m-d H:i', $deploy->created);
|
|
|
|
$deploys[$idx]->updated_at = date('Y-m-d H:i', $deploy->updated);
|
2019-03-05 16:45:04 +01:00
|
|
|
if ($deploy->finished) {
|
|
|
|
$deploys[$idx]->deploy_time = $deploy->finished - $deploy->started;
|
|
|
|
}
|
2019-03-05 15:28:29 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return [
|
|
|
|
'deploys' => $deploys,
|
|
|
|
'building' => $building,
|
2019-03-05 16:45:04 +01:00
|
|
|
'build' => $build
|
2019-03-05 15:28:29 +01:00
|
|
|
];
|
|
|
|
},
|
2019-03-06 15:47:58 +01:00
|
|
|
'fetchLogs' => function ($build) {
|
|
|
|
$settings = $this->app->storage->findOne('drone/settings') ?? FALSE;
|
|
|
|
|
|
|
|
if (!$settings || !isset($settings['url'],
|
|
|
|
$settings['owner'],
|
|
|
|
$settings['project'],
|
|
|
|
$settings['target'],
|
|
|
|
$settings['token']) || !$settings['active']) {
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
|
|
|
$url = trim($settings['url'], "/") . '/api/repos/' . $settings['owner'] . '/' . $settings['project'] . '/builds/'. $build;
|
|
|
|
|
|
|
|
$headers = [
|
|
|
|
'Content-Type: application/json',
|
|
|
|
'Accept: application/json',
|
|
|
|
'Authorization: Bearer ' . $settings['token']
|
|
|
|
];
|
|
|
|
|
|
|
|
$logstr = "";
|
|
|
|
|
|
|
|
$ch = curl_init($url);
|
|
|
|
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
|
|
|
|
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
|
|
|
|
curl_setopt($ch, CURLOPT_TIMEOUT, 5);
|
|
|
|
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'GET');
|
|
|
|
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
|
|
|
|
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
|
|
|
|
|
|
|
|
$b = curl_exec($ch);
|
|
|
|
curl_close($ch);
|
|
|
|
|
|
|
|
$b = json_decode($b);
|
|
|
|
|
|
|
|
$colors = [
|
|
|
|
'/\\033?\[(?:0;)?30m(.*?)\\033?\[0m/s' => '<span class="black">$1</span>',
|
|
|
|
'/\\033?\[(?:0;)?31m(.*?)\\033?\[0m/s' => '<span class="red">$1</span>',
|
|
|
|
'/\\033?\[(?:0;)?32m(.*?)\\033?\[0m/s' => '<span class="green">$1</span>',
|
|
|
|
'/\\033?\[(?:0;)?33m(.*?)\\033?\[0m/s' => '<span class="brown">$1</span>',
|
|
|
|
'/\\033?\[(?:0;)?34m(.*?)\\033?\[0m/s' => '<span class="blue">$1</span>',
|
|
|
|
'/\\033?\[(?:0;)?35m(.*?)\\033?\[0m/s' => '<span class="purple">$1</span>',
|
|
|
|
'/\\033?\[(?:0;)?36m(.*?)\\033?\[0m/s' => '<span class="cyan">$1</span>',
|
|
|
|
'/\\033?\[(?:0;)?37m(.*?)\\033?\[0m/s' => '<span class="light-gray">$1</span>',
|
|
|
|
|
|
|
|
'/\\033?\[1;30m(.*?)\\033?\[0m/s' => '<span class="dark-gray">$1</span>',
|
|
|
|
'/\\033?\[1;31m(.*?)\\033?\[0m/s' => '<span class="light-red">$1</span>',
|
|
|
|
'/\\033?\[1;32m(.*?)\\033?\[0m/s' => '<span class="light-green">$1</span>',
|
|
|
|
'/\\033?\[1;33m(.*?)\\033?\[0m/s' => '<span class="yellow">$1</span>',
|
|
|
|
'/\\033?\[1;34m(.*?)\\033?\[0m/s' => '<span class="light-blue">$1</span>',
|
|
|
|
'/\\033?\[1;35m(.*?)\\033?\[0m/s' => '<span class="light-purple">$1</span>',
|
|
|
|
'/\\033?\[1;36m(.*?)\\033?\[0m/s' => '<span class="light-cyan">$1</span>',
|
|
|
|
'/\\033?\[1;37m(.*?)\\033?\[0m/s' => '<span class="white">$1</span>',
|
|
|
|
|
|
|
|
'/\\033?\[0m/s' => '',
|
|
|
|
];
|
|
|
|
|
|
|
|
foreach ($b->stages as $stage) {
|
|
|
|
foreach ($stage->steps as $step) {
|
|
|
|
|
|
|
|
$logstr .= '<div class="stagename">Stage: '.$stage->name.'</div>';
|
|
|
|
$logstr .= '<h2 class="'.$step->status.'">'.$step->name.'</h2><br>';
|
|
|
|
|
|
|
|
$step_url = $url . '/logs/' . $stage->number . '/' . $step->number;
|
|
|
|
$ch = curl_init($step_url);
|
|
|
|
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
|
|
|
|
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
|
|
|
|
curl_setopt($ch, CURLOPT_TIMEOUT, 5);
|
|
|
|
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'GET');
|
|
|
|
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
|
|
|
|
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
|
|
|
|
|
|
|
|
$logs = curl_exec($ch);
|
|
|
|
curl_close($ch);
|
|
|
|
|
|
|
|
$logs = json_decode($logs);
|
|
|
|
|
2019-03-08 11:13:52 +01:00
|
|
|
$lastTime = 0;
|
2019-03-06 15:47:58 +01:00
|
|
|
foreach ($logs as $l) {
|
|
|
|
if (preg_match('/^\\+/', $l->out)) {
|
|
|
|
$logstr .= '<h3>'.htmlspecialchars($l->out).'</h3>';
|
|
|
|
} else {
|
2019-03-08 11:13:52 +01:00
|
|
|
if ($l->time != $lastTime) {
|
|
|
|
$logstr .= '<div class="timetext">'.$l->time.'s</div>';
|
|
|
|
$lastTime = $l->time;
|
|
|
|
}
|
2019-03-06 15:47:58 +01:00
|
|
|
$logstr .= preg_replace(array_keys($colors), $colors, htmlspecialchars($l->out));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return [
|
|
|
|
'out' => $logstr
|
|
|
|
];
|
|
|
|
},
|
|
|
|
|
2019-03-05 15:28:29 +01:00
|
|
|
|
|
|
|
'createDeploy' => function ($fromBuild) {
|
2019-03-06 12:26:09 +01:00
|
|
|
$settings = $this->app->storage->findOne('drone/settings');
|
2019-03-05 15:28:29 +01:00
|
|
|
|
|
|
|
if (!$fromBuild || !$settings || !isset($settings['url'],
|
|
|
|
$settings['owner'],
|
|
|
|
$settings['project'],
|
2019-03-05 15:52:53 +01:00
|
|
|
$settings['target'],
|
2019-03-06 12:26:09 +01:00
|
|
|
$settings['token']) || !$settings['active']) {
|
2019-03-05 15:28:29 +01:00
|
|
|
|
|
|
|
return array(
|
|
|
|
"error" => "missing settings"
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2019-03-05 15:52:53 +01:00
|
|
|
$url = trim($settings['url'], "/") . '/api/repos/' . $settings['owner'] . '/' . $settings['project'] . '/builds/' . $fromBuild . '/promote?target=' . $settings['target'];
|
2019-03-05 15:28:29 +01:00
|
|
|
|
|
|
|
$headers = [
|
|
|
|
'Content-Type: application/json',
|
2019-03-05 16:45:04 +01:00
|
|
|
'Accept: application/json',
|
|
|
|
'Authorization: Bearer ' . $settings['token']
|
2019-03-05 15:28:29 +01:00
|
|
|
];
|
|
|
|
|
|
|
|
$ch = curl_init($url);
|
|
|
|
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
|
|
|
|
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
|
|
|
|
curl_setopt($ch, CURLOPT_TIMEOUT, 5);
|
|
|
|
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
|
|
|
|
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
|
|
|
|
// curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
|
|
|
|
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
|
|
|
|
$result = curl_exec($ch);
|
|
|
|
$httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
|
|
|
|
curl_close($ch);
|
|
|
|
|
|
|
|
$decoded = json_decode($result);
|
|
|
|
|
|
|
|
if ($httpcode >= 400) {
|
|
|
|
$decoded->error = "ERROR: $httpcode";
|
|
|
|
}
|
|
|
|
|
|
|
|
return $decoded;
|
|
|
|
},
|
|
|
|
]);
|
|
|
|
|
|
|
|
// Include admin.
|
|
|
|
include_once __DIR__ . '/admin.php';
|
|
|
|
}
|