Skip to content

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
  • Loading branch information
andres-montanez committed Nov 1, 2014
2 parents 8524de7 + 609745c commit 5199a91
Show file tree
Hide file tree
Showing 69 changed files with 493 additions and 267 deletions.
8 changes: 2 additions & 6 deletions .gitignore
@@ -1,13 +1,9 @@
.settings
.settings/*
.project
.buildpath
.idea
vendor
mage.phar

# OS generated files # // GitHub Recommendation
######################
.DS_Store*
ehthumbs.db
Icon?
Thumbs.db
Thumbs.db
2 changes: 1 addition & 1 deletion LICENSE
Expand Up @@ -24,4 +24,4 @@ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

--------
The Yaml Library Parser is (c) by Fabien Potencier, and belongs to the Symfony Proyect
--------
--------
2 changes: 1 addition & 1 deletion LICENSE_YAML
Expand Up @@ -16,4 +16,4 @@ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
THE SOFTWARE.
2 changes: 1 addition & 1 deletion Mage/Command/AbstractCommand.php
Expand Up @@ -52,4 +52,4 @@ public function getConfig()
{
return $this->config;
}
}
}
8 changes: 4 additions & 4 deletions Mage/Command/BuiltIn/AddCommand.php
Expand Up @@ -68,7 +68,7 @@ protected function addEnvironment()
throw new Exception('The environment already exists.');
}

Console::output('Adding new environment: <dark_gray>' . $environmentName . '</dark_gray>');
Console::output('Adding new environment: <bold>' . $environmentName . '</bold>');

$releasesConfig = 'releases:' . PHP_EOL
. ' enabled: true' . PHP_EOL
Expand All @@ -93,10 +93,10 @@ protected function addEnvironment()
$result = file_put_contents($environmentConfigFile, $baseConfig);

if ($result) {
Console::output('<light_green>Success!!</light_green> Environment config file for <dark_gray>' . $environmentName . '</dark_gray> created successfully at <blue>' . $environmentConfigFile . '</blue>');
Console::output('<dark_gray>So please! Review and adjust its configuration.</dark_gray>', 2, 2);
Console::output('<light_green>Success!!</light_green> Environment config file for <bold>' . $environmentName . '</bold> created successfully at <blue>' . $environmentConfigFile . '</blue>');
Console::output('<bold>So please! Review and adjust its configuration.</bold>', 2, 2);
} else {
Console::output('<light_red>Error!!</light_red> Unable to create config file for environment called <dark_gray>' . $environmentName . '</dark_gray>', 1, 2);
Console::output('<light_red>Error!!</light_red> Unable to create config file for environment called <bold>' . $environmentName . '</bold>', 1, 2);
}
}
}
2 changes: 1 addition & 1 deletion Mage/Command/BuiltIn/CompileCommand.php
Expand Up @@ -28,7 +28,7 @@ public function run()
{
if (ini_get('phar.readonly')) {
Console::output('The <purple>php.ini</purple> variable <light_red>phar.readonly</light_red> must be <yellow>Off</yellow>.', 1, 2);
return 300;
return 200;
}

$compiler = new Compiler;
Expand Down
87 changes: 58 additions & 29 deletions Mage/Command/BuiltIn/DeployCommand.php
Expand Up @@ -3,6 +3,7 @@
* This file is part of the Magallanes package.
*
* (c) Andrés Montañez <andres@andresmontanez.com>
* (c) Alex V Kotelnikov <gudron@gudron.me>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
Expand All @@ -16,6 +17,7 @@
use Mage\Task\AbstractTask;
use Mage\Task\Releases\SkipOnOverride;
use Mage\Task\ErrorWithMessageException;
use Mage\Task\RollbackException;
use Mage\Task\SkipException;
use Mage\Console;
use Mage\Config;
Expand Down Expand Up @@ -109,20 +111,20 @@ public static function getStatus()
*/
public function run()
{
$exitCode = 1000;
$exitCode = 240;

// Check if Environment is not Locked
$lockFile = getcwd() . '/.mage/' . $this->getConfig()->getEnvironment() . '.lock';
if (file_exists($lockFile)) {
Console::output('<red>This environment is locked!</red>', 1, 2);
echo file_get_contents($lockFile);
return 1010;
return 231;
}

// Check for running instance and Lock
if (file_exists(getcwd() . '/.mage/~working.lock')) {
Console::output('<red>There is already an instance of Magallanes running!</red>', 1, 2);
return 1020;
return 230;
} else {
touch(getcwd() . '/.mage/~working.lock');
}
Expand All @@ -131,21 +133,21 @@ public function run()
$this->getConfig()->setReleaseId(date('YmdHis'));

// Deploy Summary
Console::output('<dark_gray>Deploy summary</dark_gray>', 1, 1);
Console::output('<bold>Deploy summary</bold>', 1, 1);

// Deploy Summary - Environment
Console::output('<dark_gray>Environment:</dark_gray> <purple>' . $this->getConfig()->getEnvironment() . '</purple>', 2, 1);
Console::output('<bold>Environment:</bold> <purple>' . $this->getConfig()->getEnvironment() . '</purple>', 2, 1);

// Deploy Summary - Releases
if ($this->getConfig()->release('enabled', false)) {
Console::output('<dark_gray>Release ID:</dark_gray> <purple>' . $this->getConfig()->getReleaseId() . '</purple>', 2, 1);
Console::output('<bold>Release ID:</bold> <purple>' . $this->getConfig()->getReleaseId() . '</purple>', 2, 1);
}

// Deploy Summary - SCM
if ($this->getConfig()->deployment('scm', false)) {
$scmConfig = $this->getConfig()->deployment('scm');
if (isset($scmConfig['branch'])) {
Console::output('<dark_gray>SCM Branch:</dark_gray> <purple>' . $scmConfig['branch'] . '</purple>', 2, 1);
Console::output('<bold>SCM Branch:</bold> <purple>' . $scmConfig['branch'] . '</purple>', 2, 1);
}
}

Expand All @@ -160,7 +162,7 @@ public function run()
// Check Status
if (self::$failedTasks > 0) {
self::$deployStatus = self::FAILED;
Console::output('A total of <dark_gray>' . self::$failedTasks . '</dark_gray> deployment tasks failed: <red>ABORTING</red>', 1, 2);
Console::output('A total of <bold>' . self::$failedTasks . '</bold> deployment tasks failed: <red>ABORTING</red>', 1, 2);

} else {
// Run Deployment Tasks
Expand All @@ -169,7 +171,7 @@ public function run()
// Check Status
if (self::$failedTasks > 0) {
self::$deployStatus = self::FAILED;
Console::output('A total of <dark_gray>' . self::$failedTasks . '</dark_gray> deployment tasks failed: <red>ABORTING</red>', 1, 2);
Console::output('A total of <bold>' . self::$failedTasks . '</bold> deployment tasks failed: <red>ABORTING</red>', 1, 2);
}

// Run Post-Deployment Tasks
Expand All @@ -179,15 +181,15 @@ public function run()
// Time Information Hosts
if ($this->hostsCount > 0) {
$timeTextHost = $this->transcurredTime($this->endTimeHosts - $this->startTimeHosts);
Console::output('Time for deployment: <dark_gray>' . $timeTextHost . '</dark_gray>.');
Console::output('Time for deployment: <bold>' . $timeTextHost . '</bold>.');

$timeTextPerHost = $this->transcurredTime(round(($this->endTimeHosts - $this->startTimeHosts) / $this->hostsCount));
Console::output('Average time per host: <dark_gray>' . $timeTextPerHost . '</dark_gray>.');
Console::output('Average time per host: <bold>' . $timeTextPerHost . '</bold>.');
}

// Time Information General
$timeText = $this->transcurredTime(time() - $this->startTime);
Console::output('Total time: <dark_gray>' . $timeText . '</dark_gray>.', 1, 2);
Console::output('Total time: <bold>' . $timeText . '</bold>.', 1, 2);

// Send Notifications
$this->sendNotification(self::$failedTasks > 0 ? false : true);
Expand Down Expand Up @@ -249,10 +251,10 @@ protected function runNonDeploymentTasks($stage, Config $config, $title)
}

if (count($tasksToRun) == 0) {
Console::output('<dark_gray>No </dark_gray><light_cyan>' . $title . '</light_cyan> <dark_gray>tasks defined.</dark_gray>', 1, 3);
Console::output('<bold>No </bold><light_cyan>' . $title . '</light_cyan> <bold>tasks defined.</bold>', 1, 3);

} else {
Console::output('Starting <dark_gray>' . $title . '</dark_gray> tasks:');
Console::output('Starting <bold>' . $title . '</bold> tasks:');

$tasks = 0;
$completedTasks = 0;
Expand All @@ -274,7 +276,7 @@ protected function runNonDeploymentTasks($stage, Config $config, $title)
$tasksColor = 'red';
}

Console::output('Finished <dark_gray>' . $title . '</dark_gray> tasks: <' . $tasksColor . '>' . $completedTasks . '/' . $tasks . '</' . $tasksColor . '> tasks done.', 1, 3);
Console::output('Finished <bold>' . $title . '</bold> tasks: <' . $tasksColor . '>' . $completedTasks . '/' . $tasks . '</' . $tasksColor . '> tasks done.', 1, 3);
}
}

Expand All @@ -290,7 +292,7 @@ protected function runDeploymentTasks()
self::$failedTasks = 0;

if ($this->hostsCount == 0) {
Console::output('<light_purple>Warning!</light_purple> <dark_gray>No hosts defined, skipping deployment tasks.</dark_gray>', 1, 3);
Console::output('<light_purple>Warning!</light_purple> <bold>No hosts defined, skipping deployment tasks.</bold>', 1, 3);

} else {
$this->startTimeHosts = time();
Expand All @@ -311,7 +313,7 @@ protected function runDeploymentTasks()
$tasks = 0;
$completedTasks = 0;

Console::output('Deploying to <dark_gray>' . $this->getConfig()->getHost() . '</dark_gray>');
Console::output('Deploying to <bold>' . $this->getConfig()->getHost() . '</bold>');

$tasksToRun = $this->getConfig()->getTasks();

Expand All @@ -320,8 +322,8 @@ protected function runDeploymentTasks()
array_unshift($tasksToRun, $deployStrategy);

if (count($tasksToRun) == 0) {
Console::output('<light_purple>Warning!</light_purple> <dark_gray>No </dark_gray><light_cyan>Deployment</light_cyan> <dark_gray>tasks defined.</dark_gray>', 2);
Console::output('Deployment to <dark_gray>' . $host . '</dark_gray> skipped!', 1, 3);
Console::output('<light_purple>Warning!</light_purple> <bold>No </bold><light_cyan>Deployment</light_cyan> <bold>tasks defined.</bold>', 2);
Console::output('Deployment to <bold>' . $host . '</bold> skipped!', 1, 3);

} else {
foreach ($tasksToRun as $taskData) {
Expand All @@ -341,7 +343,7 @@ protected function runDeploymentTasks()
$tasksColor = 'red';
}

Console::output('Deployment to <dark_gray>' . $this->getConfig()->getHost() . '</dark_gray> completed: <' . $tasksColor . '>' . $completedTasks . '/' . $tasks . '</' . $tasksColor . '> tasks done.', 1, 3);
Console::output('Deployment to <bold>' . $this->getConfig()->getHost() . '</bold> completed: <' . $tasksColor . '>' . $completedTasks . '/' . $tasks . '</' . $tasksColor . '> tasks done.', 1, 3);
}

// Reset Host Config
Expand All @@ -356,9 +358,9 @@ protected function runDeploymentTasks()
}

// Releasing
if (self::$deployStatus == self::SUCCEDED && $this->getConfig()->release('enabled', false) == true) {
if (self::$deployStatus == self::SUCCEDED && $this->getConfig()->release('enabled', false) === true) {
// Execute the Releases
Console::output('Starting the <dark_gray>Releasing</dark_gray>');
Console::output('Starting the <bold>Releasing</bold>');
$completedTasks = 0;
foreach ($hosts as $hostKey => $host) {

Expand All @@ -382,7 +384,7 @@ protected function runDeploymentTasks()
// Reset Host Config
$this->getConfig()->setHostConfig(null);
}
Console::output('Finished the <dark_gray>Releasing</dark_gray>', 1, 3);
Console::output('Finished the <bold>Releasing</bold>', 1, 3);

// Execute the Post-Release Tasks
foreach ($hosts as $hostKey => $host) {
Expand All @@ -403,7 +405,7 @@ protected function runDeploymentTasks()
$completedTasks = 0;

if (count($tasksToRun) > 0) {
Console::output('Starting <dark_gray>Post-Release</dark_gray> tasks for <dark_gray>' . $host . '</dark_gray>:');
Console::output('Starting <bold>Post-Release</bold> tasks for <bold>' . $host . '</bold>:');

foreach ($tasksToRun as $task) {
$task = Factory::get($task, $this->getConfig(), false, AbstractTask::STAGE_POST_RELEASE);
Expand All @@ -418,7 +420,7 @@ protected function runDeploymentTasks()
} else {
$tasksColor = 'red';
}
Console::output('Finished <dark_gray>Post-Release</dark_gray> tasks for <dark_gray>' . $host . '</dark_gray>: <' . $tasksColor . '>' . $completedTasks . '/' . $tasks . '</' . $tasksColor . '> tasks done.', 1, 3);
Console::output('Finished <bold>Post-Release</bold> tasks for <bold>' . $host . '</bold>: <' . $tasksColor . '>' . $completedTasks . '/' . $tasks . '</' . $tasksColor . '> tasks done.', 1, 3);
}

// Reset Host Config
Expand All @@ -428,6 +430,28 @@ protected function runDeploymentTasks()
}
}

protected function runRollbackTask(){
$this->getConfig()->reload();
$hosts = $this->getConfig()->getHosts();

if (count($hosts) == 0) {
Console::output('<light_purple>Warning!</light_purple> <bold>No hosts defined, unable to get releases.</bold>', 1, 3);

} else {
$result = true;
foreach ($hosts as $host) {
$this->getConfig()->setHost($host);

$this->getConfig()->setReleaseId(-1);
$task = Factory::get('releases/rollback', $this->getConfig());
$task->init();
$result = $task->run() && $result;
}
return $result;
}
return false;
}

/**
* Runs a Task
*
Expand All @@ -439,7 +463,7 @@ protected function runTask(AbstractTask $task, $title = null)
{
$task->init();

if ($title == null) {
if ($title === null) {
$title = 'Running <purple>' . $task->getName() . '</purple> ... ';
}
Console::output($title, 2, 0);
Expand All @@ -449,18 +473,23 @@ protected function runTask(AbstractTask $task, $title = null)
$runTask = false;
}

if ($runTask == true) {
if ($runTask === true) {
try {
$result = $task->run();

if ($result == true) {
if ($result === true) {
Console::output('<green>OK</green>', 0);
$result = true;

} else {
Console::output('<red>FAIL</red>', 0);
$result = false;
}
} catch (RollbackException $e) {
Console::output('<red>FAIL, Rollback started</red> [Message: ' . $e->getMessage() . ']', 0);
$this->runRollbackTask();
$result = false;

} catch (ErrorWithMessageException $e) {
Console::output('<red>FAIL</red> [Message: ' . $e->getMessage() . ']', 0);
$result = false;
Expand Down Expand Up @@ -564,7 +593,7 @@ protected function chooseDeployStrategy()

case self::DEPLOY_STRATEGY_GUESS:
default:
if ($this->getConfig()->release('enabled', false) == true) {
if ($this->getConfig()->release('enabled', false) === true) {
$deployStrategy = 'deployment/strategy/tar-gz';
} else {
$deployStrategy = 'deployment/strategy/rsync';
Expand Down
8 changes: 4 additions & 4 deletions Mage/Command/BuiltIn/InitCommand.php
Expand Up @@ -30,11 +30,11 @@ public function run()
$exitCode = 50;
$configDir = getcwd() . '/.mage';

Console::output('Initiating managing process for application with <dark_gray>Magallanes</dark_gray>');
Console::output('Initiating managing process for application with <bold>Magallanes</bold>');

// Check if there is already a config dir
if (file_exists($configDir)) {
Console::output('<light_red>Error!!</light_red> Already exists <dark_gray>.mage</dark_gray> directory.', 1, 2);
Console::output('<light_red>Error!!</light_red> Already exists <bold>.mage</bold> directory.', 1, 2);
} else {
$results = array();
$results[] = mkdir($configDir);
Expand All @@ -48,8 +48,8 @@ public function run()
$results[] = file_put_contents($configDir . '/config/general.yml', $this->getGeneralConfig());

if (!in_array(false, $results)) {
Console::output('<light_green>Success!!</light_green> The configuration for <dark_gray>Magallanes</dark_gray> has been generated at <blue>.mage</blue> directory.');
Console::output('<dark_gray>Please!! Review and adjust the configuration.</dark_gray>', 2, 2);
Console::output('<light_green>Success!!</light_green> The configuration for <bold>Magallanes</bold> has been generated at <blue>.mage</blue> directory.');
Console::output('<bold>Please!! Review and adjust the configuration.</bold>', 2, 2);
$exitCode = 0;

} else {
Expand Down
4 changes: 2 additions & 2 deletions Mage/Command/BuiltIn/InstallCommand.php
Expand Up @@ -27,7 +27,7 @@ class InstallCommand extends AbstractCommand
public function run()
{
$exitCode = 88;
Console::output('Installing <dark_gray>Magallanes</dark_gray>... ', 1, 0);
Console::output('Installing <bold>Magallanes</bold>... ', 1, 0);

// Vars
$installDir = $this->getConfig()->getParameter('installDir', '/opt/magallanes');
Expand Down Expand Up @@ -122,4 +122,4 @@ protected function recursiveCopy($from, $to)
return false;
}
}
}
}

0 comments on commit 5199a91

Please sign in to comment.