verify.provision.inc 8.45 KB
Newer Older
1 2 3
<?php

/**
helmo's avatar
helmo committed
4 5 6 7 8 9
 * @file
 * Code related to verify tasks.
 */

/**
 * Provision verify command.
10 11 12
 *
 * Disable an enabled and installed site
 */
13
function drush_provision_drupal_provision_verify_validate() {
drumm's avatar
drumm committed
14
  if (d()->type === 'site') {
15 16
    drush_bootstrap(DRUSH_BOOTSTRAP_DRUPAL_SITE);
    provision_prepare_environment();
17

drumm's avatar
drumm committed
18
    _provision_drupal_create_settings_file();
19
    provision_drupal_push_site();
20

drumm's avatar
drumm committed
21 22
    drush_errors_on();
    drush_bootstrap(DRUSH_BOOTSTRAP_DRUPAL_FULL);
23
  }
24 25 26 27
}


/**
helmo's avatar
helmo committed
28
 * Implements drush_hook_pre_COMMAND().
29
 */
30
function drush_provision_drupal_pre_provision_verify() {
drumm's avatar
drumm committed
31
  if (d()->type === 'server') {
32

33 34
    $config_root = dirname(d()->config_path);
    provision_file()->create_dir($config_root, dt('Provision configuration root'), 0711);
35
    provision_file()->create_dir(d()->config_path, dt('Provision configuration'), 0711);
36
    provision_file()->create_dir(d()->clients_path, dt('Provision client home'), 0711);
37
    if (d()->name == '@server_master') {
38
      provision_file()->create_dir(d()->backup_path, dt('Backup'), 0700);
39 40 41
      provision_file()->create_dir(d()->include_path, dt('Provision PHP configuration'), 0711);
      if (!provision_file()->exists(d()->include_path . '/global.inc')->succeed('Global configuration file exists')->status()) {
        // Create an empty global.inc so the include doesn't fail with
helmo's avatar
helmo committed
42
        // open_basedir restrictions.
43
        $config = new Provision_Config_Global_Settings(d()->name);
44 45 46 47 48 49 50
        $config->write();
      }
    }
    else {
      if (provision_file()->exists(d()->include_path)) {
        d()->service('http')->sync(d()->include_path);
      }
51
    }
52
  }
drumm's avatar
drumm committed
53
  elseif (d()->type === 'platform') {
54 55 56 57
  
    drush_log(dt("Checking for platform at path @path...", array('@path' => d()->root)), 'debug');
    drush_log(dt("Platform makefile: @makefile", array('@makefile' => d()->makefile)), 'debug');
    
helmo's avatar
helmo committed
58
    // Create a platform from a makefile, if it doesnt already exist and the makefile does.
59
    if (!provision_file()->exists(d()->root)->status() && !empty(d()->makefile)) {
60
      drush_log(dt("Platform path does not exist, fetching from drush makefile"), 'notice');
61 62
      $arguments = array();
      $options = array();
helmo's avatar
helmo committed
63
      // Propagate working-copy args downward.
64
      if (drush_get_option('working-copy') || !empty(d()->make_working_copy)) {
65
        $options[] = '--working-copy';
66
      }
67 68
      $arguments[] = d()->makefile;
      $arguments[] = d()->root;
69 70 71
      
      // Change current directory to makefile's directory until Drush fixes this bug: https://github.com/drush-ops/drush/issues/2482
      // @TODO: Remove this once is committed.
72 73 74 75
      if (!empty(d()->makefile) && file_exists(dirname(d()->makefile))) {
        chdir(dirname(d()->makefile));
      }

76
      drush_invoke_process('@none', "make", $arguments, $options);
77
      if (drush_get_error()) {
78
        return drush_set_error("DRUSH_MAKE_FAILED",
79
          "Could not download platform using drush make. No platform present");
80 81 82
      }
    }

83
    // Composer Install Support
84 85
    // Step 0: if drush variable allows composer install on platforms, and check for composer executable fails, show warning.
    if (drush_get_option('provision_composer_install_platforms', TRUE) && !shell_exec('composer')) {
86
      drush_log(dt('The composer executable was not found. Install composer using instructions located at https://getcomposer.org/doc/00-intro.md#globally or, if you wish to disable composer install, set "provision_composer_install_platforms" to FALSE in the ~/.drush/drushrc.php file.'), 'warning');
87 88 89
    }
    // If drush variable allows composer install on platforms (and composer exec passed)...
    elseif (drush_get_option('provision_composer_install_platforms', TRUE)) {
90 91 92 93
      // Detect composer-based platform and run composer install if it has not been run yet.
      // Step 1: Look for composer directory. Could be Drupal root. Could be the git repo_root.
      if (provision_file()->exists(d()->root . DIRECTORY_SEPARATOR . 'composer.json')->status()) {
        $composer_directory = d()->root;
94
      }
95
      elseif (d()->repo_path && provision_file()->exists(d()->repo_path . DIRECTORY_SEPARATOR . 'composer.json')->status()) {
96 97 98 99 100 101 102 103 104
        $composer_directory = d()->repo_path;
      }

      // Step 2: Run composer install if composer.json is present.
      // If drush option provision_composer_install_platforms_verify_always is set to
      // false, only run `composer-install` if ./vendor directory is missing.
      if (isset($composer_directory) && (!file_exists($composer_directory . DIRECTORY_SEPARATOR . 'vendor') || drush_get_option('provision_composer_install_platforms_verify_always', TRUE))) {

        // Composer Install command: Do not interact, do not show download progress.
105
        // Customizable by setting drush option 'provision_composer_install_command'
106 107
        $composer_command = drush_get_option('provision_composer_install_command', 'composer install --no-interaction --no-progress --no-dev');

108 109
        // Implement Symfony Process component for line-by-line output logging.
        provision_process($composer_command, $composer_directory);
110

111 112 113
      }
    }

114 115 116 117 118
    // Re-set the ROOT PATH for Drush.
    // In cases where we just build the platform Drush would have cleared this value.
    // See Drush commit 3b74d40b1228f022464b92243c16127eb613c2df
    drush_set_context('DRUSH_SELECTED_DRUPAL_ROOT', d()->root);

Adrian Rossouw's avatar
Adrian Rossouw committed
119
    drush_bootstrap(DRUSH_BOOTSTRAP_DRUPAL_ROOT);
120
    // If we couldn't bootstrap, there's no sense in continuing past this point.
121 122
    if (drush_get_error()) {
      $errors = drush_get_error_log();
Jon Pugh's avatar
Jon Pugh committed
123 124 125
      foreach ($errors as $type => &$error) {
        $error = "$type: {$error[0]}";
      }
126
      return drush_set_error("DRUSH_INVALID_DRUPAL_ROOT",
127 128 129 130
        dt("Drush could not bootstrap the platform at @path. Please check the platform directory exists and is readable. The errors were: @errors", array(
          '@path' => d()->root,
          '@errors' => implode('|', $errors)
        )));
131
    }
132
    provision_file()->writable(d()->root . '/sites')
133 134
      ->succeed('Drupal sites directory @path is writable by the provisioning script')
      ->fail('Drupal sites directory @path is not writable by the provisioning script', 'PROVISION_SITES_DIR_NOT_WRITABLE');
135

136 137 138 139 140 141 142 143 144
    // Ensure sites.php file exists for Drupal 8 and above.
    $sitesfile = "sites/sites.php";
    if (drush_drupal_major_version() >= 8 && !file_exists($sitesfile)) {
      if (!drush_op('copy', 'sites/example.sites.php', $sitesfile) && !drush_get_context('DRUSH_SIMULATE')) {
        return drush_set_error(dt('Failed to copy sites/sites.php to @sitesfile', array('@sitesfile' => $sitesfile)));
      }
    }

    drush_set_option('sites', array_keys((array) provision_drupal_find_sites()), 'drupal');
145
    drush_log(dt("This platform is running @short_name @version", array('@short_name' => 'drupal', '@version' => drush_drupal_version())));
146
    drush_set_option('packages', _scrub_object(provision_find_packages()), 'drupal');
147
    provision_drupal_push_site();
148
  }
drumm's avatar
drumm committed
149
  elseif (d()->type === 'site') {
150
    drush_set_option('packages', _scrub_object(provision_drupal_system_map()), 'site');
151
    // This is the actual drupal provisioning requirements.
152 153
    _provision_drupal_create_directories();
    _provision_drupal_maintain_aliases();
154
    _provision_drupal_ensure_htaccess_update();
155
    // Requires at least the database settings to complete.
156 157 158

    _provision_drupal_create_settings_file();

159 160
    // If this is the hostmaster site, save the ~/.drush/drushrc.php file.
    if (d()->root == d('@hostmaster')->root && d()->uri == d('@hostmaster')->uri) {
161 162
      $aegir_drushrc = new Provision_Config_Drushrc_Aegir();
      $aegir_drushrc->write();
163 164

      drush_invoke_process('@none', "cache-clear", array('drush'));
165 166
    }

167
    provision_drupal_push_site(drush_get_option('override_slave_authority', FALSE));
168 169 170 171
  }
}

/**
helmo's avatar
helmo committed
172
 * Implements drush_hook_provision_post_COMMAND().
173
 */
174
function drush_provision_drupal_post_provision_verify() {
drumm's avatar
drumm committed
175
  if (d()->type === 'site') {
176
    _provision_drupal_rebuild_caches();
177
    drush_set_option('installed', TRUE, 'site');
178 179

    _provision_client_create_symlink();
180
    drush_include_engine('drupal', 'cron_key');
181 182 183 184 185 186 187 188 189

    # Cleanup useless `aegir.services.yml` files.
    # See: https://www.drupal.org/project/provision/issues/3066538
    $path = d()->site_path . '/aegir.services.yml';
    if (provision_file()->exists($path)->status()) {
      provision_file()->unlink($path)
        ->succeed('Cleaned up unused Aegir services.yml file.')
        ->fail('Failed to clean up unused Aegir services.yml file.', 'PROVISION_DELETE_AEGIR_SERVICES_YML_FAILED');
    }
190 191
  }
}