From e6a0668a4551cd098655f61fe5f00071982d6ccb Mon Sep 17 00:00:00 2001 From: Adrian Rossouw Date: Tue, 13 Jul 2010 21:05:06 +0200 Subject: [PATCH] Pull the config file generation in the http classes upstream into the base service class. lots of other changes too. --- http/apache/apache_service.inc | 132 +++++++------------------ http/apache/server.tpl.php | 6 +- http/apache/vhost_disabled.tpl.php | 2 +- http/apache_ssl/apache_ssl_service.inc | 9 +- http/delete.provision.inc | 4 +- http/deploy.provision.inc | 4 +- http/disable.provision.inc | 2 +- http/enable.provision.inc | 2 +- http/http.drush.inc | 119 ++++++++++------------ http/install.provision.inc | 2 +- http/migrate.provision.inc | 4 +- http/restore.provision.inc | 4 +- provision.service.inc | 74 ++++++++++++++ 13 files changed, 183 insertions(+), 181 deletions(-) diff --git a/http/apache/apache_service.inc b/http/apache/apache_service.inc index be8a6656..ff0f940e 100644 --- a/http/apache/apache_service.inc +++ b/http/apache/apache_service.inc @@ -1,9 +1,25 @@ configs['server'][] = 'provisionConfig_apache_server'; + $this->configs['platform'][] = 'provisionConfig_apache_platform'; + $this->configs['site'][] = 'provisionConfig_apache_site'; + } + + public static function apache_restart_cmd() { $command = '/usr/sbin/apachectl'; // A proper default for most of the world foreach (explode(':', $_SERVER['PATH']) as $path) { $options[] = "$path/apache2ctl"; @@ -25,91 +41,11 @@ class provisionService_http_apache extends provisionService_http { return "sudo $command graceful"; } - function init() { - parent::init(); - $this->server->apache_site_conf_path = $this->server->config_path . '/vhost.d'; - $this->server->apache_platform_conf_path = $this->server->config_path . '/platform.d'; - $this->server->apache_conf_path = $this->server->config_path . '/apache.d'; - } - /** - * Apache uses mod_env to cloak the database credentials in settings.php. - */ - function cloaked_db_creds() { - return TRUE; - } - - /** - * Pass some extra paths to the config file classes, which specify the server relative paths - * for use in the filename() methods. - */ - function config_data() { - return array( - 'server' => $this->server, - 'http_port' => $this->server->http_port, - 'apache_site_conf_path' => $this->server->apache_site_conf_path, - 'apache_platform_conf_path' => $this->server->apache_platform_conf_path, - 'apache_conf_path' => $this->server->apache_conf_path - ); - } - - function create_site_config() { - if (d($this->context)->site_enabled) { - $config = new provisionConfig_apache_site($this->context, $this->config_data() + drush_get_context('site')); - } - else { - $config = new provisionConfig_apache_site_disabled($this->context, $this->config_data() + drush_get_context('site')); - } - - $config->write(); - } - - function delete_site_config() { - $config = new provisionConfig_apache_site($this->context, $this->config_data()); - $config->unlink(); - } - - function create_platform_config() { - $config = new provisionConfig_apache_platform($this->context, $this->config_data()); - return $config->write(); - } - - function delete_platform_config() { - $config = new provisionConfig_apache_platform($this->context, $this->config_data()); - $config->unlink(); - } - - function create_server_config() { - provision_file()->create_dir($this->server->apache_site_conf_path, dt("Virtual host configuration"), 0700); - $this->sync($this->server->apache_site_conf_path, array('exclude' => $this->server->apache_site_conf_path . '/*')); // Make sure remote directory is created - provision_file()->create_dir($this->server->apache_platform_conf_path, dt("Platforms configuration"), 0700); - $this->sync($this->server->apache_platform_conf_path, array('exclude' => $this->server->apache_platform_conf_path . '/*')); // Make sure remote directory is created - provision_file()->create_dir($this->server->apache_conf_path , dt("Apache configuration"), 0700); - $this->sync($this->server->apache_conf_path); - - $config = new provisionConfig_apache_server($this->context, $this->config_data()); - $config->write(); - - // Create a symlink to the site specific apache config in the top level config directory of each server. - // This way you can still include the same file across all servers. - - $this->server->shell_exec(sprintf('ln -sf %s %s', escapeshellarg($this->server->config_path . '/apache.conf'), escapeshellarg($this->server->aegir_root . '/config/apache.conf'))); - } - - function delete_server_config() { - $config = new provisionConfig_apache_server($this->context, $this->config_data()); - $config->unlink(); - } - + * Restart apache to pick up the new config files. + */ function parse_configs() { - // This is required to be configurable, due to the fact that different - // hosts might need to do this differently. - if ($this->server->shell_exec($this->server->http_restart_cmd)) { - drush_log(dt('Apache on %server has been restarted', array('%server' => $this->server->remote_host))); - } - else { - drush_log(dt('Web server %server could not be restarted. Changes might not be available until this has been done. (error: %msg)', array('%server' => $this->server->remote_host, '%msg' => join("\n", drush_shell_exec_output()))), 'warning'); - } + return $this->restart(); } } @@ -132,6 +68,18 @@ class provisionConfig_apache_server extends provisionConfig_apache { public $template = 'server.tpl.php'; public $description = 'apache server configuration file'; + function write() { + parent::write(); + + // We link the apache.config file on the remote server to the right version. + $cmd = sprintf('ln -sf %s %s', + escapeshellarg($this->data['server']->config_path . '/apache.conf'), + escapeshellarg($this->data['server']->aegir_root . '/config/apache.conf') + ); + + $this->data['server']->shell_exec($cmd); + } + function filename() { return $this->data['server']->config_path . '/apache.conf'; } @@ -150,7 +98,7 @@ class provisionConfig_apache_platform extends provisionConfig_apache { public $description = 'apache platform configuration file'; function filename() { - return $this->data['apache_platform_conf_path'] . '/' . ltrim($this->owner->name, '@') . '.conf'; + return $this->data['http_platformd_path'] . '/' . ltrim($this->owner->name, '@') . '.conf'; } function process() { @@ -167,7 +115,7 @@ class provisionConfig_apache_site extends provisionConfig_apache { public $description = 'apache site configuration file'; function filename() { - return $this->data['apache_site_conf_path'] . '/' . $this->uri; + return $this->data['http_vhostd_path'] . '/' . $this->uri; } function process() { @@ -175,16 +123,12 @@ class provisionConfig_apache_site extends provisionConfig_apache { $this->aliases = explode(",", $this->aliases); } + if (!$this->site_enabled) { + $this->template = 'vhost_disabled.tpl.php'; + } + $this->data['extra_config'] = "# Extra configuration from modules:\n"; $this->data['extra_config'] .= join("\n", drush_command_invoke_all('provision_apache_vhost_config', $this->uri, $this->data)); } } -/** - * Apache configuration file class for disabled sites. - */ -class provisionConfig_apache_site_disabled extends provisionConfig_apache_site { - public $template = 'vhost_disabled.tpl.php'; -} - - diff --git a/http/apache/server.tpl.php b/http/apache/server.tpl.php index f22f70ac..0654ed41 100644 --- a/http/apache/server.tpl.php +++ b/http/apache/server.tpl.php @@ -17,13 +17,13 @@ NameVirtualHost *: # virtual hosts -Include +Include # platforms -Include +Include # other configuration, not touched by aegir -Include +Include diff --git a/http/apache/vhost_disabled.tpl.php b/http/apache/vhost_disabled.tpl.php index 5d3a045f..57a1b05c 100644 --- a/http/apache/vhost_disabled.tpl.php +++ b/http/apache/vhost_disabled.tpl.php @@ -1,4 +1,4 @@ - +> site_mail) : ?> ServerAdmin site_mail; ?> diff --git a/http/apache_ssl/apache_ssl_service.inc b/http/apache_ssl/apache_ssl_service.inc index 06097686..c5322abc 100644 --- a/http/apache_ssl/apache_ssl_service.inc +++ b/http/apache_ssl/apache_ssl_service.inc @@ -11,10 +11,13 @@ class provisionService_http_apache_ssl extends provisionService_http_apache { public $ssl_enabled = TRUE; - class create_server_config() { + function init() { + parent::init(); + $this->configs['server'] = array(); + $this->configs['server'][] = 'provisionConfig_apache_server_ssl'; + $this->configs['site'][] = 'provisionConfig_apache_site_ssl'; } - } class provisionConfig_apache_server_ssl extends provisionConfig_apache_server { @@ -27,6 +30,6 @@ class provisionConfig_apache_site_ssl extends provisionConfig_apache_site { public $description = 'apache ssl configuration file'; function filename() { - $this->data['apache_site_conf_path'] . '/' . $this->uri . '_ssl'; + $this->data['http_vhostd_path'] . '/' . $this->uri . '_ssl'; } } diff --git a/http/delete.provision.inc b/http/delete.provision.inc index 98ded095..5788d263 100644 --- a/http/delete.provision.inc +++ b/http/delete.provision.inc @@ -2,7 +2,7 @@ function drush_http_provision_delete() { if (d()->type === 'site') { - d()->service('http')->delete_site_config(); + d()->service('http')->delete_config('site'); } if (d()->type === 'platform') { $sites = provision_drupal_find_sites(); @@ -10,7 +10,7 @@ function drush_http_provision_delete() { drush_set_error(dt('Existing sites were found on this platform. These sites will need to be deleted before this platform can be deleted.')); } else { - d()->service('http')->delete_platform_config(); + d()->service('http')->delete_config('platform'); } } d()->service('http')->parse_configs(); diff --git a/http/deploy.provision.inc b/http/deploy.provision.inc index fe5d45a6..9bf875ed 100644 --- a/http/deploy.provision.inc +++ b/http/deploy.provision.inc @@ -1,7 +1,7 @@ service('http')->create_site_config(); + d()->service('http')->create_config('site'); d()->service('http')->parse_configs(); } @@ -9,7 +9,7 @@ function drush_http_post_provision_deploy() { * Delete the config file created. */ function drush_http_post_provision_deploy_rollback() { - d()->service('http')->create_site_config(); + d()->service('http')->create_config('site'); d()->service('http')->parse_configs(); } diff --git a/http/disable.provision.inc b/http/disable.provision.inc index b257fe5e..0cf17535 100644 --- a/http/disable.provision.inc +++ b/http/disable.provision.inc @@ -7,7 +7,7 @@ function drush_http_provision_disable() { drush_set_option('redirect_url', drush_get_option('web_disable_url')); // TODO - this is majorly wrong i think. - d()->service('http')->create_site_config(); + d()->service('http')->create_config('site'); d()->service('http')->parse_configs(); } diff --git a/http/enable.provision.inc b/http/enable.provision.inc index e0c6ac53..ba01dc6e 100644 --- a/http/enable.provision.inc +++ b/http/enable.provision.inc @@ -4,7 +4,7 @@ * Implementation of hook_provision_enable */ function drush_http_provision_enable() { - d()->service('http')->create_site_config(); + d()->service('http')->create_config('site'); d()->service('http')->parse_configs(); } diff --git a/http/http.drush.inc b/http/http.drush.inc index 3b8d0210..5f8858b9 100644 --- a/http/http.drush.inc +++ b/http/http.drush.inc @@ -7,11 +7,38 @@ function http_provision_services() { return array('http' => NULL); } + +// Base http service class. class provisionService_http extends provisionService { public $service = 'http'; + protected $ssl_enabled = FALSE; +} +class provisionService_http_public extends provisionService_http { protected $has_port = TRUE; - protected $ssl_enabled = FALSE; + + function default_port() { + return 80; + } + + function config_data($config = null, $class = null) { + $data = parent::config_data($config, $class); + + $data['http_port'] = $this->server->http_port; + + if (!is_null($this->application_name)) { + $data['http_confd_path'] = $this->server->http_confd_path; + $data['http_platformd_path'] = $this->server->http_platformd_path; + $data['http_vhostd_path'] = $this->server->http_vhostd_path; + } + + // TODO: move away from drush_get_context entirely. + if ($config == 'site') { + $data = array_merge($data, drush_get_context_site('site')); + } + + return $data; + } function init() { parent::init(); @@ -27,23 +54,15 @@ class provisionService_http extends provisionService { $this->server->setProperty('web_disable_url', $this->server->master_url .'/hosting/disabled'); $this->server->setProperty('web_maintenence_url', $this->server->master_url .'/hosting/maintenance'); - if ($this->ssl_enabled) { - $this->init_ssl(); - } - } - function init_ssl() { - $this->server->setProperty('http_ssl_port', 443); - $this->ssl_config_path = $this->server->aegir_root . '/config/ssl.d'; - } - - function default_port() { - return 80; + if (!is_null($this->application_name)) { + $app_dir = "{$this->server->config_path}/{$this->application_name}"; + $this->server->http_confd_path = "{$app_dir}/conf.d"; + $this->server->http_platformd_path = "{$app_dir}/platform.d"; + $this->server->http_vhostd_path = "{$app_dir}/vhost.d"; + } } - function default_ssl_port() { - return 443; - } static function option_documentation() { return array( @@ -55,20 +74,24 @@ class provisionService_http extends provisionService { function verify() { - switch (d()->type) { - case 'server': - $this->create_server_config(); - break; - - case 'platform': - $this->create_platform_config(); - break; - - case 'site': - $this->create_site_config(); - break; + if (d()->type === 'server') { + if (!is_null($this->application_name)) { + provision_file()->create_dir($this->server->http_confd_path, dt("Webserver custom configuration"), 0700); + $this->sync($this->server->http_confd_path); + + provision_file()->create_dir($this->server->http_platformd_path, dt("Webserver platform configuration"), 0700); + $this->sync($this->server->http_platformd_path, array( + 'exclude' => $this->server->http_platformd_path . '/*', // Make sure remote directory is created + )); + + provision_file()->create_dir($this->server->http_vhostd_path , dt("Webserver virtual host configuration"), 0700); + $this->sync($this->server->http_vhostd_path, array( + 'exclude' => $this->server->http_vhostd_path . '/*', // Make sure remote directory is created + )); + } } + $this->create_config(d()->type); $this->parse_configs(); } @@ -79,48 +102,6 @@ class provisionService_http extends provisionService { return TRUE; } - /** - * Generate a site specific configuration file - */ - function create_site_config() { - return TRUE; - } - - /** - * Remove an existing site configuration file. - */ - function delete_site_config() { - return TRUE; - } - - /** - * Add a new platform specific configuration file. - */ - function create_platform_config() { - return TRUE; - } - - /** - * Remove an existing platform configuration file. - */ - function delete_platform_config() { - return TRUE; - } - - /** - * Create a new server specific configuration file. - */ - function create_server_config() { - return TRUE; - } - - /** - * Remove an existing server specific configuration file - */ - function delete_server_config() { - return TRUE; - } - /** * Support the ability to cloak the database credentials using environment variables. */ diff --git a/http/install.provision.inc b/http/install.provision.inc index 07366db2..b48f1b2d 100644 --- a/http/install.provision.inc +++ b/http/install.provision.inc @@ -11,7 +11,7 @@ * Implementation of hook_provision_post_install */ function drush_http_post_provision_install() { - d()->service('http')->create_site_config(); + d()->service('http')->create_config('site'); d()->service('http')->parse_configs(); } diff --git a/http/migrate.provision.inc b/http/migrate.provision.inc index 57b69dd7..b21fd923 100644 --- a/http/migrate.provision.inc +++ b/http/migrate.provision.inc @@ -1,9 +1,9 @@ service('http')->delete_site_config(); + d()->service('http')->delete_config('site'); } function drush_http_pre_provision_migrate_rollback() { - d()->service('http')->create_site_config(); + d()->service('http')->create_config('site'); } diff --git a/http/restore.provision.inc b/http/restore.provision.inc index 09b87660..0af365f4 100644 --- a/http/restore.provision.inc +++ b/http/restore.provision.inc @@ -4,11 +4,11 @@ * Implementation of hook_provision_pre_restore */ function drush_http_pre_provision_restore() { - d()->service('http')->delete_site_config(); + d()->service('http')->delete_config('site'); } function drush_http_pre_provision_restore_rollback() { - d()->service('http')->create_site_config(); + d()->service('http')->create_config('site'); d()->service('http')->parse_configs(); } diff --git a/provision.service.inc b/provision.service.inc index aa579ab5..dd5c39a7 100644 --- a/provision.service.inc +++ b/provision.service.inc @@ -8,11 +8,14 @@ require_once DRUSH_BASE_PATH . '/commands/core/rsync.core.inc'; class provisionService extends provisionChainedState { protected $server = '@server_master'; public $context; + protected $service = null; + protected $application_name = null; protected $has_restart_cmd = FALSE; protected $has_port = FALSE; + protected $configs = array(); // All services have the ability to have an associated restart command and listen port. function init() { @@ -35,7 +38,78 @@ class provisionService extends provisionChainedState { return false; } + /** + * Generate a configuration file. + * + * This method will fetch the class to instantiate from the internal + * $this->configs control array. + */ + function create_config($config) { + if (isset($this->configs[$config])) { + // We cast to array here to silently accept the non array version + foreach ((array) $this->configs[$config] as $class) { + $config = new $class($this->context, $this->config_data($config)); + $config->write(); + } + } + } + + /** + * Delete a configurationf file. + * + * This method will fetch the class to instantiate from the internal + * $this->configs control array. + */ + function delete_config($config) { + if (isset($this->configs[$config])) { + // We cast to array here to silently accept the non array version + foreach ((array) $this->configs[$config] as $class) { + $config = new $class($this->context, $this->config_data($config, $class)); + $config->unlink(); + } + } + } + + /** + * Fetch extra information the service wants to pass to he config file classes. + */ + function config_data($config = null, $class = null) { + $data = array(); + + $data['server'] = $this->server; + 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 = d($server); -- GitLab