From 3caa3d7313bdd2ec3872f7d3d0b851248baa1c45 Mon Sep 17 00:00:00 2001 From: Andrs Montaez Date: Tue, 22 Nov 2011 09:16:06 -0200 Subject: [PATCH] Import of current work --- Magallanes/Autoload.php | 10 +++ Magallanes/Config.php | 26 ++++++++ Magallanes/Console.php | 65 +++++++++++++++++++ Magallanes/Console/Colors.php | 53 +++++++++++++++ Magallanes/Task/CSM/Git.php | 16 +++++ Magallanes/Task/Deploy.php | 30 +++++++++ Magallanes/Task/Deploy/Rsync.php | 11 ++++ Magallanes/Task/Update.php | 12 ++++ bin/mage | 8 +++ bin/mage.php | 21 ++++++ docs/example-config/.mage/csm.yaml | 2 + .../.mage/environment/production.yaml | 8 +++ .../.mage/environment/staging.yaml | 6 ++ docs/example-config/.mage/global.yaml | 1 + docs/example-config/.mage/tasks/MyTask.php | 8 +++ 15 files changed, 277 insertions(+) create mode 100644 Magallanes/Autoload.php create mode 100644 Magallanes/Config.php create mode 100644 Magallanes/Console.php create mode 100644 Magallanes/Console/Colors.php create mode 100644 Magallanes/Task/CSM/Git.php create mode 100644 Magallanes/Task/Deploy.php create mode 100644 Magallanes/Task/Deploy/Rsync.php create mode 100644 Magallanes/Task/Update.php create mode 100755 bin/mage create mode 100644 bin/mage.php create mode 100644 docs/example-config/.mage/csm.yaml create mode 100644 docs/example-config/.mage/environment/production.yaml create mode 100644 docs/example-config/.mage/environment/staging.yaml create mode 100644 docs/example-config/.mage/global.yaml create mode 100644 docs/example-config/.mage/tasks/MyTask.php diff --git a/Magallanes/Autoload.php b/Magallanes/Autoload.php new file mode 100644 index 0000000..a06a4af --- /dev/null +++ b/Magallanes/Autoload.php @@ -0,0 +1,10 @@ +_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; + } +} \ No newline at end of file diff --git a/Magallanes/Console.php b/Magallanes/Console.php new file mode 100644 index 0000000..6d5e15d --- /dev/null +++ b/Magallanes/Console.php @@ -0,0 +1,65 @@ +_args = $args; + array_shift($this->_args); + } + + public function parse() + { + foreach ($this->_args as $argument) { + if ($argument == 'deploy') { + $this->_action = 'deploy'; + + } else if ($argument == 'update') { + $this->_action = 'update'; + + } else if (preg_match('/to:[\w]+/i', $argument)) { + $this->_environment = str_replace('to:', '', $argument); + } + } + } + + public function getAction() + { + return $this->_action; + } + + public function getEnvironment() + { + return $this->_environment; + } + + public static function output($message) + { + echo $message; + } + + public function run() + { + $config = new Magallanes_Config; + + switch ($this->getAction()) { + case 'deploy': + $config->loadEnvironment($this->getEnvironment()); + $task = new Magallanes_Task_Deploy; + break; + + case 'update'; + $config->loadCSM(); + $task = new Magallanes_Task_Update; + break; + } + + $task->run($config); + } +} + +define('PHP_TAB', "\t"); \ No newline at end of file diff --git a/Magallanes/Console/Colors.php b/Magallanes/Console/Colors.php new file mode 100644 index 0000000..3223b73 --- /dev/null +++ b/Magallanes/Console/Colors.php @@ -0,0 +1,53 @@ +_url = $url; + $this->_update(); + } + + private function _update() + { + Magallanes_Console::output('git pull ' . $this->_url . PHP_EOL); + } +} \ No newline at end of file diff --git a/Magallanes/Task/Deploy.php b/Magallanes/Task/Deploy.php new file mode 100644 index 0000000..19f9682 --- /dev/null +++ b/Magallanes/Task/Deploy.php @@ -0,0 +1,30 @@ +_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); + } + } + + } +} \ No newline at end of file diff --git a/Magallanes/Task/Deploy/Rsync.php b/Magallanes/Task/Deploy/Rsync.php new file mode 100644 index 0000000..b7680e6 --- /dev/null +++ b/Magallanes/Task/Deploy/Rsync.php @@ -0,0 +1,11 @@ +&1 ', $command, $result); + return $result; + } +} \ No newline at end of file diff --git a/Magallanes/Task/Update.php b/Magallanes/Task/Update.php new file mode 100644 index 0000000..004cb3c --- /dev/null +++ b/Magallanes/Task/Update.php @@ -0,0 +1,12 @@ +getCSM(); + $csm = new Magallanes_Task_CSM_Git; + $csm->run($csmConfig['url']); + } +} \ No newline at end of file diff --git a/bin/mage b/bin/mage new file mode 100755 index 0000000..8d50bd0 --- /dev/null +++ b/bin/mage @@ -0,0 +1,8 @@ +#!/bin/sh + +SCRIPT=$(readlink -f $0) +DIR=$(dirname $SCRIPT) +PHP_BIN=$(which php) + +$PHP_BIN -d safe_mode=Off -f $DIR/mage.php -- $@ + diff --git a/bin/mage.php b/bin/mage.php new file mode 100644 index 0000000..86bac7e --- /dev/null +++ b/bin/mage.php @@ -0,0 +1,21 @@ +setArgs($argv); +$console->parse(); + +$console->run(); diff --git a/docs/example-config/.mage/csm.yaml b/docs/example-config/.mage/csm.yaml new file mode 100644 index 0000000..2438e4e --- /dev/null +++ b/docs/example-config/.mage/csm.yaml @@ -0,0 +1,2 @@ +type: git +url: git://github.com/andres-montanez/Zend-Framework-Twig-example-app.git \ No newline at end of file diff --git a/docs/example-config/.mage/environment/production.yaml b/docs/example-config/.mage/environment/production.yaml new file mode 100644 index 0000000..be85934 --- /dev/null +++ b/docs/example-config/.mage/environment/production.yaml @@ -0,0 +1,8 @@ +#production +user: prod_example +deploy-from: application +deploy-to: /var/www/vhosts/example.com/www +hosts: + - s01.example.com + - s02.example.com + - s03.example.com diff --git a/docs/example-config/.mage/environment/staging.yaml b/docs/example-config/.mage/environment/staging.yaml new file mode 100644 index 0000000..29fa24c --- /dev/null +++ b/docs/example-config/.mage/environment/staging.yaml @@ -0,0 +1,6 @@ +#staging +user: stg_example +deploy-from: application +deploy-to: /var/www/vhosts/example.com/staging +hosts: + - staging.example.com diff --git a/docs/example-config/.mage/global.yaml b/docs/example-config/.mage/global.yaml new file mode 100644 index 0000000..fd135f0 --- /dev/null +++ b/docs/example-config/.mage/global.yaml @@ -0,0 +1 @@ +backupdir: /tmp \ No newline at end of file diff --git a/docs/example-config/.mage/tasks/MyTask.php b/docs/example-config/.mage/tasks/MyTask.php new file mode 100644 index 0000000..53b8c2e --- /dev/null +++ b/docs/example-config/.mage/tasks/MyTask.php @@ -0,0 +1,8 @@ +