Commit e6a0668a authored by Adrian Rossouw's avatar Adrian Rossouw

Pull the config file generation in the http classes upstream into the base service class.

lots of other changes too.
parent 9e112404
<?php
class provisionService_http_apache extends provisionService_http {
class provisionService_http_apache extends provisionService_http_public {
protected $application_name = 'apache';
protected $has_restart_cmd = TRUE;
function default_restart_cmd() {
return provisionService_http_apache::apache_restart_cmd();
}
function cloaked_db_creds() {
return TRUE;
}
function init() {
parent::init();
$this->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';
}
......@@ -17,13 +17,13 @@ NameVirtualHost *:<?php print $http_port; ?>
</IfModule>
# virtual hosts
Include <?php print $apache_site_conf_path ?>
Include <?php print $http_vhostd_path ?>
# platforms
Include <?php print $apache_platform_conf_path ?>
Include <?php print $http_platformd_path ?>
# other configuration, not touched by aegir
Include <?php print $apache_conf_path ?>
Include <?php print $http_confd_path ?>
<?php print $extra_config; ?>
<VirtualHost *:80>
<VirtualHost *:<?php print $http_port ?>>
<?php if ($this->site_mail) : ?>
ServerAdmin <?php print $this->site_mail; ?>
<?php endif;?>
......
......@@ -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';
}
}
......@@ -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();
......
<?php
function drush_http_post_provision_deploy() {
d()->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();
}
......
......@@ -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();
}
......@@ -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();
}
......@@ -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.
*/
......
......@@ -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();
}
<?php
function drush_http_pre_provision_migrate() {
d()->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');
}
......@@ -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();
}
......@@ -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);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment