| 1 |
<?php
|
| 2 |
// $Id: provision_apache.drush.inc,v 1.26 2009/09/14 11:40:25 anarcat Exp $
|
| 3 |
/**
|
| 4 |
* @file
|
| 5 |
* Apache provisioning module
|
| 6 |
* This module simply serves to generate the virtual host entry, and make sure apache gets reloaded properly.
|
| 7 |
* Because Drupal is running via the command line for the entirety of this process, it is only necessary to make
|
| 8 |
* it available online once everything has been completed.
|
| 9 |
*
|
| 10 |
* This module still requires configuration and sanity checks. Need to figure out a way to inspect the apache configuration,
|
| 11 |
* to ensure that the sites are getting loaded up correctly.
|
| 12 |
*/
|
| 13 |
|
| 14 |
/**
|
| 15 |
* Implementation of hook_drush_init()
|
| 16 |
*
|
| 17 |
* This sets a few default drush options by automatically guessing
|
| 18 |
* proper defaults and settings based on the environment.
|
| 19 |
*/
|
| 20 |
function provision_apache_drush_init() {
|
| 21 |
$command = drush_get_command();
|
| 22 |
$command = explode(" ", $command['command']);
|
| 23 |
if ($command[0] == 'provision') {
|
| 24 |
// Set up defines for platform
|
| 25 |
$docroot = drush_get_option(array("r", "root"), $_SERVER['PWD']);
|
| 26 |
|
| 27 |
$path = drush_set_default('docroot_path', rtrim(($docroot) ? $docroot : $_SERVER['DOCUMENT_ROOT'], '/'));
|
| 28 |
drush_set_default('sites_path', $path . '/sites');
|
| 29 |
drush_set_default('publish_path', drush_get_option('docroot_path'));
|
| 30 |
|
| 31 |
$parts = explode("/", $path);
|
| 32 |
array_pop($parts);
|
| 33 |
$parent_path = drush_set_default('parent_path', implode("/" , $parts));
|
| 34 |
|
| 35 |
drush_set_default('backup_path', $parent_path . '/backups');
|
| 36 |
drush_set_default('config_path', $parent_path . '/config');
|
| 37 |
|
| 38 |
$config_path = drush_get_option('config_path');
|
| 39 |
drush_set_default('vhost_path', $config_path . '/vhost.d');
|
| 40 |
|
| 41 |
// Commands
|
| 42 |
drush_set_default('restart_cmd', _provision_default_restart_cmd());
|
| 43 |
|
| 44 |
// System account
|
| 45 |
drush_set_default('web_group', _provision_default_web_group());
|
| 46 |
drush_set_default('script_user', get_current_user());
|
| 47 |
|
| 48 |
// Redirection urls
|
| 49 |
drush_set_default('master_url', $GLOBALS['base_url']);
|
| 50 |
$master_url = drush_get_option('master_url');
|
| 51 |
drush_set_default('web_disable_url', $master_url .'/hosting/disabled');
|
| 52 |
drush_set_default('web_maintenence_url', $master_url .'/hosting/maintenance');
|
| 53 |
|
| 54 |
drush_set_default('web_ip', '127.0.0.1');
|
| 55 |
drush_set_default('web_port', 80);
|
| 56 |
}
|
| 57 |
}
|
| 58 |
|
| 59 |
/**
|
| 60 |
* The default template provided for the virtual host configuration
|
| 61 |
*/
|
| 62 |
function _provision_apache_default_template() {
|
| 63 |
return file_get_contents(dirname(__FILE__) ."/provision_apache_vhost.tpl.php");
|
| 64 |
}
|
| 65 |
|
| 66 |
/**
|
| 67 |
* The template for site redirection
|
| 68 |
*/
|
| 69 |
function _provision_apache_redirect_template() {
|
| 70 |
return file_get_contents(dirname(__FILE__) ."/provision_apache_vhost_redirect.tpl.php");
|
| 71 |
}
|
| 72 |
|
| 73 |
/**
|
| 74 |
* The default template for disabled sites
|
| 75 |
*/
|
| 76 |
function _provision_apache_disabled_template() {
|
| 77 |
return file_get_contents(dirname(__FILE__) ."/provision_apache_vhost_disabled.tpl.php");
|
| 78 |
}
|
| 79 |
|
| 80 |
/**
|
| 81 |
* The template for platform configuration settings
|
| 82 |
*/
|
| 83 |
function _provision_apache_platform_template() {
|
| 84 |
return file_get_contents(dirname(__FILE__) ."/provision_apache_platform.tpl.php");
|
| 85 |
}
|
| 86 |
|
| 87 |
/**
|
| 88 |
* Delete virtual host file
|
| 89 |
*/
|
| 90 |
function _provision_apache_delete_vhost_config($url) {
|
| 91 |
// backward compatibility with 0.3
|
| 92 |
if (provision_path_exists(drush_get_option('vhost_path') .'/'. $url)) {
|
| 93 |
provision_path("unlink", drush_get_option('vhost_path') ."/" . $url, TRUE, dt("Removed apache virtual host configuration"));
|
| 94 |
} else {
|
| 95 |
$options = drush_get_merged_options();
|
| 96 |
// backward compatibility with 0.3
|
| 97 |
if (!$options['site_port'] || $options['site_port'] < 1 || $options['site_port'] > 65536) {
|
| 98 |
$options['site_port'] = 80;
|
| 99 |
}
|
| 100 |
$file = $url . '_' . $options['site_port'];
|
| 101 |
provision_path("unlink", drush_get_option('vhost_path') ."/" . $file, TRUE, dt("Removed apache virtual host configuration"));
|
| 102 |
}
|
| 103 |
drush_command_invoke_all('provision_apache_delete_vhost', $url, $options);
|
| 104 |
}
|
| 105 |
|
| 106 |
/**
|
| 107 |
* Generate virtual host file
|
| 108 |
*
|
| 109 |
* This will create a VirtualHost configuration file for the domain
|
| 110 |
* $url. This is really a stub to _create_config() that sets up the
|
| 111 |
* options properly, guesses the right template and fires a hook to
|
| 112 |
* allow other modules to add lines to the default config, using
|
| 113 |
* hook_provision_apache_vhost_config(). Parameters in the template
|
| 114 |
* are expanded based on the options available through
|
| 115 |
* drush_get_merged_options().
|
| 116 |
*
|
| 117 |
* @param $url
|
| 118 |
* the domain to generate the configuration file for
|
| 119 |
* @param $template
|
| 120 |
* the template to use, if NULL, the default will be used
|
| 121 |
*
|
| 122 |
* @see _provision_apache_default_template()
|
| 123 |
* @see _provision_apache_redirect_template()
|
| 124 |
* @see _provision_apache_create_config()
|
| 125 |
* @see hook_provision_apache_vhost_config()
|
| 126 |
* @see drush_get_merged_options()
|
| 127 |
*/
|
| 128 |
function _provision_apache_create_vhost_config($url, $template = NULL) {
|
| 129 |
$options = drush_get_merged_options();
|
| 130 |
// backward compatibility with 0.3
|
| 131 |
if (!$options['site_port'] || $options['site_port'] < 1 || $options['site_port'] > 66535) {
|
| 132 |
$options['site_port'] = 80;
|
| 133 |
}
|
| 134 |
if (is_null($template)) {
|
| 135 |
$template = _provision_apache_default_template();
|
| 136 |
}
|
| 137 |
if (!empty($options['redirection'])) {
|
| 138 |
$template .= _provision_apache_redirect_template();
|
| 139 |
}
|
| 140 |
if ($options['aliases'] && !is_array($options['aliases'])) {
|
| 141 |
$options['aliases'] = explode(",", $options['aliases']);
|
| 142 |
}
|
| 143 |
|
| 144 |
$options['extra_config'] = "# Extra configuration from modules:\n";
|
| 145 |
$options['extra_config'] .= join("\n", drush_command_invoke_all('provision_apache_vhost_config', $url, $options));
|
| 146 |
/* one file per virtualhost name/port combination
|
| 147 |
*
|
| 148 |
* the rationale here is that we can have different sites on
|
| 149 |
* different ports that will be generated at different times, so we
|
| 150 |
* will not be able to generate a full config file for all ports in
|
| 151 |
* one pass
|
| 152 |
*/
|
| 153 |
$file = $url . '_' . $options['site_port'];
|
| 154 |
// backward compatibility with 0.3
|
| 155 |
if (provision_path_exists(drush_get_option('vhost_path') .'/'. $url)) {
|
| 156 |
rename(drush_get_option('vhost_path') .'/'. $url, drush_get_option('vhost_path') .'/'. $file);
|
| 157 |
}
|
| 158 |
return _provision_apache_create_config($file, $options, $template);
|
| 159 |
}
|
| 160 |
|
| 161 |
/**
|
| 162 |
* Create a system-wide Apache configuration file.
|
| 163 |
*
|
| 164 |
* This creates a configuration file without any <VirtualHost> headers
|
| 165 |
* so that the configuration applies to all VirtualHost (in
|
| 166 |
* general). It calls hook_provision_apache_server_config() to allow
|
| 167 |
* third party extensions to add stuff to the configuration file.
|
| 168 |
*
|
| 169 |
* @see _provision_apache_default_server_template()
|
| 170 |
* @see _provision_apache_create_config()
|
| 171 |
*/
|
| 172 |
function _provision_apache_create_server_config($url) {
|
| 173 |
$options = drush_get_merged_options();
|
| 174 |
$template = _provision_apache_default_server_template();
|
| 175 |
|
| 176 |
$options['extra_config'] = "# Extra configuration from modules:\n";
|
| 177 |
$options['extra_config'] .= join("\n", drush_command_invoke_all('provision_apache_server_config', $url, $options));
|
| 178 |
return _provision_apache_create_config($options['web_host'] .".server", $options, $template);
|
| 179 |
}
|
| 180 |
|
| 181 |
/**
|
| 182 |
* default template for server-wide configurations
|
| 183 |
*
|
| 184 |
* @see _provision_apache_create_server_config()
|
| 185 |
*/
|
| 186 |
function _provision_apache_default_server_template() {
|
| 187 |
return file_get_contents(dirname(__FILE__) ."/provision_apache_server.tpl.php");
|
| 188 |
}
|
| 189 |
|
| 190 |
/**
|
| 191 |
* Write an apache configuration file
|
| 192 |
*
|
| 193 |
* Write an apache configuration file to the Aegir configuration
|
| 194 |
* directory `vhost_path` (generally /var/aegir/config/vhost.d).
|
| 195 |
*
|
| 196 |
* @param $file
|
| 197 |
* the name of the file in the directory
|
| 198 |
* @param $data
|
| 199 |
* name => value pairs of settings to be passed to the template
|
| 200 |
* @param $template
|
| 201 |
* the template expanded with provision_render_config()
|
| 202 |
*
|
| 203 |
* @see provision_render_config()
|
| 204 |
*/
|
| 205 |
function _provision_apache_create_config($file, $data, $template) {
|
| 206 |
drush_log(dt("Generating apache host configuration file %file.", array('%file' => $file)));
|
| 207 |
$writable = provision_path("writable", drush_get_option('vhost_path'), TRUE , NULL,
|
| 208 |
dt("Virtual host configuration path @path is not writable."),
|
| 209 |
'PROVISION_VHOST_PATH_NOT_WRITABLE');
|
| 210 |
|
| 211 |
if ($writable) {
|
| 212 |
$file = fopen(drush_get_option('vhost_path') .'/'. $file, "w");
|
| 213 |
$text = provision_render_config($template, $data);
|
| 214 |
fwrite($file, $text);
|
| 215 |
fclose($file);
|
| 216 |
}
|
| 217 |
}
|
| 218 |
|
| 219 |
/**
|
| 220 |
* Restart Apache
|
| 221 |
*/
|
| 222 |
function _provision_apache_restart_apache($cause_error = FALSE) {
|
| 223 |
//This is required to be configurable, due to the fact that different hosts might need to do this differently.
|
| 224 |
//TODO : test for this instead of relying on a configuration setting?
|
| 225 |
$return = drush_shell_exec(escapeshellcmd(drush_get_option('restart_cmd')));
|
| 226 |
if (!$return) {
|
| 227 |
if ($cause_error) {
|
| 228 |
return drush_set_error('PROVISION_WEB_RESTART_FAILED', dt("Web server could not be restarted. Changes might not be available until this has been done."));
|
| 229 |
}
|
| 230 |
else {
|
| 231 |
drush_log(dt("Web server could not be restarted. Changes might not be available until this has been done."), "warning");
|
| 232 |
}
|
| 233 |
}
|
| 234 |
else {
|
| 235 |
drush_log(dt('Apache has been restarted'));
|
| 236 |
}
|
| 237 |
|
| 238 |
return $return;
|
| 239 |
}
|
| 240 |
|