Commit 36eb8414 authored by Adrian Rossouw's avatar Adrian Rossouw

Committing the config file patch from #736706 , thanks drumm.

parent 1deeda62
......@@ -25,85 +25,6 @@ function file_drush_exit() {
}
class provisionService_file extends provisionService {
/**
* Perform tasks on a path.
*
* Perform tasks on a path, and logs error messages / codes on success or
* failure. This function will call another method which defines the
* functionality, and exists to provide a consistent interface for file
* operations with error logging integration.
*
* Many of the $op methods are really simple, but are wrapped in methods to
* provide a consistent interface for path to operate with.
*
* @param type
* The type of operation to perform. One of the following:
* fopen - Get a file pointer for writing.
*
* @param path
* The path you want to perform the file operation on.
*
* @param confirm
* Confirm that the final value of the file operation matches this value.
* This value defaults to TRUE, which is sufficient for most file operations.
*
* These exceptions are :
* fopen - $confirm is the $mode
*
* @param succeed_message
* Log this as a notice into the logging system, if the operation completed succesfully.
*
* @param fail_message
* Log this as a error to the logging system, if the $error_codes parameter has been set,
* otherwise, log this as a warning. If the operation specifies an additional reason for
* the operation failing, it will be appended to this message.
*
* @param error_codes
* Generate these system level errors using the provision error bitmasks.
*
* @return
* Returns TRUE if the test against $confirm passed, otherwise returns FALSE.
*/
function path($op, $path, $confirm = TRUE, $succeed_message = NULL, $fail_message = NULL, $error_codes = NULL) {
if (method_exists($this, $op)) {
// The reason variable is passed into the operation function, to allow the function
// to specify an additional reason as to why the operation failed.
$reason = '';
$value = $this->$op($path, $confirm, $reason);
clearstatcache(); // this needs to be called, otherwise we get the old info
$tokens = array("@path" => $path, "@op" => $op, "@confirm" => $confirm);
if ($reason) {
$fail_message .= " (" . $reason . ")";
}
$status = is_array($value) ? ($value[0] == $confirm) : ($value == $confirm);
if ($status) {
if (!is_null($succeed_message)) {
drush_log(dt($succeed_message, $tokens), 'message');
}
}
else {
if ($error_codes) {
// Trigger a sysem halting error
if (!is_null($fail_message)) {
drush_set_error($error_codes, dt($fail_message, $tokens));
}
else {
drush_set_error($error_codes);
}
}
else {
// Trigger a warning
if (!is_null($fail_message)) {
drush_log(dt($fail_message, $tokens), 'warning');
}
}
}
return is_array($value) ? $value[1] : $status;
}
}
/**
* Determine if $path can be written to.
*
......@@ -206,14 +127,6 @@ class provisionService_file extends provisionService {
return $this;
}
function fopen($path, $mode) {
$fp = fopen($path, $mode);
if ($fp === FALSE) {
return FALSE;
}
return array(TRUE, $fp);
}
/**
* Change the file permissions of $path to the octal value in $perms.
*
......
......@@ -163,16 +163,6 @@ function _provision_drupal_site_installed($url) {
return FALSE;
}
/**
* The default template to use while generating config files.
*
* @return
* The default template for the config file
*/
function _provision_drupal_default_template() {
return file_get_contents(dirname(__FILE__) .'/provision_drupal_settings.tpl.php');
}
/**
* Generate a settings file for the site.
*
......@@ -183,27 +173,14 @@ function _provision_drupal_default_template() {
* because the modules might provide additional information about the site.
*/
function _provision_drupal_create_settings_file($url = NULL) {
$options = drush_get_merged_options();
// As of Drupal 7 there is no more mysqli type
if (drush_drupal_major_version() >= 7) {
$options['db_type'] = ($options['db_type'] == 'mysqli') ? 'mysql' : $options['db_type'];
}
$options['extra_config'] = "# Extra configuration from modules:\n";
$options['extra_config'] .= join("\n", drush_command_invoke_all('provision_drupal_config', $url, $options));
drush_log(dt("Generate settings.php file"));
if (provision_service('file')->exists('sites/' . $url . '/settings.php')->status()) {
provision_service('file')->chmod('sites/' . $url . '/settings.php', 0640)
->succeed('Changed permissions of settings.php to @perm')
->fail('Could not change permissions of settings.php to @perm');
}
$fp = provision_service('file')->path('fopen', 'sites/' . $url . '/settings.php', 'w');
$text = _provision_drupal_default_template();
fwrite($fp, "<?php\n". provision_render_config($text, $options));
fclose($fp);
$config = new provisionConfig_drupal_settings(drush_get_merged_options());
$config->write();
# Change the permissions of the file
provision_service('file')->chmod('sites/' . $url . '/settings.php', 0440)
......@@ -215,6 +192,24 @@ function _provision_drupal_create_settings_file($url = NULL) {
->fail('Could not change group ownership of settings.php to @gid');
}
class provisionConfig_drupal_settings extends provisionConfig {
public $template = 'provision_drupal_settings.tpl.php';
public $description = 'Drupal settings.php file';
function filename() {
return $this->data['sites_path'] . '/' . $this->data['site_url'] . '/settings.php';
}
function process() {
if (drush_drupal_major_version() >= 7) {
$this->data['db_type'] = ($this->data['db_type'] == 'mysqli') ? 'mysql' : $this->data['db_type'];
}
$this->data['extra_config'] = "# Extra configuration from modules:\n";
$this->data['extra_config'] .= join("\n", drush_command_invoke_all('provision_drupal_config', $this->data['site_url'], $this->data));
}
}
/**
* Create the directories needed to host a drupal site
*
......
<?php print '<?php' ?>
/**
* The database credentials are stored in the Apache vhost config
* of the associated site with SetEnv parameters.
......
......@@ -28,14 +28,10 @@ function drush_provision_drupal_provision_verify($url = null) {
provision_service('file')->create_dir(drush_get_option('config_path'), dt('Provision configuration'), 0711);
provision_service('file')->create_dir(drush_get_option('config_path') . '/includes', dt('Provision PHP configuration'), 0711);
if (!provision_service('file')->exists(drush_get_option('config_path') . '/includes/global.inc')->succeed('Global configuration file exists')->status()) {
# create an empty global.inc so the include doesn't fail with
# open_basedir restrictions
if (!$file = provision_service('file')->path('fopen', drush_get_option('config_path') . '/includes/global.inc', 'a')) {
drush_set_error('PROVISION_FRAMEWORK_ERROR', dt('Cannot create global settings configuration'));
} else {
fwrite($file, "<?php # global settings.php");
fclose($file);
}
// Create an empty global.inc so the include doesn't fail with
// open_basedir restrictions
$config = new provisionConfig_global_settings(drush_get_merged_options());
$config->write();
}
provision_service('file')->create_dir(drush_get_option('backup_path'), dt('Backup'), 0700);
provision_service('file')->writable(drush_get_option('sites_path'))
......@@ -57,6 +53,17 @@ function drush_provision_drupal_provision_verify($url = null) {
}
}
class provisionConfig_global_settings extends provisionConfig {
public $template = 'global_settings.tpl.php';
public $description = 'Global settings.php file';
function filename() {
return $this->data['config_path'] . '/includes/global.inc';
}
function process() {
}
}
/**
* Implementation of hook_provision_post_verify
......
......@@ -90,27 +90,29 @@ function provision_save_site_data() {
drush_save_config($context);
$filename = _drush_config_file($context);
if (!drush_get_error()) {
// append the db settings in the _SERVER variable so normal drush commands can still read it.
$cache = drush_get_context($context);
$fp = provision_service('file')->path('fopen', $filename, 'a+');
$fields = array('db_type', 'db_host', 'db_user', 'db_passwd', 'db_name');
foreach ($fields as $key) {
$line = "\n\$_SERVER['$key'] = ". var_export($cache[$key], TRUE) .';';
fwrite($fp, $line);
}
fwrite($fp, "\n");
fclose($fp);
$config = new provisionConfig_drushrc(drush_get_merged_options());
$config->write();
}
provision_service('file')->chmod($filename, 0600)
provision_service('file')->chmod(_drush_config_file($context), 0600)
->succeed('Changed permissions of drushrc.php to @perm')
->fail('Could not change permissions of drushrc.php to @perm');
}
class provisionConfig_drushrc extends provisionConfig {
public $template = 'provision_drushrc.tpl.php';
public $description = 'Drush configuration file';
function filename() {
return $this->data['config-file'];
}
function process() {
$this->mode = 'a+';
}
}
/**
* @} End of "defgroup sitedata".
*/
......@@ -134,43 +136,6 @@ function provision_save_platform_data() {
return drush_save_config('drupal');
}
/**
* @defgroup provisionvalues Value replacement support for the provisioning framework
* @{
*/
/**
* Generate the text for a config file using php
*/
function provision_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
}
/**
* Write a config based on a template
*
* @see provision_render_config()
*/
function provision_write_config($drush_option, $file, $template, $data) {
$file = provision_service('file')->path('fopen', drush_get_option($drush_option) . '/' . $file, 'w');
$text = provision_render_config($template, $data);
fwrite($file, $text);
fclose($file);
}
/**
* @} End of "defgroup provisionvalues".
*/
/**
* Remove files or directories, recursively
*
......
......@@ -86,6 +86,7 @@ class provisionConfig {
public $template = null;
public $data = array();
public $description = null;
protected $mode = 'w';
function __construct($data = array()) {
if (is_null($this->template)) {
......@@ -141,7 +142,7 @@ class provisionConfig {
if ($template = $this->load_template()) {
#todo - convert to file_service
$file = fopen($filename, "w");
$file = fopen($filename, $this->mode);
$text = $this->render_template($template, $this->data);
fwrite($file, $text);
fclose($file);
......
<?php
include_once(dirname(__FILE__) . '/../provision.service.inc');
/**
* @file
*
......@@ -24,14 +25,8 @@ function provision_ssl_provision_apache_vhost_config($url, $options) {
if ($options['ssl_redirect']) {
// That's pretty bad, but if we *don't* do that, the vhost is never updated after the first write
// XXX: we need a better way to identify if this is legit
if (provision_service('file')->exists(drush_get_option('vhost_path') . '/' . $url . '_80')->status()) {
drush_log(dt('Overwriting existing vhost %url:80 with redirection', array('%url' => $url)), 'warning');
}
$newoptions = $options;
// in the redirection template, the ServerName is the first alias in the list
array_push($newoptions['aliases'], $options['site_url']);
$newoptions['site_port'] = 80;
provision_write_config('vhost_path', $url . '_80', _provision_apache_redirect_template(), $newoptions);
$config = new provisionConfig_ssl_vhost(drush_get_merged_options());
$config->write();
}
return array("php_value session.cookie_secure 1", "SSLEngine On");
} else {
......@@ -39,6 +34,20 @@ function provision_ssl_provision_apache_vhost_config($url, $options) {
}
}
class provisionConfig_ssl_vhost extends provisionConfig {
public $template = '../http/apache/vhost_redirect.tpl.php';
public $description = 'Redirect for SSL';
function filename() {
return $this->data['vhost_path'] . '/' . $this->data['site_url'] . '_80';
}
function process() {
array_push($this->data['aliases'], $this->data['site_url']);
$this->data['site_port'] = 80;
}
}
/**
* Implementation of hook_provision_apache_delete_vhost()
*
......
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