deployer/contrib/grafana.php

75 lines
2.0 KiB
PHP
Raw Normal View History

2020-04-25 23:00:08 +03:00
<?php
2020-10-02 00:11:13 +02:00
/*
## Installing
2020-04-25 23:00:08 +03:00
2020-10-02 00:11:13 +02:00
Add to your _deploy.php_
```php
require 'contrib/grafana.php';
```
## Configuration options
- **url** *(required)*: the URL to the creates annotation api endpoint.
- **token** *(required)*: authentication token. Can be created at Grafana Console.
- **time** *(optional)* set deploy time of annotation. specify epoch milliseconds. (Defaults is set to the current time in epoch milliseconds.)
- **tags** *(optional)* set tag of annotation.
- **text** *(optional)* set text of annotation. (Defaults is set to "Deployed " + git log -n 1 --format="%h")
```php
// deploy.php
set('grafana', [
'token' => 'eyJrIj...',
'url' => 'http://grafana/api/annotations',
'tags' => ['deploy', 'production'],
]);
```
## Usage
If you want to create annotation about successful end of deployment.
```php
2020-10-29 23:16:36 +01:00
after('deploy:success', 'grafana:annotation');
2020-10-02 00:11:13 +02:00
```
*/
2020-04-25 23:00:08 +03:00
namespace Deployer;
use Deployer\Utility\Httpie;
desc('Create Grafana annotation of deployment');
task('grafana:annotation', function () {
$defaultConfig = [
'url' => null,
'token' => null,
'time' => round(microtime(true) * 1000),
'tags' => [],
'text' => null,
];
$config = array_merge($defaultConfig, (array) get('grafana'));
if (!is_array($config) || !isset($config['url']) || !isset($config['token'])) {
throw new \RuntimeException("Please configure Grafana: set('grafana', ['url' => 'https://localhost/api/annotations', token' => 'eyJrIjo...']);");
}
$params = [
'time' => $config['time'],
'isRegion' => false,
'tags' => $config['tags'],
'text' => $config['text'],
];
if (!isset($params['text'])) {
$params['text'] = 'Deployed ' . trim(runLocally('git log -n 1 --format="%h"'));
}
Httpie::post($config['url'])
->header('Authorization: Bearer ' . $config['token'])
->header('Content-type: application/json')
->body($params)
->send();
});