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 <?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; protected $has_restart_cmd = TRUE;
function default_restart_cmd() { 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 $command = '/usr/sbin/apachectl'; // A proper default for most of the world
foreach (explode(':', $_SERVER['PATH']) as $path) { foreach (explode(':', $_SERVER['PATH']) as $path) {
$options[] = "$path/apache2ctl"; $options[] = "$path/apache2ctl";
...@@ -25,91 +41,11 @@ class provisionService_http_apache extends provisionService_http { ...@@ -25,91 +41,11 @@ class provisionService_http_apache extends provisionService_http {
return "sudo $command graceful"; 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. * Restart apache to pick up the new config files.
*/ */
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();
}
function parse_configs() { function parse_configs() {
// This is required to be configurable, due to the fact that different return $this->restart();
// 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');
}
} }
} }
...@@ -132,6 +68,18 @@ class provisionConfig_apache_server extends provisionConfig_apache { ...@@ -132,6 +68,18 @@ class provisionConfig_apache_server extends provisionConfig_apache {
public $template = 'server.tpl.php'; public $template = 'server.tpl.php';
public $description = 'apache server configuration file'; 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() { function filename() {
return $this->data['server']->config_path . '/apache.conf'; return $this->data['server']->config_path . '/apache.conf';
} }
...@@ -150,7 +98,7 @@ class provisionConfig_apache_platform extends provisionConfig_apache { ...@@ -150,7 +98,7 @@ class provisionConfig_apache_platform extends provisionConfig_apache {
public $description = 'apache platform configuration file'; public $description = 'apache platform configuration file';
function filename() { 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() { function process() {
...@@ -167,7 +115,7 @@ class provisionConfig_apache_site extends provisionConfig_apache { ...@@ -167,7 +115,7 @@ class provisionConfig_apache_site extends provisionConfig_apache {
public $description = 'apache site configuration file'; public $description = 'apache site configuration file';
function filename() { function filename() {
return $this->data['apache_site_conf_path'] . '/' . $this->uri; return $this->data['http_vhostd_path'] . '/' . $this->uri;
} }
function process() { function process() {
...@@ -175,16 +123,12 @@ class provisionConfig_apache_site extends provisionConfig_apache { ...@@ -175,16 +123,12 @@ class provisionConfig_apache_site extends provisionConfig_apache {
$this->aliases = explode(",", $this->aliases); $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'] = "# Extra configuration from modules:\n";
$this->data['extra_config'] .= join("\n", drush_command_invoke_all('provision_apache_vhost_config', $this->uri, $this->data)); $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; ?> ...@@ -17,13 +17,13 @@ NameVirtualHost *:<?php print $http_port; ?>
</IfModule> </IfModule>
# virtual hosts # virtual hosts
Include <?php print $apache_site_conf_path ?> Include <?php print $http_vhostd_path ?>
# platforms # platforms
Include <?php print $apache_platform_conf_path ?> Include <?php print $http_platformd_path ?>
# other configuration, not touched by aegir # other configuration, not touched by aegir
Include <?php print $apache_conf_path ?> Include <?php print $http_confd_path ?>
<?php print $extra_config; ?> <?php print $extra_config; ?>
<VirtualHost *:80> <VirtualHost *:<?php print $http_port ?>>
<?php if ($this->site_mail) : ?> <?php if ($this->site_mail) : ?>
ServerAdmin <?php print $this->site_mail; ?> ServerAdmin <?php print $this->site_mail; ?>
<?php endif;?> <?php endif;?>
......
...@@ -11,10 +11,13 @@ ...@@ -11,10 +11,13 @@
class provisionService_http_apache_ssl extends provisionService_http_apache { class provisionService_http_apache_ssl extends provisionService_http_apache {
public $ssl_enabled = TRUE; 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 { class provisionConfig_apache_server_ssl extends provisionConfig_apache_server {
...@@ -27,6 +30,6 @@ class provisionConfig_apache_site_ssl extends provisionConfig_apache_site { ...@@ -27,6 +30,6 @@ class provisionConfig_apache_site_ssl extends provisionConfig_apache_site {
public $description = 'apache ssl configuration file'; public $description = 'apache ssl configuration file';
function filename() { function filename() {
$this->data['apache_site_conf_path'] . '/' . $this->uri . '_ssl'; $this->data['http_vhostd_path'] . '/' . $this->uri . '_ssl';
} }
} }
...@@ -2,7 +2,7 @@ ...@@ -2,7 +2,7 @@
function drush_http_provision_delete() { function drush_http_provision_delete() {
if (d()->type === 'site') { if (d()->type === 'site') {
d()->service('http')->delete_site_config(); d()->service('http')->delete_config('site');
} }
if (d()->type === 'platform') { if (d()->type === 'platform') {
$sites = provision_drupal_find_sites(); $sites = provision_drupal_find_sites();
...@@ -10,7 +10,7 @@ function drush_http_provision_delete() { ...@@ -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.')); 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 { else {
d()->service('http')->delete_platform_config(); d()->service('http')->delete_config('platform');
} }
} }
d()->service('http')->parse_configs(); d()->service('http')->parse_configs();
......
<?php <?php
function drush_http_post_provision_deploy() { function drush_http_post_provision_deploy() {
d()->service('http')->create_site_config(); d()->service('http')->create_config('site');
d()->service('http')->parse_configs(); d()->service('http')->parse_configs();
} }
...@@ -9,7 +9,7 @@ function drush_http_post_provision_deploy() { ...@@ -9,7 +9,7 @@ function drush_http_post_provision_deploy() {
* Delete the config file created. * Delete the config file created.
*/ */
function drush_http_post_provision_deploy_rollback() { function drush_http_post_provision_deploy_rollback() {
d()->service('http')->create_site_config(); d()->service('http')->create_config('site');
d()->service('http')->parse_configs(); d()->service('http')->parse_configs();
} }
......
...@@ -7,7 +7,7 @@ function drush_http_provision_disable() { ...@@ -7,7 +7,7 @@ function drush_http_provision_disable() {
drush_set_option('redirect_url', drush_get_option('web_disable_url')); drush_set_option('redirect_url', drush_get_option('web_disable_url'));
// TODO - this is majorly wrong i think. // TODO - this is majorly wrong i think.
d()->service('http')->create_site_config(); d()->service('http')->create_config('site');
d()->service('http')->parse_configs(); d()->service('http')->parse_configs();
} }
...@@ -4,7 +4,7 @@ ...@@ -4,7 +4,7 @@
* Implementation of hook_provision_enable * Implementation of hook_provision_enable
*/ */
function drush_http_provision_enable() { function drush_http_provision_enable() {
d()->service('http')->create_site_config(); d()->service('http')->create_config('site');
d()->service('http')->parse_configs(); d()->service('http')->parse_configs();
} }
...@@ -7,11 +7,38 @@ function http_provision_services() { ...@@ -7,11 +7,38 @@ function http_provision_services() {
return array('http' => NULL); return array('http' => NULL);
} }
// Base http service class.
class provisionService_http extends provisionService { class provisionService_http extends provisionService {
public $service = 'http'; public $service = 'http';
protected $ssl_enabled = FALSE;
}
class provisionService_http_public extends provisionService_http {
protected $has_port = TRUE; 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() { function init() {
parent::init(); parent::init();
...@@ -27,23 +54,15 @@ class provisionService_http extends provisionService { ...@@ -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_disable_url', $this->server->master_url .'/hosting/disabled');
$this->server->setProperty('web_maintenence_url', $this->server->master_url .'/hosting/maintenance'); $this->server->setProperty('web_maintenence_url', $this->server->master_url .'/hosting/maintenance');
if ($this->ssl_enabled) {
$this->init_ssl();
}
}
function init_ssl() { if (!is_null($this->application_name)) {
$this->server->setProperty('http_ssl_port', 443); $app_dir = "{$this->server->config_path}/{$this->application_name}";
$this->ssl_config_path = $this->server->aegir_root . '/config/ssl.d'; $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_port() { }
return 80;
} }
function default_ssl_port() {
return 443;
}
static function option_documentation() { static function option_documentation() {
return array( return array(
...@@ -55,20 +74,24 @@ class provisionService_http extends provisionService { ...@@ -55,20 +74,24 @@ class provisionService_http extends provisionService {
function verify() { function verify() {
switch (d()->type) { if (d()->type === 'server') {
case 'server': if (!is_null($this->application_name)) {
$this->create_server_config(); provision_file()->create_dir($this->server->http_confd_path, dt("Webserver custom configuration"), 0700);
break; $this->sync($this->server->http_confd_path);
case 'platform': provision_file()->create_dir($this->server->http_platformd_path, dt("Webserver platform configuration"), 0700);
$this->create_platform_config(); $this->sync($this->server->http_platformd_path, array(
break; 'exclude' => $this->server->http_platformd_path . '/*', // Make sure remote directory is created
));
case 'site':
$this->create_site_config(); provision_file()->create_dir($this->server->http_vhostd_path , dt("Webserver virtual host configuration"), 0700);
break; $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(); $this->parse_configs();
} }
...@@ -79,48 +102,6 @@ class provisionService_http extends provisionService { ...@@ -79,48 +102,6 @@ class provisionService_http extends provisionService {
return TRUE; 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. * Support the ability to cloak the database credentials using environment variables.
*/ */
......
...@@ -11,7 +11,7 @@ ...@@ -11,7 +11,7 @@
* Implementation of hook_provision_post_install * Implementation of hook_provision_post_install
*/ */
function drush_http_post_provision_install() { function drush_http_post_provision_install() {
d()->service('http')->create_site_config(); d()->service('http')->create_config('site');
d()->service('http')->parse_configs(); d()->service('http')->parse_configs();
} }
<?php <?php
function drush_http_pre_provision_migrate() { 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() { function drush_http_pre_provision_migrate_rollback() {
d()->service('http')->create_site_config(); d()->service('http')->create_config('site');
} }
...@@ -4,11 +4,11 @@ ...@@ -4,11 +4,11 @@
* Implementation of hook_provision_pre_restore * Implementation of hook_provision_pre_restore
*/ */
function drush_http_pre_provision_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() { function drush_http_pre_provision_restore_rollback() {
d()->service('http')->create_site_config(); d()->service('http')->create_config('site');
d()->service('http')->parse_configs(); d()->service('http')->parse_configs();
} }
...@@ -8,11 +8,14 @@ require_once DRUSH_BASE_PATH . '/commands/core/rsync.core.inc'; ...@@ -8,11 +8,14 @@ require_once DRUSH_BASE_PATH . '/commands/core/rsync.core.inc';
class provisionService extends provisionChainedState { class provisionService extends provisionChainedState {
protected $server = '@server_master'; protected $server = '@server_master';
public $context; public $context;
protected $service = null; protected $service = null;
protected $application_name = null;
protected $has_restart_cmd = FALSE; protected $has_restart_cmd = FALSE;
protected $has_port = FALSE; protected $has_port = FALSE;
protected $configs = array();
// All services have the ability to have an associated restart command and listen port. // All services have the ability to have an associated restart command and listen port.
function init() { function init() {
...@@ -35,7 +38,78 @@ class provisionService extends provisionChainedState { ...@@ -35,7 +38,78 @@ class provisionService extends provisionChainedState {
return false; return false;
}