Browse Source

Added functionality for adding new environments.

Changed feedback and controls of who tasks are run.
1.0
Andrs Montaez 13 years ago
parent
commit
6d5e7ef22f
  1. 8
      Mage/Config.php
  2. 25
      Mage/Console.php
  3. 37
      Mage/Task/Add.php
  4. 32
      Mage/Task/Deploy.php
  5. 6
      bin/mage.php

8
Mage/Config.php

@ -29,7 +29,13 @@ class Mage_Config
public function getHosts()
{
$config = $this->getEnvironment();
return $config['hosts'];
$hosts = array();
if (isset($config['hosts'])) {
$hosts = (array) $config['hosts'];
}
return $hosts;
}
public function getTasks($type = 'tasks')

25
Mage/Console.php

@ -14,14 +14,18 @@ class Mage_Console
public function parse()
{
foreach ($this->_args as $argument) {
if ($argument == 'deploy') {
if ($this->_args[0] == 'deploy') {
$this->_action = 'deploy';
} else if ($argument == 'update') {
} else if ($this->_args[0] == 'update') {
$this->_action = 'update';
} else if (preg_match('/to:[\w]+/i', $argument)) {
} else if ($this->_args[0] == 'add') {
$this->_action = 'add';
}
foreach ($this->_args as $argument) {
if (preg_match('/to:[\w]+/i', $argument)) {
$this->_environment = str_replace('to:', '', $argument);
}
}
@ -37,11 +41,11 @@ class Mage_Console
return $this->_environment;
}
public static function output($message, $tabs = 1, $newLine = true)
public static function output($message, $tabs = 1, $newLine = 1)
{
$output = str_repeat("\t", $tabs)
. Mage_Console_Colors::color($message)
. ($newLine ? PHP_EOL : '');
. str_repeat(PHP_EOL, $newLine);
echo $output;
}
@ -71,6 +75,15 @@ class Mage_Console
$task = new Mage_Task_Update;
$task->run($config);
break;
case 'add';
switch ($this->_args[1]) {
case 'environment':
$task = new Mage_Task_Add;
$task->environment($this->_args[2]);
break;
}
break;
}
}
}

37
Mage/Task/Add.php

@ -0,0 +1,37 @@
<?php
class Mage_Task_Add
{
public function environment($environmentName)
{
$environmentName = strtolower($environmentName);
$environmentConfigFile = '.mage/config/environment/' . $environmentName . '.yaml';
Mage_Console::output('Adding new environment: <dark_gray>' . $environmentName . '</dark_gray>');
// Check if there is already an environment with the same name
if (file_exists($environmentConfigFile)) {
Mage_Console::output('<light_red>Error!!</light_red> Already exists an environment called <dark_gray>' . $environmentName . '</dark_gray>');
} else {
$baseConfig = '#' . $environmentName . PHP_EOL
. 'user: dummy' . PHP_EOL
. 'deploy-from: ./' . PHP_EOL
. 'deploy-to: /var/www/vhosts/example.com/www' . PHP_EOL
. 'hosts:' . PHP_EOL
. 'pre-tasks:' . PHP_EOL
. 'tasks:' . PHP_EOL
. ' - deployment/rsync' . PHP_EOL
. 'post-tasks:' . 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);
} else {
Mage_Console::output('<light_red>Error!!</light_red> Unable to create config file for environment called <dark_gray>' . $environmentName . '</dark_gray>');
}
}
Mage_Console::output('');
}
}

32
Mage/Task/Deploy.php

@ -11,14 +11,26 @@ class Mage_Task_Deploy
$this->_runNonDeploymentTasks('pre', $config);
// Run Tasks for Deployment
foreach ($config->getHosts() as $host) {
$hosts = $config->getHosts();
if (count($hosts) == 0) {
Mage_Console::output('<light_purple>Warning!</light_purple> <dark_gray>No hosts defined, skipping deployment tasks.</dark_gray>', 1, 3);
} else {
foreach ($hosts as $host) {
$taskConfig = $config->getConfig($host);
$tasks = 0;
$completedTasks = 0;
Mage_Console::output('Deploying to <dark_gray>' . $host . '</dark_gray>');
foreach ($config->getTasks() as $taskName) {
$tasksToRun = $config->getTasks();
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 $taskName) {
$tasks++;
$task = Mage_Task_Factory::get($taskName, $taskConfig);
$task->init();
@ -40,7 +52,9 @@ class Mage_Task_Deploy
$tasksColor = 'red';
}
Mage_Console::output('Deployment to <dark_gray>' . $host . '</dark_gray> compted: <' . $tasksColor . '>' . $completedTasks . '/' . $tasks . '</' . $tasksColor . '> tasks done.' . PHP_EOL . PHP_EOL);
Mage_Console::output('Deployment to <dark_gray>' . $host . '</dark_gray> compted: <' . $tasksColor . '>' . $completedTasks . '/' . $tasks . '</' . $tasksColor . '> tasks done.', 1, 3);
}
}
}
// Run Post-Deployment Tasks
@ -49,13 +63,19 @@ class Mage_Task_Deploy
private function _runNonDeploymentTasks($type, Mage_Config $config)
{
$tasksToRun = $config->getTasks($type);
if (count($tasksToRun) == 0) {
Mage_Console::output('<dark_gray>No </dark_gray><light_cyan>' . ucfirst($type) . '-Deployment</light_cyan> <dark_gray>tasks defined.</dark_gray>', 1, 3);
} else {
Mage_Console::output('Starting <dark_gray>' . ucfirst($type) . '-Deployment</dark_gray> tasks:');
$taskConfig = $config->getConfig();
$tasks = 0;
$completedTasks = 0;
foreach ($config->getTasks($type) as $taskName) {
foreach ($tasksToRun as $taskName) {
$tasks++;
$task = Mage_Task_Factory::get($taskName, $taskConfig);
$task->init();
@ -77,7 +97,9 @@ class Mage_Task_Deploy
$tasksColor = 'red';
}
Mage_Console::output('Finished <dark_gray>' . ucfirst($type) . '-Deployment</dark_gray> tasks: <' . $tasksColor . '>' . $completedTasks . '/' . $tasks . '</' . $tasksColor . '> tasks done.' . PHP_EOL . PHP_EOL);
Mage_Console::output('Finished <dark_gray>' . ucfirst($type) . '-Deployment</dark_gray> tasks: <' . $tasksColor . '>' . $completedTasks . '/' . $tasks . '</' . $tasksColor . '> tasks done.', 1, 3);
}
}

6
bin/mage.php

@ -16,9 +16,7 @@ $baseDir = dirname(dirname(__FILE__));
require_once $baseDir . '/Mage/Autoload.php';
spl_autoload_register(array('Mage_Autoload', 'autoload'));
Mage_Console::output('Starting <blue>Magallanes</blue>', 0);
Mage_Console::output('');
Mage_Console::output('Starting <blue>Magallanes</blue>', 0, 2);
$console = new Mage_Console;
$console->setArgs($argv);
@ -27,4 +25,4 @@ $console->parse();
$console->run();
Mage_Console::output('Finished <blue>Magallanes</blue>', 0);
Mage_Console::output('Finished <blue>Magallanes</blue>', 0, 2);

Loading…
Cancel
Save