install.hostmaster.inc 11.6 KB
Newer Older
1 2 3
<?php

/**
4
 * @file install the hostmaster system
5 6
 */

7 8 9 10 11 12 13 14 15
/**
 * Implements drush_COMMAND_pre_validate().
 *
 * Ensure the database cannot be accessed by anonymous users, as it will
 * otherwise fail later in the install, and thus be harder to recover from.
 *
 * @see Provision_Service_db_mysql::grant_host().
 */
function drush_provision_hostmaster_install_pre_validate() {
16
  $command = sprintf('mysql -u intntnllyInvalid -h %s -P %s -e "SELECT VERSION()"', drush_get_option('aegir_db_host', 'localhost'), drush_get_option('aegir_db_port', '3306'));
17 18 19 20 21 22 23 24 25 26 27 28 29
  drush_shell_exec($command);

  if (preg_match("/Access denied for user 'intntnllyInvalid'@'([^']*)'/", implode('', drush_shell_exec_output()), $match)) {
    return;
  }
  elseif (preg_match("/Host '([^']*)' is not allowed to connect to/", implode('', drush_shell_exec_output()), $match)) {
    return;
  }
  else {
    return drush_set_error('PROVISION_DB_CONNECT_FAIL', dt('Dummy connection failed to fail. Either your MySQL permissions are too lax, or the response was not understood. See http://is.gd/Y6i4FO for more information. %msg', array('%msg' => join("\n", drush_shell_exec_output()))));
  }
}

30
function drush_provision_hostmaster_install_validate($site = NULL) {
31 32
  // set defaults for this whole script
  // those are settings that are not prompted to the user but still overridable
33
  drush_set_default('version', provision_version());
34 35 36
  drush_set_default('profile', 'hostmaster');

  // Get values needed to set other defaults
37
  $version = drush_get_option('version');
38
  $aegir_root = drush_set_default('aegir_root', drush_server_home());
39 40 41
  $profile = drush_get_option('profile');

  drush_set_default('root', $aegir_root . '/' . $profile . '-' . $version);
42 43 44 45
  drush_set_default('r', drush_get_option('root'));
  drush_set_default('script_user', provision_current_user());
  drush_set_default('web_group', _provision_default_web_group());
  drush_set_default('http_service_type', 'apache');
46
  drush_set_default('http_port', '80');
anarcat's avatar
anarcat committed
47
  drush_set_default('aegir_db_user', 'root');
48
  drush_set_default('aegir_db_port', '3306');
49
  drush_set_default('client_name', 'admin');
anarcat's avatar
anarcat committed
50
  $aegir_db_user = drush_get_option('aegir_db_user');
51

52
  // Generate "makefile" message only if there is one set.
53 54 55 56 57 58 59 60 61
  $root = drush_get_option(array('r', 'root'));
  if (is_dir($root) && !drush_get_option('makefile', FALSE)) {
    // Don't assume we know the makefile used to build an existing platform
    $makefile_msg = '';
  }
  else {
    drush_set_default('makefile', dirname(__FILE__) . '/aegir.make');
    $makefile_msg = dt("Aegir makefile: !makefile\n", array('!makefile' => drush_get_option('makefile')));
  }
62

63 64 65 66 67 68 69 70
  // Generate "profile" message only if there is one set.
  if (!drush_get_option('profile', FALSE)) {
    $profile_msg = '';
  }
  else {
    $profile_msg = dt("Aegir install profile: !profile\n", array('!profile' => $profile));
  }

71
  drush_print("Aegir $version automated install script");
72
  drush_print("==============================================================================");
73

74
  if (!$site || !drush_get_option('aegir_host', NULL) || !drush_get_option('aegir_db_pass', NULL) || filter_var(drush_get_option('client_email'), FILTER_VALIDATE_EMAIL)) {
75
    drush_print("Some settings have not been provided and will now be prompted.
76 77 78 79
Don't worry: you will get to review those settings after the final install");
  }
  // now we prompt the user for settings if not provided or not sane
  if (!$site) {
80
    $site = drush_prompt(dt("Aegir frontend URL"), provision_fqdn());
81
  }
82
  drush_set_option('site', $site);
83

84
  drush_set_default('aegir_host', provision_fqdn());
85
  drush_set_default('aegir_db_host', 'localhost');
86 87 88 89 90 91

  if (is_null(drush_get_option('aegir_db_pass', NULL))) {
    // XXX: may not be portable everywhere?
    system('stty -echo');
    drush_set_option('aegir_db_pass', drush_prompt(dt('MySQL privileged user ("!root") password', array('!root' => $aegir_db_user))));
    system('stty echo');
92
    print "\n"; // add a newline since the user's didn't print
93 94
  }

95
  if (drush_get_option('aegir_host') == 'localhost') {
96 97
    $default_email = 'webmaster@example.com';
  } else {
98
    $default_email = 'webmaster@' . drush_get_option('aegir_host');
99
  }
100
  drush_set_default('client_email', $default_email);
101
  while (!filter_var(drush_get_option('client_email'), FILTER_VALIDATE_EMAIL) && !drush_get_context('DRUSH_AFFIRMATIVE')) {
102
    drush_set_option('client_email', drush_prompt(dt("Admin user e-mail"), $default_email));
103 104
  }

105
  drush_print(dt('
106 107 108
This script will operate the following changes in your system:

1. Create server-level configuration directories
109 110 111
2. Create the Hostmaster frontend platform
3. Install the frontend site
4. Setup the dispatcher (a user cron job)
112 113

We are making the following assumptions:
114
 * you have read and are following the install instructions at:
helmo's avatar
helmo committed
115
    http://docs.aegirproject.org/en/3.x/install/
116
 * the FQDN of this machine is valid and resolves
117
 * you are executing this script as your "aegir" user
118

119 120
The following settings will be used:
 Aegir frontend URL: !site
anarcat's avatar
anarcat committed
121
 Master server FQDN: !fqdn
122 123 124 125
 Aegir root: !home
 Aegir user: !user
 Web group: !web
 Web server: !web_server
126
 Web server port: !web_server_port
127
 Aegir DB host: !db_host
128 129
 Aegir DB user: !db_user
 Aegir DB password: !db_pass
130
 Aegir DB port: !db_port
131 132
 Aegir version: !version
 Aegir platform path: !root
133
 Admin email: !email
134 135
 !makefile
 !profile',
136 137 138 139 140 141 142 143 144 145 146 147 148 149 150
  array(
    '!site' => $site,
    '!fqdn' => drush_get_option('aegir_host'),
    '!home' => drush_get_option('aegir_root'),
    '!user' => drush_get_option('script_user'),
    '!web' => drush_get_option('web_group'),
    '!web_server' => drush_get_option('http_service_type'),
    '!web_server_port' => drush_get_option('http_port'),
    '!db_host' => drush_get_option('aegir_db_host'),
    '!db_user' => drush_get_option('aegir_db_user'),
    '!db_pass' => is_null(drush_get_option('aegir_db_pass', NULL, 'process')) ? '<previously set>' : '<prompted>',
    '!db_port' => drush_get_option('aegir_db_port'),
    '!version' => drush_get_option('version'),
    '!root' => $root,
    '!makefile' => $makefile_msg,
151
    '!profile' => $profile_msg,
152 153
    '!email' => drush_get_option('client_email'),
  )));
154

155
  if (!drush_confirm(dt('Do you really want to proceed with the install'))) {
156
    return drush_set_error('PROVISION_CANCEL_INSTALL', dt('Installation aborted'));
157
  }
158

159 160 161
  return TRUE;
}

162 163 164
/**
 * Drush command to install hostmaster.
 */
165
function drush_provision_hostmaster_install($site = NULL) {
166
  $version = drush_get_option('version');
167
  $site = drush_get_option('site', provision_fqdn());
168 169
  $aegir_root = drush_get_option('aegir_root');
  $platform = drush_get_option(array('r', 'root'));
170

171 172
  $aegir_http_host = drush_get_option('aegir_host');
  $aegir_http_port = drush_get_option('http_port');
173 174
  $aegir_db_user = drush_get_option('aegir_db_user');
  $aegir_db_pass = drush_get_option('aegir_db_pass');
175
  $aegir_db_port = drush_get_option('aegir_db_port');
176
  $aegir_db_host = drush_get_option('aegir_db_host');
177
  $aegir_db_grant_all_hosts = drush_get_option('aegir_db_grant_all_hosts');
178

179
  $server = '@server_master';
180
  $master_context = array(
181
    'context_type' => 'server',
182
    // files
183
    'remote_host' => $aegir_http_host,
184
    'aegir_root' => $aegir_root,
185
    'script_user' => drush_get_option('script_user'),
186
   // apache or nginx or..
187
    'http_service_type' => drush_get_option('http_service_type'),
188
    'http_port' => $aegir_http_port,
189
    'web_group' => drush_get_option('web_group'),
190
    'master_url' => "http://" . $site,
191
    'db_port' => $aegir_db_port,
192 193
  );

194
  $master_db = sprintf("mysql://%s:%s@%s:%s", urlencode($aegir_db_user), urlencode($aegir_db_pass), $aegir_db_host, $aegir_db_port);
195
  if ($aegir_http_host == $aegir_db_host) {
196 197 198 199
    $master_context['db_service_type'] = 'mysql';
    $master_context['master_db'] = $master_db;
    $dbserver = $server;
  } else {
200
    $dbserver = '@server_' . $aegir_db_host;
201
    $dbserver_context = array(
202
      'remote_host' => $aegir_db_host,
203 204 205
      'context_type' => 'server',
      'db_service_type' => 'mysql',
      'master_db' => $master_db,
206
      'db_grant_all_hosts' => $aegir_db_grant_all_hosts,
207
      'db_port' => $aegir_db_port,
208
    );
209
    drush_invoke_process('@none', "provision-save", array($dbserver), $dbserver_context);
210 211
    provision_backend_invoke($dbserver, 'provision-verify');
  }
212
  drush_invoke_process('@none', "provision-save", array($server), $master_context);
213
  provision_backend_invoke($server, 'provision-verify');
214

215
  // exit if an error has occured.
216
  if (drush_get_error()) {
helmo's avatar
helmo committed
217 218
    drush_log('Caught drush error, ending drush_provision_hostmaster_install', 'error');
    drush_log(print_r(drush_get_error_log(), 1), 'error');
219 220 221
    return false;
  }

222
  if (drush_get_option('backend-only')) {
223
    drush_log('Skipping hostmaster frontend installation, backend-only option set.', 'notice');
224 225 226
    return;
  }

227 228
  drush_log('Preparing the hostmaster frontend installation.', 'notice');

229
  $platform_name = '@platform_hostmaster';
230
  drush_invoke_process('@none', "provision-save", array($platform_name), array(
231 232 233
    'context_type' => 'platform',
    'server' => $server,
    'web_server' => $server,
234
    'root' => $platform,
anarcat's avatar
anarcat committed
235
    'makefile' => drush_get_option('makefile'),
236
  ));
237 238 239 240
  // propagate working-copy args downward
  $options = array();
  if (drush_get_option('working-copy')) {
    $options['working-copy'] = 1;
241 242 243

    # Don't rewrite module info files if using working-copy.
    $options['no-gitinfofile'] = 1;
244 245
  }
  provision_backend_invoke($platform_name, 'provision-verify', array(), $options);
246

247
  // exit if an error has occured.
248
  if (drush_get_error()) {
249 250
    drush_log('Caught drush error, ending drush_provision_hostmaster_install - B', 'debug');
    drush_log(print_r(drush_get_error_log(), 1), 'debug');
251 252 253
    return false;
  }

254 255 256
  drush_set_default('profile', 'hostmaster');
  $profile = drush_get_option('profile');

257
  $site_name = '@hostmaster';
258
  drush_invoke_process('@none', "provision-save", array($site_name), array(
259 260
    'context_type' => 'site',
    'platform' => $platform_name,
261
    'db_server' => $dbserver,
262
    'uri' => $site,
263
    'client_name' => drush_get_option('client_name'),
264
    'client_email' => drush_get_option('client_email'),
265
    'profile' => $profile,
266
    'drush_aliases' => 'hm',
267
  ));
268 269

  drush_log('Starting with the hostmaster frontend installation.', 'notice');
270
  $data = provision_backend_invoke($site_name, 'provision-install', array(), array('client_email' => drush_get_option('client_email')));
271
  provision_backend_invoke($site_name, 'provision-verify');
272

273
  // exit if an error has occured.
274
  if (drush_get_error()) {
275 276
    drush_log('Caught drush error, ending drush_provision_hostmaster_install - C', 'debug');
    drush_log(print_r(drush_get_error_log(), 1), 'debug');
277 278 279 280
    return false;
  }


mig5's avatar
mig5 committed
281
  drush_print(dt("Initializing the hosting system"));
282
  drush_invoke_process('@none', 'cache-clear', array('drush'));
283
  provision_backend_invoke($site_name, 'hosting-setup');
284

285 286 287 288 289 290 291 292 293 294 295 296 297
  drush_print("");
  drush_print("==============================================================================");
  drush_print("");
  drush_print("");
  drush_print(dt("Congratulations, Aegir has now been installed."));
  drush_print("");
  drush_print(dt("You should now log in to the Aegir frontend by opening the following link in your web browser:"));
  drush_print("");
  drush_print($data['context']['login_link']);
  drush_print("");
  drush_print("");
  drush_print("==============================================================================");
  drush_print("");
298 299
}

300 301 302 303 304 305 306 307 308
/**
 *  Implements drush_hook_post_hostmaster_install().
 */
function drush_provision_post_hostmaster_install() {
  $backend_only = drush_get_option('backend-only');
  if (empty($backend_only)) {
    drush_invoke_process('@hostmaster', 'cache-clear', array('drush'));
  }
}