mirror of
https://github.com/hauke68/Magallanes.git
synced 2026-09-08 14:28:57 +02:00
Major overhauling and refactoring of Magallanes Command and Tasks modulairty and workflow.
ADVICE: there is no Backwards Compatibility with custom tasks, those using the _config instance will be broken or those using the getEnvironmentName().
This commit is contained in:
+299
-161
@@ -1,88 +1,132 @@
|
||||
<?php
|
||||
class Mage_Config
|
||||
{
|
||||
private $_environmentName = null;
|
||||
private $_environment = null;
|
||||
private $_scm = null;
|
||||
private $_general = null;
|
||||
private $_arguments = array();
|
||||
private $_parameters = array();
|
||||
private $_environment = false;
|
||||
private $_host = null;
|
||||
private $_releaseId = null;
|
||||
private $_config = array(
|
||||
'general' => array(),
|
||||
'scm' => array(),
|
||||
'environment' => array(),
|
||||
);
|
||||
|
||||
public function reloadConfig()
|
||||
/**
|
||||
* Load the Configuration and parses the Arguments
|
||||
*
|
||||
* @param array $arguments
|
||||
*/
|
||||
public function load($arguments)
|
||||
{
|
||||
$this->loadGeneral();
|
||||
$this->loadSCM();
|
||||
$this->loadEnvironment($this->getEnvironmentName());
|
||||
$this->_parse($arguments);
|
||||
$this->_loadGeneral();
|
||||
$this->_loadSCM();
|
||||
$this->_loadEnvironment();
|
||||
}
|
||||
|
||||
public function loadEnvironment($environment)
|
||||
/**
|
||||
* Return the invocation argument based on a position
|
||||
* 0 = Invoked Command Name
|
||||
*
|
||||
* @param integer $position
|
||||
* @return mixed
|
||||
*/
|
||||
public function getArgument($position = 0)
|
||||
{
|
||||
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']) == '') {
|
||||
$this->_environment['deployment']['source']['temporal'] = '/tmp';
|
||||
}
|
||||
$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')) {
|
||||
if (isset($this->_arguments[$position])) {
|
||||
return $this->_arguments[$position];
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public function loadSCM()
|
||||
/**
|
||||
* Returns all the invocation arguments
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function getArguments()
|
||||
{
|
||||
if (file_exists('.mage/config/scm.yml')) {
|
||||
$this->_scm = spyc_load_file('.mage/config/scm.yml');
|
||||
return $this->_arguments;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the a parameter
|
||||
*
|
||||
* @param string $name
|
||||
* @return mixed
|
||||
*/
|
||||
public function getParameter($name, $default = null)
|
||||
{
|
||||
if (isset($this->_parameters[$name])) {
|
||||
return $this->_parameters[$name];
|
||||
} else {
|
||||
return $default;
|
||||
}
|
||||
}
|
||||
|
||||
public function loadGeneral()
|
||||
{
|
||||
if (file_exists('.mage/config/general.yml')) {
|
||||
$this->_general = spyc_load_file('.mage/config/general.yml');
|
||||
}
|
||||
/**
|
||||
* Returns all the invocation arguments
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function getParameters()
|
||||
{
|
||||
return $this->_parameters;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the Current environment
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public function getEnvironment()
|
||||
{
|
||||
return $this->_environment;
|
||||
}
|
||||
|
||||
public function getEnvironmentName()
|
||||
/**
|
||||
* Reloads the configuration
|
||||
*/
|
||||
public function reload()
|
||||
{
|
||||
return $this->_environmentName;
|
||||
$this->_loadGeneral();
|
||||
$this->_loadSCM();
|
||||
$this->_loadEnvironment();
|
||||
}
|
||||
|
||||
public function getSCM()
|
||||
/**
|
||||
* Get the Tasks to execute
|
||||
*
|
||||
* @param string $stage
|
||||
* @return array
|
||||
*/
|
||||
public function getTasks($stage = 'on-deploy')
|
||||
{
|
||||
return $this->_scm;
|
||||
}
|
||||
|
||||
public function getGlobal()
|
||||
{
|
||||
return $this->_global;
|
||||
$tasks = array();
|
||||
$config = $this->_getEnvironmentOption('tasks', array());
|
||||
if (isset($config[$stage])) {
|
||||
$tasks = ($config[$stage] ? (array) $config[$stage] : array());
|
||||
}
|
||||
|
||||
return $tasks;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the current Hosts to deploy
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function getHosts()
|
||||
{
|
||||
$config = $this->getEnvironment();
|
||||
$hosts = array();
|
||||
|
||||
if (isset($config['hosts'])) {
|
||||
if (is_array($config['hosts'])) {
|
||||
$hosts = (array) $config['hosts'];
|
||||
} else if (is_string($config['hosts'])) {
|
||||
$fileContent = fopen($config['hosts'], 'r');
|
||||
if (isset($this->_config['environment']['hosts'])) {
|
||||
if (is_array($this->_config['environment']['hosts'])) {
|
||||
$hosts = (array) $this->_config['environment']['hosts'];
|
||||
} else if (is_string($this->_config['environment']['hosts']) && file_exists($this->_config['environment']['hosts']) && is_readable($this->_config['environment']['hosts'])) {
|
||||
$fileContent = fopen($this->_config['environment']['hosts'], 'r');
|
||||
while (($host = fgets($fileContent)) == true) {
|
||||
$host = trim($host);
|
||||
if ($host != '') {
|
||||
@@ -95,18 +139,34 @@ class Mage_Config
|
||||
return $hosts;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the current host
|
||||
*
|
||||
* @param string $host
|
||||
* @return Mage_Config
|
||||
*/
|
||||
public function setHost($host)
|
||||
{
|
||||
$this->_host = $host;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the current host name
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getHostName()
|
||||
{
|
||||
$info = explode(':', $this->_host);
|
||||
return $info[0];
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the current Host Port
|
||||
*
|
||||
* @return unknown
|
||||
*/
|
||||
public function getHostPort()
|
||||
{
|
||||
$info = explode(':', $this->_host);
|
||||
@@ -114,144 +174,222 @@ class Mage_Config
|
||||
return $info[1];
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the current Host
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getHost()
|
||||
{
|
||||
return $this->_host;
|
||||
}
|
||||
|
||||
public function setReleaseId($id)
|
||||
/**
|
||||
* Gets General Configuration
|
||||
*
|
||||
* @param string $option
|
||||
* @param string $default
|
||||
* @return mixed
|
||||
*/
|
||||
public function general($option, $default = false)
|
||||
{
|
||||
$this->_releaseId = $id;
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function getReleaseId()
|
||||
{
|
||||
return $this->_releaseId;
|
||||
}
|
||||
|
||||
public function getTasks($stage = 'on-deploy')
|
||||
{
|
||||
switch ($stage) {
|
||||
case 'pre-deploy':
|
||||
$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();
|
||||
|
||||
if (isset($config[$type]) && isset($config[$type][$stage])) {
|
||||
$tasks = ($config[$type][$stage] ? (array) $config[$type][$stage] : array());
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
public function setFrom($from)
|
||||
{
|
||||
$this->_environment['deployment']['from'] = $from;
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function deployment($option, $default = false)
|
||||
{
|
||||
$options = $this->getEnvironment();
|
||||
if (isset($options['deployment'][$option])) {
|
||||
if (is_array($default) && ($options['deployment'][$option] == '')) {
|
||||
$config = $this->_config['general'];
|
||||
if (isset($config[$option])) {
|
||||
if (is_array($default) && ($config[$option] == '')) {
|
||||
return $default;
|
||||
} else {
|
||||
return $options['deployment'][$option];
|
||||
return $config[$option];
|
||||
}
|
||||
} else {
|
||||
return $default;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets SCM Configuration
|
||||
*
|
||||
* @param string $option
|
||||
* @param string $default
|
||||
* @return mixed
|
||||
*/
|
||||
public function scm($option, $default = false)
|
||||
{
|
||||
$config = $this->_config['scm'];
|
||||
if (isset($config[$option])) {
|
||||
if (is_array($default) && ($config[$option] == '')) {
|
||||
return $default;
|
||||
} else {
|
||||
return $config[$option];
|
||||
}
|
||||
} else {
|
||||
return $default;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get deployment configuration
|
||||
*
|
||||
* @param string $option
|
||||
* @param string $default
|
||||
* @return string
|
||||
*/
|
||||
public function deployment($option, $default = false)
|
||||
{
|
||||
$config = $this->_getEnvironmentOption('deployment', array());
|
||||
if (isset($config[$option])) {
|
||||
if (is_array($default) && ($config[$option] == '')) {
|
||||
return $default;
|
||||
} else {
|
||||
return $config[$option];
|
||||
}
|
||||
} else {
|
||||
return $default;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns Releaseing Options
|
||||
*
|
||||
* @param string $option
|
||||
* @param string $default
|
||||
* @return mixed
|
||||
*/
|
||||
public function release($option, $default = false)
|
||||
{
|
||||
$options = $this->getEnvironment();
|
||||
if (isset($options['releases'][$option])) {
|
||||
if (is_array($default) && ($options['releases'][$option] == '')) {
|
||||
$config = $this->_getEnvironmentOption('releases', array());
|
||||
if (isset($config[$option])) {
|
||||
if (is_array($default) && ($config[$option] == '')) {
|
||||
return $default;
|
||||
} else {
|
||||
return $options['releases'][$option];
|
||||
return $config[$option];
|
||||
}
|
||||
} else {
|
||||
return $default;
|
||||
}
|
||||
}
|
||||
|
||||
public function scm($option, $default = false)
|
||||
{
|
||||
$options = $this->_scm;
|
||||
if (isset($options[$option])) {
|
||||
if (is_array($default) && ($options[$option] == '')) {
|
||||
return $default;
|
||||
} else {
|
||||
return $options[$option];
|
||||
}
|
||||
return $options[$option];
|
||||
} else {
|
||||
return $default;
|
||||
}
|
||||
/**
|
||||
* Set From Deployment Path
|
||||
*
|
||||
* @param string $from
|
||||
* @return Mage_Config
|
||||
*/
|
||||
public function setFrom($from)
|
||||
{
|
||||
$this->_config['environment']['deployment']['from'] = $from;
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function general($option, $default = false)
|
||||
{
|
||||
$options = $this->_general;
|
||||
if (isset($options[$option])) {
|
||||
if (is_array($default) && ($options[$option] == '')) {
|
||||
return $default;
|
||||
} else {
|
||||
return $options[$option];
|
||||
}
|
||||
} else {
|
||||
return $default;
|
||||
}
|
||||
/**
|
||||
* Sets the Current Release ID
|
||||
*
|
||||
* @param integer $id
|
||||
* @return Mage_Config
|
||||
*/
|
||||
public function setReleaseId($id)
|
||||
{
|
||||
$this->_releaseId = $id;
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function mail($option, $default = false)
|
||||
{
|
||||
$options = $this->_general;
|
||||
if (isset($options['mail'][$option])) {
|
||||
if (is_array($default) && ($options['mail'][$option] == '')) {
|
||||
return $default;
|
||||
} else {
|
||||
return $options['mail'][$option];
|
||||
}
|
||||
} else {
|
||||
return $default;
|
||||
}
|
||||
/**
|
||||
* Gets the Current Release ID
|
||||
*
|
||||
* @return integer
|
||||
*/
|
||||
public function getReleaseId()
|
||||
{
|
||||
return $this->_releaseId;
|
||||
}
|
||||
|
||||
/**
|
||||
* Parse the Command Line options
|
||||
* @return boolean
|
||||
*/
|
||||
private function _parse($arguments)
|
||||
{
|
||||
foreach ($arguments as $argument) {
|
||||
if (preg_match('/to:[\w]+/i', $argument)) {
|
||||
$this->_environment = str_replace('to:', '', $argument);
|
||||
|
||||
} else if (preg_match('/--[\w]+/i', $argument)) {
|
||||
$optionValue = explode('=', substr($argument, 2));
|
||||
if (count($optionValue) == 1) {
|
||||
$this->_parameters[$optionValue[0]] = true;
|
||||
} else if (count($optionValue) == 2) {
|
||||
$this->_parameters[$optionValue[0]] = $optionValue[1];
|
||||
}
|
||||
} else {
|
||||
$this->_arguments[] = $argument;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Loads the General Configuration
|
||||
*/
|
||||
private function _loadGeneral()
|
||||
{
|
||||
if (file_exists('.mage/config/general.yml')) {
|
||||
$this->_config['general'] = spyc_load_file('.mage/config/general.yml');
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Loads the SCM Configuration
|
||||
*/
|
||||
private function _loadSCM()
|
||||
{
|
||||
if (file_exists('.mage/config/scm.yml')) {
|
||||
$this->_config['scm'] = spyc_load_file('.mage/config/scm.yml');
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Loads the Environment configuration
|
||||
*
|
||||
* @throws Exception
|
||||
* @return boolean
|
||||
*/
|
||||
private function _loadEnvironment()
|
||||
{
|
||||
$environment = $this->getEnvironment();
|
||||
if (($environment != false) && file_exists('.mage/config/environment/' . $environment . '.yml')) {
|
||||
$this->_config['environment'] = spyc_load_file('.mage/config/environment/' . $environment . '.yml');
|
||||
|
||||
// Create temporal directory for clone
|
||||
if (isset($this->_config['environment']['deployment']['source']) && is_array($this->_config['environment']['deployment']['source'])) {
|
||||
if (trim($this->_config['environment']['deployment']['source']['temporal']) == '') {
|
||||
$this->_config['environment']['deployment']['source']['temporal'] = '/tmp';
|
||||
}
|
||||
$newTemporal = rtrim($this->_config['environment']['deployment']['source']['temporal'], '/')
|
||||
. '/' . md5(microtime()) . '/';
|
||||
$this->_config['environment']['deployment']['source']['temporal'] = $newTemporal;
|
||||
}
|
||||
return true;
|
||||
|
||||
} else if (($environment != '') && !file_exists('.mage/config/environment/' . $environment . '.yml')) {
|
||||
throw new Exception('Environment does not exists.');
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get Environment root option
|
||||
*
|
||||
* @param string $option
|
||||
* @param mixed $default
|
||||
* @return mixed
|
||||
*/
|
||||
private function _getEnvironmentOption($option, $default = array())
|
||||
{
|
||||
$config = $this->_config['environment'];
|
||||
if (isset($config[$option])) {
|
||||
return $config[$option];
|
||||
} else {
|
||||
return $default;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user