Browse Source

New reorder of classes and configuration. New instantiation of Tasks.

1.0
Andrs Montaez 13 years ago
parent
commit
b161220e69
  1. 5
      .buildpath
  2. 22
      .project
  3. 3
      .settings/org.eclipse.php.core.prefs
  4. 26
      Magallanes/Config.php
  5. 16
      Magallanes/Task/CSM/Git.php
  6. 30
      Magallanes/Task/Deploy.php
  7. 11
      Magallanes/Task/Deploy/Rsync.php
  8. 12
      Magallanes/Task/Update.php
  9. 2
      Mage/Autoload.php
  10. 51
      Mage/Config.php
  11. 24
      Mage/Console.php
  12. 2
      Mage/Console/Colors.php
  13. 28
      Mage/Task/BuiltIn/Deployment/Rsync.php
  14. 26
      Mage/Task/BuiltIn/Scm/Update.php
  15. 37
      Mage/Task/Deploy.php
  16. 16
      Mage/Task/Factory.php
  17. 17
      Mage/Task/TaskAbstract.php
  18. 16
      bin/mage.php
  19. 11
      docs/example-config/.mage/config/environment/production.yaml
  20. 9
      docs/example-config/.mage/config/environment/staging.yaml
  21. 0
      docs/example-config/.mage/config/global.yaml
  22. 0
      docs/example-config/.mage/config/scm.yaml
  23. 1
      docs/example-config/.mage/logs/.gitignore

5
.buildpath

@ -0,0 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<buildpath>
<buildpathentry kind="src" path=""/>
<buildpathentry kind="con" path="org.eclipse.php.core.LANGUAGE"/>
</buildpath>

22
.project

@ -0,0 +1,22 @@
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>Magallanes</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.wst.validation.validationbuilder</name>
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>org.eclipse.dltk.core.scriptbuilder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.php.core.PHPNature</nature>
</natures>
</projectDescription>

3
.settings/org.eclipse.php.core.prefs

@ -0,0 +1,3 @@
#Mon Nov 21 16:37:20 UYST 2011
eclipse.preferences.version=1
include_path=0;/Magallanes

26
Magallanes/Config.php

@ -1,26 +0,0 @@
<?php
class Magallanes_Config
{
private $_environment = null;
private $_csm = null;
public function loadEnvironment($environment)
{
$this->_environment = yaml_parse_file('.mage/environment/' . $environment . '.yaml');
}
public function loadCSM()
{
$this->_csm = yaml_parse_file('.mage/csm.yaml');
}
public function getEnvironment()
{
return $this->_environment;
}
public function getCSM()
{
return $this->_csm;
}
}

16
Magallanes/Task/CSM/Git.php

@ -1,16 +0,0 @@
<?php
class Magallanes_Task_CSM_Git
{
private $_url = null;
public function run($url)
{
$this->_url = $url;
$this->_update();
}
private function _update()
{
Magallanes_Console::output('git pull ' . $this->_url . PHP_EOL);
}
}

30
Magallanes/Task/Deploy.php

@ -1,30 +0,0 @@
<?php
class Magallanes_Task_Deploy
{
private $_config = null;
public function run(Magallanes_Config $config)
{
$this->_config = $config;
$this->_rsync();
}
private function _rsync()
{
$config = $this->_config->getEnvironment();
$user = $config['user'];
$to = $config['deploy-to'];
$from = $config['deploy-from'];
foreach ($config['hosts'] as $host) {
Magallanes_Console::output(PHP_TAB . 'Deploying to: ' . $host);
$result = Magallanes_Task_Deploy_Rsync::exec($user, $host, $from, $to);
if ($result == 0) {
Magallanes_Console::output(PHP_TAB . 'OK' . PHP_EOL);
} else {
Magallanes_Console::output(PHP_TAB . 'FAIL' . PHP_EOL);
}
}
}
}

11
Magallanes/Task/Deploy/Rsync.php

@ -1,11 +0,0 @@
<?php
class Magallanes_Task_Deploy_Rsync
{
public static function exec($user, $host, $from, $to)
{
$output = array();
$command = 'rsync -avz ' . $from . ' ' . $user . '@' . $host . ':' . $to;
exec($command . ' 2>&1 ', $command, $result);
return $result;
}
}

12
Magallanes/Task/Update.php

@ -1,12 +0,0 @@
<?php
class Magallanes_Task_Update
{
private $_config = null;
public function run(Magallanes_Config $config)
{
$csmConfig = $config->getCSM();
$csm = new Magallanes_Task_CSM_Git;
$csm->run($csmConfig['url']);
}
}

2
Magallanes/Autoload.php → Mage/Autoload.php

@ -1,5 +1,5 @@
<?php <?php
class Magallanes_Autoload class Mage_Autoload
{ {
public static function autoload($className) public static function autoload($className)
{ {

51
Mage/Config.php

@ -0,0 +1,51 @@
<?php
class Mage_Config
{
private $_environment = null;
private $_scm = null;
public function loadEnvironment($environment)
{
$this->_environment = yaml_parse_file('.mage/config/environment/' . $environment . '.yaml');
}
public function loadSCM()
{
$this->_scm = yaml_parse_file('.mage/config/scm.yaml');
}
public function getEnvironment()
{
return $this->_environment;
}
public function getSCM()
{
return $this->_scm;
}
public function getHosts()
{
$config = $this->getEnvironment();
return $config['hosts'];
}
public function getTasks()
{
$config = $this->getEnvironment();
return $config['tasks'];
}
public function getConfig($host)
{
$taskConfig = array();
$taskConfig['deploy'] = $this->getEnvironment();
$taskConfig['deploy']['host'] = $host;
$taskConfig['scm'] = $this->getSCM();
unset($taskConfig['deploy']['tasks']);
unset($taskConfig['deploy']['hosts']);
return $taskConfig;
}
}

24
Magallanes/Console.php → Mage/Console.php

@ -1,5 +1,5 @@
<?php <?php
class Magallanes_Console class Mage_Console
{ {
private $_args; private $_args;
private $_action; private $_action;
@ -42,23 +42,33 @@ class Magallanes_Console
echo $message; echo $message;
} }
public static function executeCommand($command)
{
ob_start();
system($command . ' 2>&1', $return);
$log = ob_get_clean();
return !$return;
}
public function run() public function run()
{ {
$config = new Magallanes_Config; $config = new Mage_Config;
$config->loadEnvironment($this->getEnvironment());
$config->loadSCM();
switch ($this->getAction()) { switch ($this->getAction()) {
case 'deploy': case 'deploy':
$config->loadEnvironment($this->getEnvironment()); $task = new Mage_Task_Deploy;
$task = new Magallanes_Task_Deploy; $task->run($config);
break; break;
case 'update'; case 'update';
$config->loadCSM(); $config->loadCSM();
$task = new Magallanes_Task_Update; $task = new Mage_Task_Update;
$task->run($config);
break; break;
} }
$task->run($config);
} }
} }

2
Magallanes/Console/Colors.php → Mage/Console/Colors.php

@ -1,5 +1,5 @@
<?php <?php
class Magallanes_Console_Colors { class Mage_Console_Colors {
private static $foreground_colors = array (); private static $foreground_colors = array ();
private static $background_colors = array (); private static $background_colors = array ();

28
Mage/Task/BuiltIn/Deployment/Rsync.php

@ -0,0 +1,28 @@
<?php
class Mage_Task_BuiltIn_Deployment_Rsync
extends Mage_Task_TaskAbstract
{
public function getName()
{
return 'Rsync (built-in)';
}
public function run($config)
{
$ignores = array(
'--exclude .git',
'--exclude .svn',
'--exclude .mage',
'--exclude .gitignore'
);
$command = 'rsync -avz '
. implode(' ', $ignores) .' '
. $config['deploy']['deploy-from'] . ' '
. $config['deploy']['user'] . '@' . $config['deploy']['host'] . ':' . $config['deploy']['deploy-to'];
$result = $this->_runLocalCommand($command);
return $result;
}
}

26
Mage/Task/BuiltIn/Scm/Update.php

@ -0,0 +1,26 @@
<?php
class Mage_Task_BuiltIn_Scm_Update
extends Mage_Task_TaskAbstract
{
public function getName()
{
return 'SCM Update (built-in)';
}
public function run($config)
{
switch ($config['scm']['type']) {
case 'git':
$command = 'git pull';
break;
case 'svn':
$command = 'svn update';
break;
}
$result = $this->_runLocalCommand($command);
return $result;
}
}

37
Mage/Task/Deploy.php

@ -0,0 +1,37 @@
<?php
class Mage_Task_Deploy
{
private $_config = null;
public function run(Mage_Config $config)
{
$this->_config = $config;
foreach ($config->getHosts() as $host)
{
$taskConfig = $config->getConfig($host);
$tasks = 0;
$completedTasks = 0;
Mage_Console::output(PHP_TAB . 'Deploying to ' . $host . PHP_EOL);
foreach ($config->getTasks() as $taskName) {
$tasks++;
$task = Mage_Task_Factory::get($taskName);
Mage_Console::output(PHP_TAB . PHP_TAB . 'Running ' . $task->getName() . ' ... ');
$result = $task->run($taskConfig);
if ($result == true) {
Mage_Console::output(PHP_TAB . 'OK' . PHP_EOL);
$completedTasks++;
} else {
Mage_Console::output(PHP_TAB . 'FAIL' . PHP_EOL);
}
}
Mage_Console::output(PHP_TAB . 'Deployment to ' . $host . ' compted: ' . $completedTasks . '/' . $tasks . ' tasks done.' . PHP_EOL . PHP_EOL);
}
}
}

16
Mage/Task/Factory.php

@ -0,0 +1,16 @@
<?php
class Mage_Task_Factory
{
/**
*
*
* @param string $taskName
* @return Mage_Task_TaskAbstract
*/
public static function get($taskName)
{
$taskName = str_replace(' ', '_', ucwords(str_replace('/', ' ', $taskName)));
$className = 'Mage_Task_BuiltIn_' . $taskName;
return new $className;
}
}

17
Mage/Task/TaskAbstract.php

@ -0,0 +1,17 @@
<?php
abstract class Mage_Task_TaskAbstract
{
public abstract function getName();
public abstract function run($config);
protected function _runLocalCommand($command)
{
return Mage_Console::executeCommand($command);
}
protected function _runRemoteCommand($command)
{
}
}

16
bin/mage.php

@ -1,4 +1,12 @@
<?php <?php
# mage install
# mage init
# mage config add environment [production]
# mage config add host prod_example@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 deploy to:production # mage deploy to:production
# mage update # mage update
# mage up # mage up
@ -9,12 +17,12 @@
$baseDir = dirname(dirname(__FILE__)); $baseDir = dirname(dirname(__FILE__));
require_once $baseDir . '/Magallanes/Autoload.php'; require_once $baseDir . '/Mage/Autoload.php';
spl_autoload_register(array('Magallanes_Autoload', 'autoload')); spl_autoload_register(array('Mage_Autoload', 'autoload'));
Magallanes_Console::output('Begining Magallanes' . PHP_EOL . PHP_EOL); Mage_Console::output('Begining Magallanes' . PHP_EOL . PHP_EOL);
$console = new Magallanes_Console; $console = new Mage_Console;
$console->setArgs($argv); $console->setArgs($argv);
$console->parse(); $console->parse();

11
docs/example-config/.mage/config/environment/production.yaml

@ -0,0 +1,11 @@
#production
user: root
deploy-from: ./
deploy-to: /var/www/vhosts/example.com/www
hosts:
- s01.example.com
- s02.example.com
- s03.example.com
tasks:
- scm/update
- deployment/rsync

9
docs/example-config/.mage/config/environment/staging.yaml

@ -0,0 +1,9 @@
#staging
user: stg_example
deploy-from: application
deploy-to: /var/www/vhosts/example.com/staging
hosts:
- staging.example.com
tasks:
- scm/update
- deployment/rsync

0
docs/example-config/.mage/global.yaml → docs/example-config/.mage/config/global.yaml

0
docs/example-config/.mage/csm.yaml → docs/example-config/.mage/config/scm.yaml

1
docs/example-config/.mage/logs/.gitignore vendored

@ -0,0 +1 @@
log-*
Loading…
Cancel
Save