diff --git a/Provision/Context.php b/Provision/Context.php index e7a579393d6243b8662b47dd74570c8d584f3a4f..8f63ed0047b6bd67f806f052fc240a58adf042a8 100644 --- a/Provision/Context.php +++ b/Provision/Context.php @@ -248,7 +248,7 @@ class Provision_Context { return $this->{$this->parent_key}->service($service, ($name) ? $name : $this->name); } else { - return new provisionService_null($this->name); + return new Provision_Service_null($this->name); } } diff --git a/Provision/Context/server.php b/Provision/Context/server.php index f5f6bdf6d5834dd4869d748065f723f92aeafa24..edca709005fd1bb01699c68818fc4398238cff35 100644 --- a/Provision/Context/server.php +++ b/Provision/Context/server.php @@ -15,7 +15,7 @@ class Provision_Context_server extends Provision_Context { /** * Associative array of services for this server. * - * @see provisionService + * @see Provision_Service */ protected $services = array(); @@ -106,7 +106,7 @@ class Provision_Context_server extends Provision_Context { } } else { - $this->services[$service] = new provisionService_null($this->name); + $this->services[$service] = new Provision_Service_null($this->name); } } diff --git a/Provision/Service.php b/Provision/Service.php new file mode 100644 index 0000000000000000000000000000000000000000..38a2f62bf906a44d7fcef93425865f6eafd66112 --- /dev/null +++ b/Provision/Service.php @@ -0,0 +1,323 @@ +<?php + +require_once DRUSH_BASE_PATH . '/commands/core/rsync.core.inc'; + + +class Provision_Service extends Provision_ChainedState { + + /** + * The server this service is associated to + */ + protected $server = '@server_master'; + + /** + * The context in which this service stores its data + * + * This is usually an object made from a class derived from the + * Provision_Context base class + * + * @see Provision_Context + */ + public $context; + + protected $service = null; + protected $application_name = null; + + protected $has_restart_cmd = FALSE; + protected $has_port = FALSE; + + protected $configs = array(); + + + protected $config_cache = array(); + private $_config = null; + + + /** + * Implement the __call magic method. + * + * This implementation is really simple. It simply return null if the + * method doesn't exist. + * + * This is used so that we can create methods for drush commands, and + * can fail safely. + */ + function __call($name, $args = array()) { + return provision::method_invoke($this, $name, $args); + } + + + function init() { + + } + + // All services have the ability to have an associated restart command and listen port. + function init_server() { + if (!is_null($this->service)) { + if ($this->has_port) { + $this->server->setProperty($this->service . '_port', $this->default_port()); + } + if ($this->has_restart_cmd) { + $this->server->setProperty($this->service . '_restart_cmd', $this->default_restart_cmd()); + } + } + return TRUE; + } + + function init_platform() { + + } + + function init_site() { + + } + + + function default_port() { + return false; + } + + function default_restart_cmd() { + return false; + } + + /** + * Set the currently active configuration object. + * + * @param $config + * String: Name of config file. The key to the $this->configs array. + * @param $data + * Any optional information to be made available to templates. If a string, it will be + * turned into an array with the 'name' property the value of the string. + */ + function config($config, $data = array()) { + $this->_config = null; + + if (!isset($this->configs[$config])) { + $service = (!is_null($this->application_name)) ? $this->application_name : $this->service; + drush_log(dt('%service has no %name config file', array( + '%service' => $service, + '%name' => $config)) + ); + return $this; + } + + if (!is_array($data) && is_string($data)) { + $data = array('name' => $data); + } + + if (!isset($this->config_cache[$this->context->name][$config])) { + foreach ((array) $this->configs[$config] as $class) { + $this->config_cache[$this->context->name][$config] = new $class($this->context, array_merge($this->config_data($config), $data)); + } + } + + if (isset($this->config_cache[$this->context->name][$config])) { + $this->_config = $this->config_cache[$this->context->name][$config]; + } + + return $this; + } + + /** + * Unlink the currently active config file. + */ + function unlink() { + if (is_object($this->_config)) { + $this->_config->unlink(); + } + + return $this; + } + + /** + * Write the currently active config file. + */ + function write() { + if (is_object($this->_config)) { + $this->_config->write(); + } + + return $this; + } + + /** + * Set a record on the data store of the currently active config file (if applicable). + */ + function record_set($arg1, $arg2 = null) { + if (is_object($this->_config)) { + if (is_object($this->_config->store)) { + if (is_array($arg1)) { + $this->_config->store->records = array_merge($this->_config->store->records, $arg1); + } + elseif (!is_numeric($arg1)) { + if (is_array($arg2)) { + if (!isset($this->_config->store->loaded_records[$arg1]) + || !is_array($this->_config->store->loaded_records[$arg1])) + { + $this->_config->store->loaded_records[$arg1] = array(); + } + if (!isset($this->_config->store->records[$arg1]) + || !is_array($this->_config->store->records[$arg1])) + { + $this->_config->store->records[$arg1] = array(); + } + $this->_config->store->records[$arg1] = array_merge($this->_config->store->loaded_records[$arg1], $this->_config->store->records[$arg1], $arg2); + } else { + $this->_config->store->records[$arg1] = $arg2; + } + } + } + } + return $this; + } + + /** + * Delete a record from the data store of the currently active config file (if applicable). + */ + function record_del($record) { + return $this->record_set($record, null); + } + + /** + * Check if a record exists in the data store of the currently active config file (if applicable). + */ + function record_exists($record) { + if (is_object($this->_config)) { + if (is_object($this->_config->store)) { + return array_key_exists($record, $this->_config->store->merged_records()); + } + } + return FALSE; + } + + /** + * Fetch record(s) from the data store of the currently active config file (if applicable). + */ + function record_get($key = null, $default = null) { + if (is_object($this->_config)) { + if (is_object($this->_config->store)) { + $records = $this->_config->store->merged_records(); + + if (is_null($key)) { + return $records; + } + + if (isset($records[$key])) { + return $records[$key]; + } + } + } + return $default; + } + + + /** + * Generate a configuration file. + * + * This method will fetch the class to instantiate from the internal + * $this->configs control array. + */ + function create_config($config, $data = array()) { + $this->config($config, $data)->write(); + } + + /** + * Delete a configuration file. + * + * This method will fetch the class to instantiate from the internal + * $this->configs control array. + */ + function delete_config($config, $data = array()) { + $this->config($config, $data)->unlink(); + } + + /** + * Fetch extra information the service wants to pass to he config file classes. + */ + function config_data($config = null, $class = null) { + $data = array(); + // Always pass the server this service is running on to configs. + $data['server'] = $this->server; + + if (!is_null($this->application_name)) { + // This value may be useful to standardize paths in config files. + $data['application_name'] = $this->application_name; + } + return $data; + } + + + /** + * Restart the service using the provided restart command. + */ + function restart() { + // Only attempt to restart real services can have restart commands. + if (!is_null($this->service) && $this->has_restart_cmd) { + $service = (!is_null($this->application_name)) ? $this->application_name : $this->service; + + // Only attempt to restart if the command has been filled in. + if ($cmd = $this->server->{"{$this->service}_restart_cmd"}) { + if ($this->server->shell_exec($cmd)) { + drush_log(dt('%service on %server has been restarted', array( + '%service' => $service, + '%server' => $this->server->remote_host)) + ); + + return TRUE; + } + else { + drush_log(dt('%service on %server could not be restarted.'. + ' Changes might not be available until this has been done. (error: %msg)', array( + '%service' => $service, + '%server' => $this->server->remote_host, + '%msg' => join("\n", drush_shell_exec_output()))), 'warning'); + } + } + } + return FALSE; + } + + function __construct($server) { + $this->server = is_object($server) ? $server : d($server); + } + + /** + * Set the currently active context of the service. + * + * @arg mixed $context + * the context to store this services data into. this can be an + * object, or a string in which case the object will be loaded + * dynamically with d() + * + * @see d() + */ + function setContext($context) { + $this->context = is_object($context) ? $context : d($context); + } + + /** + * Sync filesystem changes to the server hosting this service. + */ + function sync($path = NULL, $additional_options = array()) { + return $this->server->sync($path, $additional_options); + } + + function fetch($path = NULL) { + return $this->server->fetch($path); + } + + function verify() { + return TRUE; + } + + /** + * Return service-specific configuration options for help. + * + * @return + * array('--option' => 'description') + */ + static function option_documentation() { + return array(); + } +} diff --git a/Provision/Service/null.php b/Provision/Service/null.php new file mode 100644 index 0000000000000000000000000000000000000000..5efd8d20352177b8552e7b970c78c527cf37c4c4 --- /dev/null +++ b/Provision/Service/null.php @@ -0,0 +1,27 @@ +<?php + +/** + * Null service class. + * + * Not all services are necessary or turned on. + * This class ensures that not having a specific service + * doesnt result in catastrophic failure. + */ +class Provision_Service_null extends Provision_Service { + + function __get($name) { + return false; + } + + function __call($name, $args) { + return false; + } + + /** + * Null services do not synch files to the remote server, + * because they have no associated config files. + */ + function sync() { + return null; + } +} diff --git a/db/db.drush.inc b/db/db.drush.inc index 79850b096907b899d89a8d8064b92618a3357754..d6d5acc1cd076ee5948812cb06d5d4674c25eb95 100644 --- a/db/db.drush.inc +++ b/db/db.drush.inc @@ -33,7 +33,7 @@ function db_drush_help($section) { } -class provisionService_db extends provisionService { +class provisionService_db extends Provision_Service { protected $service = 'db'; /** diff --git a/dns/dns.drush.inc b/dns/dns.drush.inc index 9605e3aa4a159bd22c6cc966b319ec77a0a4724f..abbd805036bedf54ee9d6762cf78e8c7f7535a00 100644 --- a/dns/dns.drush.inc +++ b/dns/dns.drush.inc @@ -73,7 +73,7 @@ function dns_provision_services() { -class provisionService_dns extends provisionService { +class provisionService_dns extends Provision_Service { public $service = 'dns'; public $slave = null; diff --git a/example/example.drush.inc b/example/example.drush.inc index db8489712a6d384406f954519361805b5a531b84..a4197f673b31ea511f3a4526a4ff481cb7c76bab 100644 --- a/example/example.drush.inc +++ b/example/example.drush.inc @@ -35,7 +35,7 @@ function example_provision_services() { * This class should define the 'public API' to be used by the rest * of the system, which should not expose implementation details. */ -class provisionService_example extends provisionService { +class provisionService_example extends Provision_Service { public $service = 'http'; /** diff --git a/http/http.drush.inc b/http/http.drush.inc index 2f3b81164643c0a56a5f4899736b85bcd6d65b94..97439d6f268703a7cf91c6b56efc583a965db37d 100644 --- a/http/http.drush.inc +++ b/http/http.drush.inc @@ -8,7 +8,7 @@ function http_provision_services() { // Base http service class. -class provisionService_http extends provisionService { +class provisionService_http extends Provision_Service { public $service = 'http'; protected $ssl_enabled = FALSE; diff --git a/provision.service.inc b/provision.service.inc index 68270f791c36a7ce939250dc30a1806f13e4439d..e08dd2b59d92c518b3bea197ec5e5a38b6336824 100644 --- a/provision.service.inc +++ b/provision.service.inc @@ -1,352 +1,5 @@ <?php +// We leave this here as a compatilibity layer, essentially all this does is +// load the autoloader. require_once('provision.inc'); - -require_once DRUSH_BASE_PATH . '/commands/core/rsync.core.inc'; - - -class provisionService extends Provision_ChainedState { - - /** - * The server this service is associated to - */ - protected $server = '@server_master'; - - /** - * The context in which this service stores its data - * - * This is usually an object made from a class derived from the - * Provision_Context base class - * - * @see Provision_Context - */ - public $context; - - protected $service = null; - protected $application_name = null; - - protected $has_restart_cmd = FALSE; - protected $has_port = FALSE; - - protected $configs = array(); - - - protected $config_cache = array(); - private $_config = null; - - - /** - * Implement the __call magic method. - * - * This implementation is really simple. It simply return null if the - * method doesn't exist. - * - * This is used so that we can create methods for drush commands, and - * can fail safely. - */ - function __call($name, $args = array()) { - return provision::method_invoke($this, $name, $args); - } - - - function init() { - - } - - // All services have the ability to have an associated restart command and listen port. - function init_server() { - if (!is_null($this->service)) { - if ($this->has_port) { - $this->server->setProperty($this->service . '_port', $this->default_port()); - } - if ($this->has_restart_cmd) { - $this->server->setProperty($this->service . '_restart_cmd', $this->default_restart_cmd()); - } - } - return TRUE; - } - - function init_platform() { - - } - - function init_site() { - - } - - - function default_port() { - return false; - } - - function default_restart_cmd() { - return false; - } - - /** - * Set the currently active configuration object. - * - * @param $config - * String: Name of config file. The key to the $this->configs array. - * @param $data - * Any optional information to be made available to templates. If a string, it will be - * turned into an array with the 'name' property the value of the string. - */ - function config($config, $data = array()) { - $this->_config = null; - - if (!isset($this->configs[$config])) { - $service = (!is_null($this->application_name)) ? $this->application_name : $this->service; - drush_log(dt('%service has no %name config file', array( - '%service' => $service, - '%name' => $config)) - ); - return $this; - } - - if (!is_array($data) && is_string($data)) { - $data = array('name' => $data); - } - - if (!isset($this->config_cache[$this->context->name][$config])) { - foreach ((array) $this->configs[$config] as $class) { - $this->config_cache[$this->context->name][$config] = new $class($this->context, array_merge($this->config_data($config), $data)); - } - } - - if (isset($this->config_cache[$this->context->name][$config])) { - $this->_config = $this->config_cache[$this->context->name][$config]; - } - - return $this; - } - - /** - * Unlink the currently active config file. - */ - function unlink() { - if (is_object($this->_config)) { - $this->_config->unlink(); - } - - return $this; - } - - /** - * Write the currently active config file. - */ - function write() { - if (is_object($this->_config)) { - $this->_config->write(); - } - - return $this; - } - - /** - * Set a record on the data store of the currently active config file (if applicable). - */ - function record_set($arg1, $arg2 = null) { - if (is_object($this->_config)) { - if (is_object($this->_config->store)) { - if (is_array($arg1)) { - $this->_config->store->records = array_merge($this->_config->store->records, $arg1); - } - elseif (!is_numeric($arg1)) { - if (is_array($arg2)) { - if (!isset($this->_config->store->loaded_records[$arg1]) - || !is_array($this->_config->store->loaded_records[$arg1])) - { - $this->_config->store->loaded_records[$arg1] = array(); - } - if (!isset($this->_config->store->records[$arg1]) - || !is_array($this->_config->store->records[$arg1])) - { - $this->_config->store->records[$arg1] = array(); - } - $this->_config->store->records[$arg1] = array_merge($this->_config->store->loaded_records[$arg1], $this->_config->store->records[$arg1], $arg2); - } else { - $this->_config->store->records[$arg1] = $arg2; - } - } - } - } - return $this; - } - - /** - * Delete a record from the data store of the currently active config file (if applicable). - */ - function record_del($record) { - return $this->record_set($record, null); - } - - /** - * Check if a record exists in the data store of the currently active config file (if applicable). - */ - function record_exists($record) { - if (is_object($this->_config)) { - if (is_object($this->_config->store)) { - return array_key_exists($record, $this->_config->store->merged_records()); - } - } - return FALSE; - } - - /** - * Fetch record(s) from the data store of the currently active config file (if applicable). - */ - function record_get($key = null, $default = null) { - if (is_object($this->_config)) { - if (is_object($this->_config->store)) { - $records = $this->_config->store->merged_records(); - - if (is_null($key)) { - return $records; - } - - if (isset($records[$key])) { - return $records[$key]; - } - } - } - return $default; - } - - - /** - * Generate a configuration file. - * - * This method will fetch the class to instantiate from the internal - * $this->configs control array. - */ - function create_config($config, $data = array()) { - $this->config($config, $data)->write(); - } - - /** - * Delete a configuration file. - * - * This method will fetch the class to instantiate from the internal - * $this->configs control array. - */ - function delete_config($config, $data = array()) { - $this->config($config, $data)->unlink(); - } - - /** - * Fetch extra information the service wants to pass to he config file classes. - */ - function config_data($config = null, $class = null) { - $data = array(); - // Always pass the server this service is running on to configs. - $data['server'] = $this->server; - - if (!is_null($this->application_name)) { - // This value may be useful to standardize paths in config files. - $data['application_name'] = $this->application_name; - } - return $data; - } - - - /** - * Restart the service using the provided restart command. - */ - function restart() { - // Only attempt to restart real services can have restart commands. - if (!is_null($this->service) && $this->has_restart_cmd) { - $service = (!is_null($this->application_name)) ? $this->application_name : $this->service; - - // Only attempt to restart if the command has been filled in. - if ($cmd = $this->server->{"{$this->service}_restart_cmd"}) { - if ($this->server->shell_exec($cmd)) { - drush_log(dt('%service on %server has been restarted', array( - '%service' => $service, - '%server' => $this->server->remote_host)) - ); - - return TRUE; - } - else { - drush_log(dt('%service on %server could not be restarted.'. - ' Changes might not be available until this has been done. (error: %msg)', array( - '%service' => $service, - '%server' => $this->server->remote_host, - '%msg' => join("\n", drush_shell_exec_output()))), 'warning'); - } - } - } - return FALSE; - } - - function __construct($server) { - $this->server = is_object($server) ? $server : d($server); - } - - /** - * Set the currently active context of the service. - * - * @arg mixed $context - * the context to store this services data into. this can be an - * object, or a string in which case the object will be loaded - * dynamically with d() - * - * @see d() - */ - function setContext($context) { - $this->context = is_object($context) ? $context : d($context); - } - - /** - * Sync filesystem changes to the server hosting this service. - */ - function sync($path = NULL, $additional_options = array()) { - return $this->server->sync($path, $additional_options); - } - - function fetch($path = NULL) { - return $this->server->fetch($path); - } - - function verify() { - return TRUE; - } - - /** - * Return service-specific configuration options for help. - * - * @return - * array('--option' => 'description') - */ - static function option_documentation() { - return array(); - } -} - -/** - * Null service class. - * - * Not all services are necessary or turned on. - * This class ensures that not having a specific service - * doesnt result in catastrophic failure. - */ -class provisionService_null extends provisionService { - - function __get($name) { - return false; - } - - function __call($name, $args) { - return false; - } - - /** - * Null services do not synch files to the remote server, - * because they have no associated config files. - */ - function sync() { - return null; - } -} -