provision_apache.drush.inc 8.15 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
<?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.
 */

anarcat's avatar
anarcat committed
14
15
16
17
18
19
/**
 * Implementation of hook_drush_init()
 *
 * This sets a few default drush options by automatically guessing
 * proper defaults and settings based on the environment.
 */
20
function provision_apache_drush_init() {
21
22
23
24
25
  $command = drush_get_command();
  $command = explode(" ", $command['command']);
  if ($command[0] == 'provision') {
    // Set up defines for platform
    $docroot = drush_get_option(array("r", "root"), $_SERVER['PWD']);
26

27
28
29
    $path = drush_set_default('docroot_path', rtrim(($docroot) ? $docroot : $_SERVER['DOCUMENT_ROOT'], '/'));
    drush_set_default('sites_path', $path . '/sites');
    drush_set_default('publish_path', drush_get_option('docroot_path'));
30

31
32
33
    $parts = explode("/", $path);
    array_pop($parts);
    $parent_path = drush_set_default('parent_path', implode("/" , $parts));
34

35
36
    drush_set_default('backup_path', $parent_path . '/backups');
    drush_set_default('config_path', $parent_path . '/config');
37

38
39
    $config_path = drush_get_option('config_path');
    drush_set_default('vhost_path', $config_path . '/vhost.d');
40

41
42
    // Commands
    drush_set_default('restart_cmd', _provision_default_restart_cmd());
43

44
45
46
    // System account
    drush_set_default('web_group', _provision_default_web_group());
    drush_set_default('script_user', get_current_user());
47

48
49
50
51
52
53
54
55
56
    // Redirection urls
    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);
  }
57
58
}

59
60
61
62
63
64
65
66
/**
 * The default template provided for the virtual host configuration
 */
function _provision_apache_default_template() {
  return file_get_contents(dirname(__FILE__) ."/provision_apache_vhost.tpl.php");
}

/**
67
 * The template for site redirection
68
69
70
71
72
 */
function _provision_apache_redirect_template() {
  return file_get_contents(dirname(__FILE__) ."/provision_apache_vhost_redirect.tpl.php");
}

73
74
75
76
77
78
79
/**
 * The default template for disabled sites
 */
function _provision_apache_disabled_template() {
  return file_get_contents(dirname(__FILE__) ."/provision_apache_vhost_disabled.tpl.php");
}

80
81
82
83
84
85
86
/**
 * The template for platform configuration settings
 */
function _provision_apache_platform_template() {
  return file_get_contents(dirname(__FILE__) ."/provision_apache_platform.tpl.php");
}

87
88
89
/**
 * Delete virtual host file
 */
90
91
function _provision_apache_delete_vhost_config($url) {
  provision_path("unlink", drush_get_option('vhost_path') ."/$url", TRUE, dt("Removed apache virtual host configuration"));
92
93
94
95
}

/**
 * Generate virtual host file
anarcat's avatar
anarcat committed
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
 *
 * This will create a VirtualHost configuration file for the domain
 * $url. This is really a stub to _create_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_apache_create_config()
 * @see hook_provision_apache_vhost_config()
 * @see drush_get_merged_options()
115
 */
116
117
function _provision_apache_create_vhost_config($url, $template = NULL) {
  $options = drush_get_merged_options();
118
  // backward compatibility with 0.3
119
  if (!$options['site_port'] || $options['site_port'] < 1 || $options['site_port'] > 66535) { 
120
121
    $options['site_port'] = 80;
  }
122
  if (is_null($template)) {
123
    $template = _provision_apache_default_template();
124
  }
125
126
127
  if ($options['redirection']) {
    $template .= _provision_apache_redirect_template();
  }
128
129
130
131
  if (!is_array($options['aliases'])) {
    $options['aliases'] = explode(",", $options['aliases']);
  }

132
  $options['extra_config'] = "# Extra configuration from modules:\n";
133
  $options['extra_config'] .= join("\n", drush_command_invoke_all('provision_apache_vhost_config', $url, $options));
134
135
136
137
138
139
140
141
142
143
144
145
146
  /* 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_path_exists(drush_get_option('vhost_path') .'/'. $url)) {
    rename(drush_get_option('vhost_path') .'/'. $url, drush_get_option('vhost_path') .'/'. $file);
  }
  return _provision_apache_create_config($file, $options, $template);
147
148
}

anarcat's avatar
anarcat committed
149
150
151
152
153
154
155
156
157
158
159
/**
 * Create a system-wide Apache configuration file.
 *
 * This creates a configuration file without any <VirtualHost> headers
 * so that the configuration applies to all VirtualHost (in
 * general). It calls hook_provision_apache_server_config() to allow
 * third party extensions to add stuff to the configuration file.
 *
 * @see _provision_apache_default_server_template()
 * @see _provision_apache_create_config()
 */
160
161
162
163
164
165
166
167
168
function _provision_apache_create_server_config($url) {
  $options = drush_get_merged_options();
  $template = _provision_apache_default_server_template();
  
  $options['extra_config'] = "# Extra configuration from modules:\n";
  $options['extra_config'] .= join("\n", drush_command_invoke_all('provision_apache_server_config', $url, $options));
  return _provision_apache_create_config($options['web_host'] .".server", $options, $template);
}

anarcat's avatar
anarcat committed
169
170
171
172
173
/**
 * default template for server-wide configurations
 * 
 * @see _provision_apache_create_server_config()
 */
174
175
176
177
function _provision_apache_default_server_template() {
    return file_get_contents(dirname(__FILE__) ."/provision_apache_server.tpl.php");
}

178
179
/**
 * Write an apache configuration file
anarcat's avatar
anarcat committed
180
181
182
183
184
185
186
187
188
189
190
191
 *
 * Write an apache configuration file to the Aegir configuration
 * directory `vhost_path` (generally /var/aegir/config/vhost.d).
 *
 * @param $file
 *    the name of the file in the directory
 * @param $data
 *    name => value pairs of settings to be passed to the template
 * @param $template
 *    the template expanded with provision_render_config()
 *
 * @see provision_render_config()
192
193
194
195
196
197
 */
function _provision_apache_create_config($file, $data, $template) {
  drush_log(dt("Generating apache host configuration file %file.", array('%file' => $file)));
  $writable = provision_path("writable", drush_get_option('vhost_path'), TRUE , NULL,
      dt("Virtual host configuration path @path is not writable."), 
      'PROVISION_VHOST_PATH_NOT_WRITABLE');
198
199
  
  if ($writable) {
200
201
    $file = fopen(drush_get_option('vhost_path') .'/'. $file, "w");
    $text = provision_render_config($template, $data);
202
203
204
205
    fwrite($file, $text);
    fclose($file);
  }
}
206

207
208
209
210
211
212
/**
 * Restart Apache
 */
function _provision_apache_restart_apache($cause_error = FALSE) {
  //This is required to be configurable, due to the fact that different hosts might need to do this differently.
  //TODO : test for this instead of relying on a configuration setting?
213
  $return = drush_shell_exec(escapeshellcmd(drush_get_option('restart_cmd')));
214
215
  if (!$return) {
    if ($cause_error) {
216
      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."));
217
218
219
    }
    else {
      drush_log(dt("Web server could not be restarted. Changes might not be available until this has been done."), "warning");
220
221
222
    }
  }
  else {
223
    drush_log(dt('Apache has been restarted'));
224
225
  }

226
  return $return;
227
228
}