Verify if environment is defined. Verify that rollback has release point.

This commit is contained in:
Andrés Montañez 2012-09-19 18:53:14 -03:00
parent 7d8bf35df4
commit d63ca9cc04
4 changed files with 171 additions and 141 deletions

View file

@ -7,13 +7,13 @@ class Mage_Config
private $_general = null;
private $_host = null;
private $_releaseId = null;
public function loadEnvironment($environment)
{
if (($environment != '') && file_exists('.mage/config/environment/' . $environment . '.yml')) {
$this->_environment = spyc_load_file('.mage/config/environment/' . $environment . '.yml');
$this->_environmentName = $environment;
// Create temporal directory for clone
if (isset($this->_environment['deployment']['source']) && is_array($this->_environment['deployment']['source'])) {
if (trim($this->_environment['deployment']['source']['temporal']) == '') {
@ -22,34 +22,40 @@ class Mage_Config
$newTemporal = rtrim($this->_environment['deployment']['source']['temporal'], '/')
. '/' . md5(microtime()) . '/';
$this->_environment['deployment']['source']['temporal'] = $newTemporal;
}
}
return true;
} else if (($environment != '') && !file_exists('.mage/config/environment/' . $environment . '.yml')) {
return false;
}
return true;
}
public function loadSCM()
{
if (file_exists('.mage/config/scm.yml')) {
$this->_scm = spyc_load_file('.mage/config/scm.yml');
$this->_scm = spyc_load_file('.mage/config/scm.yml');
}
}
public function loadGeneral()
{
if (file_exists('.mage/config/general.yml')) {
$this->_general = spyc_load_file('.mage/config/general.yml');
}
}
public function getEnvironment()
{
return $this->_environment;
}
public function getEnvironmentName()
{
return $this->_environmentName;
}
public function getSCM()
{
return $this->_scm;
@ -59,64 +65,64 @@ class Mage_Config
{
return $this->_global;
}
public function getHosts()
{
$config = $this->getEnvironment();
$hosts = array();
if (isset($config['hosts'])) {
if (is_array($config['hosts'])) {
$hosts = (array) $config['hosts'];
$hosts = (array) $config['hosts'];
} else if (is_string($config['hosts'])) {
$fileContent = fopen($config['hosts'], 'r');
while (($host = fgets($fileContent)) == true) {
$host = trim($host);
if ($host != '') {
$hosts[] = $host;
$hosts[] = $host;
}
}
}
}
return $hosts;
}
public function setHost($host)
{
$this->_host = $host;
return $this;
}
public function getHostName()
{
$info = explode(':', $this->_host);
return $info[0];
}
public function getHostPort()
{
$info = explode(':', $this->_host);
$info[] = $this->deployment('port', '22');
return $info[1];
}
public function getHost()
{
return $this->_host;
}
public function setReleaseId($id)
{
$this->_releaseId = $id;
return $this;
}
public function getReleaseId()
{
return $this->_releaseId;
}
public function getTasks($stage = 'on-deploy')
{
switch ($stage) {
@ -124,24 +130,24 @@ class Mage_Config
$type = 'tasks';
$stage = 'pre-deploy';
break;
case 'post-deploy':
$type = 'tasks';
$stage = 'post-deploy';
break;
case 'post-release':
$type = 'releases';
$stage = 'post-release';
break;
case 'on-deploy':
default:
$type = 'tasks';
$stage = 'on-deploy';
break;
}
$tasks = array();
$config = $this->getEnvironment();
@ -151,17 +157,17 @@ class Mage_Config
return $tasks;
}
public function getConfig($host = false)
{
$taskConfig = array();
$taskConfig['deploy'] = $this->getEnvironment();
$taskConfig['deploy']['host'] = $host;
$taskConfig['scm'] = $this->getSCM();
unset($taskConfig['deploy']['tasks']);
unset($taskConfig['deploy']['hosts']);
return $taskConfig;
}
@ -170,7 +176,7 @@ class Mage_Config
$this->_environment['deployment']['from'] = $from;
return $this;
}
public function deployment($option, $default = false)
{
$options = $this->getEnvironment();
@ -178,13 +184,13 @@ class Mage_Config
if (is_array($default) && ($options['deployment'][$option] == '')) {
return $default;
} else {
return $options['deployment'][$option];
return $options['deployment'][$option];
}
} else {
return $default;
}
}
public function release($option, $default = false)
{
$options = $this->getEnvironment();
@ -198,7 +204,7 @@ class Mage_Config
return $default;
}
}
public function scm($option, $default = false)
{
$options = $this->_scm;
@ -213,7 +219,7 @@ class Mage_Config
return $default;
}
}
public function general($option, $default = false)
{
$options = $this->_general;
@ -227,7 +233,7 @@ class Mage_Config
return $default;
}
}
public function mail($option, $default = false)
{
$options = $this->_general;

View file

@ -9,13 +9,13 @@ class Mage_Console
private static $_logEnabled = true;
private static $_screenBuffer = '';
private static $_commandsOutput = '';
public function setArgs($args)
{
$this->_args = $args;
array_shift($this->_args);
}
public function parse()
{
if (count($this->_args) == 0) {
@ -27,7 +27,7 @@ class Mage_Console
} else if ($this->_args[0] == 'releases') {
$this->_action = 'releases';
} else if ($this->_args[0] == 'update') {
$this->_action = 'update';
@ -36,20 +36,20 @@ class Mage_Console
} else if ($this->_args[0] == 'add') {
$this->_action = 'add';
} else if ($this->_args[0] == 'install') {
$this->_action = 'install';
} else if ($this->_args[0] == 'upgrade') {
$this->_action = 'upgrade';
} else if ($this->_args[0] == 'version') {
$this->_action = 'version';
} else if ($this->_args[0] == 'init') {
$this->_action = 'init';
}
}
foreach ($this->_args as $argument) {
if (preg_match('/to:[\w]+/i', $argument)) {
$this->_environment = str_replace('to:', '', $argument);
@ -64,17 +64,17 @@ class Mage_Console
}
}
}
public function getAction()
{
return $this->_action;
}
public function getEnvironment()
{
return $this->_environment;
}
public static function getActionOption($name, $default = false)
{
if (isset(self::$_actionOptions[$name])) {
@ -83,22 +83,22 @@ class Mage_Console
return $default;
}
}
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;
}
public static function executeCommand($command, &$output = null)
{
self::log('---------------------------------');
@ -108,24 +108,24 @@ class Mage_Console
$log = array();
exec($command . ' 2>&1', $log, $return);
$log = implode(PHP_EOL, $log);
if (!$return) {
$output = trim($log);
$output = trim($log);
}
self::$_commandsOutput .= PHP_EOL . trim($log) . PHP_EOL;
self::$_commandsOutput .= PHP_EOL . trim($log) . PHP_EOL;
self::log($log);
self::log('---------------------------------');
return !$return;
}
public function run()
{
{
// Load Config
$config = new Mage_Config;
$config->loadGeneral();
$config->loadEnvironment($this->getEnvironment());
$environmentOk = $config->loadEnvironment($this->getEnvironment());
$config->loadSCM();
// Logging
@ -136,59 +136,72 @@ class Mage_Console
} else {
self::$_logEnabled = $config->general('logging', false);
}
// Grettings
if ($showGrettings) {
Mage_Console::output('Starting <blue>Magallanes</blue>', 0, 2);
}
switch ($this->getAction()) {
case 'deploy':
$task = new Mage_Task_Deploy;
$task->run($config);
break;
case 'releases':
$task = new Mage_Task_Releases;
switch ($this->_args[1]) {
case 'list':
$task->setAction($this->_args[1]);
break;
if (!$environmentOk) {
Mage_Console::output('<red>You have selected an invalid environment</red>', 0, 2);
case 'rollback':
$task->setAction($this->_args[1]);
$task->setRelease($this->_args[2]);
break;
}
$task->run($config);
break;
} else {
switch ($this->getAction()) {
case 'deploy':
$task = new Mage_Task_Deploy;
$task->run($config);
break;
case 'update';
case 'releases':
$task = new Mage_Task_Releases;
if (!isset($this->_args[1])) {
Mage_Console::output('<red>You must indicate a task</red>', 0, 2);
break;
}
switch ($this->_args[1]) {
case 'list':
$task->setAction($this->_args[1]);
break;
case 'rollback':
if (!isset($this->_args[2])) {
Mage_Console::output('<red>You must indicate a release point</red>', 0, 2);
break 2;
}
$task->setAction($this->_args[1]);
$task->setRelease($this->_args[2]);
break;
}
$task->run($config);
break;
case 'update';
$task = new Mage_Task_Update;
$task->run($config);
break;
case 'compile';
case 'compile';
$task = new Mage_Task_Compile;
$task->run($config);
break;
case 'install';
case 'install';
$task = new Mage_Task_Install;
$task->run();
break;
case 'upgrade';
case 'upgrade';
$task = new Mage_Task_Upgrade;
$task->run();
break;
case 'init';
case 'init';
$task = new Mage_Task_Init;
$task->run();
break;
case 'add';
case 'add';
switch ($this->_args[1]) {
case 'environment':
if (isset($this->_args[3]) && ($this->_args[3] == '--with-releases')) {
@ -202,35 +215,36 @@ class Mage_Console
break;
}
break;
case 'version';
case 'version';
$this->showVersion();
break;
default:
Mage_Console::output('<red>Invalid action</red>', 0, 2);
break;
default:
Mage_Console::output('<red>Invalid action</red>', 0, 2);
break;
}
}
if ($showGrettings) {
Mage_Console::output('Finished <blue>Magallanes</blue>', 0, 2);
}
}
public function showVersion()
{
Mage_Console::output('Running <blue>Magallanes</blue> version <dark_gray>' . MAGALLANES_VERSION .'</dark_gray>', 0, 2);
}
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);
fwrite(self::$_log, $message . PHP_EOL);
}
}
}

View file

@ -7,26 +7,31 @@ class Mage_Task_Deploy
private $_startTimeHosts = null;
private $_endTimeHosts = null;
private $_hostsCount = 0;
public function __construct()
{
$this->_releaseId = date('YmdHis');
}
public function run(Mage_Config $config)
{
$this->_startTime = time();
$this->_config = $config;
if ($config->getEnvironment() == '') {
Mage_Console::output('<red>You must specify an environment</red>', 0, 2);
return;
}
// Run Pre-Deployment Tasks
$this->_runNonDeploymentTasks('pre-deploy', $config, 'Pre-Deployment');
// Run Tasks for Deployment
$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 {
$this->_startTimeHosts = time();
foreach ($hosts as $host) {
@ -34,9 +39,9 @@ class Mage_Task_Deploy
$config->setHost($host);
$tasks = 0;
$completedTasks = 0;
Mage_Console::output('Deploying to <dark_gray>' . $config->getHost() . '</dark_gray>');
$tasksToRun = $config->getTasks();
array_unshift($tasksToRun, 'deployment/rsync');
@ -54,10 +59,10 @@ class Mage_Task_Deploy
$tasks++;
$task = Mage_Task_Factory::get($taskName, $config, false, 'deploy');
$task->init();
Mage_Console::output('Running <purple>' . $task->getName() . '</purple> ... ', 2, false);
$result = $task->run();
if ($result == true) {
Mage_Console::output('<green>OK</green>', 0);
$completedTasks++;
@ -65,13 +70,13 @@ class Mage_Task_Deploy
Mage_Console::output('<red>FAIL</red>', 0);
}
}
if ($completedTasks == $tasks) {
$tasksColor = 'green';
$tasksColor = 'green';
} else {
$tasksColor = 'red';
$tasksColor = 'red';
}
Mage_Console::output('Deployment to <dark_gray>' . $config->getHost() . '</dark_gray> compted: <' . $tasksColor . '>' . $completedTasks . '/' . $tasks . '</' . $tasksColor . '> tasks done.', 1, 3);
}
}
@ -85,24 +90,24 @@ class Mage_Task_Deploy
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>.');
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);
}
private function _runNonDeploymentTasks($stage, Mage_Config $config, $title)
{
$tasksToRun = $config->getTasks($stage);
// Look for Remote Source
if (is_array($this->_config->deployment('source', null))) {
if ($stage == 'pre-deploy') {
array_unshift($tasksToRun, 'scm/clone');
array_unshift($tasksToRun, 'scm/clone');
} elseif ($stage == 'post-deploy') {
array_unshift($tasksToRun, 'scm/remove-clone');
}
@ -110,60 +115,60 @@ class Mage_Task_Deploy
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 $taskName) {
$tasks++;
$task = Mage_Task_Factory::get($taskName, $config, false, $stage);
$task->init();
Mage_Console::output('Running <purple>' . $task->getName() . '</purple> ... ', 2, 0);
$result = $task->run();
if ($result == true) {
Mage_Console::output('<green>OK</green>', 0);
$completedTasks++;
} else {
Mage_Console::output('<red>FAIL</red>', 0);
}
}
}
if ($completedTasks == $tasks) {
$tasksColor = 'green';
$tasksColor = 'green';
} else {
$tasksColor = 'red';
$tasksColor = 'red';
}
Mage_Console::output('Finished <dark_gray>' . $title . '</dark_gray> tasks: <' . $tasksColor . '>' . $completedTasks . '/' . $tasks . '</' . $tasksColor . '> tasks done.', 1, 3);
Mage_Console::output('Finished <dark_gray>' . $title . '</dark_gray> tasks: <' . $tasksColor . '>' . $completedTasks . '/' . $tasks . '</' . $tasksColor . '> tasks done.', 1, 3);
}
}
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

@ -4,39 +4,44 @@ class Mage_Task_Releases
private $_config = null;
private $_action = null;
private $_release = null;
public function setAction($action)
{
$this->_action = $action;
return $this;
}
public function getAction()
{
return $this->_action;
}
public function setRelease($releaseId)
{
$this->_release = $releaseId;
return $this;
}
public function getRelease()
{
return $this->_release;
}
public function run(Mage_Config $config)
{
$this->_config = $config;
if ($config->getEnvironment() == '') {
Mage_Console::output('<red>You must specify an environment</red>', 0, 2);
return;
}
// Run Tasks for Deployment
$hosts = $config->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) {
$config->setHost($host);
@ -46,7 +51,7 @@ class Mage_Task_Releases
$task->init();
$result = $task->run();
break;
case 'rollback':
$task = Mage_Task_Factory::get('releases/rollback', $config);
$task->init();