1
0
mirror of https://github.com/hauke68/Magallanes.git synced 2025-08-25 21:00:18 +02:00

Compare commits

..

No commits in common. "0.9.14" and "master" have entirely different histories.

181 changed files with 8260 additions and 3633 deletions

15
.gitignore vendored
View File

@ -1,11 +1,4 @@
.settings
.settings/*
.project
.buildpath
# OS generated files # // GitHub Recommendation
######################
.DS_Store*
ehthumbs.db
Icon?
Thumbs.db
/vendor/
/build
composer.lock
.mage.yml

20
.travis.yml Normal file
View File

@ -0,0 +1,20 @@
language: php
php:
- '7.2'
- '7.3'
- '7.4'
jobs:
fast_finish: true
allow_failures:
- php: 7.1
install:
- composer install
script:
- mkdir -p build/logs
- vendor/bin/phpunit --coverage-clover build/logs/coverage.xml
after_script:
- vendor/bin/coveralls -v --coverage_clover build/logs/coverage.xml

9
CHANGELOG.md Normal file
View File

@ -0,0 +1,9 @@
CHANGELOG for 4.X
=================
* 4.0.0 (2018-04-02)
* v4 series release
* Refactored for Symfony 4 and PHP 7.1
* Symfony Pool Clear task added
* Symfony Pool Prune task added
* Symfony Assetic task removed

70
CONTRIBUTING.md Normal file
View File

@ -0,0 +1,70 @@
Contributor Guidelines for Magallanes
=====================================
Welcome to Magallanes! We are much appreciated you've decided to contribute to this project!
Please read the following guidelines to make your and our work easier and cleaner.
**TL;DR**
1. Write clean code with no mess left
2. Contribute the docs when adding configurable new feature
3. Create your pull request from `nostromo` branch
4. Ensure your code is fully covered by tests
----------
# Reporting Issues
If you have a problem or you've noticed a bug, please feel free to open a new issue. However follow the rules below:
* First, make sure that similar/the same issue doesn't already exists
* If you've already found the solution of the problem you are about to report, please feel free to open a new Pull Request. Then follow the rules below in **Developing Magallanes** section.
* If you are able to, include some test cases or steps to reproduce the bug for us to examine the problem and find a solution.
## Opening Pull Requests
Pull Request is a very powerful tool, so let's be measured in its usage. Always commit code which has at least 95% of coverage, and if it's a new feature always provide concrete tests.
In order to have the PRs prioritized name them with the following tags.
```
[#66] Add new CONTRIBUTING document
[FIX] Set correct permissions on deploy stage
[FEATURE] Create new PermissionsTask
[HOTFIX] Exception not caught on deployment
```
All Pull Requests must be done to the `nostromo` branch, only exception are Hotfixes.
Remember of square brackets when adding issue number. If you'd forget adding them, your whole message will be a comment!
# Developing Magallanes
## Branches
The flow is pretty simple.
In most common cases we work on the `nostromo` branch. It's the branch with the main development for the current major version. All Pull Requests must merge with that branch. The `master` branch is used to move the validated code and generate the releases in an orderly fashion, also we could use it for hotfixes.
If you want to use developing branch in your code, simple pass `dev-nostromo` to dependency version in your `composer.json` file:
```json
{
"require": {
"andres-montanez/magallanes": "dev-nostromo"
}
}
```
## Organization and code quality
We use [PSR2](http://www.php-fig.org/psr/psr-2/) as PHP coding standard.
### Tools you can use to ensure your code quality
1. PHP-CodeSniffer
2. [PHP Mess Detector](https://phpmd.org/)
3. PHP Copy/Paste Detector
4. PHP Dead Code Detector
5. [PHP Coding Standards Fixer](http://cs.sensiolabs.org) with --level=psr2
## Testing and quality
We use PHPUnit to test our code. Most of the project is covered with tests, so if you want your code to be merged push it with proper testing and coverage (at least 95%). To execute the tests with code coverage report:
```
vendor/bin/phpunit --coverage-clover build/logs/coverage.xml
vendor/bin/coveralls -v --coverage_clover build/logs/coverage.xml
```
Tests structure follow almost the same structure as production code with `Test` suffix in class and file name. Follow the tests already made as guidelines.
# Last Words
Thank you for using Magallanes, and special thanks for making it better. When adding features always have in mind the main goal *Deploy code from A to B, and run some tasks*, and think if the feature is aiming at that.

View File

@ -1,4 +1,4 @@
Copyright (c) 2011 - 2013 Andrés Montañez
Copyright (c) 2011 - 2017 Andrés Montañez
Permission is hereby granted, free of charge, to any
person obtaining a copy of this software and associated

View File

@ -1,23 +0,0 @@
<?php
class Mage_Autoload
{
public static function autoload($className)
{
$baseDir = dirname(dirname(__FILE__));
$classFile = $baseDir . '/' . str_replace('_', '/', $className . '.php');
require_once $classFile;
}
public static function isLoadable($className)
{
$baseDir = dirname(dirname(__FILE__));
$classFile = $baseDir . '/' . str_replace('_', '/', $className . '.php');
return (file_exists($classFile) && is_readable($classFile));
}
public static function loadUserTask($taskName)
{
$classFile = '.mage/tasks/' . ucfirst($taskName) . '.php';
require_once $classFile;
}
}

View File

@ -1,67 +0,0 @@
<?php
class Mage_Command_BuiltIn_Add
extends Mage_Command_CommandAbstract
{
public function run()
{
$subCommand = $this->getConfig()->getArgument(1);
try {
switch ($subCommand) {
case 'environment':
$this->_environment();
break;
}
} catch (Exception $e) {
Mage_Console::output('<red>' . $e->getMessage() . '</red>', 1, 2);
}
}
private function _environment()
{
$withReleases = $this->getConfig()->getParameter('enableReleases', false);
$environmentName = strtolower($this->getConfig()->getParameter('name'));
if ($environmentName == '') {
throw new Exception('You must specify a name for the environment.');
}
$environmentConfigFile = '.mage/config/environment/' . $environmentName . '.yml';
if (file_exists($environmentConfigFile)) {
throw new Exception('The environment already exists.');
}
Mage_Console::output('Adding new environment: <dark_gray>' . $environmentName . '</dark_gray>');
$releasesConfig = 'releases:' . PHP_EOL
. ' enabled: true' . PHP_EOL
. ' max: 10' . PHP_EOL
. ' symlink: current' . PHP_EOL
. ' directory: releases' . PHP_EOL;
$baseConfig = '#' . $environmentName . PHP_EOL
. 'deployment:' . PHP_EOL
. ' user: dummy' . PHP_EOL
. ' from: ./' . PHP_EOL
. ' to: /var/www/vhosts/example.com/www' . PHP_EOL
. ' excludes:' . PHP_EOL
. ($withReleases ? $releasesConfig : '')
. 'hosts:' . PHP_EOL
. 'tasks:' . PHP_EOL
. ' pre-deploy:' . PHP_EOL
. ' on-deploy:' . PHP_EOL
. ' - deployment/rsync' . PHP_EOL
. ($withReleases ? (' post-release:' . PHP_EOL) : '')
. ' post-deploy:' . PHP_EOL;
$result = file_put_contents($environmentConfigFile, $baseConfig);
if ($result) {
Mage_Console::output('<light_green>Success!!</light_green> Environment config file for <dark_gray>' . $environmentName . '</dark_gray> created successfully at <blue>' . $environmentConfigFile . '</blue>');
Mage_Console::output('<dark_gray>So please! Review and adjust its configuration.</dark_gray>', 2, 2);
} else {
Mage_Console::output('<light_red>Error!!</light_red> Unable to create config file for environment called <dark_gray>' . $environmentName . '</dark_gray>', 1, 2);
}
}
}

View File

@ -1,22 +0,0 @@
<?php
/**
* Class Mage_Command_BuiltIn_Compile
*
* @author Ismael Ambrosi<ismaambrosi@gmail.com>
*/
class Mage_Command_BuiltIn_Compile
extends Mage_Command_CommandAbstract
{
/**
* @see Mage_Compile::compile()
*/
public function run ()
{
Mage_Console::output('Compiling <dark_gray>Magallanes</dark_gray>... ', 1, 0);
$compiler = new Mage_Compiler();
$compiler->compile();
Mage_Console::output('Mage compiled successfully');
}
}

View File

@ -1,276 +0,0 @@
<?php
class Mage_Command_BuiltIn_Deploy
extends Mage_Command_CommandAbstract
implements Mage_Command_RequiresEnvironment
{
private $_startTime = null;
private $_startTimeHosts = null;
private $_endTimeHosts = null;
private $_hostsCount = 0;
public function __construct()
{
}
public function run()
{
$this->getConfig()->setReleaseId(date('YmdHis'));
$failedTasks = 0;
$this->_startTime = time();
$lockFile = '.mage/' . $this->getConfig()->getEnvironment() . '.lock';
if (file_exists($lockFile)) {
Mage_Console::output('<red>This environment is locked!</red>', 1, 2);
return;
}
// Run Pre-Deployment Tasks
$this->_runNonDeploymentTasks('pre-deploy', $this->getConfig(), 'Pre-Deployment');
// Run Tasks for Deployment
$hosts = $this->getConfig()->getHosts();
$this->_hostsCount = count($hosts);
if ($this->_hostsCount == 0) {
Mage_Console::output('<light_purple>Warning!</light_purple> <dark_gray>No hosts defined, skipping deployment tasks.</dark_gray>', 1, 3);
} else {
$this->_startTimeHosts = time();
foreach ($hosts as $host) {
$this->getConfig()->setHost($host);
$tasks = 0;
$completedTasks = 0;
Mage_Console::output('Deploying to <dark_gray>' . $this->getConfig()->getHost() . '</dark_gray>');
$tasksToRun = $this->getConfig()->getTasks();
array_unshift($tasksToRun, 'deployment/rsync');
if (count($tasksToRun) == 0) {
Mage_Console::output('<light_purple>Warning!</light_purple> <dark_gray>No </dark_gray><light_cyan>Deployment</light_cyan> <dark_gray>tasks defined.</dark_gray>', 2);
Mage_Console::output('Deployment to <dark_gray>' . $host . '</dark_gray> skipped!', 1, 3);
} else {
foreach ($tasksToRun as $taskData) {
$tasks++;
$task = Mage_Task_Factory::get($taskData, $this->getConfig(), false, 'deploy');
if ($this->_runTask($task)) {
$completedTasks++;
} else {
$failedTasks++;
}
}
if ($completedTasks == $tasks) {
$tasksColor = 'green';
} else {
$tasksColor = 'red';
}
Mage_Console::output('Deployment to <dark_gray>' . $this->getConfig()->getHost() . '</dark_gray> completed: <' . $tasksColor . '>' . $completedTasks . '/' . $tasks . '</' . $tasksColor . '> tasks done.', 1, 3);
}
}
$this->_endTimeHosts = time();
if ($failedTasks > 0) {
Mage_Console::output('A total of <dark_gray>' . $failedTasks . '</dark_gray> deployment tasks failed: <red>ABORTING</red>', 1, 2);
return;
}
// Releasing
if ($this->getConfig()->release('enabled', false) == true) {
// Execute the Releases
Mage_Console::output('Starting the <dark_gray>Releaseing</dark_gray>');
foreach ($hosts as $host) {
$this->getConfig()->setHost($host);
$task = Mage_Task_Factory::get('deployment/release', $this->getConfig(), false, 'deploy');
if ($this->_runTask($task, 'Releasing on host <purple>' . $host . '</purple> ... ')) {
$completedTasks++;
}
}
Mage_Console::output('Finished the <dark_gray>Releaseing</dark_gray>', 1, 3);
// Execute the Post-Release Tasks
foreach ($hosts as $host) {
$this->getConfig()->setHost($host);
$tasksToRun = $this->getConfig()->getTasks('post-release');
$tasks = count($tasksToRun);
$completedTasks = 0;
if (count($tasksToRun) > 0) {
Mage_Console::output('Starting <dark_gray>Post-Release</dark_gray> tasks for <dark_gray>' . $host . '</dark_gray>:');
foreach ($tasksToRun as $task) {
$task = Mage_Task_Factory::get($task, $this->getConfig(), false, 'post-release');
if ($this->_runTask($task)) {
$completedTasks++;
}
}
if ($completedTasks == $tasks) {
$tasksColor = 'green';
} else {
$tasksColor = 'red';
}
Mage_Console::output('Finished <dark_gray>Post-Release</dark_gray> tasks for <dark_gray>' . $host . '</dark_gray>: <' . $tasksColor . '>' . $completedTasks . '/' . $tasks . '</' . $tasksColor . '> tasks done.', 1, 3);
}
}
}
}
// Run Post-Deployment Tasks
$this->_runNonDeploymentTasks('post-deploy', $this->getConfig(), 'Post-Deployment');
// Time Information Hosts
if ($this->_hostsCount > 0) {
$timeTextHost = $this->_transcurredTime($this->_endTimeHosts - $this->_startTimeHosts);
Mage_Console::output('Time for deployment: <dark_gray>' . $timeTextHost . '</dark_gray>.');
$timeTextPerHost = $this->_transcurredTime(round(($this->_endTimeHosts - $this->_startTimeHosts) / $this->_hostsCount));
Mage_Console::output('Average time per host: <dark_gray>' . $timeTextPerHost . '</dark_gray>.');
}
// Time Information General
$timeText = $this->_transcurredTime(time() - $this->_startTime);
Mage_Console::output('Total time: <dark_gray>' . $timeText . '</dark_gray>.', 1, 2);
}
/**
* Execute Pre and Post Deployment Tasks
*
* @param string $stage
* @param Mage_Config $config
* @param string $title
*/
private function _runNonDeploymentTasks($stage, Mage_Config $config, $title)
{
$tasksToRun = $config->getTasks($stage);
// PreDeployment Hook
if ($stage == 'pre-deploy') {
// Look for Remote Source
if (is_array($this->_config->deployment('source', null))) {
array_unshift($tasksToRun, 'scm/clone');
}
// Change Branch
if ($this->getConfig()->deployment('scm', false)) {
array_unshift($tasksToRun, 'scm/change-branch');
}
}
// PostDeployment Hook
if ($stage == 'post-deploy') {
// Change Branch Back
if ($this->getConfig()->deployment('scm', false)) {
array_unshift($tasksToRun, 'scm/change-branch-back');
}
// Remove Remote Source
if (is_array($this->_config->deployment('source', null))) {
array_push($tasksToRun, 'scm/remove-clone');
}
}
if (count($tasksToRun) == 0) {
Mage_Console::output('<dark_gray>No </dark_gray><light_cyan>' . $title . '</light_cyan> <dark_gray>tasks defined.</dark_gray>', 1, 3);
} else {
Mage_Console::output('Starting <dark_gray>' . $title . '</dark_gray> tasks:');
$tasks = 0;
$completedTasks = 0;
foreach ($tasksToRun as $taskData) {
$tasks++;
$task = Mage_Task_Factory::get($taskData, $config, false, $stage);
if ($this->_runTask($task)) {
$completedTasks++;
}
}
if ($completedTasks == $tasks) {
$tasksColor = 'green';
} else {
$tasksColor = 'red';
}
Mage_Console::output('Finished <dark_gray>' . $title . '</dark_gray> tasks: <' . $tasksColor . '>' . $completedTasks . '/' . $tasks . '</' . $tasksColor . '> tasks done.', 1, 3);
}
}
private function _runTask($task, $title = null)
{
$task->init();
if ($title == null) {
$title = 'Running <purple>' . $task->getName() . '</purple> ... ';
}
Mage_Console::output($title, 2, 0);
$runTask = true;
if (($task instanceOf Mage_Task_Releases_SkipOnOverride) && $this->getConfig()->getParameter('overrideRelease', false)) {
$runTask == false;
}
$result = false;
if ($runTask == true) {
try {
$result = $task->run();
if ($result == true) {
Mage_Console::output('<green>OK</green>', 0);
$result = true;
} else {
Mage_Console::output('<red>FAIL</red>', 0);
$result = false;
}
} catch (Mage_Task_SkipException $e) {
Mage_Console::output('<yellow>SKIPPED</yellow>', 0);
$result = true;
} catch (Exception $e) {
Mage_Console::output('<red>FAIL</red>', 0);
$result = false;
}
} else {
Mage_Console::output('<yellow>SKIPPED</yellow>', 0);
$result = true;
}
return $result;
}
/**
* Humanize Transcurred time
* @param integer $time
* @return string
*/
private function _transcurredTime($time)
{
$hours = floor($time / 3600);
$minutes = floor(($time - ($hours * 3600)) / 60);
$seconds = $time - ($minutes * 60) - ($hours * 3600);
$timeText = array();
if ($hours > 0) {
$timeText[] = $hours . ' hours';
}
if ($minutes > 0) {
$timeText[] = $minutes . ' minutes';
}
if ($seconds > 0) {
$timeText[] = $seconds . ' seconds';
}
return implode(' ', $timeText);
}
}

View File

@ -1,32 +0,0 @@
<?php
class Mage_Command_BuiltIn_Init
extends Mage_Command_CommandAbstract
{
public function run()
{
$configDir = '.mage';
Mage_Console::output('Initiating managing process for application with <dark_gray>Magallanes</dark_gray>');
// Check if there is already a config dir
if (file_exists($configDir)) {
Mage_Console::output('<light_red>Error!!</light_red> Already exists <dark_gray>.mage</dark_gray> directory.', 1, 2);
} else {
$results = array();
$results[] = mkdir($configDir);
$results[] = mkdir($configDir . '/logs');
$results[] = mkdir($configDir . '/tasks');
$results[] = mkdir($configDir . '/config');
$results[] = mkdir($configDir . '/config/environment');
$results[] = file_put_contents($configDir . '/config/general.yml', '#global settings' . PHP_EOL . PHP_EOL);
$results[] = file_put_contents($configDir . '/config/scm.yml', '#scm settings' . PHP_EOL . PHP_EOL);
if (!in_array(false, $results)) {
Mage_Console::output('<light_green>Success!!</light_green> The configuration for <dark_gray>Magallanes</dark_gray> has been generated at <blue>.mage</blue> directory.');
Mage_Console::output('<dark_gray>Please!! Review and adjust the configuration.</dark_gray>', 2, 2);
} else {
Mage_Console::output('<light_red>Error!!</light_red> Unable to generate the configuration.', 1, 2);
}
}
}
}

View File

@ -1,92 +0,0 @@
<?php
class Mage_Command_BuiltIn_Install
extends Mage_Command_CommandAbstract
{
public function run()
{
Mage_Console::output('Installing <dark_gray>Magallanes</dark_gray>... ', 1, 0);
// Vars
$installDir = $this->getConfig()->getParameter('installDir', '/opt/magallanes');
$systemWide = $this->getConfig()->getParameter('systemWide', false);
// Clean vars
$baseDir = realpath(dirname($installDir));
$installDir = basename($installDir);
// Check if install dir is available
if (!is_dir($baseDir) || !is_writable($baseDir)) {
Mage_Console::output('<red>Failure: install directory is invalid.</red>', 0, 2);
// Chck if it is a system wide install the user is root
} else if ($systemWide && (getenv('LOGNAME') != 'root')) {
Mage_Console::output('<red>Failure: you have to be root to perform a system wide install.</red>', 0, 2);
} else {
$destinationDir = $baseDir . '/' . $installDir;
if (!is_dir($destinationDir)) {
mkdir($destinationDir);
}
// Copy
$this->_recursiveCopy('./', $destinationDir . '/' . MAGALLANES_VERSION);
// Check if there is already a symlink
if (file_exists($destinationDir . '/' . 'latest')
&& is_link($destinationDir . '/' . 'latest')) {
unlink($destinationDir . '/' . 'latest');
}
// Create "latest" symlink
symlink(
$destinationDir . '/' . MAGALLANES_VERSION,
$destinationDir . '/' . 'latest'
);
chmod($destinationDir . '/' . MAGALLANES_VERSION . '/bin/mage', 0755);
if ($systemWide) {
if (!file_exists('/usr/bin/mage')) {
symlink($destinationDir . '/latest/bin/mage', '/usr/bin/mage');
}
}
Mage_Console::output('<light_green>Success!</light_green>', 0, 2);
}
}
private function _recursiveCopy($from, $to)
{
if (is_dir($from)) {
mkdir($to);
$files = scandir($from);
if (count($files) > 0) {
foreach ($files as $file) {
if (strpos($file, '.') === 0) {
continue;
}
if (is_dir($from . DIRECTORY_SEPARATOR . $file)) {
$this->_recursiveCopy(
$from . DIRECTORY_SEPARATOR . $file,
$to . DIRECTORY_SEPARATOR . $file
);
} else {
copy(
$from . DIRECTORY_SEPARATOR . $file,
$to . DIRECTORY_SEPARATOR . $file
);
}
}
}
return true;
} elseif (is_file($from)) {
return copy($from, $to);
} else {
return false;
}
}
}

View File

@ -1,14 +0,0 @@
<?php
class Mage_Command_BuiltIn_Lock
extends Mage_Command_CommandAbstract
implements Mage_Command_RequiresEnvironment
{
public function run()
{
$lockFile = '.mage/' . $this->getConfig()->getEnvironment() . '.lock';
file_put_contents($lockFile, 'Locked environment at date: ' . date('Y-m-d H:i:s'));
Mage_Console::output('Locked deployment to <light_purple>' . $this->getConfig()->getEnvironment() . '</light_purple> environment', 1, 2);
}
}

View File

@ -1,45 +0,0 @@
<?php
class Mage_Command_BuiltIn_Releases
extends Mage_Command_CommandAbstract
implements Mage_Command_RequiresEnvironment
{
private $_release = null;
public function run()
{
$subcommand = $this->getConfig()->getArgument(1);
$lockFile = '.mage/' . $this->getConfig()->getEnvironment() . '.lock';
if (file_exists($lockFile) && ($subcommand == 'rollback')) {
Mage_Console::output('<red>This environment is locked!</red>', 0, 2);
return;
}
// Run Tasks for Deployment
$hosts = $this->getConfig()->getHosts();
if (count($hosts) == 0) {
Mage_Console::output('<light_purple>Warning!</light_purple> <dark_gray>No hosts defined, unable to get releases.</dark_gray>', 1, 3);
} else {
foreach ($hosts as $host) {
$this->getConfig()->setHost($host);
switch ($subcommand) {
case 'list':
$task = Mage_Task_Factory::get('releases/list', $this->getConfig());
$task->init();
$result = $task->run();
break;
case 'rollback':
$releaseId = $this->getConfig()->getParameter('release', '');
$task = Mage_Task_Factory::get('releases/rollback', $this->getConfig());
$task->init();
$task->setRelease($releaseId);
$result = $task->run();
break;
}
}
}
}
}

View File

@ -1,16 +0,0 @@
<?php
class Mage_Command_BuiltIn_Unlock
extends Mage_Command_CommandAbstract
implements Mage_Command_RequiresEnvironment
{
public function run()
{
$lockFile = '.mage/' . $this->getConfig()->getEnvironment() . '.lock';
if (file_exists($lockFile)) {
@unlink($lockFile);
}
Mage_Console::output('Unlocked deployment to <light_purple>' . $this->getConfig()->getEnvironment() . '</light_purple> environment', 1, 2);
}
}

View File

@ -1,20 +0,0 @@
<?php
class Mage_Command_BuiltIn_Update
extends Mage_Command_CommandAbstract
{
public function run()
{
$task = Mage_Task_Factory::get('scm/update', $this->getConfig());
$task->init();
Mage_Console::output('Updating application via ' . $task->getName() . ' ... ', 1, 0);
$result = $task->run();
if ($result == true) {
Mage_Console::output('<green>OK</green>' . PHP_EOL, 0);
} else {
Mage_Console::output('<red>FAIL</red>' . PHP_EOL, 0);
}
}
}

View File

@ -1,115 +0,0 @@
<?php
class Mage_Command_BuiltIn_Upgrade
extends Mage_Command_CommandAbstract
{
const DOWNLOAD = 'https://github.com/andres-montanez/Magallanes/tarball/stable';
public function run ()
{
Mage_Console::output('Upgrading <dark_gray>Magallanes</dark_gray> ... ', 1, 0);
$user = '';
// Check if user is root
Mage_Console::executeCommand('whoami', $user);
if ($user != 'root') {
Mage_Console::output('<red>FAIL</red>', 0, 1);
Mage_Console::output('You need to be the <dark_gray>root</dark_gray> user to perform the upgrade.', 2);
} else {
// Download Package
$tarball = file_get_contents(self::DOWNLOAD);
$tarballFile = tempnam('/tmp', 'magallanes_download');
rename($tarballFile, $tarballFile . '.tar.gz');
$tarballFile .= '.tar.gz';
file_put_contents($tarballFile, $tarball);
// Unpackage
if (file_exists('/tmp/__magallanesDownload')) {
Mage_Console::executeCommand('rm -rf /tmp/__magallanesDownload');
}
Mage_Console::executeCommand('mkdir /tmp/__magallanesDownload');
Mage_Console::executeCommand('cd /tmp/__magallanesDownload && tar xfz ' . $tarballFile);
Mage_Console::executeCommand('rm -f ' . $tarballFile);
// Find Package
$tarballDir = opendir('/tmp/__magallanesDownload');
while (($file = readdir($tarballDir)) == true) {
if ($file == '.' || $file == '..') {
continue;
} else {
$packageDir = $file;
break;
}
}
// Get Version
$version = false;
if (file_exists('/tmp/__magallanesDownload/' . $packageDir . '/bin/mage')) {
list(, $version) = file('/tmp/__magallanesDownload/' . $packageDir . '/bin/mage');
$version = trim(str_replace('#VERSION:', '', $version));
}
if ($version != false) {
$versionCompare = version_compare(MAGALLANES_VERSION, $version);
if ($versionCompare == 0) {
Mage_Console::output('<yellow>SKIP</yellow>', 0, 1);
Mage_Console::output('Your current version is up to date.', 2);
} else if ($versionCompare > 0) {
Mage_Console::output('<yellow>SKIP</yellow>', 0, 1);
Mage_Console::output('Your current version is newer.', 2);
} else {
$this->_recursiveCopy('/tmp/__magallanesDownload/' . $packageDir, '/opt/magallanes-' . $version);
unlink('/opt/magallanes');
symlink('/opt/magallanes-' . $version, '/opt/magallanes');
chmod('/opt/magallanes/bin/mage', 0755);
Mage_Console::output('<green>OK</green>', 0, 1);
}
} else {
Mage_Console::output('<red>FAIL</red>', 0, 1);
Mage_Console::output('Corrupted download.', 2);
}
}
}
private function _recursiveCopy ($from, $to)
{
if (is_dir($from)) {
mkdir($to);
$files = scandir($from);
if (count($files) > 0) {
foreach ($files as $file) {
if (strpos($file, '.') === 0) {
continue;
}
if (is_dir($from . DIRECTORY_SEPARATOR . $file)) {
$this->_recursiveCopy(
$from . DIRECTORY_SEPARATOR . $file,
$to . DIRECTORY_SEPARATOR . $file
);
} else {
copy(
$from . DIRECTORY_SEPARATOR . $file,
$to . DIRECTORY_SEPARATOR . $file
);
}
}
}
return true;
} elseif (is_file($from)) {
return copy($from, $to);
} else {
return false;
}
}
}

View File

@ -1,10 +0,0 @@
<?php
class Mage_Command_BuiltIn_Version
extends Mage_Command_CommandAbstract
{
public function run()
{
Mage_Console::output('Running <blue>Magallanes</blue> version <dark_gray>' . MAGALLANES_VERSION .'</dark_gray>', 0, 2);
}
}

View File

@ -1,21 +0,0 @@
<?php
abstract class Mage_Command_CommandAbstract
{
protected $_config = null;
public abstract function run();
public function setConfig(Mage_Config $config)
{
$this->_config = $config;
}
/**
*
* @return Mage_Config
*/
public function getConfig()
{
return $this->_config;
}
}

View File

@ -1,37 +0,0 @@
<?php
class Mage_Command_Factory
{
/**
*
*
* @param string $commandName
* @param Mage_Config $config
* @return Mage_Command_CommandAbstract
*/
public static function get($commandName, Mage_Config $config)
{
$instance = null;
$commandName = ucwords(str_replace('-', ' ', $commandName));
$commandName = str_replace(' ', '', $commandName);
// if (strpos($commandName, '/') === false) {
// Mage_Autoload::loadUserTask($taskName);
// $className = 'Task_' . ucfirst($taskName);
// $instance = new $className($taskConfig, $inRollback, $stage);
// } else {
$commandName = str_replace(' ', '_', ucwords(str_replace('/', ' ', $commandName)));
$className = 'Mage_Command_BuiltIn_' . $commandName;
if (Mage_Autoload::isLoadable($className)) {
$instance = new $className;
$instance->setConfig($config);
} else {
throw new Exception('Command not found.');
}
// }
assert($instance instanceOf Mage_Command_CommandAbstract);
return $instance;
}
}

View File

@ -1,4 +0,0 @@
<?php
interface Mage_Command_RequiresEnvironment
{
}

View File

@ -1,52 +0,0 @@
<?php
/**
* Class Mage_Compiler
*
* Compiles the library into a .phar file
*
* @author Ismael Ambrosi<ismaambrosi@gmail.com>
*/
class Mage_Compiler
{
/**
* Compiles the library
*
* @param string $file
*/
public function compile($file = 'mage.phar')
{
if (file_exists($file)) {
unlink($file);
}
$phar = new Phar($file, 0, 'mage.phar');
$phar->setSignatureAlgorithm(Phar::SHA1);
$phar->startBuffering();
$iterator = new RecursiveIteratorIterator(new RecursiveDirectoryIterator(__DIR__), RecursiveIteratorIterator::CHILD_FIRST);
/** @var $path SplFileInfo */
foreach ($iterator as $path) {
if ($path->isFile()) {
$phar->addFromString(str_replace(dirname(__DIR__).'/', '', $path->getPathname()), file_get_contents($path));
}
}
$phar->addFromString('mage', str_replace(
'$baseDir = dirname(dirname(__FILE__));',
'$baseDir = __DIR__;',
file_get_contents(__DIR__.'/../bin/mage.php')
));
$phar->setStub("#!/usr/bin/env php\n<?php Phar::mapPhar('mage.phar'); require 'phar://mage.phar/mage'; __HALT_COMPILER();");
$phar->stopBuffering();
unset($phar);
chmod($file, 0755);
}
}

View File

@ -1,414 +0,0 @@
<?php
class Mage_Config
{
private $_arguments = array();
private $_parameters = array();
private $_environment = false;
private $_host = null;
private $_releaseId = null;
private $_config = array(
'general' => array(),
'scm' => array(),
'environment' => array(),
);
/**
* Load the Configuration and parses the Arguments
*
* @param array $arguments
*/
public function load($arguments)
{
$this->_parse($arguments);
$this->_loadGeneral();
$this->_loadSCM();
$this->_loadEnvironment();
}
/**
* Return the invocation argument based on a position
* 0 = Invoked Command Name
*
* @param integer $position
* @return mixed
*/
public function getArgument($position = 0)
{
if (isset($this->_arguments[$position])) {
return $this->_arguments[$position];
} else {
return false;
}
}
/**
* Returns all the invocation arguments
*
* @return array
*/
public function getArguments()
{
return $this->_arguments;
}
/**
* Return the a parameter
*
* @param string $name
* @return mixed
*/
public function getParameter($name, $default = null, $extraParameters = array())
{
if (isset($this->_parameters[$name])) {
return $this->_parameters[$name];
} else if (isset($extraParameters[$name])) {
return $extraParameters[$name];
} else {
return $default;
}
}
/**
* Returns all the invocation arguments
*
* @return array
*/
public function getParameters()
{
return $this->_parameters;
}
/**
* Returns the Current environment
*
* @return mixed
*/
public function getEnvironment()
{
return $this->_environment;
}
/**
* Reloads the configuration
*/
public function reload()
{
$this->_loadGeneral();
$this->_loadSCM();
$this->_loadEnvironment();
}
/**
* Get the Tasks to execute
*
* @param string $stage
* @return array
*/
public function getTasks($stage = 'on-deploy')
{
$tasks = array();
$config = $this->_getEnvironmentOption('tasks', array());
if (isset($config[$stage])) {
$tasksData = ($config[$stage] ? (array) $config[$stage] : array());
foreach ($tasksData as $taskName => $taskData) {
if (is_array($taskData)) {
;
$tasks[] = array(
'name' => key($taskData),
'parameters' => current($taskData),
);
} else {
$tasks[] = $taskData;
}
}
}
return $tasks;
}
/**
* Get the current Hosts to deploy
*
* @return array
*/
public function getHosts()
{
$hosts = array();
if (isset($this->_config['environment']['hosts'])) {
if (is_array($this->_config['environment']['hosts'])) {
$hosts = (array) $this->_config['environment']['hosts'];
} else if (is_string($this->_config['environment']['hosts']) && file_exists($this->_config['environment']['hosts']) && is_readable($this->_config['environment']['hosts'])) {
$fileContent = fopen($this->_config['environment']['hosts'], 'r');
while (($host = fgets($fileContent)) == true) {
$host = trim($host);
if ($host != '') {
$hosts[] = $host;
}
}
}
}
return $hosts;
}
/**
* Set the current host
*
* @param string $host
* @return Mage_Config
*/
public function setHost($host)
{
$this->_host = $host;
return $this;
}
/**
* Get the current host name
*
* @return string
*/
public function getHostName()
{
$info = explode(':', $this->_host);
return $info[0];
}
/**
* Get the current Host Port
*
* @return unknown
*/
public function getHostPort()
{
$info = explode(':', $this->_host);
$info[] = $this->deployment('port', '22');
return $info[1];
}
/**
* Get the current Host
*
* @return string
*/
public function getHost()
{
return $this->_host;
}
/**
* Gets General Configuration
*
* @param string $option
* @param string $default
* @return mixed
*/
public function general($option, $default = false)
{
$config = $this->_config['general'];
if (isset($config[$option])) {
if (is_array($default) && ($config[$option] == '')) {
return $default;
} else {
return $config[$option];
}
} else {
return $default;
}
}
/**
* Gets SCM Configuration
*
* @param string $option
* @param string $default
* @return mixed
*/
public function scm($option, $default = false)
{
$config = $this->_config['scm'];
if (isset($config[$option])) {
if (is_array($default) && ($config[$option] == '')) {
return $default;
} else {
return $config[$option];
}
} else {
return $default;
}
}
/**
* Get deployment configuration
*
* @param string $option
* @param string $default
* @return string
*/
public function deployment($option, $default = false)
{
$config = $this->_getEnvironmentOption('deployment', array());
if (isset($config[$option])) {
if (is_array($default) && ($config[$option] == '')) {
return $default;
} else {
return $config[$option];
}
} else {
return $default;
}
}
/**
* Returns Releaseing Options
*
* @param string $option
* @param string $default
* @return mixed
*/
public function release($option, $default = false)
{
$config = $this->_getEnvironmentOption('releases', array());
if (isset($config[$option])) {
if (is_array($default) && ($config[$option] == '')) {
return $default;
} else {
return $config[$option];
}
} else {
return $default;
}
}
/**
* Set From Deployment Path
*
* @param string $from
* @return Mage_Config
*/
public function setFrom($from)
{
$this->_config['environment']['deployment']['from'] = $from;
return $this;
}
/**
* Sets the Current Release ID
*
* @param integer $id
* @return Mage_Config
*/
public function setReleaseId($id)
{
$this->_releaseId = $id;
return $this;
}
/**
* Gets the Current Release ID
*
* @return integer
*/
public function getReleaseId()
{
return $this->_releaseId;
}
/**
* Parse the Command Line options
* @return boolean
*/
private function _parse($arguments)
{
foreach ($arguments as $argument) {
if (preg_match('/to:[\w]+/i', $argument)) {
$this->_environment = str_replace('to:', '', $argument);
} else if (preg_match('/--[\w]+/i', $argument)) {
$optionValue = explode('=', substr($argument, 2));
if (count($optionValue) == 1) {
$this->_parameters[$optionValue[0]] = true;
} else if (count($optionValue) == 2) {
if (strtolower($optionValue[1]) == 'true') {
$this->_parameters[$optionValue[0]] = true;
} else if (strtolower($optionValue[1]) == 'false') {
$this->_parameters[$optionValue[0]] = false;
} else {
$this->_parameters[$optionValue[0]] = $optionValue[1];
}
}
} else {
$this->_arguments[] = $argument;
}
}
}
/**
* Loads the General Configuration
*/
private function _loadGeneral()
{
if (file_exists('.mage/config/general.yml')) {
$this->_config['general'] = spyc_load_file('.mage/config/general.yml');
}
}
/**
* Loads the SCM Configuration
*/
private function _loadSCM()
{
if (file_exists('.mage/config/scm.yml')) {
$this->_config['scm'] = spyc_load_file('.mage/config/scm.yml');
}
}
/**
* Loads the Environment configuration
*
* @throws Exception
* @return boolean
*/
private function _loadEnvironment()
{
$environment = $this->getEnvironment();
if (($environment != false) && file_exists('.mage/config/environment/' . $environment . '.yml')) {
$this->_config['environment'] = spyc_load_file('.mage/config/environment/' . $environment . '.yml');
// Create temporal directory for clone
if (isset($this->_config['environment']['deployment']['source']) && is_array($this->_config['environment']['deployment']['source'])) {
if (trim($this->_config['environment']['deployment']['source']['temporal']) == '') {
$this->_config['environment']['deployment']['source']['temporal'] = '/tmp';
}
$newTemporal = rtrim($this->_config['environment']['deployment']['source']['temporal'], '/')
. '/' . md5(microtime()) . '/';
$this->_config['environment']['deployment']['source']['temporal'] = $newTemporal;
}
return true;
} else if (($environment != '') && !file_exists('.mage/config/environment/' . $environment . '.yml')) {
throw new Exception('Environment does not exists.');
}
return false;
}
/**
* Get Environment root option
*
* @param string $option
* @param mixed $default
* @return mixed
*/
private function _getEnvironmentOption($option, $default = array())
{
$config = $this->_config['environment'];
if (isset($config[$option])) {
return $config[$option];
} else {
return $default;
}
}
}

View File

@ -1,135 +0,0 @@
<?php
class Mage_Console
{
private static $_log = null;
private static $_logEnabled = true;
private static $_screenBuffer = '';
private static $_commandsOutput = '';
/**
* Runns a Magallanes Command
* @throws Exception
*/
public function run($arguments)
{
$configError = false;
try {
// Load Config
$config = new Mage_Config;
$config->load($arguments);
$configLoadedOk = true;
} catch (Exception $e) {
$configError = $e->getMessage();
}
// Command Option
$commandName = $config->getArgument(0);
// Logging
$showGrettings = true;
if (in_array($commandName, array('install', 'upgrade', 'version'))) {
self::$_logEnabled = false;
$showGrettings = false;
} else {
self::$_logEnabled = $config->general('logging', false);
}
// Grettings
if ($showGrettings) {
Mage_Console::output('Starting <blue>Magallanes</blue>', 0, 2);
}
// Run Command
if ($configError !== false) {
Mage_Console::output('<red>' . $configError . '</red>', 1, 2);
} else {
try {
$command = Mage_Command_Factory::get($commandName, $config);
if ($command instanceOf Mage_Command_RequiresEnvironment) {
if ($config->getEnvironment() == false) {
throw new Exception('You must specify an environment for this command.');
}
}
$command->run();
} catch (Exception $e) {
Mage_Console::output('<red>' . $e->getMessage() . '</red>', 1, 2);
}
}
if ($showGrettings) {
Mage_Console::output('Finished <blue>Magallanes</blue>', 0, 2);
}
}
/**
* Outputs a message to the user screen
*
* @param string $message
* @param integer $tabs
* @param integer $newLine
*/
public static function output($message, $tabs = 1, $newLine = 1)
{
self::log(strip_tags($message));
self::$_screenBuffer .= str_repeat("\t", $tabs)
. strip_tags($message)
. str_repeat(PHP_EOL, $newLine);
$output = str_repeat("\t", $tabs)
. Mage_Console_Colors::color($message)
. str_repeat(PHP_EOL, $newLine);
echo $output;
}
/**
* Executes a Command on the Shell
*
* @param string $command
* @param string $output
* @return boolean
*/
public static function executeCommand($command, &$output = null)
{
self::log('---------------------------------');
self::log('---- Executing: $ ' . $command);
$return = 1;
$log = array();
exec($command . ' 2>&1', $log, $return);
$log = implode(PHP_EOL, $log);
if (!$return) {
$output = trim($log);
}
self::$_commandsOutput .= PHP_EOL . trim($log) . PHP_EOL;
self::log($log);
self::log('---------------------------------');
return !$return;
}
/**
* Log a message to the logfile.
*
* @param string $message
* @param boolean $continuation
*/
public static function log($message, $continuation = false)
{
if (self::$_logEnabled) {
if (self::$_log == null) {
self::$_log = fopen('.mage/logs/log-' . date('Ymd-His') . '.log', 'w');
}
$message = date('Y-m-d H:i:s -- ') . $message;
fwrite(self::$_log, $message . PHP_EOL);
}
}
}

View File

@ -1,42 +0,0 @@
<?php
class Mage_Console_Colors
{
private static $foreground_colors = array(
'black' => '0;30',
'dark_gray' => '1;30',
'blue' => '0;34',
'light_blue' => '1;34',
'green' => '0;32',
'light_green' => '1;32',
'cyan' => '0;36',
'light_cyan' => '1;36',
'red' => '0;31',
'light_red' => '1;31',
'purple' => '0;35',
'light_purple' => '1;35',
'brown' => '0;33',
'yellow' => '1;33',
'light_gray' => '0;37',
'white' => '1;37'
);
// Returns colored string
public static function color($string)
{
foreach (self::$foreground_colors as $key => $code) {
$replaceFrom = array(
'<' . $key . '>',
'</' . $key . '>'
);
$replaceTo = array(
"\033[" . $code . 'm',
"\033[0m"
);
$string = str_replace($replaceFrom, $replaceTo, $string);
}
return $string;
}
}

View File

@ -1,44 +0,0 @@
<?php
class Mage_Task_BuiltIn_Deployment_Release
extends Mage_Task_TaskAbstract
implements Mage_Task_Releases_BuiltIn, Mage_Task_Releases_SkipOnOverride
{
public function getName()
{
return 'Releasing [built-in]';
}
public function run()
{
if ($this->getConfig()->release('enabled', false) == true) {
$releasesDirectory = $this->getConfig()->release('directory', 'releases');
$symlink = $this->getConfig()->release('symlink', 'current');
if (substr($symlink, 0, 1) == '/') {
$releasesDirectory = rtrim($this->getConfig()->deployment('to'), '/') . '/' . $releasesDirectory;
}
$currentCopy = $releasesDirectory . '/' . $this->getConfig()->getReleaseId();
// Fetch the user and group from base directory
$userGroup = '33:33';
$resultFetch = $this->_runRemoteCommand('ls -ld . | awk \'{print \$3":"\$4}\'', $userGroup);
// Remove symlink if exists; create new symlink and change owners
$command = 'rm -f ' . $symlink
. ' ; '
. 'ln -sf ' . $currentCopy . ' ' . $symlink
. ' && '
. 'chown -h ' . $userGroup . ' ' . $symlink
. ' && '
. 'chown -R ' . $userGroup . ' ' . $currentCopy;
$result = $this->_runRemoteCommand($command);
return $result;
} else {
return false;
}
}
}

View File

@ -1,108 +0,0 @@
<?php
class Mage_Task_BuiltIn_Deployment_Rsync
extends Mage_Task_TaskAbstract
implements Mage_Task_Releases_BuiltIn
{
public function getName()
{
if ($this->getConfig()->release('enabled', false) == true) {
if ($this->getConfig()->getParameter('overrideRelease', false) == true) {
return 'Rsync (with Releases override) [built-in]';
} else {
return 'Rsync (with Releases) [built-in]';
}
} else {
return 'Rsync [built-in]';
}
}
public function run()
{
$overrideRelease = $this->getConfig()->getParameter('overrideRelease', false);
if ($overrideRelease == true) {
$releaseToOverride = false;
$resultFetch = $this->_runRemoteCommand('ls -ld current | cut -d"/" -f2', $releaseToOverride);
if (is_numeric($releaseToOverride)) {
$this->getConfig()->setReleaseId($releaseToOverride);
}
}
$excludes = array(
'.git',
'.svn',
'.mage',
'.gitignore'
);
// Look for User Excludes
$userExcludes = $this->getConfig()->deployment('excludes', array());
// If we are working with releases
$deployToDirectory = $this->getConfig()->deployment('to');
if ($this->getConfig()->release('enabled', false) == true) {
$releasesDirectory = $this->getConfig()->release('directory', 'releases');
$deployToDirectory = rtrim($this->getConfig()->deployment('to'), '/')
. '/' . $releasesDirectory
. '/' . $this->getConfig()->getReleaseId();
$this->_runRemoteCommand('mkdir -p ' . $releasesDirectory . '/' . $this->getConfig()->getReleaseId());
}
$command = 'rsync -avz '
. '--rsh="ssh -p' . $this->getConfig()->getHostPort() . '" '
. $this->_excludes(array_merge($excludes, $userExcludes)) . ' '
. $this->getConfig()->deployment('from') . ' '
. $this->getConfig()->deployment('user') . '@' . $this->getConfig()->getHostName() . ':' . $deployToDirectory;
$result = $this->_runLocalCommand($command);
// Count Releases
if ($this->getConfig()->release('enabled', false) == true) {
$releasesDirectory = $this->getConfig()->release('directory', 'releases');
$symlink = $this->getConfig()->release('symlink', 'current');
if (substr($symlink, 0, 1) == '/') {
$releasesDirectory = rtrim($this->getConfig()->deployment('to'), '/') . '/' . $releasesDirectory;
}
$maxReleases = $this->getConfig()->release('max', false);
if (($maxReleases !== false) && ($maxReleases > 0)) {
$releasesList = '';
$countReleasesFetch = $this->_runRemoteCommand('ls -1 ' . $releasesDirectory, $releasesList);
$releasesList = trim($releasesList);
if ($releasesList != '') {
$releasesList = explode(PHP_EOL, $releasesList);
if (count($releasesList) > $maxReleases) {
$releasesToDelete = array_diff($releasesList, array($this->getConfig()->getReleaseId()));
sort($releasesToDelete);
$releasesToDeleteCount = count($releasesToDelete) - $maxReleases;
$releasesToDelete = array_slice($releasesToDelete, 0, $releasesToDeleteCount + 1);
foreach ($releasesToDelete as $releaseIdToDelete) {
$directoryToDelete = $releasesDirectory . '/' . $releaseIdToDelete;
if ($directoryToDelete != '/') {
$command = 'rm -rf ' . $directoryToDelete;
$result = $result && $this->_runRemoteCommand($command);
}
}
}
}
}
}
return $result;
}
private function _excludes(Array $excludes)
{
$excludesRsync = '';
foreach ($excludes as $exclude) {
$excludesRsync .= ' --exclude ' . $exclude . ' ';
}
$excludesRsync = trim($excludesRsync);
return $excludesRsync;
}
}

View File

@ -1,121 +0,0 @@
<?php
class Mage_Task_BuiltIn_Releases_List
extends Mage_Task_TaskAbstract
implements Mage_Task_Releases_BuiltIn
{
public function getName()
{
return 'Listing releases [built-in]';
}
public function run()
{
if ($this->getConfig()->release('enabled', false) == true) {
$releasesDirectory = $this->getConfig()->release('directory', 'releases');
$symlink = $this->getConfig()->release('symlink', 'current');
Mage_Console::output('Releases available on <dark_gray>' . $this->getConfig()->getHost() . '</dark_gray>');
// Get Releases
$output = '';
$result = $this->_runRemoteCommand('ls -1 ' . $releasesDirectory, $output);
$releases = ($output == '') ? array() : explode(PHP_EOL, $output);
// Get Current
$result = $this->_runRemoteCommand('ls -l ' . $symlink, $output);
$currentRelease = explode('/', $output);
$currentRelease = trim(array_pop($currentRelease));
if (count($releases) == 0) {
Mage_Console::output('<dark_gray>No releases available</dark_gray> ... ', 2);
} else {
rsort($releases);
$releases = array_slice($releases, 0, 10);
foreach ($releases as $releaseIndex => $release) {
$release = trim($release);
$releaseIndex = str_pad($releaseIndex * -1, 2, ' ', STR_PAD_LEFT);
$releaseDate = $release[0] . $release[1] . $release[2] .$release[3]
. '-'
. $release[4] . $release[5]
. '-'
. $release[6] . $release[7]
. ' '
. $release[8] . $release[9]
. ':'
. $release[10] . $release[11]
. ':'
. $release[12] . $release[13];
$isCurrent = '';
if ($currentRelease == $release) {
$isCurrent = ' <- current';
}
$dateDiff = $this->_dateDiff($releaseDate);
Mage_Console::output(
'Release: <purple>' . $release . '</purple> '
. '- Date: <dark_gray>' . $releaseDate . '</dark_gray> '
. '- Index: <dark_gray>' . $releaseIndex . '</dark_gray>' . $dateDiff . $isCurrent, 2);
}
}
Mage_Console::output('');
return $result;
} else {
Mage_Console::output('');
return false;
}
}
private function _dateDiff($releaseDate)
{
$textDiff = '';
$releaseDate = new DateTime($releaseDate);
$now = new DateTime();
$diff = $now->diff($releaseDate);
if ($diff->format('%a') <= 7) {
if ($diff->format('%d') == 7) {
$textDiff = ' [a week ago] ';
} else if ($diff->format('%d') > 0 && $diff->format('%d') < 7) {
$days = $diff->format('%d');
if ($days <= 1) {
$textDiff = ' [one day ago] ';
} else {
$textDiff = ' [' . $days . ' days ago] ';
}
} else if ($diff->format('%d') == 0 && $diff->format('%h') > 0) {
$hours = $diff->format('%h');
if ($hours <= 1) {
$textDiff = ' [one hour ago] ';
} else {
$textDiff = ' [' . $hours . ' hours ago] ';
}
} else if ($diff->format('%d') == 0 && $diff->format('%h') == 0) {
$minutes = $diff->format('%i');
if ($minutes <= 1) {
$textDiff = ' [one minute ago] ';
} else {
$textDiff = ' [' . $minutes . ' minutes ago] ';
}
} else if ($diff->format('%d') == 0 && $diff->format('%h') == 0 && $diff->format('%i') == 0) {
$seconds = $diff->format('%s');
if ($seconds < 10) {
$textDiff = ' [just now!] ';
} else {
$textDiff = ' [' . $seconds . ' seconds ago] ';
}
}
}
return $textDiff;
}
}

View File

@ -1,132 +0,0 @@
<?php
class Mage_Task_BuiltIn_Releases_Rollback
extends Mage_Task_TaskAbstract
implements Mage_Task_Releases_BuiltIn
{
private $_release = null;
public function getName()
{
return 'Rollback release [built-in]';
}
public function setRelease($releaseId)
{
$this->_release = $releaseId;
return $this;
}
public function getRelease()
{
return $this->_release;
}
public function run()
{
if ($this->getConfig()->release('enabled', false) == true) {
$releasesDirectory = $this->getConfig()->release('directory', 'releases');
$symlink = $this->getConfig()->release('symlink', 'current');
$output = '';
$result = $this->_runRemoteCommand('ls -1 ' . $releasesDirectory, $output);
$releases = ($output == '') ? array() : explode(PHP_EOL, $output);
if (count($releases) == 0) {
Mage_Console::output('Release are not available for <dark_gray>' . $this->getConfig()->getHost() . '</dark_gray> ... <red>FAIL</red>');
} else {
rsort($releases);
$releaseIsAvailable = false;
if ($this->getRelease() == '') {
$releaseId = $releases[0];
$releaseIsAvailable = true;
} else if ($this->getRelease() <= 0) {
$index = $this->getRelease() * -1;
if (isset($releases[$index])) {
$releaseId = $releases[$index];
$releaseIsAvailable = true;
}
} else {
if (in_array($this->getRelease(), $releases)) {
$releaseId = $this->getRelease();
$releaseIsAvailable = true;
}
}
if (!$releaseIsAvailable) {
Mage_Console::output('Release <dark_gray>' . $this->getRelease() . '</dark_gray> is invalid or unavailable for <dark_gray>' . $this->getConfig()->getHost() . '</dark_gray> ... <red>FAIL</red>');
} else {
Mage_Console::output('Rollback release on <dark_gray>' . $this->getConfig()->getHost() . '</dark_gray>');
$rollbackTo = $releasesDirectory . '/' . $releaseId;
// Tasks
$tasks = 1;
$completedTasks = 0;
$tasksToRun = $this->getConfig()->getTasks();
$this->getConfig()->setReleaseId($releaseId);
if (count($tasksToRun) == 0) {
Mage_Console::output('<light_purple>Warning!</light_purple> <dark_gray>No </dark_gray><light_cyan>Deployment</light_cyan> <dark_gray>tasks defined.</dark_gray>', 2);
Mage_Console::output('Deployment to <dark_gray>' . $this->getConfig()->getHost() . '</dark_gray> skipped!', 1, 3);
} else {
foreach ($tasksToRun as $taskData) {
$task = Mage_Task_Factory::get($taskData, $this->getConfig(), true, 'deploy');
$task->init();
Mage_Console::output('Running <purple>' . $task->getName() . '</purple> ... ', 2, false);
if ($task instanceOf Mage_Task_Releases_RollbackAware) {
$tasks++;
$result = $task->run();
if ($result == true) {
Mage_Console::output('<green>OK</green>', 0);
$completedTasks++;
} else {
Mage_Console::output('<red>FAIL</red>', 0);
}
} else {
Mage_Console::output('<yellow>SKIPPED</yellow>', 0);
}
}
}
// Changing Release
Mage_Console::output('Running <purple>Rollback Release [id=' . $releaseId . ']</purple> ... ', 2, false);
$userGroup = '';
$resultFetch = $this->_runRemoteCommand('ls -ld ' . $rollbackTo . ' | awk \'{print \$3":"\$4}\'', $userGroup);
$command = 'rm -f ' . $symlink
. ' && '
. 'ln -sf ' . $rollbackTo . ' ' . $symlink
. ' && '
. 'chown -h ' . $userGroup . ' ' . $symlink;
$result = $this->_runRemoteCommand($command);
if ($result) {
Mage_Console::output('<green>OK</green>', 0);
$completedTasks++;
} else {
Mage_Console::output('<red>FAIL</red>', 0);
}
if ($completedTasks == $tasks) {
$tasksColor = 'green';
} else {
$tasksColor = 'red';
}
Mage_Console::output('Release rollback on <dark_gray>' . $this->getConfig()->getHost() . '</dark_gray> compted: <' . $tasksColor . '>' . $completedTasks . '/' . $tasks . '</' . $tasksColor . '> tasks done.', 1, 3);
}
}
return $result;
} else {
return false;
}
}
}

View File

@ -1,58 +0,0 @@
<?php
class Mage_Task_BuiltIn_Scm_ChangeBranch
extends Mage_Task_TaskAbstract
{
private $_name = 'SCM Changing branch [built-in]';
public function getName()
{
return $this->_name;
}
public function init()
{
switch ($this->getConfig()->scm('type')) {
case 'git':
$this->_name = 'SCM Changing branch (GIT) [built-in]';
break;
case 'svn':
$this->_name = 'SCM Changing branch (Subversion) [built-in]';
break;
}
}
public function run()
{
switch ($this->getConfig()->scm('type')) {
case 'git':
$command = 'git branch | grep \'*\' | cut -d\' \' -f 2';
$currentBranch = 'master';
$result = $this->_runLocalCommand($command, $currentBranch);
$scmData = $this->getConfig()->deployment('scm', false);
if ($result && is_array($scmData) && isset($scmData['branch'])) {
$branch = $this->getParameter('branch', $scmData['branch']);
$command = 'git checkout ' . $branch;
$result = $this->_runLocalCommand($command);
$oldBranchFile = '.mage/' . $this->getConfig()->getEnvironment() . '.oldBranch';
file_put_contents($oldBranchFile, $currentBranch);
} else {
throw new Mage_Task_SkipException;
}
break;
default:
return false;
break;
}
$this->getConfig()->reload();
return $result;
}
}

View File

@ -1,47 +0,0 @@
<?php
class Mage_Task_BuiltIn_Scm_ChangeBranchBack
extends Mage_Task_TaskAbstract
{
private $_name = 'SCM Changing branch Back [built-in]';
public function getName()
{
return $this->_name;
}
public function init()
{
switch ($this->getConfig()->scm('type')) {
case 'git':
$this->_name = 'SCM Changing branch Back (GIT) [built-in]';
break;
case 'svn':
$this->_name = 'SCM Changing branch Back (Subversion) [built-in]';
break;
}
}
public function run()
{
switch ($this->getConfig()->scm('type')) {
case 'git':
$oldBranchFile = '.mage/' . $this->getConfig()->getEnvironment() . '.oldBranch';
$currentBranch = trim(file_get_contents($oldBranchFile));
$command = 'git checkout ' . $currentBranch;
$result = $this->_runLocalCommand($command);
@unlink($oldBranchFile);
break;
default:
return false;
break;
}
$this->getConfig()->reload();
return $result;
}
}

View File

@ -1,52 +0,0 @@
<?php
class Mage_Task_BuiltIn_Scm_Clone
extends Mage_Task_TaskAbstract
{
private $_name = 'SCM Clone [built-in]';
private $_source = null;
public function getName()
{
return $this->_name;
}
public function init()
{
$this->_source = $this->getConfig()->deployment('source');
switch ($this->_source['type']) {
case 'git':
$this->_name = 'SCM Clone (GIT) [built-in]';
break;
case 'svn':
$this->_name = 'SCM Clone (Subversion) [built-in]';
break;
}
}
public function run()
{
$this->_runLocalCommand('mkdir -p ' . $this->_source['temporal']);
switch ($this->_source['type']) {
case 'git':
// Clone Repo
$command = 'cd ' . $this->_source['temporal'] . ' ; '
. 'git clone ' . $this->_source['repository'] . ' . ';
$result = $this->_runLocalCommand($command);
// Checkout Branch
$command = 'cd ' . $this->_source['temporal'] . ' ; '
. 'git checkout ' . $this->_source['from'];
$result = $result && $this->_runLocalCommand($command);
$this->getConfig()->setFrom($this->_source['temporal']);
break;
case 'svn':
return false;
break;
}
return $result;
}
}

View File

@ -1,22 +0,0 @@
<?php
class Mage_Task_BuiltIn_Scm_RemoveClone
extends Mage_Task_TaskAbstract
{
private $_name = 'SCM Remove Clone [built-in]';
private $_source = null;
public function getName()
{
return $this->_name;
}
public function init()
{
$this->_source = $this->getConfig()->deployment('source');
}
public function run()
{
return $this->_runLocalCommand('rm -rf ' . $this->_source['temporal']);
}
}

View File

@ -1,46 +0,0 @@
<?php
class Mage_Task_BuiltIn_Scm_Update
extends Mage_Task_TaskAbstract
{
private $_name = 'SCM Update [built-in]';
public function getName()
{
return $this->_name;
}
public function init()
{
switch ($this->getConfig()->scm('type')) {
case 'git':
$this->_name = 'SCM Update (GIT) [built-in]';
break;
case 'svn':
$this->_name = 'SCM Update (Subversion) [built-in]';
break;
}
}
public function run()
{
switch ($this->getConfig()->scm('type')) {
case 'git':
$command = 'git pull';
break;
case 'svn':
$command = 'svn update';
break;
default:
return false;
break;
}
$result = $this->_runLocalCommand($command);
$this->getConfig()->reload();
return $result;
}
}

View File

@ -1,17 +0,0 @@
<?php
class Mage_Task_BuiltIn_Symfony1_ClearCache
extends Mage_Task_TaskAbstract
{
public function getName()
{
return 'Symfony v1 - Clear Cache [built-in]';
}
public function run()
{
$command = 'symfony cc';
$result = $this->_runLocalCommand($command);
return $result;
}
}

View File

@ -1,17 +0,0 @@
<?php
class Mage_Task_BuiltIn_Symfony1_Migrate
extends Mage_Task_TaskAbstract
{
public function getName()
{
return 'Symfony v1 - Run Migrations [built-in]';
}
public function run()
{
$command = 'symfony doctrine:migrate';
$result = $this->_runLocalCommand($command);
return $result;
}
}

View File

@ -1,17 +0,0 @@
<?php
class Mage_Task_BuiltIn_Symfony2_CacheClear
extends Mage_Task_TaskAbstract
{
public function getName()
{
return 'Symfony v2 - Cache Clear [built-in]';
}
public function run()
{
$command = 'app/console cache:clear';
$result = $this->_runLocalCommand($command);
return $result;
}
}

View File

@ -1,17 +0,0 @@
<?php
class Mage_Task_BuiltIn_Symfony2_CacheWarmup
extends Mage_Task_TaskAbstract
{
public function getName()
{
return 'Symfony v2 - Cache Warmup [built-in]';
}
public function run()
{
$command = 'app/console cache:warmup';
$result = $this->_runLocalCommand($command);
return $result;
}
}

View File

@ -1,17 +0,0 @@
<?php
class Mage_Task_BuiltIn_Symfony2_Migrate
extends Mage_Task_TaskAbstract
{
public function getName()
{
return 'Symfony v2 - Run Migrations [built-in]';
}
public function run()
{
$command = 'app/console doctrine:migrations:migrate';
$result = $this->_runLocalCommand($command);
return $result;
}
}

View File

@ -1,39 +0,0 @@
<?php
class Mage_Task_Factory
{
/**
*
*
* @param string|array $taskData
* @param boolean $inRollback
* @return Mage_Task_TaskAbstract
*/
public static function get($taskData, Mage_Config $taskConfig, $inRollback = false, $stage = null)
{
if (is_array($taskData)) {
$taskName = $taskData['name'];
$taskParameters = $taskData['parameters'];
} else {
$taskName = $taskData;
$taskParameters = array();
}
$instance = null;
$taskName = ucwords(str_replace('-', ' ', $taskName));
$taskName = str_replace(' ', '', $taskName);
if (strpos($taskName, '/') === false) {
Mage_Autoload::loadUserTask($taskName);
$className = 'Task_' . ucfirst($taskName);
$instance = new $className($taskConfig, $inRollback, $stage, $taskParameters);
} else {
$taskName = str_replace(' ', '_', ucwords(str_replace('/', ' ', $taskName)));
$className = 'Mage_Task_BuiltIn_' . $taskName;
$instance = new $className($taskConfig, $inRollback, $stage, $taskParameters);
}
assert($instance instanceOf Mage_Task_TaskAbstract);
return $instance;
}
}

View File

@ -1,4 +0,0 @@
<?php
interface Mage_Task_Releases_BuiltIn
{
}

View File

@ -1,4 +0,0 @@
<?php
interface Mage_Task_Releases_RollbackAware
{
}

View File

@ -1,4 +0,0 @@
<?php
interface Mage_Task_Releases_SkipOnOverride
{
}

View File

@ -1,6 +0,0 @@
<?php
class Mage_Task_SkipException
extends Exception
{
}

View File

@ -1,81 +0,0 @@
<?php
abstract class Mage_Task_TaskAbstract
{
protected $_config = null;
protected $_inRollback = false;
protected $_stage = null;
protected $_parameters = array();
public abstract function getName();
public abstract function run();
public final function __construct(Mage_Config $config, $inRollback = false, $stage = null, $parameters = array())
{
$this->_config = $config;
$this->_inRollback = $inRollback;
$this->_stage = $stage;
$this->_parameters = $parameters;
}
public function inRollback()
{
return $this->_inRollback;
}
public function getStage()
{
return $this->_stage;
}
public function getConfig()
{
return $this->_config;
}
public function init()
{
}
/**
* Return the a parameter
*
* @param string $name
* @return mixed
*/
public function getParameter($name, $default = null)
{
return $this->getConfig()->getParameter($name, $default, $this->_parameters);
}
protected final function _runLocalCommand($command, &$output = null)
{
return Mage_Console::executeCommand($command, $output);
}
protected final function _runRemoteCommand($command, &$output = null)
{
if ($this->_config->release('enabled', false) == true) {
if ($this instanceOf Mage_Task_Releases_BuiltIn) {
$releasesDirectory = '';
} else {
$releasesDirectory = '/'
. $this->_config->release('directory', 'releases')
. '/'
. $this->_config->getReleaseId();
}
} else {
$releasesDirectory = '';
}
$localCommand = 'ssh -p ' . $this->_config->getHostPort() . ' '
. '-q -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no '
. $this->_config->deployment('user') . '@' . $this->_config->getHostName() . ' '
. '"cd ' . rtrim($this->_config->deployment('to'), '/') . $releasesDirectory . ' && '
. str_replace('"', '\"', $command) . '"';
return $this->_runLocalCommand($localCommand, $output);
}
}

File diff suppressed because it is too large Load Diff

View File

@ -1,47 +1,29 @@
# Magallanes #
# Magallanes
[![SensioLabsInsight](https://insight.sensiolabs.com/projects/ed0de53a-a12e-459b-9464-34def5907b56/mini.png)](https://insight.sensiolabs.com/projects/ed0de53a-a12e-459b-9464-34def5907b56)
[![Build Status](https://img.shields.io/travis/andres-montanez/Magallanes/master.svg)](https://travis-ci.org/andres-montanez/Magallanes)
[![Coverage Status](https://img.shields.io/coveralls/andres-montanez/Magallanes/master.svg)](https://coveralls.io/github/andres-montanez/Magallanes?branch=master)
[![Code Quality](https://img.shields.io/scrutinizer/g/andres-montanez/Magallanes.svg)](https://scrutinizer-ci.com/g/andres-montanez/Magallanes/)
[![Latest Stable Version](https://img.shields.io/packagist/v/andres-montanez/magallanes.svg?label=stable)](https://packagist.org/packages/andres-montanez/magallanes)
[![Pre Release](https://img.shields.io/packagist/vpre/andres-montanez/magallanes.svg?label=dev)](https://packagist.org/packages/andres-montanez/magallanes)
[![Total Downloads](https://img.shields.io/packagist/dt/andres-montanez/magallanes.svg)](https://packagist.org/packages/andres-montanez/magallanes)
[![License](https://img.shields.io/packagist/l/andres-montanez/magallanes.svg)](https://packagist.org/packages/andres-montanez/magallanes)
### What's Magallanes? ###
Magallanes is a deployment tool for PHP applications; it's quite simple to use and manage.
It will get your application to a safe harbor.
### What's Magallanes?
**Magallanes** is a deployment tool for made with PHP for PHP applications; it's quite simple to use and manage. For more information and documentation please visit https://magephp.com
### Installing
Simply add the following dependency to your projects composer.json file:
### So, What can it do? ###
You can instruct Magallanes to deploy your code to all the servers you want (via rsync over ssh),
and run tasks for that freshly deployed code.
### Can you give me some examples/ideas? ###
**Sure!**
Suppose you have a checkout of your app and you have to deploy it to four servers;
and after each deploy you have to run some boring tasks, like fixing file permissions, creating symlinks, etc.
You can define all this on Magallanes and with *just one command* you can do all this at once!
Like this:
```json
"require": {
"andres-montanez/magallanes": "^4.0"
}
```
$ mage deploy to:production
Finally you can use **Magallanes** from the vendor's bin:
```bash
$ vendor/bin/mage version
```
### What's this sorcery?! ###
Easy boy. It's not sorcery, just some *technomagick*!
In Magallanes you define environments like *testing*, *staging*, or *production* like on the example above.
Then, on that environment, you can configure a setup specifing to which hosts you want to deploy and what tasks to run (*after*, *on*, and *before* deploying).
And you are done!
### And what spells... tasks! What tasks has it built in? ###
Just a few, for now...
- **deployment/rsync** - This task is for deploying your code to the remote servers.
- **scm/update** - This task is for updating (git/svn) your base working copy.
But that's not all! You can create your own tasks, and execute commands on your working copy and in your deployed code!
Also we are working on creating more built in tasks.
### This is awesome! Where can I learn more? ###
You can read the whole source code (naaah!); or checkout the documentation wiki at: http://magallanes.zenreworks.com/wiki
Enjoy your magic trip with **Magallanes** to the land of the easily deployable apps!!
### Codename Discovery One
Each new mayor version of **Magallanes** will have a codename (like Ubuntu), version 3 was _Nostromo_, and in the current version it is **_Discovery One_**, in homage to the spaceship from the ground breaking film *2001: A Space Odyssey (1968)*.

View File

@ -2,18 +2,18 @@
<?php
date_default_timezone_set('UTC');
$baseDir = dirname(dirname(__FILE__));
if (file_exists(__DIR__ . '/../../../autoload.php')) {
require __DIR__ . '/../../../autoload.php';
} elseif (file_exists(__DIR__ . '/../vendor/autoload.php')) {
require __DIR__ . '/../vendor/autoload.php';
}
define('MAGALLANES_VERSION', '0.9.14');
use Mage\MageApplication;
// Preload
require_once $baseDir . '/Mage/spyc.php';
require_once $baseDir . '/Mage/Autoload.php';
spl_autoload_register(array('Mage_Autoload', 'autoload'));
// Clean arguments
array_shift($argv);
// Run Magallanes
$console = new Mage_Console;
$console->run($argv);
try {
$mage = new MageApplication('.mage.yml');
$mage->run();
} catch (Exception $exception) {
printf('Error: %s' . PHP_EOL, $exception->getMessage());
exit(9);
}

View File

@ -1,11 +1,49 @@
{
"name": "andres-montanez/magallanes",
"description": "A Deployment Tool for PHP Applications",
"homepage": "http://magephp.com",
"license": "MIT",
"type": "library",
"keywords": "deployment",
"require": {
"php": ">=5.3"
"name": "hauke68/magallanes",
"description": "The Deployment Tool for PHP Applications",
"homepage": "https://magephp.com",
"license": "MIT",
"type": "library",
"keywords": ["deployment"],
"authors": [
{
"name": "Andrés Montañez",
"email": "andresmontanez@gmail.com"
},
{
"name": "Hauke Zühl",
"email": "hzuehl@web.de"
}
],
"require": {
"php": "^7.2.5",
"monolog/monolog": "~1.11||^2.0",
"symfony/console": "^5.0",
"symfony/filesystem": "^5.0",
"symfony/event-dispatcher": "^5.0",
"symfony/finder": "^5.0",
"symfony/yaml": "^5.0",
"symfony/process": "^5.0"
},
"require-dev": {
"phpunit/phpunit": "^7.0",
"satooshi/php-coveralls": "^2.0"
},
"autoload": {
"psr-4": {
"Mage\\": "src/"
}
},
"autoload-dev": {
"psr-4": {
"Mage\\Tests\\": "tests/"
}
},
"bin": ["bin/mage"],
"extra": {
"branch-alias": {
"dev-master": "4.0.x-dev",
"dev-discovery-one": "4.x-dev"
}
}
}

View File

@ -1,59 +0,0 @@
### List of Commands ###
# Installs Magallanes on the system
sudo mage install
# Displays Magallanes version
mage version
# Creats a Magallanes instance configuration
mage init
# Creates a compiled version of Magallanes using phar
mage compile
# Upgrades Magallanes itself
mage upgrade
# Add a new Environment configuration
mage add environment --name=production
# Add a new Environment configuration with releases enabled
mage add environment --name=production --enableReleases
# Performs a SCM Update, if configured
mage update
# Deploys Application to Production environment
mage deploy to:production
# Deploys Application to Production environment, overriding the current release
mage deploy to:production --overrideRelease
# Locks deployment to Production environment
mage lock to:production
# Unlocks deployment to Production environment
mage unlock to:production
# Lists all Releases on the Production environment
mage releases list to:production
# Rollback to the last Release on the Production environment
mage releases rollback to:production
mage releases rollback --release=0 to:production
# Rollback to the first, second, or thrith Release before the current Release on the Production environment
mage releases rollback --release=-1 to:production
mage releases rollback --release=-2 to:production
mage releases rollback --release=-3 to:production
# Rollback to a specific Release on the Production environment
# mage releases rollback --release=20120101172148 to:production
### List of UPCOMING Commands ###
# mage config add host s05.example.com to:[production]
# mage config git git://github.com/andres-montanez/Zend-Framework-Twig-example-app.git
# mage config svn svn://example.com/repo
# mage task:deployment/rsync to:production

View File

@ -0,0 +1,19 @@
version: '2'
services:
php5:
container_name: mage-php5
build: ./php5
volumes:
- ../../:/home/magephp
php7.0:
container_name: mage-php7.0
build: ./php7.0
volumes:
- ../../:/home/magephp
php7.1:
container_name: mage-php7.1
build: ./php7.1
volumes:
- ../../:/home/magephp

View File

@ -0,0 +1,9 @@
FROM ubuntu:14.04
RUN apt-get update && apt-get upgrade -y
RUN apt-get install -y vim curl git unzip
RUN apt-get install -y php5-cli php5-curl
RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/bin/ --filename=composer
WORKDIR /home/magephp

View File

@ -0,0 +1,9 @@
FROM ubuntu:16.04
RUN apt-get update && apt-get upgrade -y
RUN apt-get install -y vim curl git unzip
RUN apt-get install -y php7.0-cli php-zip php7.0-curl php7.0-xml
RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/bin/ --filename=composer
WORKDIR /home/magephp

View File

@ -0,0 +1,9 @@
FROM ubuntu:17.10
RUN apt-get update && apt-get upgrade -y
RUN apt-get install -y vim curl git unzip
RUN apt-get install -y php7.1-cli php-zip php7.1-curl php7.1-xml php7.1-mbstring
RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/bin/ --filename=composer
WORKDIR /home/magephp

31
docs/example-config.yml Normal file
View File

@ -0,0 +1,31 @@
magephp:
environments:
production:
user: app
branch: test
from: ./
host_path: /var/www/test
releases: 4
exclude:
- vendor
- app/cache
- app/log
- web/app_dev.php
hosts:
- webserver1
- webserver2
- webserver3
pre-deploy:
- git/update
- composer/install
- composer/generate-autoload
on-deploy:
- symfony/cache-warmup: { env: 'prod' }
- symfony/assets-install: { env: 'prod' }
- symfony/assetic-dump: { env: 'prod' }
on-release:
post-release:
post-deploy:
- magic
custom_tasks:
- App\Deployment\MagicTask

View File

@ -1,28 +0,0 @@
#production
deployment:
user: root
# from: ./
source:
type: git
repository: git://github.com/andres-montanez/Zend-Framework-Twig-example-app.git
from: production
temporal: /tmp/myAppClone
to: /var/www/vhosts/example.com/www
excludes:
- application/data/cache/twig/*
releases:
enabled: true
max: 5
symlink: current
directory: releases
hosts:
- s01.example.com:22
- s02.example.com
tasks:
pre-deploy:
- scm/update
on-deploy:
- privileges
- sampleTask
- sampleTaskRollbackAware
#post-deploy:

View File

@ -1,30 +0,0 @@
#staging
deployment:
user: root
from: ./
to: /var/www/
scm:
branch: master
releases:
enabled: true
max: 5
symlink: current
directory: releases
hosts:
# - localhost
# - dbserver
tasks:
pre-deploy:
- sampleTask
- scm/update
on-deploy:
- privileges
- sampleTask
- sampleTaskRollbackAware
- taskWithParameters:
booleanOption: true
- taskWithParameters
post-release:
# - sampleTask
post-deploy:
- sampleTask

View File

@ -1,11 +0,0 @@
#general settings
logging: true
mail:
enabled: true
from: andresmontanez@gmail.com
password: xxxxxx
smtp: smtp.gmail.com
to:
- andresmontanez@gmail.com
- andres.montanez@zenreworks.com

View File

@ -1,3 +0,0 @@
#scm settings
type: git
uri: git://github.com/andres-montanez/Zend-Framework-Twig-example-app.git

View File

@ -1 +0,0 @@
log-*

View File

@ -1,17 +0,0 @@
<?php
class Task_Permissions
extends Mage_Task_TaskAbstract
{
public function getName()
{
return 'Fixing file permissions';
}
public function run()
{
$command = 'chmod 755 . -R';
$result = $this->_runRemoteCommand($command);
return $result;
}
}

View File

@ -1,17 +0,0 @@
<?php
class Task_Privileges
extends Mage_Task_TaskAbstract
{
public function getName()
{
return 'Fixing file privileges';
}
public function run()
{
$command = 'chown 33:33 . -R';
$result = $this->_runRemoteCommand($command);
return $result;
}
}

View File

@ -1,14 +0,0 @@
<?php
class Task_SampleTask
extends Mage_Task_TaskAbstract
{
public function getName()
{
return 'A Sample Task';
}
public function run()
{
return true;
}
}

View File

@ -1,19 +0,0 @@
<?php
class Task_SampleTaskRollbackAware
extends Mage_Task_TaskAbstract
implements Mage_Task_Releases_RollbackAware
{
public function getName()
{
if ($this->inRollback()) {
return 'A Sample Task aware of rollbacks [in rollback]';
} else {
return 'A Sample Task aware of rollbacks [not in rollback]';
}
}
public function run()
{
return true;
}
}

View File

@ -1,22 +0,0 @@
<?php
class Task_TaskWithParameters
extends Mage_Task_TaskAbstract
{
public function getName()
{
$booleanOption = $this->getParameter('booleanOption', false);
if ($booleanOption) {
return 'A Sample Task With Parameters [booleanOption=true]';
} else {
return 'A Sample Task With Parameters [booleanOption=false]';
}
}
public function run()
{
//throw new Mage_Task_SkipException;
//return false;
return true;
}
}

37
phpunit.xml.dist Normal file
View File

@ -0,0 +1,37 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="http://schema.phpunit.de/4.1/phpunit.xsd"
backupGlobals="false"
colors="true"
bootstrap="vendor/autoload.php"
>
<php>
<ini name="error_reporting" value="-1" />
<ini name="intl.default_locale" value="en" />
<ini name="intl.error_level" value="0" />
<ini name="memory_limit" value="-1" />
</php>
<testsuites>
<testsuite name="Magallanes Test Suite">
<directory>./tests/</directory>
</testsuite>
</testsuites>
<groups>
<exclude>
<group>benchmark</group>
<group>intl-data</group>
</exclude>
</groups>
<filter>
<whitelist>
<directory>./src/</directory>
<exclude>
<directory>./tests/</directory>
</exclude>
</whitelist>
</filter>
</phpunit>

View File

@ -0,0 +1,81 @@
<?php
/*
* This file is part of the Magallanes package.
*
* (c) Andrés Montañez <andres@andresmontanez.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Mage\Command;
use Mage\MageApplication;
use Mage\Utils;
use Mage\Runtime\Runtime;
use Psr\Log\LogLevel;
use Symfony\Component\Console\Command\Command;
/**
* Abstract base class for Magallanes Commands
*
* @author Andrés Montañez <andresmontanez@gmail.com>
*/
abstract class AbstractCommand extends Command
{
/**
* @var int
*/
protected $statusCode = 0;
/**
* @var Runtime Current Runtime instance
*/
protected $runtime;
/**
* Set the Runtime configuration
*
* @param Runtime $runtime Runtime container
* @return AbstractCommand
*/
public function setRuntime(Runtime $runtime)
{
$this->runtime = $runtime;
return $this;
}
/**
* Logs a message
*
* @param string $message
* @param string $level
*/
public function log($message, $level = LogLevel::DEBUG)
{
$this->runtime->log($message, $level);
}
/**
* Get the Human friendly Stage name
*
* @return string
*/
protected function getStageName()
{
$utils = new Utils();
return $utils->getStageName($this->runtime->getStage());
}
/**
* Requires the configuration to be loaded
*/
protected function requireConfig()
{
$app = $this->getApplication();
if ($app instanceof MageApplication) {
$app->configure();
}
}
}

View File

@ -0,0 +1,56 @@
<?php
/*
* This file is part of the Magallanes package.
*
* (c) Andrés Montañez <andres@andresmontanez.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Mage\Command\BuiltIn\Config;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Mage\Command\AbstractCommand;
/**
* Command for Dumping the Configuration
*
* @author Andrés Montañez <andresmontanez@gmail.com>
*/
class DumpCommand extends AbstractCommand
{
/**
* Configure the Command
*/
protected function configure()
{
$this
->setName('config:dump')
->setDescription('Dumps the Magallanes configuration')
;
}
/**
* Execute the Command
*
* @param InputInterface $input
* @param OutputInterface $output
* @return int|mixed
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
$this->requireConfig();
$output->writeln('Starting <fg=blue>Magallanes</>');
$output->writeln('');
$output->writeln(sprintf('<comment>%s</comment>', var_export($this->runtime->getConfiguration(), true)));
$output->writeln('');
$output->writeln('Finished <fg=blue>Magallanes</>');
return 0;
}
}

View File

@ -0,0 +1,71 @@
<?php
/*
* This file is part of the Magallanes package.
*
* (c) Andrés Montañez <andres@andresmontanez.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Mage\Command\BuiltIn\Config;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Helper\Table;
use Mage\Command\AbstractCommand;
/**
* Command for listing all the Environments
*
* @author Andrés Montañez <andresmontanez@gmail.com>
*/
class EnvironmentsCommand extends AbstractCommand
{
/**
* Configure the Command
*/
protected function configure()
{
$this
->setName('config:environments')
->setDescription('List all Magallanes configured Environments')
;
}
/**
* Execute the Command
*
* @param InputInterface $input
* @param OutputInterface $output
* @return int|mixed
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
$this->requireConfig();
$output->writeln('Starting <fg=blue>Magallanes</>');
$output->writeln('');
$table = new Table($output);
$table->setHeaders(['Environment', 'User', 'Branch', 'Hosts']);
$configuration = $this->runtime->getConfigOption('environments');
foreach ($configuration as $environment => $config) {
$row = [$environment];
$row[] = (isset($config['user']) ? $config['user'] : '-');
$row[] = (isset($config['branch']) ? $config['branch'] : '-');
$row[] = (isset($config['hosts']) ? implode(PHP_EOL, $config['hosts']) : '-');
$table->addRow($row);
}
$table->render();
$output->writeln('');
$output->writeln('Finished <fg=blue>Magallanes</>');
return 0;
}
}

View File

@ -0,0 +1,245 @@
<?php
/*
* This file is part of the Magallanes package.
*
* (c) Andrés Montañez <andres@andresmontanez.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Mage\Command\BuiltIn;
use Mage\Deploy\Strategy\StrategyInterface;
use Mage\Runtime\Exception\RuntimeException;
use Mage\Runtime\Runtime;
use Mage\Task\ExecuteOnRollbackInterface;
use Mage\Task\AbstractTask;
use Mage\Task\Exception\ErrorException;
use Mage\Task\Exception\SkipException;
use Mage\Task\TaskFactory;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Mage\Command\AbstractCommand;
/**
* The Deployment Command
*
* @author Andrés Montañez <andresmontanez@gmail.com>
*/
class DeployCommand extends AbstractCommand
{
/**
* @var TaskFactory
*/
protected $taskFactory;
/**
* Configure the Command
*/
protected function configure()
{
$this
->setName('deploy')
->setDescription('Deploy code to hosts')
->addArgument('environment', InputArgument::REQUIRED, 'Name of the environment to deploy to')
->addOption('branch', null, InputOption::VALUE_REQUIRED, 'Force to switch to a branch other than the one defined', false)
;
}
/**
* Execute the Command
*
* @param InputInterface $input
* @param OutputInterface $output
* @return integer
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
$this->requireConfig();
$output->writeln('Starting <fg=blue>Magallanes</>');
$output->writeln('');
try {
$this->runtime->setEnvironment($input->getArgument('environment'));
$strategy = $this->runtime->guessStrategy();
$this->taskFactory = new TaskFactory($this->runtime);
$output->writeln(sprintf(' Environment: <fg=green>%s</>', $this->runtime->getEnvironment()));
$this->log(sprintf('Environment: %s', $this->runtime->getEnvironment()));
if ($this->runtime->getEnvOption('releases', false)) {
$this->runtime->generateReleaseId();
$output->writeln(sprintf(' Release ID: <fg=green>%s</>', $this->runtime->getReleaseId()));
$this->log(sprintf('Release ID: %s', $this->runtime->getReleaseId()));
}
if ($this->runtime->getConfigOption('log_file', false)) {
$output->writeln(sprintf(' Logfile: <fg=green>%s</>', $this->runtime->getConfigOption('log_file')));
}
$output->writeln(sprintf(' Strategy: <fg=green>%s</>', $strategy->getName()));
if ($input->getOption('branch') !== false) {
$this->runtime->setEnvOption('branch', $input->getOption('branch'));
}
if ($this->runtime->getEnvOption('branch', false)) {
$output->writeln(sprintf(' Branch: <fg=green>%s</>', $this->runtime->getEnvOption('branch')));
}
$output->writeln('');
$this->runDeployment($output, $strategy);
} catch (RuntimeException $exception) {
$output->writeln('');
$output->writeln(sprintf('<error>%s</error>', $exception->getMessage()));
$output->writeln('');
$this->statusCode = 7;
}
$output->writeln('Finished <fg=blue>Magallanes</>');
return $this->statusCode;
}
/**
* Run the Deployment Process
*
* @param OutputInterface $output
* @param StrategyInterface $strategy
* @throws RuntimeException
*/
protected function runDeployment(OutputInterface $output, StrategyInterface $strategy)
{
// Run "Pre Deploy" Tasks
$this->runtime->setStage(Runtime::PRE_DEPLOY);
if (!$this->runTasks($output, $strategy->getPreDeployTasks())) {
throw $this->getException();
}
// Run "On Deploy" Tasks
$this->runtime->setStage(Runtime::ON_DEPLOY);
$this->runOnHosts($output, $strategy->getOnDeployTasks());
// Run "On Release" Tasks
$this->runtime->setStage(Runtime::ON_RELEASE);
$this->runOnHosts($output, $strategy->getOnReleaseTasks());
// Run "Post Release" Tasks
$this->runtime->setStage(Runtime::POST_RELEASE);
$this->runOnHosts($output, $strategy->getPostReleaseTasks());
// Run "Post Deploy" Tasks
$this->runtime->setStage(Runtime::POST_DEPLOY);
if (!$this->runTasks($output, $strategy->getPostDeployTasks())) {
throw $this->getException();
}
}
protected function runOnHosts(OutputInterface $output, $tasks)
{
$hosts = $this->runtime->getEnvOption('hosts');
if (!is_array($hosts) && !$hosts instanceof \Countable) {
$hosts = [];
}
if (count($hosts) == 0) {
$output->writeln(sprintf(' No hosts defined, skipping %s tasks', $this->getStageName()));
$output->writeln('');
return true;
}
foreach ($hosts as $host) {
$this->runtime->setWorkingHost($host);
if (!$this->runTasks($output, $tasks)) {
$this->runtime->setWorkingHost(null);
throw $this->getException();
}
$this->runtime->setWorkingHost(null);
}
}
/**
* Runs all the tasks
*
* @param OutputInterface $output
* @param $tasks
* @return bool
* @throws RuntimeException
*/
protected function runTasks(OutputInterface $output, $tasks)
{
if (count($tasks) == 0) {
$output->writeln(sprintf(' No tasks defined for <fg=black;options=bold>%s</> stage', $this->getStageName()));
$output->writeln('');
return true;
}
if ($this->runtime->getHostName() !== null) {
$output->writeln(sprintf(' Starting <fg=black;options=bold>%s</> tasks on host <fg=black;options=bold>%s</>:', $this->getStageName(), $this->runtime->getHostName()));
} else {
$output->writeln(sprintf(' Starting <fg=black;options=bold>%s</> tasks:', $this->getStageName()));
}
$totalTasks = count($tasks);
$succeededTasks = 0;
foreach ($tasks as $taskName) {
/** @var AbstractTask $task */
$task = $this->taskFactory->get($taskName);
$output->write(sprintf(' Running <fg=magenta>%s</> ... ', $task->getDescription()));
$this->log(sprintf('Running task %s (%s)', $task->getDescription(), $task->getName()));
if ($this->runtime->inRollback() && !$task instanceof ExecuteOnRollbackInterface) {
$succeededTasks++;
$output->writeln('<fg=yellow>SKIPPED</>');
$this->log(sprintf('Task %s (%s) finished with SKIPPED, it was in a Rollback', $task->getDescription(), $task->getName()));
} else {
try {
if ($task->execute()) {
$succeededTasks++;
$output->writeln('<fg=green>OK</>');
$this->log(sprintf('Task %s (%s) finished with OK', $task->getDescription(), $task->getName()));
} else {
$output->writeln('<fg=red>FAIL</>');
$this->statusCode = 180;
$this->log(sprintf('Task %s (%s) finished with FAIL', $task->getDescription(), $task->getName()));
}
} catch (SkipException $exception) {
$succeededTasks++;
$output->writeln('<fg=yellow>SKIPPED</>');
$this->log(sprintf('Task %s (%s) finished with SKIPPED, thrown SkipException', $task->getDescription(), $task->getName()));
} catch (ErrorException $exception) {
$output->writeln(sprintf('<fg=red>ERROR</> [%s]', $exception->getTrimmedMessage()));
$this->log(sprintf('Task %s (%s) finished with FAIL, with Error "%s"', $task->getDescription(), $task->getName(), $exception->getMessage()));
$this->statusCode = 190;
}
}
if ($this->statusCode !== 0) {
break;
}
}
$alertColor = 'red';
if ($succeededTasks == $totalTasks) {
$alertColor = 'green';
}
$output->writeln(sprintf(' Finished <fg=%s>%d/%d</> tasks for <fg=black;options=bold>%s</>.', $alertColor, $succeededTasks, $totalTasks, $this->getStageName()));
$output->writeln('');
return ($succeededTasks == $totalTasks);
}
/**
* Exception for halting the the current process
*/
protected function getException()
{
return new RuntimeException(sprintf('Stage "%s" did not finished successfully, halting command.', $this->getStageName()), 50);
}
}

View File

@ -0,0 +1,151 @@
<?php
/*
* This file is part of the Magallanes package.
*
* (c) Andrés Montañez <andres@andresmontanez.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Mage\Command\BuiltIn\Releases;
use Mage\Utils;
use Mage\Runtime\Exception\RuntimeException;
use Symfony\Component\Process\Process;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Output\OutputInterface;
use Mage\Command\AbstractCommand;
/**
* Command for Listing all Releases
*
* @author Andrés Montañez <andresmontanez@gmail.com>
*/
class ListCommand extends AbstractCommand
{
/**
* @var int
*/
protected $statusCode = 0;
/**
* Configure the Command
*/
protected function configure()
{
$this
->setName('releases:list')
->setDescription('List the releases on an environment')
->addArgument('environment', InputArgument::REQUIRED, 'Name of the environment to deploy to')
;
}
/**
* Execute the Command
*
* @param InputInterface $input
* @param OutputInterface $output
* @return int|mixed
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
$this->requireConfig();
$utils = new Utils();
$output->writeln('Starting <fg=blue>Magallanes</>');
$output->writeln('');
try {
$this->runtime->setEnvironment($input->getArgument('environment'));
if (!$this->runtime->getEnvOption('releases', false)) {
throw new RuntimeException('Releases are not enabled', 70);
}
$output->writeln(sprintf(' Environment: <fg=green>%s</>', $this->runtime->getEnvironment()));
$this->log(sprintf('Environment: %s', $this->runtime->getEnvironment()));
if ($this->runtime->getConfigOption('log_file', false)) {
$output->writeln(sprintf(' Logfile: <fg=green>%s</>', $this->runtime->getConfigOption('log_file')));
}
$output->writeln('');
$hosts = $this->runtime->getEnvOption('hosts');
if (!is_array($hosts) && !$hosts instanceof \Countable) {
$hosts = [];
}
if (count($hosts) == 0) {
$output->writeln('No hosts defined');
$output->writeln('');
} else {
$hostPath = rtrim($this->runtime->getEnvOption('host_path'), '/');
foreach ($hosts as $host) {
$this->runtime->setWorkingHost($host);
// Get List of Releases
$cmdListReleases = sprintf('ls -1 %s/releases', $hostPath);
/** @var Process $process */
$process = $this->runtime->runRemoteCommand($cmdListReleases, false);
if (!$process->isSuccessful()) {
throw new RuntimeException(sprintf('Unable to retrieve releases from host "%s"', $host), 80);
}
$releases = [];
if (trim($process->getOutput()) != '') {
$releases = explode(PHP_EOL, trim($process->getOutput()));
rsort($releases);
}
if (count($releases) == 0) {
$output->writeln(sprintf(' No releases available on host <fg=black;options=bold>%s</>:', $host));
} else {
// Get Current Release
$cmdCurrentRelease = sprintf('readlink -f %s/current', $hostPath);
/** @var Process $process */
$process = $this->runtime->runRemoteCommand($cmdCurrentRelease, false);
if (!$process->isSuccessful()) {
throw new RuntimeException(sprintf('Unable to retrieve current release from host "%s"', $host), 85);
}
$currentReleaseId = explode('/', trim($process->getOutput()));
$currentReleaseId = $currentReleaseId[count($currentReleaseId) - 1];
$output->writeln(sprintf(' Releases on host <fg=black;options=bold>%s</>:', $host));
foreach ($releases as $releaseId) {
$releaseDate = $utils->getReleaseDate($releaseId);
$output->write(sprintf(' Release ID: <fg=magenta>%s</> - Date: <fg=black;options=bold>%s</> [%s]',
$releaseId,
$releaseDate->format('Y-m-d H:i:s'),
$utils->getTimeDiff($releaseDate)
));
if ($releaseId == $currentReleaseId) {
$output->writeln(' <fg=red;options=bold>[current]</>');
} else {
$output->writeln('');
}
}
}
$this->runtime->setWorkingHost(null);
$output->writeln('');
}
}
} catch (RuntimeException $exception) {
$output->writeln(sprintf('<error>%s</error>', $exception->getMessage()));
$this->statusCode = $exception->getCode();
}
$output->writeln('Finished <fg=blue>Magallanes</>');
return $this->statusCode;
}
}

View File

@ -0,0 +1,140 @@
<?php
/*
* This file is part of the Magallanes package.
*
* (c) Andrés Montañez <andres@andresmontanez.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Mage\Command\BuiltIn\Releases;
use Mage\Task\TaskFactory;
use Mage\Runtime\Exception\RuntimeException;
use Symfony\Component\Process\Process;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Output\OutputInterface;
use Mage\Command\BuiltIn\DeployCommand;
/**
* Command for Rolling Back a Releases
*
* @author Andrés Montañez <andresmontanez@gmail.com>
*/
class RollbackCommand extends DeployCommand
{
/**
* @var int
*/
protected $statusCode = 0;
/**
* Configure the Command
*/
protected function configure()
{
$this
->setName('releases:rollback')
->setDescription('Rollback to a release on an environment')
->addArgument('environment', InputArgument::REQUIRED, 'Name of the environment to deploy to')
->addArgument('release', InputArgument::REQUIRED, 'The ID or the Index of the release to rollback to')
;
}
/**
* Execute the Command
*
* @param InputInterface $input
* @param OutputInterface $output
* @return int|mixed
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
$this->requireConfig();
$output->writeln('Starting <fg=blue>Magallanes</>');
$output->writeln('');
try {
$this->runtime->setEnvironment($input->getArgument('environment'));
$strategy = $this->runtime->guessStrategy();
$this->taskFactory = new TaskFactory($this->runtime);
if (!$this->runtime->getEnvOption('releases', false)) {
throw new RuntimeException('Releases are not enabled', 70);
}
$releaseToRollback = $input->getArgument('release');
if (($releaseId = $this->checkReleaseAvailability($releaseToRollback)) === false) {
throw new RuntimeException(sprintf('Release "%s" is not available on all hosts', $releaseToRollback), 72);
}
$this->runtime->setReleaseId($releaseId)->setRollback(true);
$output->writeln(sprintf(' Environment: <fg=green>%s</>', $this->runtime->getEnvironment()));
$this->log(sprintf('Environment: %s', $this->runtime->getEnvironment()));
$output->writeln(sprintf(' Rollback to Release Id: <fg=green>%s</>', $this->runtime->getReleaseId()));
$this->log(sprintf('Release ID: %s', $this->runtime->getReleaseId()));
if ($this->runtime->getConfigOption('log_file', false)) {
$output->writeln(sprintf(' Logfile: <fg=green>%s</>', $this->runtime->getConfigOption('log_file')));
}
$output->writeln(sprintf(' Strategy: <fg=green>%s</>', $strategy->getName()));
$output->writeln('');
$this->runDeployment($output, $strategy);
} catch (RuntimeException $exception) {
$output->writeln(sprintf('<error>%s</error>', $exception->getMessage()));
$this->statusCode = $exception->getCode();
}
$output->writeln('Finished <fg=blue>Magallanes</>');
return $this->statusCode;
}
/**
* Check if the provided Release ID is available in all hosts
*
* @param string $releaseToRollback Release ID
* @return bool
*/
protected function checkReleaseAvailability($releaseToRollback)
{
$hosts = $this->runtime->getEnvOption('hosts');
$hostPath = rtrim($this->runtime->getEnvOption('host_path'), '/');
$availableInHosts = 0;
foreach ($hosts as $host) {
$releases = [];
$this->runtime->setWorkingHost($host);
// Get List of Releases
$cmdListReleases = sprintf('ls -1 %s/releases', $hostPath);
/** @var Process $process */
$process = $this->runtime->runRemoteCommand($cmdListReleases, false);
if ($process->isSuccessful()) {
$releases = explode(PHP_EOL, trim($process->getOutput()));
rsort($releases);
}
if (in_array($releaseToRollback, $releases)) {
$availableInHosts++;
}
$this->runtime->setWorkingHost(null);
}
if ($availableInHosts === count($hosts)) {
return $releaseToRollback;
}
return false;
}
}

View File

@ -0,0 +1,49 @@
<?php
/*
* This file is part of the Magallanes package.
*
* (c) Andrés Montañez <andres@andresmontanez.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Mage\Command\BuiltIn;
use Mage\Mage;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Mage\Command\AbstractCommand;
/**
* Version Command, return the current version of Magallanes
*
* @author Andrés Montañez <andresmontanez@gmail.com>
*/
class VersionCommand extends AbstractCommand
{
/**
* Configure the Command
*/
protected function configure()
{
$this
->setName('version')
->setDescription('Get the version of Magallanes')
;
}
/**
* Executes the Command
*
* @param InputInterface $input
* @param OutputInterface $output
* @return int
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
$output->writeln(sprintf('Magallanes v%s [%s]', Mage::VERSION, Mage::CODENAME));
return 0;
}
}

View File

@ -0,0 +1,122 @@
<?php
/*
* This file is part of the Magallanes package.
*
* (c) Andrés Montañez <andres@andresmontanez.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Mage\Deploy\Strategy;
use Mage\Runtime\Exception\RuntimeException;
use Mage\Runtime\Runtime;
/**
* Strategy for Deployment with Releases, using Tar and SCP
*
* @author Andrés Montañez <andresmontanez@gmail.com>
*/
class ReleasesStrategy implements StrategyInterface
{
/**
* @var Runtime
*/
protected $runtime;
public function getName()
{
return 'Releases';
}
public function setRuntime(Runtime $runtime)
{
$this->runtime = $runtime;
}
public function getPreDeployTasks()
{
$this->checkStage(Runtime::PRE_DEPLOY);
$tasks = $this->runtime->getTasks();
if ($this->runtime->getBranch() && !$this->runtime->inRollback() && !in_array('git/change-branch', $tasks)) {
array_unshift($tasks, 'git/change-branch');
}
if (!$this->runtime->inRollback() && !in_array('deploy/tar/prepare', $tasks)) {
array_push($tasks, 'deploy/tar/prepare');
}
return $tasks;
}
public function getOnDeployTasks()
{
$this->checkStage(Runtime::ON_DEPLOY);
$tasks = $this->runtime->getTasks();
if (!$this->runtime->inRollback() && !in_array('deploy/tar/copy', $tasks)) {
array_unshift($tasks, 'deploy/tar/copy');
}
if (!$this->runtime->inRollback() && !in_array('deploy/release/prepare', $tasks)) {
array_unshift($tasks, 'deploy/release/prepare');
}
return $tasks;
}
public function getOnReleaseTasks()
{
$this->checkStage(Runtime::ON_RELEASE);
$tasks = $this->runtime->getTasks();
if (!in_array('deploy/release', $tasks)) {
array_unshift($tasks, 'deploy/release');
}
return $tasks;
}
public function getPostReleaseTasks()
{
$this->checkStage(Runtime::POST_RELEASE);
$tasks = $this->runtime->getTasks();
if (!in_array('deploy/release/cleanup', $tasks)) {
array_unshift($tasks, 'deploy/release/cleanup');
}
return $tasks;
}
public function getPostDeployTasks()
{
$this->checkStage(Runtime::POST_DEPLOY);
$tasks = $this->runtime->getTasks();
if (!$this->runtime->inRollback() && !in_array('deploy/tar/cleanup', $tasks)) {
array_unshift($tasks, 'deploy/tar/cleanup');
}
if ($this->runtime->getBranch() && !$this->runtime->inRollback() && !in_array('git/change-branch', $tasks)) {
array_push($tasks, 'git/change-branch');
}
return $tasks;
}
/**
* Check the runtime stage is correct
*
* @param $stage
* @throws RuntimeException
*/
private function checkStage($stage)
{
if ($this->runtime->getStage() !== $stage) {
throw new RuntimeException(sprintf('Invalid stage, got "%s" but expected "%"', $this->runtime->getStage(), $stage));
}
}
}

View File

@ -0,0 +1,96 @@
<?php
/*
* This file is part of the Magallanes package.
*
* (c) Andrés Montañez <andres@andresmontanez.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Mage\Deploy\Strategy;
use Mage\Runtime\Exception\RuntimeException;
use Mage\Runtime\Runtime;
/**
* Strategy for Deployment with Rsync
*
* @author Andrés Montañez <andresmontanez@gmail.com>
*/
class RsyncStrategy implements StrategyInterface
{
/**
* @var Runtime
*/
protected $runtime;
public function getName()
{
return 'Rsync';
}
public function setRuntime(Runtime $runtime)
{
$this->runtime = $runtime;
}
public function getPreDeployTasks()
{
$this->checkStage(Runtime::PRE_DEPLOY);
$tasks = $this->runtime->getTasks();
if ($this->runtime->getBranch() && !$this->runtime->inRollback() && !in_array('git/change-branch', $tasks)) {
array_unshift($tasks, 'git/change-branch');
}
return $tasks;
}
public function getOnDeployTasks()
{
$this->checkStage(Runtime::ON_DEPLOY);
$tasks = $this->runtime->getTasks();
if (!$this->runtime->inRollback() && !in_array('deploy/rsync', $tasks)) {
array_unshift($tasks, 'deploy/rsync');
}
return $tasks;
}
public function getOnReleaseTasks()
{
return [];
}
public function getPostReleaseTasks()
{
return [];
}
public function getPostDeployTasks()
{
$this->checkStage(Runtime::POST_DEPLOY);
$tasks = $this->runtime->getTasks();
if ($this->runtime->getBranch() && !$this->runtime->inRollback() && !in_array('git/change-branch', $tasks)) {
array_push($tasks, 'git/change-branch');
}
return $tasks;
}
/**
* Check the runtime stage is correct
*
* @param $stage
* @throws RuntimeException
*/
private function checkStage($stage)
{
if ($this->runtime->getStage() !== $stage) {
throw new RuntimeException(sprintf('Invalid stage, got "%s" but expected "%"', $this->runtime->getStage(), $stage));
}
}
}

View File

@ -0,0 +1,35 @@
<?php
/*
* This file is part of the Magallanes package.
*
* (c) Andrés Montañez <andres@andresmontanez.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Mage\Deploy\Strategy;
use Mage\Runtime\Runtime;
/**
* Interface for Deploy Strategies
*
* @author Andrés Montañez <andresmontanez@gmail.com>
*/
interface StrategyInterface
{
public function getName();
public function setRuntime(Runtime $runtime);
public function getPreDeployTasks();
public function getOnDeployTasks();
public function getOnReleaseTasks();
public function getPostReleaseTasks();
public function getPostDeployTasks();
}

22
src/Mage.php Normal file
View File

@ -0,0 +1,22 @@
<?php
/*
* This file is part of the Magallanes package.
*
* (c) Andrés Montañez <andres@andresmontanez.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Mage;
/**
* Mallaganes base class
*
* @author Andrés Montañez <andresmontanez@gmail.com>
*/
class Mage
{
const VERSION = '4.0.0';
const CODENAME = 'Discovery One';
}

143
src/MageApplication.php Normal file
View File

@ -0,0 +1,143 @@
<?php
/*
* This file is part of the Magallanes package.
*
* (c) Andrés Montañez <andres@andresmontanez.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Mage;
use Mage\Command\AbstractCommand;
use Mage\Runtime\Runtime;
use Symfony\Component\Finder\Finder;
use Symfony\Component\Finder\SplFileInfo;
use Monolog\Logger;
use Monolog\Handler\StreamHandler;
use Symfony\Component\EventDispatcher\EventDispatcher;
use Symfony\Component\Console\Event\ConsoleErrorEvent;
use Symfony\Component\Console\ConsoleEvents;
use Symfony\Component\Console\Application;
use Symfony\Component\Yaml\Parser;
use Symfony\Component\Yaml\Exception\ParseException;
use ReflectionClass;
use Mage\Runtime\Exception\RuntimeException;
/**
* The Console Application for launching the Mage command in a standalone instance
*
* @author Andrés Montañez <andresmontanez@gmail.com>
*/
class MageApplication extends Application
{
protected $runtime;
protected $file;
/**
* @param string $file The YAML file from which to read the configuration
*/
public function __construct($file)
{
parent::__construct('Magallanes', Mage::VERSION);
$this->file = $file;
$dispatcher = new EventDispatcher();
$this->setDispatcher($dispatcher);
$dispatcher->addListener(ConsoleEvents::ERROR, function (ConsoleErrorEvent $event) {
$output = $event->getOutput();
$command = $event->getCommand();
$output->writeln(sprintf('Oops, exception thrown while running command <info>%s</info>', $command->getName()));
$exitCode = $event->getExitCode();
$event->setError(new \LogicException('Caught exception', $exitCode, $event->getError()));
});
$this->runtime = $this->instantiateRuntime();
$this->loadBuiltInCommands();
}
/**
* Configure the Magallanes Application
*
* @throws RuntimeException
*/
public function configure()
{
if (!file_exists($this->file) || !is_readable($this->file)) {
throw new RuntimeException(sprintf('The file "%s" does not exists or is not readable.', $this->file));
}
try {
$parser = new Parser();
$config = $parser->parse(file_get_contents($this->file));
} catch (ParseException $exception) {
throw new RuntimeException(sprintf('Error parsing the file "%s".', $this->file));
}
if (array_key_exists('magephp', $config) && is_array($config['magephp'])) {
$logger = null;
if (array_key_exists('log_dir', $config['magephp']) && file_exists($config['magephp']['log_dir']) && is_dir($config['magephp']['log_dir'])) {
$logfile = sprintf('%s/%s.log', $config['magephp']['log_dir'], date('Ymd_His'));
$config['magephp']['log_file'] = $logfile;
$logger = new Logger('magephp');
$logger->pushHandler(new StreamHandler($logfile));
} elseif (array_key_exists('log_dir', $config['magephp']) && !is_dir($config['magephp']['log_dir'])) {
throw new RuntimeException(sprintf('The configured log_dir "%s" does not exists or is not a directory.', $config['magephp']['log_dir']));
}
$this->runtime->setConfiguration($config['magephp']);
$this->runtime->setLogger($logger);
return;
}
throw new RuntimeException(sprintf('The file "%s" does not have a valid Magallanes configuration.', $this->file));
}
/**
* Loads the BuiltIn Commands
*/
protected function loadBuiltInCommands()
{
$finder = new Finder();
$finder->files()->in(__DIR__ . '/Command/BuiltIn')->name('*Command.php');
/** @var SplFileInfo $file */
foreach ($finder as $file) {
$class = substr('\\Mage\\Command\\BuiltIn\\' . str_replace('/', '\\', $file->getRelativePathname()), 0, -4);
if (class_exists($class)) {
$reflex = new ReflectionClass($class);
if ($reflex->isInstantiable()) {
$command = new $class();
if ($command instanceof AbstractCommand) {
$command->setRuntime($this->runtime);
$this->add($command);
}
}
}
}
}
/**
* Gets the Runtime instance to use
*
* @return Runtime
*/
protected function instantiateRuntime()
{
return new Runtime();
}
/**
* Get the Runtime instance
*
* @return Runtime
*/
public function getRuntime()
{
return $this->runtime;
}
}

View File

@ -0,0 +1,22 @@
<?php
/*
* This file is part of the Magallanes package.
*
* (c) Andrés Montañez <andres@andresmontanez.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Mage\Runtime\Exception;
use Exception;
/**
* An Error occurred while running
*
* @author Andrés Montañez <andresmontanez@gmail.com>
*/
class RuntimeException extends Exception
{
}

550
src/Runtime/Runtime.php Normal file
View File

@ -0,0 +1,550 @@
<?php
/*
* This file is part of the Magallanes package.
*
* (c) Andrés Montañez <andres@andresmontanez.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Mage\Runtime;
use Mage\Deploy\Strategy\ReleasesStrategy;
use Mage\Deploy\Strategy\RsyncStrategy;
use Mage\Deploy\Strategy\StrategyInterface;
use Psr\Log\LoggerInterface;
use Psr\Log\LogLevel;
use Symfony\Component\Process\Process;
use Mage\Runtime\Exception\RuntimeException;
/**
* Runtime is a container of all run in time configuration, stages of progress, hosts being deployed, etc.
*
* @author Andrés Montañez <andresmontanez@gmail.com>
*/
class Runtime
{
const PRE_DEPLOY = 'pre-deploy';
const ON_DEPLOY = 'on-deploy';
const POST_DEPLOY = 'post-deploy';
const ON_RELEASE = 'on-release';
const POST_RELEASE = 'post-release';
/**
* @var array Magallanes configuration
*/
protected $configuration = [];
/**
* @var string|null Environment being deployed
*/
protected $environment;
/**
* @var string Stage of Deployment
*/
protected $stage;
/**
* @var string|null The host being deployed to
*/
protected $workingHost = null;
/**
* @var string|null The Release ID
*/
protected $releaseId = null;
/**
* @var array Hold a bag of variables for sharing information between tasks, if needed
*/
protected $vars = [];
/**
* @var LoggerInterface|null The logger instance
*/
protected $logger;
/**
* @var bool Indicates if a Rollback operation is in progress
*/
protected $rollback = false;
/**
* Generate the Release ID
*
* @return Runtime
*/
public function generateReleaseId()
{
$this->setReleaseId(date('YmdHis'));
return $this;
}
/**
* Sets the Release ID
*
* @param string $releaseId Release ID
* @return Runtime
*/
public function setReleaseId($releaseId)
{
$this->releaseId = $releaseId;
return $this;
}
/**
* Retrieve the current Release ID
*
* @return null|string Release ID
*/
public function getReleaseId()
{
return $this->releaseId;
}
/**
* Sets the Runtime in Rollback mode On or Off
*
* @param bool $inRollback
* @return Runtime
*/
public function setRollback($inRollback)
{
$this->rollback = $inRollback;
return $this;
}
/**
* Indicates if Runtime is in rollback
*
* @return bool
*/
public function inRollback()
{
return $this->rollback;
}
/**
* Sets a value in the Vars bag
*
* @param string $key Variable name
* @param string $value Variable value
* @return Runtime
*/
public function setVar($key, $value)
{
$this->vars[$key] = $value;
return $this;
}
/**
* Retrieve a value from the Vars bag
*
* @param string $key Variable name
* @param mixed $default Variable default value, returned if not found
* @return string
*/
public function getVar($key, $default = null)
{
if (array_key_exists($key, $this->vars)) {
return $this->vars[$key];
}
return $default;
}
/**
* Sets the Logger instance
*
* @param LoggerInterface $logger Logger instance
* @return Runtime
*/
public function setLogger(LoggerInterface $logger = null)
{
$this->logger = $logger;
return $this;
}
/**
* Sets the Magallanes Configuration to the Runtime
*
* @param array $configuration Configuration
* @return Runtime
*/
public function setConfiguration($configuration)
{
$this->configuration = $configuration;
return $this;
}
/**
* Retrieve the Configuration
*
* @return array
*/
public function getConfiguration()
{
return $this->configuration;
}
/**
* Retrieves the Configuration Option for a specific section in the configuration
*
* @param string $key Section name
* @param mixed $default Default value
* @return mixed
*/
public function getConfigOption($key, $default = null)
{
if (array_key_exists($key, $this->configuration)) {
return $this->configuration[$key];
}
return $default;
}
/**
* Returns the Configuration Option for a specific section the current Environment
*
* @param string $key Section/Parameter name
* @param mixed $default Default value
* @return mixed
*/
public function getEnvOption($key, $default = null)
{
if (!array_key_exists('environments', $this->configuration) || !is_array($this->configuration['environments'])) {
return $default;
}
if (!array_key_exists($this->environment, $this->configuration['environments'])) {
return $default;
}
if (array_key_exists($key, $this->configuration['environments'][$this->environment])) {
return $this->configuration['environments'][$this->environment][$key];
}
return $default;
}
/**
* Shortcut to get the the configuration option for a specific environment and merge it with
* the global one (environment specific overrides the global one if present).
*
* @param $key
* @param array $defaultEnv
*
* @return array
*/
public function getMergedOption($key, $defaultEnv = [])
{
$userGlobalOptions = $this->getConfigOption($key, $defaultEnv);
$userEnvOptions = $this->getEnvOption($key, $defaultEnv);
return array_merge(
(is_array($userGlobalOptions) ? $userGlobalOptions : []),
(is_array($userEnvOptions) ? $userEnvOptions : [])
);
}
/**
* Overwrites an Environment Configuration Option
*
* @param string $key
* @param mixed $value
* @return Runtime
*/
public function setEnvOption($key, $value)
{
if (array_key_exists('environments', $this->configuration) && is_array($this->configuration['environments'])) {
if (array_key_exists($this->environment, $this->configuration['environments'])) {
$this->configuration['environments'][$this->environment][$key] = $value;
}
}
return $this;
}
/**
* Sets the working Environment
*
* @param string $environment Environment name
* @return Runtime
* @throws RuntimeException
*/
public function setEnvironment($environment)
{
if (array_key_exists('environments', $this->configuration) && array_key_exists($environment, $this->configuration['environments'])) {
$this->environment = $environment;
return $this;
}
throw new RuntimeException(sprintf('The environment "%s" does not exists.', $environment), 100);
}
/**
* Returns the current working Environment
*
* @return null|string
*/
public function getEnvironment()
{
return $this->environment;
}
/**
* Sets the working stage
*
* @param string $stage Stage code
* @return Runtime
*/
public function setStage($stage)
{
$this->stage = $stage;
return $this;
}
/**
* Retrieve the current working Stage
*
* @return string
*/
public function getStage()
{
return $this->stage;
}
/**
* Retrieve the defined Tasks for the current Environment and Stage
*
* @return array
*/
public function getTasks()
{
if (!array_key_exists('environments', $this->configuration) || !is_array($this->configuration['environments'])) {
return [];
}
if (!array_key_exists($this->environment, $this->configuration['environments'])) {
return [];
}
if (array_key_exists($this->stage, $this->configuration['environments'][$this->environment])) {
if (is_array($this->configuration['environments'][$this->environment][$this->stage])) {
return $this->configuration['environments'][$this->environment][$this->stage];
}
}
return [];
}
/**
* Sets the working Host
*
* @param string $host Host name
* @return Runtime
*/
public function setWorkingHost($host)
{
$this->workingHost = $host;
return $this;
}
/**
* Retrieve the working Host
*
* @return null|string
*/
public function getWorkingHost()
{
return $this->workingHost;
}
/**
* Logs a Message into the Logger
*
* @param string $message Log message
* @param string $level Log Level
*/
public function log($message, $level = LogLevel::DEBUG)
{
if ($this->logger instanceof LoggerInterface) {
$this->logger->log($level, $message);
}
}
/**
* Executes a command, it will be run Locally or Remotely based on the working Stage
*
* @param string $cmd Command to execute
* @param int $timeout Seconds to wait
* @return Process
*/
public function runCommand($cmd, $timeout = 120)
{
switch ($this->getStage()) {
case self::ON_DEPLOY:
case self::ON_RELEASE:
case self::POST_RELEASE:
return $this->runRemoteCommand($cmd, true, $timeout);
default:
return $this->runLocalCommand($cmd, $timeout);
}
}
/**
* Execute a command locally
*
* @param string $cmd Command to execute
* @param int $timeout Seconds to wait
* @return Process
*/
public function runLocalCommand($cmd, $timeout = 120)
{
$this->log($cmd, LogLevel::INFO);
$process = Process::fromShellCommandline($cmd);
$process->setTimeout($timeout);
$process->run();
$this->log($process->getOutput(), LogLevel::DEBUG);
if (!$process->isSuccessful()) {
$this->log($process->getErrorOutput(), LogLevel::ERROR);
}
return $process;
}
/**
* Executes a command remotely, if jail is true, it will run inside the Host Path and the Release (if available)
*
* @param string $cmd Command to execute
* @param bool $jail Jail the command
* @param int $timeout Seconds to wait
* @return Process
*/
public function runRemoteCommand($cmd, $jail, $timeout = 120)
{
$user = $this->getEnvOption('user', $this->getCurrentUser());
$sudo = $this->getEnvOption('sudo', false);
$host = $this->getHostName();
$sshConfig = $this->getSSHConfig();
$cmdDelegate = $cmd;
if ($sudo === true) {
$cmdDelegate = sprintf('sudo %s', $cmd);
}
$hostPath = rtrim($this->getEnvOption('host_path'), '/');
if ($jail && $this->getReleaseId() !== null) {
$cmdDelegate = sprintf('cd %s/releases/%s && %s', $hostPath, $this->getReleaseId(), $cmdDelegate);
} elseif ($jail) {
$cmdDelegate = sprintf('cd %s && %s', $hostPath, $cmdDelegate);
}
$cmdRemote = str_replace('"', '\"', $cmdDelegate);
$cmdLocal = sprintf('ssh -p %d %s %s@%s "%s"', $sshConfig['port'], $sshConfig['flags'], $user, $host, $cmdRemote);
return $this->runLocalCommand($cmdLocal, $timeout);
}
/**
* Get the SSH configuration based on the environment
*
* @return array
*/
public function getSSHConfig()
{
$sshConfig = $this->getEnvOption('ssh', ['port' => 22, 'flags' => '-q -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no']);
if ($this->getHostPort() !== null) {
$sshConfig['port'] = $this->getHostPort();
}
if (!array_key_exists('port', $sshConfig)) {
$sshConfig['port'] = '22';
}
if (!array_key_exists('flags', $sshConfig)) {
$sshConfig['flags'] = '-q -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no';
}
return $sshConfig;
}
/**
* Get the current Host Port or default ssh port
*
* @return integer
*/
public function getHostPort()
{
$info = explode(':', $this->getWorkingHost());
return isset($info[1]) ? $info[1] : null;
}
/**
* Get the current Host Name
*
* @return string
*/
public function getHostName()
{
if (strpos($this->getWorkingHost(), ':') === false) {
return $this->getWorkingHost();
}
$info = explode(':', $this->getWorkingHost());
return $info[0];
}
/**
* Gets a Temporal File name
*
* @return string
*/
public function getTempFile()
{
return tempnam(sys_get_temp_dir(), 'mage');
}
/**
* Get the current user
*
* @return string
*/
public function getCurrentUser()
{
$userData = posix_getpwuid(posix_geteuid());
return $userData['name'];
}
/**
* Shortcut for getting Branch information
*
* @return boolean|string
*/
public function getBranch()
{
return $this->getEnvOption('branch', false);
}
/**
* Guesses the Deploy Strategy to use
*
* @return StrategyInterface
*/
public function guessStrategy()
{
$strategy = new RsyncStrategy();
if ($this->getEnvOption('releases', false)) {
$strategy = new ReleasesStrategy();
}
$strategy->setRuntime($this);
return $strategy;
}
}

89
src/Task/AbstractTask.php Normal file
View File

@ -0,0 +1,89 @@
<?php
/*
* This file is part of the Magallanes package.
*
* (c) Andrés Montañez <andres@andresmontanez.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Mage\Task;
use Mage\Runtime\Runtime;
/**
* Abstract base class for Magallanes Tasks
*
* @author Andrés Montañez <andresmontanez@gmail.com>
*/
abstract class AbstractTask
{
/**
* @var array Task custom options
*/
protected $options = [];
/**
* @var Runtime
*/
protected $runtime;
/**
* Get the Name/Code of the Task
*
* @return string
*/
abstract public function getName();
/**
* Get a short Description of the Task
*
* @return string
*/
abstract public function getDescription();
/**
* Executes the Command
*
* @return bool
*/
abstract public function execute();
/**
* Set additional Options for the Task
*
* @param array $options Options
* @return AbstractTask
*/
public function setOptions($options = [])
{
if (!is_array($options)) {
$options = [];
}
$this->options = array_merge($this->getDefaults(), $options);
return $this;
}
/**
* Set the Runtime instance
*
* @param Runtime $runtime
* @return AbstractTask
*/
public function setRuntime(Runtime $runtime)
{
$this->runtime = $runtime;
return $this;
}
/**
* Return Default options
* @return array
*/
public function getDefaults()
{
return [];
}
}

View File

@ -0,0 +1,38 @@
<?php
/*
* This file is part of the Magallanes package.
*
* (c) Andrés Montañez <andres@andresmontanez.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Mage\Task\BuiltIn\Composer;
use Mage\Task\AbstractTask;
/**
* Abstract Composer Task
*
* @author Andrés Montañez <andresmontanez@gmail.com>
*/
abstract class AbstractComposerTask extends AbstractTask
{
protected function getOptions()
{
$options = array_merge(
['path' => 'composer'],
$this->getComposerOptions(),
$this->runtime->getMergedOption('composer'),
$this->options
);
return $options;
}
protected function getComposerOptions()
{
return [];
}
}

View File

@ -0,0 +1,47 @@
<?php
/*
* This file is part of the Magallanes package.
*
* (c) Andrés Montañez <andres@andresmontanez.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Mage\Task\BuiltIn\Composer;
use Symfony\Component\Process\Process;
/**
* Composer Task - Generate Autoload
*
* @author Andrés Montañez <andresmontanez@gmail.com>
*/
class DumpAutoloadTask extends AbstractComposerTask
{
public function getName()
{
return 'composer/dump-autoload';
}
public function getDescription()
{
return '[Composer] Dump Autoload';
}
public function execute()
{
$options = $this->getOptions();
$cmd = sprintf('%s dump-autoload %s', $options['path'], $options['flags']);
/** @var Process $process */
$process = $this->runtime->runCommand(trim($cmd));
return $process->isSuccessful();
}
protected function getComposerOptions()
{
return ['flags' => '--optimize'];
}
}

View File

@ -0,0 +1,47 @@
<?php
/*
* This file is part of the Magallanes package.
*
* (c) Andrés Montañez <andres@andresmontanez.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Mage\Task\BuiltIn\Composer;
use Symfony\Component\Process\Process;
/**
* Composer Task - Install Vendors
*
* @author Andrés Montañez <andresmontanez@gmail.com>
*/
class InstallTask extends AbstractComposerTask
{
public function getName()
{
return 'composer/install';
}
public function getDescription()
{
return '[Composer] Install';
}
public function execute()
{
$options = $this->getOptions();
$cmd = sprintf('%s install %s', $options['path'], $options['flags']);
/** @var Process $process */
$process = $this->runtime->runCommand(trim($cmd), $options['timeout']);
return $process->isSuccessful();
}
protected function getComposerOptions()
{
return ['flags' => '--optimize-autoloader', 'timeout' => 120];
}
}

View File

@ -0,0 +1,86 @@
<?php
/*
* This file is part of the Magallanes package.
*
* (c) Andrés Montañez <andres@andresmontanez.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Mage\Task\BuiltIn\Composer;
use Mage\Task\Exception\SkipException;
use Symfony\Component\Process\Process;
use DateTime;
/**
* Composer Task - Self update
*
* @author Yanick Witschi <https://github.com/Toflar>
*/
class SelfUpdateTask extends AbstractComposerTask
{
public function getName()
{
return 'composer/self-update';
}
public function getDescription()
{
return '[Composer] Self Update';
}
public function execute()
{
$options = $this->getOptions();
$cmdVersion = sprintf('%s --version', $options['path']);
/** @var Process $process */
$process = $this->runtime->runCommand(trim($cmdVersion));
if (!$process->isSuccessful()) {
return false;
}
$buildDate = $this->getBuildDate($process->getOutput());
if (!$buildDate instanceof DateTime) {
return false;
}
$compareDate = $this->getCompareDate();
if ($buildDate >= $compareDate) {
throw new SkipException();
}
$cmdUpdate = sprintf('%s self-update', $options['path']);
/** @var Process $process */
$process = $this->runtime->runCommand(trim($cmdUpdate));
return $process->isSuccessful();
}
protected function getBuildDate($output)
{
$buildDate = null;
$output = explode(PHP_EOL, $output);
foreach ($output as $row) {
if (strpos($row, 'Composer version ') === 0) {
$buildDate = DateTime::createFromFormat('Y-m-d H:i:s', substr(trim($row), -19));
}
}
return $buildDate;
}
protected function getCompareDate()
{
$options = $this->getOptions();
$compareDate = new DateTime();
$compareDate->modify(sprintf('now -%d days', $options['days']));
return $compareDate;
}
protected function getComposerOptions()
{
return ['days' => 60];
}
}

View File

@ -0,0 +1,67 @@
<?php
/*
* This file is part of the Magallanes package.
*
* (c) Andrés Montañez <andres@andresmontanez.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Mage\Task\BuiltIn\Deploy\Release;
use Symfony\Component\Process\Process;
use Mage\Task\AbstractTask;
/**
* Release Task - Cleanup old releases
*
* @author Andrés Montañez <andresmontanez@gmail.com>
*/
class CleanupTask extends AbstractTask
{
public function getName()
{
return 'deploy/release/cleanup';
}
public function getDescription()
{
return '[Release] Cleaning up old Releases';
}
public function execute()
{
$hostPath = rtrim($this->runtime->getEnvOption('host_path'), '/');
$currentReleaseId = $this->runtime->getReleaseId();
$maxReleases = $this->runtime->getEnvOption('releases');
$cmdListReleases = sprintf('ls -1 %s/releases', $hostPath);
/** @var Process $process */
$process = $this->runtime->runRemoteCommand($cmdListReleases, false);
if ($process->isSuccessful()) {
$releases = $process->getOutput();
$releases = explode(PHP_EOL, trim($releases));
if (count($releases) > $maxReleases) {
sort($releases);
$releasesToDelete = array_slice($releases, 0, count($releases) - $maxReleases);
foreach ($releasesToDelete as $releaseId) {
if ($releaseId != $currentReleaseId) {
$cmdDeleteRelease = sprintf('rm -rf %s/releases/%s', $hostPath, $releaseId);
/** @var Process $process */
$process = $this->runtime->runRemoteCommand($cmdDeleteRelease, false);
if (!$process->isSuccessful()) {
return false;
}
}
}
}
return true;
}
return false;
}
}

View File

@ -0,0 +1,43 @@
<?php
/*
* This file is part of the Magallanes package.
*
* (c) Andrés Montañez <andres@andresmontanez.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Mage\Task\BuiltIn\Deploy\Release;
use Symfony\Component\Process\Process;
use Mage\Task\AbstractTask;
/**
* Release Task - Create the Release Directory
*
* @author Andrés Montañez <andresmontanez@gmail.com>
*/
class PrepareTask extends AbstractTask
{
public function getName()
{
return 'deploy/release/prepare';
}
public function getDescription()
{
return '[Release] Preparing Release';
}
public function execute()
{
$hostPath = rtrim($this->runtime->getEnvOption('host_path'), '/');
$cmdMakeDir = sprintf('mkdir -p %s/releases/%s', $hostPath, $this->runtime->getReleaseId());
/** @var Process $process */
$process = $this->runtime->runRemoteCommand($cmdMakeDir, false);
return $process->isSuccessful();
}
}

View File

@ -0,0 +1,50 @@
<?php
/*
* This file is part of the Magallanes package.
*
* (c) Andrés Montañez <andres@andresmontanez.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Mage\Task\BuiltIn\Deploy;
use Mage\Task\Exception\ErrorException;
use Mage\Task\ExecuteOnRollbackInterface;
use Symfony\Component\Process\Process;
use Mage\Task\AbstractTask;
/**
* Release Task - Create the Symlink
*
* @author Andrés Montañez <andresmontanez@gmail.com>
*/
class ReleaseTask extends AbstractTask implements ExecuteOnRollbackInterface
{
public function getName()
{
return 'deploy/release';
}
public function getDescription()
{
return '[Release] Creating Symlink';
}
public function execute()
{
if (!$this->runtime->getEnvOption('releases', false)) {
throw new ErrorException('This task is only available with releases enabled', 40);
}
$hostPath = rtrim($this->runtime->getEnvOption('host_path'), '/');
$releaseId = $this->runtime->getReleaseId();
$cmdLinkRelease = sprintf('cd %s && ln -snf releases/%s current', $hostPath, $releaseId);
/** @var Process $process */
$process = $this->runtime->runRemoteCommand($cmdLinkRelease, false, null);
return $process->isSuccessful();
}
}

View File

@ -0,0 +1,67 @@
<?php
/*
* This file is part of the Magallanes package.
*
* (c) Andrés Montañez <andres@andresmontanez.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Mage\Task\BuiltIn\Deploy;
use Mage\Task\Exception\ErrorException;
use Symfony\Component\Process\Process;
use Mage\Task\AbstractTask;
/**
* Rsync Task - Copy files with Rsync
*
* @author Andrés Montañez <andresmontanez@gmail.com>
*/
class RsyncTask extends AbstractTask
{
public function getName()
{
return 'deploy/rsync';
}
public function getDescription()
{
return '[Deploy] Copying files with Rsync';
}
public function execute()
{
$flags = $this->runtime->getEnvOption('rsync', '-avz');
$sshConfig = $this->runtime->getSSHConfig();
$user = $this->runtime->getEnvOption('user', $this->runtime->getCurrentUser());
$host = $this->runtime->getHostName();
$hostPath = rtrim($this->runtime->getEnvOption('host_path'), '/');
$targetDir = rtrim($hostPath, '/');
if ($this->runtime->getEnvOption('releases', false)) {
throw new ErrorException('Can\'t be used with Releases, use "deploy/tar/copy"');
}
$excludes = $this->getExcludes();
$from = $this->runtime->getEnvOption('from', './');
$cmdRsync = sprintf('rsync -e "ssh -p %d %s" %s %s %s %s@%s:%s', $sshConfig['port'], $sshConfig['flags'], $flags, $excludes, $from, $user, $host, $targetDir);
/** @var Process $process */
$process = $this->runtime->runLocalCommand($cmdRsync, null);
return $process->isSuccessful();
}
protected function getExcludes()
{
$excludes = $this->runtime->getMergedOption('exclude', []);
$excludes = array_merge(['.git'], array_filter($excludes));
foreach ($excludes as &$exclude) {
$exclude = '--exclude=' . $exclude;
}
return implode(' ', $excludes);
}
}

View File

@ -0,0 +1,48 @@
<?php
/*
* This file is part of the Magallanes package.
*
* (c) Andrés Montañez <andres@andresmontanez.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Mage\Task\BuiltIn\Deploy\Tar;
use Mage\Task\Exception\ErrorException;
use Symfony\Component\Process\Process;
use Mage\Task\AbstractTask;
/**
* Tar Task - Delete temporal Tar
*
* @author Andrés Montañez <andresmontanez@gmail.com>
*/
class CleanupTask extends AbstractTask
{
public function getName()
{
return 'deploy/tar/cleanup';
}
public function getDescription()
{
return '[Deploy] Cleanup Tar file';
}
public function execute()
{
if (!$this->runtime->getEnvOption('releases', false)) {
throw new ErrorException('This task is only available with releases enabled', 40);
}
$tarLocal = $this->runtime->getVar('tar_local');
$cmdDeleteTar = sprintf('rm %s', $tarLocal);
/** @var Process $process */
$process = $this->runtime->runLocalCommand($cmdDeleteTar);
return $process->isSuccessful();
}
}

View File

@ -0,0 +1,69 @@
<?php
/*
* This file is part of the Magallanes package.
*
* (c) Andrés Montañez <andres@andresmontanez.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Mage\Task\BuiltIn\Deploy\Tar;
use Mage\Task\Exception\ErrorException;
use Symfony\Component\Process\Process;
use Mage\Task\AbstractTask;
/**
* Tar Task - Copy Tar
*
* @author Andrés Montañez <andresmontanez@gmail.com>
*/
class CopyTask extends AbstractTask
{
public function getName()
{
return 'deploy/tar/copy';
}
public function getDescription()
{
return '[Deploy] Copying files with Tar';
}
public function execute()
{
if (!$this->runtime->getEnvOption('releases', false)) {
throw new ErrorException('This task is only available with releases enabled', 40);
}
$user = $this->runtime->getEnvOption('user', $this->runtime->getCurrentUser());
$host = $this->runtime->getHostName();
$sshConfig = $sshConfig = $this->runtime->getSSHConfig();
$hostPath = rtrim($this->runtime->getEnvOption('host_path'), '/');
$currentReleaseId = $this->runtime->getReleaseId();
$tarPath = $this->runtime->getEnvOption('tar_extract_path', 'tar');
$flags = $this->runtime->getEnvOption('tar_extract', 'xfzop');
$targetDir = sprintf('%s/releases/%s', $hostPath, $currentReleaseId);
$tarLocal = $this->runtime->getVar('tar_local');
$tarRemote = basename($tarLocal);
$cmdCopy = sprintf('scp -P %d %s %s %s@%s:%s/%s', $sshConfig['port'], $sshConfig['flags'], $tarLocal, $user, $host, $targetDir, $tarRemote);
/** @var Process $process */
$process = $this->runtime->runLocalCommand($cmdCopy, 300);
if ($process->isSuccessful()) {
$cmdUnTar = sprintf('cd %s && %s %s %s', $targetDir, $tarPath, $flags, $tarRemote);
$process = $this->runtime->runRemoteCommand($cmdUnTar, false, 600);
if ($process->isSuccessful()) {
$cmdDelete = sprintf('rm %s/%s', $targetDir, $tarRemote);
$process = $this->runtime->runRemoteCommand($cmdDelete, false);
return $process->isSuccessful();
}
}
return false;
}
}

View File

@ -0,0 +1,65 @@
<?php
/*
* This file is part of the Magallanes package.
*
* (c) Andrés Montañez <andres@andresmontanez.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Mage\Task\BuiltIn\Deploy\Tar;
use Mage\Task\Exception\ErrorException;
use Symfony\Component\Process\Process;
use Mage\Task\AbstractTask;
/**
* Tar Task - Create temporal Tar
*
* @author Andrés Montañez <andresmontanez@gmail.com>
*/
class PrepareTask extends AbstractTask
{
public function getName()
{
return 'deploy/tar/prepare';
}
public function getDescription()
{
return '[Deploy] Preparing Tar file';
}
public function execute()
{
if (!$this->runtime->getEnvOption('releases', false)) {
throw new ErrorException('This task is only available with releases enabled', 40);
}
$tarLocal = $this->runtime->getTempFile();
$this->runtime->setVar('tar_local', $tarLocal);
$excludes = $this->getExcludes();
$tarPath = $this->runtime->getEnvOption('tar_create_path', 'tar');
$flags = $this->runtime->getEnvOption('tar_create', 'cfzp');
$from = $this->runtime->getEnvOption('from', './');
$cmdTar = sprintf('%s %s %s %s %s', $tarPath, $flags, $tarLocal, $excludes, $from);
/** @var Process $process */
$process = $this->runtime->runLocalCommand($cmdTar, 300);
return $process->isSuccessful();
}
protected function getExcludes()
{
$excludes = $this->runtime->getMergedOption('exclude', []);
$excludes = array_merge(['.git'], array_filter($excludes));
foreach ($excludes as &$exclude) {
$exclude = '--exclude="' . $exclude . '"';
}
return implode(' ', $excludes);
}
}

View File

@ -0,0 +1,76 @@
<?php
/*
* This file is part of the Magallanes package.
*
* (c) Andrés Montañez <andres@andresmontanez.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Mage\Task\BuiltIn;
use Mage\Task\Exception\ErrorException;
use Mage\Task\AbstractTask;
use Symfony\Component\Process\Process;
/**
* Exec task. Allows you to execute arbitrary commands.
*
* @author Yanick Witschi <https://github.com/Toflar>
*/
class ExecTask extends AbstractTask
{
/**
* @return string
*/
public function getName()
{
return 'exec';
}
/**
* @return string
*/
public function getDescription()
{
$options = $this->getOptions();
if ($options['desc']) {
return '[Exec] ' . $options['desc'];
}
return '[Exec] Custom command';
}
/**
* @return bool
*
* @throws ErrorException
*/
public function execute()
{
$options = $this->getOptions();
if (!$options['cmd']) {
throw new ErrorException('Parameter "cmd" is not defined');
}
/** @var Process $process */
$process = $this->runtime->runCommand($options['cmd'], $options['timeout']);
return $process->isSuccessful();
}
/**
* @return array
*/
protected function getOptions()
{
$options = array_merge(
['cmd' => '', 'desc' => '', 'timeout' => 120],
$this->options
);
return $options;
}
}

View File

@ -0,0 +1,79 @@
<?php
/*
* This file is part of the Magallanes package.
*
* (c) Andrés Montañez <andres@andresmontanez.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Mage\Task\BuiltIn\FS;
use Mage\Task\Exception\ErrorException;
use Mage\Task\AbstractTask;
/**
* File System Task - Abstract Base class for File Operations
*
* @author Andrés Montañez <andresmontanez@gmail.com>
*/
abstract class AbstractFileTask extends AbstractTask
{
/**
* Returns the Task options
*
* @return array
* @throws ErrorException
*/
protected function getOptions()
{
$mandatory = $this->getParameters();
$defaults = array_keys($this->getDefaults());
$missing = array_diff($mandatory, $defaults);
foreach ($missing as $parameter) {
if (!array_key_exists($parameter, $this->options)) {
throw new ErrorException(sprintf('Parameter "%s" is not defined', $parameter));
}
}
return $this->options;
}
/**
* Returns the mandatory parameters
*
* @return array
*/
abstract protected function getParameters();
/**
* Returns a file with the placeholders replaced
*
* @param string $file
* @return string
* @throws ErrorException
*/
protected function getFile($file)
{
$mapping = [
'%environment%' => $this->runtime->getEnvironment(),
];
if ($this->runtime->getHostName() !== null) {
$mapping['%host%'] = $this->runtime->getHostName();
}
if ($this->runtime->getReleaseId() !== null) {
$mapping['%release%'] = $this->runtime->getReleaseId();
}
$options = $this->getOptions();
return str_replace(
array_keys($mapping),
array_values($mapping),
$options[$file]
);
}
}

View File

@ -0,0 +1,60 @@
<?php
/*
* This file is part of the Magallanes package.
*
* (c) Andrés Montañez <andres@andresmontanez.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Mage\Task\BuiltIn\FS;
use Symfony\Component\Process\Process;
use Exception;
/**
* File System Task - Copy a File
*
* @author Marian Bäuerle
*/
class ChangeModeTask extends AbstractFileTask
{
public function getName()
{
return 'fs/chmod';
}
public function getDescription()
{
try {
return sprintf('[FS] Change mode of "%s" to "%s"', $this->getFile('file'), $this->options['mode']);
} catch (Exception $exception) {
return '[FS] Chmod [missing parameters]';
}
}
public function execute()
{
$file = $this->getFile('file');
$mode = $this->options['mode'];
$flags = $this->options['flags'];
$cmd = sprintf('chmod %s %s "%s"', $flags, $mode, $file);
/** @var Process $process */
$process = $this->runtime->runCommand($cmd);
return $process->isSuccessful();
}
protected function getParameters()
{
return ['file', 'mode', 'flags'];
}
public function getDefaults()
{
return ['flags' => null];
}
}

View File

@ -0,0 +1,60 @@
<?php
/*
* This file is part of the Magallanes package.
*
* (c) Andrés Montañez <andres@andresmontanez.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Mage\Task\BuiltIn\FS;
use Symfony\Component\Process\Process;
use Exception;
/**
* File System Task - Copy a File
*
* @author Andrés Montañez <andresmontanez@gmail.com>
*/
class CopyTask extends AbstractFileTask
{
public function getName()
{
return 'fs/copy';
}
public function getDescription()
{
try {
return sprintf('[FS] Copy "%s" to "%s"', $this->getFile('from'), $this->getFile('to'));
} catch (Exception $exception) {
return '[FS] Copy [missing parameters]';
}
}
public function execute()
{
$copyFrom = $this->getFile('from');
$copyTo = $this->getFile('to');
$flags = $this->options['flags'];
$cmd = sprintf('cp %s "%s" "%s"', $flags, $copyFrom, $copyTo);
/** @var Process $process */
$process = $this->runtime->runCommand($cmd);
return $process->isSuccessful();
}
protected function getParameters()
{
return ['from', 'to', 'flags'];
}
public function getDefaults()
{
return ['flags' => '-p'];
}
}

View File

@ -0,0 +1,60 @@
<?php
/*
* This file is part of the Magallanes package.
*
* (c) Andrés Montañez <andres@andresmontanez.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Mage\Task\BuiltIn\FS;
use Symfony\Component\Process\Process;
use Exception;
/**
* File System Task - Symlink a File
*
* @author Andrés Montañez <andresmontanez@gmail.com>
*/
class LinkTask extends AbstractFileTask
{
public function getName()
{
return 'fs/link';
}
public function getDescription()
{
try {
return sprintf('[FS] Link "%s" to "%s"', $this->getFile('from'), $this->getFile('to'));
} catch (Exception $exception) {
return '[FS] Link [missing parameters]';
}
}
public function execute()
{
$linkFrom = $this->getFile('from');
$linkTo = $this->getFile('to');
$flags = $this->options['flags'];
$cmd = sprintf('ln %s "%s" "%s"', $flags, $linkFrom, $linkTo);
/** @var Process $process */
$process = $this->runtime->runCommand($cmd);
return $process->isSuccessful();
}
protected function getParameters()
{
return ['from', 'to', 'flags'];
}
public function getDefaults()
{
return ['flags' => '-snf'];
}
}

View File

@ -0,0 +1,60 @@
<?php
/*
* This file is part of the Magallanes package.
*
* (c) Andrés Montañez <andres@andresmontanez.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Mage\Task\BuiltIn\FS;
use Symfony\Component\Process\Process;
use Exception;
/**
* File System Task - Move a File
*
* @author Andrés Montañez <andresmontanez@gmail.com>
*/
class MoveTask extends AbstractFileTask
{
public function getName()
{
return 'fs/move';
}
public function getDescription()
{
try {
return sprintf('[FS] Move "%s" to "%s"', $this->getFile('from'), $this->getFile('to'));
} catch (Exception $exception) {
return '[FS] Move [missing parameters]';
}
}
public function execute()
{
$moveFrom = $this->getFile('from');
$moveTo = $this->getFile('to');
$flags = $this->options['flags'];
$cmd = sprintf('mv %s "%s" "%s"', $flags, $moveFrom, $moveTo);
/** @var Process $process */
$process = $this->runtime->runCommand($cmd);
return $process->isSuccessful();
}
protected function getParameters()
{
return ['from', 'to', 'flags'];
}
public function getDefaults()
{
return ['flags' => null];
}
}

View File

@ -0,0 +1,59 @@
<?php
/*
* This file is part of the Magallanes package.
*
* (c) Andrés Montañez <andres@andresmontanez.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Mage\Task\BuiltIn\FS;
use Symfony\Component\Process\Process;
use Exception;
/**
* File System Task - Remove a File
*
* @author Andrés Montañez <andresmontanez@gmail.com>
*/
class RemoveTask extends AbstractFileTask
{
public function getName()
{
return 'fs/remove';
}
public function getDescription()
{
try {
return sprintf('[FS] Remove "%s"', $this->getFile('file'));
} catch (Exception $exception) {
return '[FS] Remove [missing parameters]';
}
}
public function execute()
{
$file = $this->getFile('file');
$flags = $this->options['flags'];
$cmd = sprintf('rm %s "%s"', $flags, $file);
/** @var Process $process */
$process = $this->runtime->runCommand($cmd);
return $process->isSuccessful();
}
protected function getParameters()
{
return ['file', 'flags'];
}
public function getDefaults()
{
return ['flags' => null];
}
}

View File

@ -0,0 +1,81 @@
<?php
/*
* This file is part of the Magallanes package.
*
* (c) Andrés Montañez <andres@andresmontanez.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Mage\Task\BuiltIn\Git;
use Mage\Task\Exception\SkipException;
use Symfony\Component\Process\Process;
use Mage\Task\AbstractTask;
/**
* Git Task - Checkout Branch
*
* @author Andrés Montañez <andresmontanez@gmail.com>
*/
class ChangeBranchTask extends AbstractTask
{
public function getName()
{
return 'git/change-branch';
}
public function getDescription()
{
$options = $this->getOptions();
$branch = $options['branch'];
if ($this->runtime->getVar('git_revert_branch', false)) {
$branch = $this->runtime->getVar('git_revert_branch');
}
return sprintf('[Git] Change Branch (%s)', $branch);
}
public function execute()
{
$options = $this->getOptions();
$branch = $this->runtime->getVar('git_revert_branch', false);
if ($branch === false) {
$cmdGetCurrent = sprintf('%s branch | grep "*"', $options['path']);
/** @var Process $process */
$process = $this->runtime->runLocalCommand($cmdGetCurrent);
if (!$process->isSuccessful()) {
return false;
}
$currentBranch = str_replace('* ', '', trim($process->getOutput()));
if ($currentBranch == $options['branch']) {
throw new SkipException();
}
$branch = $options['branch'];
$this->runtime->setVar('git_revert_branch', $currentBranch);
}
$cmdChange = sprintf('%s checkout %s', $options['path'], $branch);
/** @var Process $process */
$process = $this->runtime->runLocalCommand($cmdChange);
return $process->isSuccessful();
}
protected function getOptions()
{
$branch = $this->runtime->getEnvOption('branch', 'master');
$options = array_merge(
['path' => 'git', 'branch' => $branch],
$this->options
);
return $options;
}
}

View File

@ -0,0 +1,54 @@
<?php
/*
* This file is part of the Magallanes package.
*
* (c) Andrés Montañez <andres@andresmontanez.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Mage\Task\BuiltIn\Git;
use Symfony\Component\Process\Process;
use Mage\Task\AbstractTask;
/**
* Git Task - Pull
*
* @author Andrés Montañez <andresmontanez@gmail.com>
*/
class UpdateTask extends AbstractTask
{
public function getName()
{
return 'git/update';
}
public function getDescription()
{
return '[Git] Update';
}
public function execute()
{
$options = $this->getOptions();
$command = $options['path'] . ' pull';
/** @var Process $process */
$process = $this->runtime->runLocalCommand($command);
return $process->isSuccessful();
}
protected function getOptions()
{
$branch = $this->runtime->getEnvOption('branch', 'master');
$options = array_merge(
['path' => 'git', 'branch' => $branch],
$this->options
);
return $options;
}
}

Some files were not shown because too many files have changed in this diff Show More