provision.drush.inc 12.8 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
<?php
/**
 * Drush commands
 *
 *
 * This module provides a framework for a Drupal site to manage and install new Drupal sites, using the command line
 * Drush utility. 
 *
 * It allows for pluggable 'provisioning modules' that can extend and modify the tasks that are taken during installation.
 *
 * Each site has the following commands that can be run on it.
 *
 * Implemented : 
14
15
 *   install - Install a new Drupal site. The install command uses 3 separate hooks to do its job,
 *             namely hook_pre_provision_install(), hook_provision_install() and hook_post_provision_install().
16
17
18
19
20
21
22
23
24
 *   verify  - Recreate all configuration files, to be in synch with changes in the front end. And test that they are correct.
 *   stats   - Return an associated array of site statistics. (implemented in provision_stats module, is thus optional)
 *   import  - Import the details of an already existing site into the provisioning framework.
 *             This command inspects the settings.php and generates the site.php file that the framework uses for configuration.
 *   backup  - Generates a tarball containing the sites directory, the site data configuration and the database dump.
 *             This allows the tarball to act as a 'site package', which can be redeployed on other installations,
 *             or used for an upgrade.
 *   disable - Disable an installed Drupal site. Changes the virtual host config file so that it redirects to provision_disabled_site_redirect_url
 *   enable  - Re-enable a site that has already been disabled. Recreates the virtual host file.
25
26
 *   delete  - In a site context: generates a back up of the site, and then removes all references to it.
 *             In a platform context: removes the platform and its vhost config from the server if no sites are currently running on it
27
28
 *   restore - Revert to a previous backup of the site.
 *
29
 *   deploy  - Accepts a site package (backup) as argument, and redeploys it, running the upgrade processes on it.
30
31
32
 *             Uses hook_provision_pre_upgrade(), hook_provision_upgrade() and hook_provision_post_upgrade() hooks,
 *             and allows clean roll back if any errors occur. Will include stringent checking of module versions,
 *             and allow unit tests to be run.
33
34
35
36
 *   lock    - Lock a platform so that sites cannot be provisioned on it. This does not disable or delete the platform
 *             nor any sites currently provisioned on it.
 *   unlock  - Unlock a platform so that sites can be provisioned on it.
 *
Adrian Rossouw's avatar
Adrian Rossouw committed
37
38
 *   login_reset - Generate a one-time login reset URL.
 *
39
 * Not implemented yet :
40
41
42
 *   rename  - Change the url of a site. This requires moving of files, and numerous other issues.
 */

43
44
45
// Do not allow the program to be run as the root user. ever
$name = posix_getpwuid(posix_geteuid());
if ($name['name'] == 'root') {
46
  return drush_set_error('PROVISION_IS_ROOT', dt('You are running the provision script as the root user. Exiting'));
47
48
}

49
50
51
52
/**
 * @defgroup provisiondrush Command line interface for Provision.
 * @{
 */
53
include_once('provision.inc');
54

55
56
57
58
59
60
61
62
63
64
/**
 * implementation of hook_drush_init().
 *
 * This will abort any process running drush if the load is critical.
 *
 * @see provision_load_critical()
 */
function provision_drush_init() {
  $load = sys_getloadavg();
  if (provision_load_critical($load)) {
65
    drush_set_error('PROVISION_OVERLOAD', dt("load on system too heavy (@load), aborting", array('@load' => join(" ", $load))));
66
67
68
69
    exit(1);
  }
}

70
71
72
73
/**
 * Implementation of hook_drush_command().
 */
function provision_drush_command() {
74
75
76
77
78
  $items['provision-named-context-save'] = array(
    'arguments' => array(),
    'description' => dt('Save Drush aliases'),
    'bootstrap' => DRUSH_BOOTSTRAP_DRUSH,
  );
79
  $items['provision-install'] = array(
80
81
    'arguments' => array('domain.com' => dt('The domain of the site to install.')),
    'description' => dt('Provision a new site using the provided data.'),
82
    'bootstrap' => DRUSH_BOOTSTRAP_DRUPAL_ROOT
83
84
  );

85
  $items['provision-import'] = array(
86
87
    'arguments' => array('domain.com' => dt('The domain of the site to import.')),
    'description' => dt('Turn an already running site into a provisioned site.'),
88
    'bootstrap' => DRUSH_BOOTSTRAP_DRUPAL_ROOT
89
90
  );

91
  $items['provision-backup'] = array(
92
93
    'arguments' => array('domain.com' => dt('The domain of the site to back up.')),
    'optional arguments' => array('backup-file' => dt('The file to save the backup to. This will be a gzipped tarball.')),
94
    'description' => dt('Generate a back up for the site.'),
95
    'bootstrap' => DRUSH_BOOTSTRAP_DRUPAL_ROOT
96
97
  );
  
98
  $items['provision-enable'] = array(
99
    'arguments' => array('domain.com' => dt('The domain of the site to enable (only if enabled).')),
100
    'description' => 'Enable a disabled site.',
101
    'bootstrap' => DRUSH_BOOTSTRAP_DRUPAL_ROOT
102
  );
103
  $items['provision-disable'] = array(
104
    'arguments' => array('domain.com' => dt('The domain of the site to disable (only if disabled).')),    
105
    'description' => 'Disable a site.',
106
    'bootstrap' => DRUSH_BOOTSTRAP_DRUPAL_ROOT
107
108
  );

109
110
111
112
113
114
115
116
117
118
  $items['provision-lock'] = array(
    'description' => 'Lock a platform from having any other sites provisioned on it.',
    'bootstrap' => DRUSH_BOOTSTRAP_DRUPAL_ROOT
  );

  $items['provision-unlock'] = array(
    'description' => 'Unlock a platform so that sites can be provisioned on it.',
    'bootstrap' => DRUSH_BOOTSTRAP_DRUPAL_ROOT
  );

119
  $items['provision-verify'] = array(
120
121
    'arguments' => array('domain.com' => dt('The domain of the site to verify).')),    
    'description' => 'Verify that the provisioning framework is correctly installed.',
122
    'bootstrap' => DRUSH_BOOTSTRAP_DRUSH, 
123
124
  );
 
125
  $items['provision-restore'] = array(
126
    'description' => 'Restore the site to a previous backup. This will also generate a backup of the site as it was.',
127
    'arguments' => array('domain.com' => dt('The domain of the site to be restored'),
128
      'site_backup.tar.gz' => dt('The backup to restore the site to.')),
129
    'bootstrap' => DRUSH_BOOTSTRAP_DRUPAL_ROOT
130
131
  );

132
  $items['provision-deploy'] = array(
133
134
135
    'description' => 'Deploy an existing backup to a new url.',
    'arguments' => array('domain.com' => dt('The domain to deploy the site package to.'),
      'site_backup.tar.gz' => dt('The backup to deploy.')),
136
    'bootstrap' => DRUSH_BOOTSTRAP_DRUPAL_ROOT
137
  );
138

139
  $items['provision-migrate'] = array(
140
141
142
    'description' => 'Migrate a site between platforms.',
    'arguments' => array('domain.com' => dt('The domain to migrate. Any outstanding updates will be run.'),
      '/path/to/platform' => dt('The platform to migrate the site to.')),
143
    'bootstrap' => DRUSH_BOOTSTRAP_DRUPAL_ROOT
144
  );
145

146
  $items['provision-clone'] = array(
147
148
149
150
151
152
    'description' => 'Clone a site between platforms.',
    'arguments' => array('domain.com' => dt('The domain to clone. Any outstanding updates will be run.'),
      'new.domain.com' => dt('The new domain name to use.'),
      '/path/to/platform' => dt('The platform to clone the site to.')),
    'bootstrap' => DRUSH_BOOTSTRAP_DRUPAL_ROOT
  );
153
 
154
  $items['provision-delete'] = array(
155
    'description' => 'Delete a site.',
156
    'bootstrap' => DRUSH_BOOTSTRAP_DRUPAL_ROOT
157
158
  );

159
  $items['provision-login_reset'] = array(
Adrian Rossouw's avatar
Adrian Rossouw committed
160
161
162
163
164
    'description' => 'Generate a one-time login reset URL.',
    'bootstrap' => DRUSH_BOOTSTRAP_DRUPAL_ROOT
  );
  

165
  $items['hostmaster-migrate'] = array(
166
167
168
169
170
171
172
173
    'description' => dt('Migrate an instance of the Hostmaster front end to a new platform'),
    'bootstrap' => DRUSH_BOOTSTRAP_DRUPAL_ROOT,
    'arguments' => array(
      'example.com' => dt('The name of the site to migrate'),
      '/path/to/platform' => dt('The platform to migrate the site to.')
    ),
  );

174
  $items['hostmaster-make'] = array(
175
176
177
178
179
180
181
    'description' => dt('Build a platform containing the Hostmaster user interface for provision.'),
    'bootstrap' => DRUSH_BOOTSTRAP_DRUSH,
    'arguments' => array(
      '/path/to/platform' => dt('The path to create the platform in.')
    ),
  );

182
183
184
185
186
187
188
189
190
  $items['hostmaster-install'] = array(
    'description' => dt('Install and verify the Hostmaster frontend.'),
    'bootstrap' => DRUSH_BOOTSTRAP_DRUSH,
    'arguments' => array(
      'example.com' => dt('The name of the site to install'),
      '/path/to/platform' => dt('The platform to install the site on.'),
      'you@example.com' => dt('The email account to send a welcome mail to'),
    ),
  );
191
  
192
  $items['hostmaster-pause'] = array(
193
194
195
196
197
    'description' => dt('Prepare the site to be migrated to a new platform.'),
    'arguments' => array(
      'example.com' => dt('The url of the site being migrated.')),
  );

198
  $items['hostmaster-resume'] = array(
199
200
201
202
203
    'description' => dt('Complete the migration of the site to a new platform.'),
    'arguments' => array(
      'example.com' => dt('The url of the site being migrated.')),
  );

204
205
206
  return $items;
}

207
208
209
210
function drush_provision_hostmaster_make($platform) {
  drush_backend_invoke('make', array(dirname(__FILE__) . '/aegir.make', $platform));
}

drumm's avatar
drumm committed
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
function drush_http_provision_named_context_save() {
  if (PROVISION_CONTEXT_SERVER) {
    $parents = array();
    $contexts = array(
      drush_get_option('hosting_name') => array('parents'),
    );

    foreach (provision_service('all') as $service) {
      foreach ($service->write_alias() as $key => $value) {
        $parents[] = drush_get_option('hosting_name') . '-' . $key;
        $contexts[drush_get_option('hosting_name') . '-' . $key] = $value; 
      }
    }

    drush_set_option('parents', '@' . implode(',@', $parents));
    $config = new provisionConfig_drushrc_alias($contexts);
    $config->write();
  }
}

231
232
function _provision_default_restart_cmd() {
  $command = '/usr/sbin/apachectl'; # a proper default for most of the world
233
234
235
236
237
238
239
240
241
242
  foreach (explode(':', $_SERVER['PATH']) as $path) {
    $options[] = "$path/apache2ctl";
    $options[] = "$path/apachectl";
  }
  # try to detect the apache restart command
  $options[] = '/usr/local/sbin/apachectl'; # freebsd
  $options[] = '/usr/sbin/apache2ctl'; # debian + apache2
  $options[] = $command;

  foreach ($options as $test) {
243
244
    if (is_executable($test)) {
      $command = $test;
245
      break;
246
247
248
249
250
251
    }
  }

  return "sudo $command graceful";
}

252
function _provision_default_web_group() {
253
  $info = posix_getgrgid(posix_getgid());
254
255
256
257
  $common_groups = array(
    'httpd',
    'www-data', 
    'apache',
drumm's avatar
#770482    
drumm committed
258
    'webservd',
259
260
    'nogroup',
    'nobody',
261
    $info['name']);
262
263
264
265
266
267
268
269
270
271

  foreach ($common_groups as $group) {
    if (provision_posix_groupname($group)) {
      return $group;
      break;
    }
  }
  return null;
}

272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
/**
 * determine the number of CPU on the machine
 *
 * This tries a best guess at the number of CPUs running on the system. This is
 * useful for calculating sane load threshold.
 *
 * On Linux, this parses /proc/cpuinfo and looks for lines like this:
 * 
 * processor	: 0
 * ...
 * processor	: 1
 * processor	: n
 *
 * The number of CPUs on the system is n+1, we just count the number of lines.
 *
 * Other systems remain to be implemented, and would be best implemetend
 * through a PECL (or similar) extension that would use the POSIX sysconf
 * interface, as such:
 *
 * ncpus = sysconf(_SC_NPROCESSORS_ONLN);
 *
 * If no method can be found to figure out the number of CPUs, this will return
 * FALSE.
 *
296
297
298
299
300
 * People wishing to extend this to other platforms should look at
 * suggestions at:
 *
 * http://groups.google.com/group/sage-devel/browse_thread/thread/d65209f7ad6057fc
 *
301
302
303
304
305
 * @see provision_load_critical()
 * @todo implement for other systems than Linux
 */
function provision_count_cpus() {
  $ncpus = FALSE;
306
307
308
309
310
311
312
  if (file_exists("/proc/cpuinfo")) {
    # this should work on Linux with a /proc filesystem
    $cpuinfo = file_get_contents("/proc/cpuinfo");
    if ($cpuinfo !== FALSE) {
      if (preg_match_all("/^processor.*:.*[0-9]+$/m", $cpuinfo, $matches)) {
        $ncpus = count(array_pop($matches));
      }
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
    }
  }
  return $ncpus;
}

/**
 * determine if overall load of the machine is critical
 *
 * We use the "average system load" of the system as a metric, as available
 * through 'uptime' or in PHP sys_getloadavg() since 5.1. The load is usually
 * defined as "the number of processes in the system run queue"
 *
 * It's not a really reliable metric, but it's the best shot we've got without
 * getting into real specific details about I/O, CPU or memory load that are
 * going to be even tougher to evaluate.
 *
 * We base our evaluation on the number of CPUs on the servers. If there are
 * more than 5 processes waiting per CPU, we abort completely. If we ignore the
 * number of available CPUs, we assume a critical limit is a load of 10.
 *
 * @see sys_getloadavg()
 */
function provision_load_critical($load = null, $threshold = null) {
  if (is_null($load)) {
    $load = sys_getloadavg();
  }
  if (is_null($threshold)) {
    if ($ncpus = provision_count_cpus()) {
      $threshold = $ncpus * 5;
    } else {
      // can't determine the number of CPU, we hardcode at load 10
      $threshold = 10;
    }
  }
  return ($load[0] > $threshold);
}
349