mirror of
https://github.com/hauke68/Magallanes.git
synced 2025-09-13 12:40:18 +02:00
Added functionality for adding new environments.
Changed feedback and controls of who tasks are run.
This commit is contained in:
parent
80c9b723b1
commit
6d5e7ef22f
@ -29,7 +29,13 @@ class Mage_Config
|
|||||||
public function getHosts()
|
public function getHosts()
|
||||||
{
|
{
|
||||||
$config = $this->getEnvironment();
|
$config = $this->getEnvironment();
|
||||||
return $config['hosts'];
|
$hosts = array();
|
||||||
|
|
||||||
|
if (isset($config['hosts'])) {
|
||||||
|
$hosts = (array) $config['hosts'];
|
||||||
|
}
|
||||||
|
|
||||||
|
return $hosts;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getTasks($type = 'tasks')
|
public function getTasks($type = 'tasks')
|
||||||
|
@ -14,14 +14,18 @@ class Mage_Console
|
|||||||
|
|
||||||
public function parse()
|
public function parse()
|
||||||
{
|
{
|
||||||
foreach ($this->_args as $argument) {
|
if ($this->_args[0] == 'deploy') {
|
||||||
if ($argument == 'deploy') {
|
|
||||||
$this->_action = 'deploy';
|
$this->_action = 'deploy';
|
||||||
|
|
||||||
} else if ($argument == 'update') {
|
} else if ($this->_args[0] == 'update') {
|
||||||
$this->_action = '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);
|
$this->_environment = str_replace('to:', '', $argument);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -37,11 +41,11 @@ class Mage_Console
|
|||||||
return $this->_environment;
|
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)
|
$output = str_repeat("\t", $tabs)
|
||||||
. Mage_Console_Colors::color($message)
|
. Mage_Console_Colors::color($message)
|
||||||
. ($newLine ? PHP_EOL : '');
|
. str_repeat(PHP_EOL, $newLine);
|
||||||
|
|
||||||
echo $output;
|
echo $output;
|
||||||
}
|
}
|
||||||
@ -71,6 +75,15 @@ class Mage_Console
|
|||||||
$task = new Mage_Task_Update;
|
$task = new Mage_Task_Update;
|
||||||
$task->run($config);
|
$task->run($config);
|
||||||
break;
|
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
Normal file
37
Mage/Task/Add.php
Normal file
@ -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('');
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -11,14 +11,71 @@ class Mage_Task_Deploy
|
|||||||
$this->_runNonDeploymentTasks('pre', $config);
|
$this->_runNonDeploymentTasks('pre', $config);
|
||||||
|
|
||||||
// Run Tasks for Deployment
|
// Run Tasks for Deployment
|
||||||
foreach ($config->getHosts() as $host) {
|
$hosts = $config->getHosts();
|
||||||
$taskConfig = $config->getConfig($host);
|
|
||||||
|
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>');
|
||||||
|
|
||||||
|
$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();
|
||||||
|
|
||||||
|
Mage_Console::output('Running <purple>' . $task->getName() . '</purple> ... ', 2, false);
|
||||||
|
$result = $task->run();
|
||||||
|
|
||||||
|
if ($result == true) {
|
||||||
|
Mage_Console::output('<green>OK</green>');
|
||||||
|
$completedTasks++;
|
||||||
|
} else {
|
||||||
|
Mage_Console::output('<red>FAIL</red>');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($completedTasks == $tasks) {
|
||||||
|
$tasksColor = 'green';
|
||||||
|
} else {
|
||||||
|
$tasksColor = 'red';
|
||||||
|
}
|
||||||
|
|
||||||
|
Mage_Console::output('Deployment to <dark_gray>' . $host . '</dark_gray> compted: <' . $tasksColor . '>' . $completedTasks . '/' . $tasks . '</' . $tasksColor . '> tasks done.', 1, 3);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Run Post-Deployment Tasks
|
||||||
|
$this->_runNonDeploymentTasks('post', $config);
|
||||||
|
}
|
||||||
|
|
||||||
|
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;
|
$tasks = 0;
|
||||||
$completedTasks = 0;
|
$completedTasks = 0;
|
||||||
|
|
||||||
Mage_Console::output('Deploying to <dark_gray>' . $host . '</dark_gray>');
|
foreach ($tasksToRun as $taskName) {
|
||||||
|
|
||||||
foreach ($config->getTasks() as $taskName) {
|
|
||||||
$tasks++;
|
$tasks++;
|
||||||
$task = Mage_Task_Factory::get($taskName, $taskConfig);
|
$task = Mage_Task_Factory::get($taskName, $taskConfig);
|
||||||
$task->init();
|
$task->init();
|
||||||
@ -40,44 +97,9 @@ class Mage_Task_Deploy
|
|||||||
$tasksColor = 'red';
|
$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('Finished <dark_gray>' . ucfirst($type) . '-Deployment</dark_gray> tasks: <' . $tasksColor . '>' . $completedTasks . '/' . $tasks . '</' . $tasksColor . '> tasks done.', 1, 3);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Run Post-Deployment Tasks
|
|
||||||
$this->_runNonDeploymentTasks('post', $config);
|
|
||||||
}
|
|
||||||
|
|
||||||
private function _runNonDeploymentTasks($type, Mage_Config $config)
|
|
||||||
{
|
|
||||||
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) {
|
|
||||||
$tasks++;
|
|
||||||
$task = Mage_Task_Factory::get($taskName, $taskConfig);
|
|
||||||
$task->init();
|
|
||||||
|
|
||||||
Mage_Console::output('Running <purple>' . $task->getName() . '</purple> ... ', 2, false);
|
|
||||||
$result = $task->run();
|
|
||||||
|
|
||||||
if ($result == true) {
|
|
||||||
Mage_Console::output('<green>OK</green>');
|
|
||||||
$completedTasks++;
|
|
||||||
} else {
|
|
||||||
Mage_Console::output('<red>FAIL</red>');
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if ($completedTasks == $tasks) {
|
|
||||||
$tasksColor = 'green';
|
|
||||||
} else {
|
|
||||||
$tasksColor = 'red';
|
|
||||||
}
|
|
||||||
|
|
||||||
Mage_Console::output('Finished <dark_gray>' . ucfirst($type) . '-Deployment</dark_gray> tasks: <' . $tasksColor . '>' . $completedTasks . '/' . $tasks . '</' . $tasksColor . '> tasks done.' . PHP_EOL . PHP_EOL);
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -16,9 +16,7 @@ $baseDir = dirname(dirname(__FILE__));
|
|||||||
require_once $baseDir . '/Mage/Autoload.php';
|
require_once $baseDir . '/Mage/Autoload.php';
|
||||||
spl_autoload_register(array('Mage_Autoload', 'autoload'));
|
spl_autoload_register(array('Mage_Autoload', 'autoload'));
|
||||||
|
|
||||||
Mage_Console::output('Starting <blue>Magallanes</blue>', 0);
|
Mage_Console::output('Starting <blue>Magallanes</blue>', 0, 2);
|
||||||
Mage_Console::output('');
|
|
||||||
|
|
||||||
|
|
||||||
$console = new Mage_Console;
|
$console = new Mage_Console;
|
||||||
$console->setArgs($argv);
|
$console->setArgs($argv);
|
||||||
@ -27,4 +25,4 @@ $console->parse();
|
|||||||
$console->run();
|
$console->run();
|
||||||
|
|
||||||
|
|
||||||
Mage_Console::output('Finished <blue>Magallanes</blue>', 0);
|
Mage_Console::output('Finished <blue>Magallanes</blue>', 0, 2);
|
||||||
|
Loading…
Reference in New Issue
Block a user