Commit 6b78a6d6 authored by Adrian Rossouw's avatar Adrian Rossouw

introduced a new config file class, that drastically reduced to amount of duplication

for the apache module.

also introduce an http service class with the apache driver.

needs love.
parent 5163f7db
<?php
class provisionService_http_apache extends provisionService {
function templates() {
$this->templates['vhost'] = 'vhost.tpl.php';
$this->templates['vhost_disabled'] = 'vhost_disabled.tpl.php';
$this->templates['platform'] = 'platform.tpl.php';
$this->templates['server'] = 'server.tpl.php';
}
class provisionService_http_apache extends provisionService_http {
function create_site_config($url) {
$file = drush_get_option('vhost_path') . '/' . $url . '_' . drush_get_option('site_port');
drush_log(dt("Generating apache host configuration file %file.", array('%file' => basename($file))));
return $this->write_config($file, 'vhost', drush_get_merged_options());
$config = new provisionConfig_apache_site(drush_get_merged_options());
return $config->write();
}
function delete_site_config($url) {
$file = drush_get_option('vhost_path') . '/' . $url . '_' . drush_get_option('site_port');
provision_path("unlink", $file, TRUE, dt("Removed apache virtual host configuration"));
$config = new provisionConfig_apache_site(drush_get_merged_options());
return $config->unlink();
}
function create_platform_config() {
$file = drush_get_option('platform_conf_path') . '/platform_' . drush_get_option('platform') . '.conf';
drush_log(dt("Generating apache host configuration file %file.", array('%file' => basename($file))));
return $this->write_config($file, 'platform', drush_get_merged_options());
$config = new provisionConfig_apache_platform(drush_get_merged_options());
return $config->write();
}
function delete_site_config($url) {
$file = drush_get_option('platform_conf_path') . '/platform_' . drush_get_option('platform');
provision_path("unlink", $file, TRUE, dt("Removed apache platform configuration"));
function delete_platform_config($url) {
$config = new provisionConfig_apache_platform(drush_get_merged_options());
return $config->unlink();
}
function create_server_config() {
$file = drush_get_option('conf_path') . '/apache.conf';
$config = new provisionConfig_apache_server(drush_get_merged_options());
return $config->write();
}
function delete_server_config($url) {
$config = new provisionConfig_apache_server(drush_get_merged_options());
return $config->unlink();
}
drush_log(dt("Generating apache host configuration file %file.", array('%file' => basename($file))));
return $this->write_config($file, 'server', drush_get_merged_options());
}
function verify($url) {
if (PROVISION_CONTEXT_PLATFORM) {
_provision_create_dir(drush_get_option('vhost_path'), dt("Virtual host configuration"), 0700);
_provision_create_dir(drush_get_option('platform_conf_path'), dt("Platforms configuration"), 0700);
provision_service('file')->create_dir(drush_get_option('vhost_path'), dt("Virtual host configuration"), 0700);
provision_service('file')->create_dir(drush_get_option('platform_conf_path'), dt("Platforms configuration"), 0700);
if (drush_get_option('platform', null)) {
$this->create_platform_config();
......@@ -79,8 +77,61 @@ class provisionService_http_apache extends provisionService {
}
}
class provisionConfig_apache extends provisionConfig {
}
class provisionConfig_apache_server extends provisionConfig_apache {
public $template = 'server.tpl.php';
public $description = 'apache server configuration file';
function filename() {
return $this->data['config_path'] . '/' . 'apache.conf';
}
function process() {
$this->data['extra_config'] = "# Extra configuration from modules:\n";
$this->data['extra_config'] .= join("\n", drush_command_invoke_all('provision_apache_server_config', $this->data));
}
}
class provisionConfig_apache_platform extends provisionConfig_apache {
public $template = 'platform.tpl.php';
public $description = 'apache platform configuration file';
function delete_server_config() {
function filename() {
return $this->data['platform_conf_path'] . '/platform_' . $this->data['platform'] . '.conf';
}
function process() {
$this->data['extra_config'] = "# Extra configuration from modules:\n";
$this->data['extra_config'] .= join("\n", drush_command_invoke_all('provision_apache_dir_config', $this->data));
}
}
class provisionConfig_apache_site extends provisionConfig_apache {
public $template = 'vhost.tpl.php';
public $description = 'apache site configuration file';
function filename() {
return $this->data['vhost_path'] . '/' . $this->data['site_url'] . '_' . $this->data['site_port'];
}
function process() {
if (!$this->data['site_port'] || $this->data['site_port'] < 1 || $this->data['site_port'] > 66535) {
$this->data['site_port'] = 80;
}
if ($this->data['aliases'] && !is_array($this->data['aliases'])) {
$this->data['aliases'] = explode(",", $this->data['aliases']);
}
$this->data['extra_config'] = "# Extra configuration from modules:\n";
$this->data['extra_config'] .= join("\n", drush_command_invoke_all('provision_apache_vhost_config', $this->data['site_url'], $this->data));
}
}
<Directory <?php print $publish_path; ?>>
Order allow,deny
Allow from all
<?php print $extra_config; ?>
</Directory>
# Aegir web server configuration file
<?php if (is_array($web_ports)) :
foreach ($web_ports as $web_port) :?>
NameVirtualHost *:<?php print $web_port; ?>
<VirtualHost *:<?php print $web_port; ?>>
ServerName default
Redirect 404 /
</VirtualHost>
<?php
endforeach;
endif;
?>
<IfModule !env_module>
LoadModule env_module modules/mod_env.so
</IfModule>
<IfModule !rewrite_module>
LoadModule rewrite_module modules/mod_rewrite.so
</IfModule>
Include <?php print $config_path ?>/vhost.d/
Include <?php print $config_path ?>/platform.d/
<?php print $extra_config; ?>
<VirtualHost *:<?php print $site_port; ?>>
<?php if ($site_mail) : ?>
ServerAdmin <?php print $site_mail; ?>
<?php endif;?>
DocumentRoot <?php print $publish_path; ?>
ServerName <?php print $site_url; ?>
SetEnv db_type <?php print urlencode($db_type); ?>
SetEnv db_name <?php print urlencode($db_name); ?>
SetEnv db_user <?php print urlencode($db_user); ?>
SetEnv db_passwd <?php print urlencode($db_passwd); ?>
SetEnv db_host <?php print urlencode($db_host); ?>
<?php if (!$redirection && is_array($aliases)) :
foreach ($aliases as $alias_url) :
if (trim($alias_url)) : ?>
ServerAlias <?php print $alias_url; ?>
<?php
endif;
endforeach;
endif; ?>
<?php print $extra_config; ?>
# Error handler for Drupal > 4.6.7
<Directory "<?php print $publish_path; ?>/sites/<?php print trim($site_url, '/'); ?>/files">
SetHandler This_is_a_Drupal_security_line_do_not_remove
</Directory>
# @todo make this configurable and more intelligent
# php_admin_value open_basedir /tmp:<?php print rtrim($publish_path, '/') ?>/:<?php print rtrim($config_path, '/') ?>/includes/:/usr/share/php/
</VirtualHost>
<?php
if ($redirection) {
include('vhost_redirect.tpl.php');
}
<VirtualHost *:80>
<?php if ($site_mail) : ?>
ServerAdmin <?php print $site_mail; ?>
<?php endif;?>
DocumentRoot <?php print $publish_path; ?>
ServerName <?php print $site_url; ?>
<?php if (is_array($aliases)) :
foreach ($aliases as $alias) : ?>
ServerAlias <?php print $alias; ?>
<?php
endforeach;
endif; ?>
RewriteEngine on
# the ? at the end is to remove any query string in the original url
RewriteRule ^(.*)$ <?php print $redirect_url . '/' . $site_url ?>?
</VirtualHost>
<VirtualHost *:<?php print $site_port; ?>>
<?php if ($site_mail) : ?>
ServerAdmin <?php print $site_mail; ?>
<?php endif;?>
<?php if (is_array($aliases) && count($aliases)): ?>
ServerName <?php print array_pop($aliases); ?>
<?php if (count($aliases)): ?>
ServerAlias <?php print join(" ", $aliases); ?>
<?php endif; ?>
<?php else:
# this should never happen and has the potential of creating an infinite redirection loop
?>
ServerName <?php print $site_url ?>
<?php endif; ?>
<?php if ($ssl_redirect): ?>
RedirectMatch permanent ^(.*) https://<?php print $site_url ?>$1
<?php else: ?>
RedirectMatch permanent ^(.*) http://<?php print $site_url ?>$1
<?php endif; ?>
</VirtualHost>
<?php
function drush_provision_apache_provision_delete($url = NULL) {
function drush_http_provision_delete($url = NULL) {
if (PROVISION_CONTEXT_SITE) {
_provision_apache_delete_vhost_config($url);
provision_service('http')->delete_site_config($url);
}
if (PROVISION_CONTEXT_PLATFORM) {
$options = drush_get_merged_options();
$sites = provision_drupal_find_sites();
if($sites) {
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 {
_provision_apache_delete_platform_config('platform_' . $options['platform'] . '.conf');
provision_service('http')->delete_platform_config();
}
}
return _provision_apache_restart_apache();
return provision_service('http')->parse_configs();
}
<?php
function drush_http_post_provision_deploy($url = NULL) {
provision_service('http')->create_site_config($url);
provision_service('http')->parse_configs();
}
/**
* Delete the config file created.
*/
function drush_http_post_provision_deploy_rollback($url = NULL) {
provision_service('http')->create_site_config($url);
provision_service('http')->parse_configs();
}
......@@ -3,9 +3,11 @@
/**
* Implementation of hook_provision_disable
*/
function drush_provision_apache_provision_disable($url = NULL) {
function drush_http_provision_disable($url = NULL) {
drush_set_option('redirect_url', drush_get_option('web_disable_url'));
_provision_apache_create_vhost_config($url, _provision_apache_disabled_template());
_provision_apache_restart_apache();
// TODO - this is majorly wrong i think.
provision_service('http')->create_site_config($url);
provision_service('http')->parse_configs();
}
<?php
/**
* Implementation of hook_provision_enable
*/
function drush_http_provision_enable($url = NULL) {
provision_service('http')->create_site_config($url);
provision_service('http')->parse_configs();
}
......@@ -5,7 +5,12 @@ include_once(dirname(__FILE__) . '/../provision.service.inc');
function http_drush_init() {
$command = drush_get_command();
$command = explode(" ", $command['command']);
if (preg_match("/^provision-/", $command[0])) {
if (preg_match("/^provision-/", $command[0])) {
include_once('apache/apache_service.inc');
provision_service('http', new provisionService_http_apache());
// Set up defines for platform
$docroot = drush_get_option(array("r", "root"), $_SERVER['PWD']);
......@@ -52,7 +57,7 @@ class provisionService_http extends provisionService {
* Ask the web server to check for and load configuration changes.
*/
function parse_configs() {
return TRUE
return TRUE;
}
/**
......
......@@ -10,8 +10,8 @@
/**
* Implementation of hook_provision_post_install
*/
function drush_provision_apache_post_provision_install($url = NULL) {
_provision_apache_create_vhost_config($url);
_provision_apache_restart_apache();
function drush_http_post_provision_install($url = NULL) {
provision_service('http')->create_site_config($url);
provision_service('http')->parse_configs();
}
<?php
/**
* Implementation of hook_provision_pre_restore
*/
function drush_http_pre_provision_restore($url) {
drush_set_option('redirect_url', drush_get_option('web_maintenance_url'));
provision_service('http')->create_site_config($url);
provision_service('http')->parse_configs();
}
function drush_http_pre_provision_restore_rollback($url) {
provision_service('http')->create_site_config($url);
provision_service('http')->parse_configs();
}
function drush_http_post_provision_restore($url) {
provision_service('http')->create_site_config($url);
provision_service('http')->parse_configs();
}
// The old db details should have been switched back by the mysql rollback at this point.
function drush_http_post_provision_restore_rollback($url) {
drush_http_post_provision_restore($url);
}
<?php
function drush_provision_apache_provision_verify($url = null) {
function drush_http_provision_verify($url = null) {
provision_service('http')->verify($url);
}
function drush_provision_apache_post_provision_verify($url = null) {
function drush_http_post_provision_verify($url = null) {
if (PROVISION_CONTEXT_PLATFORM) {
drush_set_option('master_url', drush_get_option('master_url'), 'drupal');
drush_set_option('publish_path', realpath(drush_get_option('publish_path')), 'drupal');
......
<?php
class provisionService {
protected $templates = array();
function verify() {
return TRUE;
function __construct() {
$this->init();
}
function templates() {
$this->templates = array();
function verify() {
return TRUE;
}
function render_config($template, $variables) {
drush_errors_off();
extract($variables, EXTR_SKIP); // Extract the variables to a local namespace
ob_start(); // Start output buffering
eval('?>'. $template); // Generate content
$contents = ob_get_contents(); // Get the contents of the buffer
ob_end_clean(); // End buffering and discard
drush_errors_on();
return $contents; // Return the contents
function init() {
return TRUE;
}
function write_config($file, $key, $extra_data = array()) {
$data = drush_get_merged_options();
if (is_writeable($file)) {
if ($template = $this->load_template($key)) {
$file = fopen($file, "w");
$text = $this->render_config($template, $data);
fwrite($file, $text);
fclose($file);
}
}
}
}
class provisionConfig {
public $template = null;
public $data = array();
public $description = null;
function __construct() {
function __construct($data = array()) {
if (is_null($this->template)) {
throw(exception);
}
if (sizeof($data)) {
$this->data = $data;
}
}
function process() {
parent::process();
return true;
}
function filename() {
return false;
}
function load() {
$reflect = new reflectionObject($this);
$base_dir = $reflect->getFilename();
if (isset($this->templates[$key])) {
$file = $base_dir . '/' . $this->templates[$key];
private function load_template() {
$reflect = new reflectionObject($this);
$base_dir = dirname($reflect->getFilename());
if (isset($this->template)) {
$file = $base_dir . '/' . $this->template;
drush_log("Template loaded: $file");
if (file_exists($file) && is_readable($file)) {
return file_get_contents($file);
}
......@@ -63,12 +54,39 @@ class provisionConfig {
}
function render($template, $variables) {
private function render_template($template, $variables) {
drush_errors_off();
extract($variables, EXTR_SKIP); // Extract the variables to a local namespace
ob_start(); // Start output buffering
eval('?>'. $template); // Generate content
$contents = ob_get_contents(); // Get the contents of the buffer
ob_end_clean(); // End buffering and discard
drush_errors_on();
return $contents; // Return the contents
}
function write_config($extra_data) {
function write() {
$filename = $this->filename();
if ($filename && is_writeable(dirname($filename))) {
// manipulate data before passing to template.
$this->process();
if ($template = $this->load_template()) {
#todo - convert to file_service
$file = fopen($filename, "w");
$text = $this->render_template($template, $this->data);
fwrite($file, $text);
fclose($file);
if ($this->description) {
drush_log("Generated config : " . $this->description, 'success');
}
}
}
}
function unlink() {
return provision_service('file')->path('unlink', $this->filename());
}
}
......
<?php
function drush_provision_apache_post_provision_deploy($url = NULL) {
_provision_apache_create_vhost_config($url);
_provision_apache_restart_apache();
}
/**
* Delete the config file created.
*/
function drush_provision_apache_post_provision_deploy_rollback($url = NULL) {
_provision_apache_delete_vhost_config($url);
_provision_apache_restart_apache();
}
<?php
/**
* Implementation of hook_provision_enable
*/
function drush_provision_apache_provision_enable($url = NULL) {
_provision_apache_create_vhost_config($url);
_provision_apache_restart_apache();
}
<?php
// $Id$
/**
* @file
* Apache provisioning module
* This module simply serves to generate the virtual host entry, and make sure apache gets reloaded properly.
* Because Drupal is running via the command line for the entirety of this process, it is only necessary to make
* it available online once everything has been completed.
*
* This module still requires configuration and sanity checks. Need to figure out a way to inspect the apache configuration,
* to ensure that the sites are getting loaded up correctly.
*/
/**
* Implementation of hook_drush_init()
*
* This sets a few default drush options by automatically guessing
* proper defaults and settings based on the environment.
*/
function provision_apache_drush_init() {
$command = drush_get_command();
$command = explode(" ", $command['command']);
if (preg_match("/^provision-/", $command[0])) {
// Set up defines for platform
$docroot = drush_get_option(array("r", "root"), $_SERVER['PWD']);
$path = drush_set_default('docroot_path', rtrim(($docroot) ? $docroot : $_SERVER['DOCUMENT_ROOT'], '/'));
drush_set_default('sites_path', $path . '/sites');
drush_set_default('publish_path', realpath(drush_get_option('docroot_path')));
$parts = explode("/", $path);
array_pop($parts);
$parent_path = drush_set_default('parent_path', implode("/" , $parts));
drush_set_default('backup_path', $parent_path . '/backups');
drush_set_default('config_path', $parent_path . '/config');
$config_path = drush_get_option('config_path');
drush_set_default('vhost_path', $config_path . '/vhost.d');
drush_set_default('platform_conf_path', $config_path . '/platform.d');
// Commands
drush_set_default('restart_cmd', _provision_default_restart_cmd());
// System account
drush_set_default('web_group', _provision_default_web_group());
drush_set_default('script_user', get_current_user());
// Redirection urls
if (isset($GLOBALS['base_url'])) {
drush_set_default('master_url', $GLOBALS['base_url']);
$master_url = drush_get_option('master_url');
drush_set_default('web_disable_url', $master_url .'/hosting/disabled');
drush_set_default('web_maintenence_url', $master_url .'/hosting/maintenance');
}
drush_set_default('web_ip', '127.0.0.1');
drush_set_default('web_port', 80);
}
}
/**
* Delete virtual host file
*/
function _provision_apache_delete_vhost_config($url) {
// backward compatibility with 0.3
if (provision_service('file')->path('exists', drush_get_option('vhost_path') .'/'. $url)) {
provision_service('file')->path("unlink", drush_get_option('vhost_path') ."/" . $url, TRUE, dt("Removed apache virtual host configuration"));
} else {
$options = drush_get_merged_options();
// backward compatibility with 0.3
if (!$options['site_port'] || $options['site_port'] < 1 || $options['site_port'] > 65536) {
$options['site_port'] = 80;
}
$file = $url . '_' . $options['site_port'];
provision_service('file')->path("unlink", drush_get_option('vhost_path') ."/" . $file, TRUE, dt("Removed apache virtual host configuration"));
}
drush_command_invoke_all('provision_apache_delete_vhost', $url, $options);
}
/**
* Generate virtual host file
*
* This will create a VirtualHost configuration file for the domain
* $url. This is really a stub to provision_write_config() that sets up the
* options properly, guesses the right template and fires a hook to
* allow other modules to add lines to the default config, using
* hook_provision_apache_vhost_config(). Parameters in the template
* are expanded based on the options available through
* drush_get_merged_options().
*
* @param $url
* the domain to generate the configuration file for
* @param $template
* the template to use, if NULL, the default will be used
*
* @see _provision_apache_default_template()
* @see _provision_apache_redirect_template()
* @see provision_write_config()
* @see hook_provision_apache_vhost_config()
* @see drush_get_merged_options()
*/
function _provision_apache_create_vhost_config($url, $template = NULL) {
$options = drush_get_merged_options();
// backward compatibility with 0.3
if (!$options['site_port'] || $options['site_port'] < 1 || $options['site_port'] > 66535) {
$options['site_port'] = 80;
}
if (is_null($template)) {
$template = _provision_apache_default_template();
}
if (!empty($options['redirection'])) {
$template .= _provision_apache_redirect_template();
}
if ($options['aliases'] && !is_array($options['aliases'])) {
$options['aliases'] = explode(",", $options['aliases']);
}
$options['extra_config'] = "# Extra configuration from modules:\n";
$options['extra_config'] .= join("\n", drush_command_invoke_all('provision_apache_vhost_config', $url, $options));
/* one file per virtualhost name/port combination
*
* the rationale here is that we can have different sites on
* different ports that will be generated at different times, so we
* will not be able to generate a full config file for all ports in
* one pass
*/
$file = $url . '_' . $options['site_port'];
// backward compatibility with 0.3
if (provision_service('file')->path('exists', drush_get_option('vhost_path') .'/'. $url)) {
rename(drush_get_option('vhost_path') .'/'. $url, drush_get_option('vhost_path') .'/'. $file);
}
drush_log(dt("Generating apache host configuration file %file.", array('%file' => $file)));
$writable = provision_service('file')->path("writable", drush_get_option('vhost_path'), TRUE , NULL,
dt("Virtual host configuration path @path is not writable."),
'PROVISION_VHOST_PATH_NOT_WRITABLE');
if ($writable) {
return provision_write_config('vhost_path', $file, $template, $options);
} else {
return $writable;
}
}
/**
* Create a platform-level Apache cofniguration file
*
* This creates a config file for the <Directory> the platform is in
*
* most of what is done here is add the relevant .htaccess directives to
* the platform configuration.
*/
function _provision_apache_create_platform_config($url) {
// only create a platform level config if the platform is specified
$data = drush_get_merged_options();
$options = drush_get_merged_options();
$data['extra_config'] = "# Extra configuration from modules:\n";
$data['extra_config'] .= join("\n", drush_command_invoke_all('provision_apache_dir_config', $data));
$file = 'platform_' . $options['platform'] . '.conf';
drush_log(dt("Generating apache platform configuration file %file.", array('%file' => $file)));
$writable = provision_service('file')->path("writable", drush_get_option('platform_conf_path'), TRUE , NULL, dt("Platform configuration path @path is not writable."), 'PROVISION_VHOST_PATH_NOT_WRITABLE');