Add plugin Mage

This commit is contained in:
Stepan Strelets 2017-03-16 18:46:28 +03:00 committed by Dmitry Khomutov
parent 62afcee352
commit 232d1d3195
3 changed files with 146 additions and 0 deletions

View file

@ -43,6 +43,7 @@ Plugins
* [Hipchat](plugins/hipchat_notify.md) - `hipchat_notify`
* [IRC](plugins/irc.md) - `irc`
* [Lint](plugins/lint.md) - `lint`
* [Mage](plugins/mage.md) - `mage`
* [MySQL](plugins/mysql.md) - `mysql`
* [Package Build](plugins/package_build.md) - `package_build`
* [PDepend](plugins/pdepend.md) - `pdepend`

30
docs/en/plugins/mage.md Normal file
View file

@ -0,0 +1,30 @@
Plugin Mage
===========
Triggers a deployment of the project to run via [Mage](https://github.com/andres-montanez/Magallanes)
Configuration
-------------
### Options
* **env** [required, string] - The environment name
### Examples
```yaml
deploy:
mage:
env: production
```
### Options for config.yml
* **bin** [optional, string] - The mage executable path
### Examples
```yaml
mage:
bin: /usr/local/bin/mage
```

View file

@ -0,0 +1,115 @@
<?php
/**
* PHPCensor - Continuous Integration for PHP
*/
namespace PHPCensor\Plugin;
use PHPCensor\Builder;
use PHPCensor\Model\Build;
use Psr\Log\LogLevel;
/**
* Integrates PHPCensor with Mage: https://github.com/andres-montanez/Magallanes
* @package PHPCensor
* @subpackage Plugins
*/
class Mage extends \PHPCensor\Plugin
{
protected $mage_bin = 'mage';
protected $mage_env;
/**
* {@inheritdoc}
*/
public static function pluginName()
{
return 'mage';
}
/**
* {@inheritdoc}
*/
public function __construct(Builder $builder, Build $build, array $options = [])
{
parent::__construct($builder, $build, $options);
$config = $builder->getSystemConfig('mage');
if (isset($config['bin'])) {
$this->mage_bin = $config['bin'];
}
if (isset($options['env'])) {
$this->mage_env = $options['env'];
}
}
/**
* {@inheritdoc}
*/
public function execute()
{
if (empty($this->mage_env)) {
$this->builder->logFailure('You must specify environment.');
return false;
}
$result = $this->builder->executeCommand($this->mage_bin . ' deploy to:' . $this->mage_env);
try {
$this->builder->log('########## MAGE LOG BEGIN ##########');
$this->builder->log($this->getMageLog());
$this->builder->log('########## MAGE LOG END ##########');
} catch (\Exception $e) {
$this->builder->log($e->getMessage(), LogLevel::NOTICE);
}
return $result;
}
/**
* Get mage log lines
* @return array
* @throws \Exception
*/
protected function getMageLog()
{
$logs_dir = $this->build->getBuildPath() . '/.mage/logs';
if (!is_dir($logs_dir)) {
throw new \Exception('Log directory not found');
}
$list = scandir($logs_dir);
if ($list === false) {
throw new \Exception('Log dir read fail');
}
$list = array_filter($list, function ($name) {
return preg_match('/^log-\d+-\d+\.log$/', $name);
});
if (empty($list)) {
throw new \Exception('Log dir filter fail');
}
$res = sort($list);
if ($res === false) {
throw new \Exception('Logs sort fail');
}
$last_log_file = end($list);
if ($last_log_file === false) {
throw new \Exception('Get last Log name fail');
}
$log_content = file_get_contents($logs_dir . '/' . $last_log_file);
if ($log_content === false) {
throw new \Exception('Get last Log content fail');
}
$lines = explode("\n", $log_content);
$lines = array_map('trim', $lines);
$lines = array_filter($lines);
return $lines;
}
}