From 256b32bd812fc38a0befd026f9dd974838bc1d55 Mon Sep 17 00:00:00 2001 From: Stepan Strelets Date: Mon, 4 Sep 2017 14:36:41 +0300 Subject: [PATCH] Add mage v3 plugin (deployment) --- docs/en/README.md | 1 + docs/en/plugins/mage3.md | 32 +++++++++ src/PHPCensor/Plugin/Mage3.php | 121 +++++++++++++++++++++++++++++++++ 3 files changed, 154 insertions(+) create mode 100644 docs/en/plugins/mage3.md create mode 100644 src/PHPCensor/Plugin/Mage3.php diff --git a/docs/en/README.md b/docs/en/README.md index 9f65030e..7ba204ad 100644 --- a/docs/en/README.md +++ b/docs/en/README.md @@ -44,6 +44,7 @@ Plugins * [IRC](plugins/irc.md) - `irc` * [Lint](plugins/lint.md) - `lint` * [Mage](plugins/mage.md) - `mage` +* [Mage v3](plugins/mage3.md) - `mage3` * [MySQL](plugins/mysql.md) - `mysql` * [Package Build](plugins/package_build.md) - `package_build` * [PDepend](plugins/pdepend.md) - `pdepend` diff --git a/docs/en/plugins/mage3.md b/docs/en/plugins/mage3.md new file mode 100644 index 00000000..ae441d75 --- /dev/null +++ b/docs/en/plugins/mage3.md @@ -0,0 +1,32 @@ +Plugin Mage v3 +============== + +Triggers a deployment of the project to run via [Mage v3](https://github.com/andres-montanez/Magallanes) + +Configuration +------------- + +### Options + +* **env** [required, string] - The environment name + +### Examples + +```yaml +deploy: + mage3: + env: production +``` + +### Options for config.yml + +* **bin** [optional, string] - The mage executable path +* **log_dir** [optional, string] - The mage logs path + +### Examples + +```yaml +mage: + bin: /usr/local/bin/mage + log_dir: ./var/log +``` diff --git a/src/PHPCensor/Plugin/Mage3.php b/src/PHPCensor/Plugin/Mage3.php new file mode 100644 index 00000000..b3f9ac2f --- /dev/null +++ b/src/PHPCensor/Plugin/Mage3.php @@ -0,0 +1,121 @@ +getSystemConfig('mage3'); + if (!empty($config['bin'])) { + $this->mage_bin = $config['bin']; + } + + if (isset($options['env'])) { + $this->mage_env = $builder->interpolate($options['env']); + } + + if (isset($options['log_dir'])) { + $this->mage_log_dir = $builder->interpolate($options['log_dir']); + } + } + + /** + * {@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 . ' -n deploy ' . $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() . (!empty($this->mage_log_dir) ? '/' . $this->mage_log_dir : ''); + 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('/^\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; + } +}