site_install.drush.inc

  1. 8.0.x commands/core/site_install.drush.inc
  2. 6.x commands/core/site_install.drush.inc
  3. 7.x commands/core/site_install.drush.inc
  4. 3.x commands/core/site_install.drush.inc
  5. 4.x commands/core/site_install.drush.inc
  6. 5.x commands/core/site_install.drush.inc
  7. master commands/core/site_install.drush.inc

Functions

Namesort descending Description
drush_core_pre_site_install Perform setup tasks for installation.
drush_core_site_install Command callback.
drush_core_site_install_validate Command validate.
site_install_drush_command
site_install_drush_help_alter Implements hook_drush_help_alter().

File

commands/core/site_install.drush.inc
View source
  1. <?php
  2. use Drush\Log\LogLevel;
  3. use Drupal\Core\Config\FileStorage;
  4. function site_install_drush_command() {
  5. $items['site-install'] = array(
  6. 'description' => 'Install Drupal along with modules/themes/configuration using the specified install profile.',
  7. 'arguments' => array(
  8. // In Drupal 7 installation profiles can be marked as exclusive by placing
  9. // a
  10. // @code
  11. // exclusive: true
  12. // @endcode
  13. // line in the profile's info file.
  14. // See https://www.drupal.org/node/1022020 for more information.
  15. //
  16. // In Drupal 8 you can turn your installation profile into a distribution
  17. // by providing a
  18. // @code
  19. // distribution:
  20. // name: 'Distribution name'
  21. // @endcode
  22. // block in the profile's info YAML file.
  23. // See https://www.drupal.org/node/2210443 for more information.
  24. 'profile' => 'The install profile you wish to run. Defaults to \'default\' in D6, \'standard\' in D7+, unless an install profile is marked as exclusive (or as a distribution in D8+ terminology) in which case that is used.',
  25. 'key=value...' => 'Any additional settings you wish to pass to the profile. The key is in the form [form name].[parameter name].',
  26. ),
  27. 'options' => array(
  28. 'db-url' => array(
  29. 'description' => 'A Drupal 6 style database URL. Only required for initial install - not re-install.',
  30. 'example-value' => 'mysql://root:pass@host/db',
  31. ),
  32. 'db-prefix' => 'An optional table prefix to use for initial install. Can be a key-value array of tables/prefixes in a drushrc file (not the command line).',
  33. 'db-su' => array(
  34. 'description' => 'Account to use when creating a new database. Must have Grant permission (mysql only). Optional.',
  35. 'example-value' => 'root',
  36. ),
  37. 'db-su-pw' => array(
  38. 'description' => 'Password for the "db-su" account. Optional.',
  39. 'example-value' => 'pass',
  40. ),
  41. 'account-name' => 'uid1 name. Defaults to admin',
  42. 'account-pass' => 'uid1 pass. Defaults to a randomly generated password. If desired, set a fixed password in drushrc.php.',
  43. 'account-mail' => 'uid1 email. Defaults to admin@example.com',
  44. 'locale' => array(
  45. 'description' => 'A short language code. Sets the default site language. Language files must already be present. You may use download command to get them.',
  46. 'example-value' => 'en-GB',
  47. ),
  48. 'clean-url'=> 'Defaults to clean; use --no-clean-url to disable. Note that Drupal 8 and later requires clean.',
  49. 'site-name' => 'Defaults to Site-Install',
  50. 'site-mail' => 'From: for system mailings. Defaults to admin@example.com',
  51. 'sites-subdir' => array(
  52. 'description' => "Name of directory under 'sites' which should be created. Only needed when the subdirectory does not already exist. Defaults to 'default'",
  53. 'value' => 'required',
  54. 'example-value' => 'directory_name',
  55. ),
  56. 'config-dir' => 'A path pointing to a full set of configuration which should be imported after installation.',
  57. ),
  58. 'examples' => array(
  59. 'drush site-install expert --locale=uk' => '(Re)install using the expert install profile. Set default language to Ukrainian.',
  60. 'drush site-install --db-url=mysql://root:pass@localhost:port/dbname' => 'Install using the specified DB params.',
  61. 'drush site-install --db-url=sqlite://sites/example.com/files/.ht.sqlite' => 'Install using SQLite (D7+ only).',
  62. 'drush site-install --account-name=joe --account-pass=mom' => 'Re-install with specified uid1 credentials.',
  63. 'drush site-install standard install_configure_form.site_default_country=FR my_profile_form.my_settings.key=value' => 'Pass additional arguments to the profile (D7 example shown here - for D6, omit the form id).',
  64. "drush site-install standard install_configure_form.update_status_module='array(FALSE,FALSE)'" => 'Disable email notification during install and later. If your server has no smtp, this gets rid of an error during install.',
  65. ),
  66. 'bootstrap' => DRUSH_BOOTSTRAP_DRUPAL_ROOT,
  67. 'aliases' => array('si'),
  68. );
  69. return $items;
  70. }
  71. /**
  72. * Implements hook_drush_help_alter().
  73. */
  74. function site_install_drush_help_alter(&$command) {
  75. // Drupal version-specific customizations.
  76. if ($command['command'] == 'site-install') {
  77. if (drush_drupal_major_version() >= 8) {
  78. unset($command['options']['clean-url']);
  79. }
  80. else {
  81. unset($command['options']['config-dir']);
  82. }
  83. }
  84. }
  85. /**
  86. * Command validate.
  87. */
  88. function drush_core_site_install_validate() {
  89. if ($sites_subdir = drush_get_option('sites-subdir')) {
  90. $lower = strtolower($sites_subdir);
  91. if ($sites_subdir != $lower) {
  92. drush_log(dt('Only lowercase sites-subdir are valid. Switching to !lower.', array('!lower' => $lower)), LogLevel::WARNING);
  93. drush_set_option('sites-subdir', $lower);
  94. }
  95. // Make sure that we will bootstrap to the 'sites-subdir' site.
  96. drush_set_context('DRUSH_SELECTED_URI', 'http://' . $sites_subdir);
  97. }
  98. if ($config = drush_get_option('config-dir')) {
  99. if (!file_exists($config)) {
  100. return drush_set_error('config_import_target', 'The config source directory does not exist.');
  101. }
  102. if (!is_dir($config)) {
  103. return drush_set_error('config_import_target', 'The config source is not a directory.');
  104. }
  105. }
  106. }
  107. /**
  108. * Perform setup tasks for installation.
  109. * @return bool
  110. * @throws \Drush\Sql\SqlException
  111. */
  112. function drush_core_pre_site_install() {
  113. $sql = drush_sql_get_class();
  114. if (!$db_spec = $sql->db_spec()) {
  115. drush_set_error(dt('Could not determine database connection parameters. Pass --db-url option.'));
  116. return;
  117. }
  118. // Make sure URI is set so we get back a proper $alias_record. Needed for quick-drupal.
  119. _drush_bootstrap_selected_uri();
  120. $alias_record = drush_sitealias_get_record('@self');
  121. $sites_subdir = drush_sitealias_local_site_path($alias_record);
  122. // Override with sites-subdir if specified.
  123. if ($dir = drush_get_option('sites-subdir')) {
  124. $sites_subdir = "sites/$dir";
  125. }
  126. $conf_path = $sites_subdir;
  127. $settingsfile = "$conf_path/settings.php";
  128. $sitesfile = "sites/sites.php";
  129. $default = realpath($alias_record['root'] . '/sites/default');
  130. $sitesfile_write = drush_drupal_major_version() >= 8 && $conf_path != $default && !file_exists($sitesfile);
  131. if (!file_exists($settingsfile)) {
  132. $msg[] = dt('create a @settingsfile file', array('@settingsfile' => $settingsfile));
  133. }
  134. if ($sitesfile_write) {
  135. $msg[] = dt('create a @sitesfile file', array('@sitesfile' => $sitesfile));
  136. }
  137. if ($sql->db_exists()) {
  138. $msg[] = dt("DROP all tables in your '@db' database.", array('@db' => $db_spec['database']));
  139. }
  140. else {
  141. $msg[] = dt("CREATE the '@db' database.", array('@db' => $db_spec['database']));
  142. }
  143. if (!drush_confirm(dt('You are about to ') . implode(dt(' and '), $msg) . ' Do you want to continue?')) {
  144. return drush_user_abort();
  145. }
  146. // Can't install without sites subdirectory and settings.php.
  147. if (!file_exists($conf_path)) {
  148. if (!drush_mkdir($conf_path) && !drush_get_context('DRUSH_SIMULATE')) {
  149. drush_set_error(dt('Failed to create directory @conf_path', array('@conf_path' => $conf_path)));
  150. return;
  151. }
  152. }
  153. else {
  154. drush_log(dt('Sites directory @subdir already exists - proceeding.', array('@subdir' => $conf_path)));
  155. }
  156. if (!drush_file_not_empty($settingsfile)) {
  157. if (!drush_op('copy', 'sites/default/default.settings.php', $settingsfile) && !drush_get_context('DRUSH_SIMULATE')) {
  158. return drush_set_error(dt('Failed to copy sites/default/default.settings.php to @settingsfile', array('@settingsfile' => $settingsfile)));
  159. }
  160. }
  161. // Write an empty sites.php if we are on D8 and using multi-site.
  162. if ($sitesfile_write) {
  163. if (!drush_op('copy', 'sites/example.sites.php', $sitesfile) && !drush_get_context('DRUSH_SIMULATE')) {
  164. return drush_set_error(dt('Failed to copy sites/sites.php to @sitesfile', array('@sitesfile' => $sitesfile)));
  165. }
  166. }
  167. // We need to be at least at DRUSH_BOOTSTRAP_DRUPAL_SITE to select the site uri to install to
  168. define('MAINTENANCE_MODE', 'install');
  169. drush_bootstrap(DRUSH_BOOTSTRAP_DRUPAL_SITE);
  170. if (!$sql->drop_or_create()) {
  171. return drush_set_error(dt('Failed to create database: @error', array('@error' => implode(drush_shell_exec_output()))));
  172. }
  173. return TRUE;
  174. }
  175. /**
  176. * Command callback.
  177. */
  178. function drush_core_site_install($profile = NULL) {
  179. $args = func_get_args();
  180. $form_options = array();
  181. if ($args) {
  182. // The first argument is the profile.
  183. $profile = array_shift($args);
  184. // Subsequent arguments are additional form values.
  185. foreach ($args as $arg) {
  186. list($key, $value) = explode('=', $arg, 2);
  187. // Allow for numeric and NULL values to be passed in.
  188. if (is_numeric($value)) {
  189. $value = intval($value);
  190. }
  191. elseif ($value == 'NULL') {
  192. $value = NULL;
  193. }
  194. $form_options[$key] = $value;
  195. }
  196. }
  197. // --config-dir fails with Standard profile and any other one that carries content entities.
  198. // Force to minimal install profile.
  199. if (drush_get_option('config-dir')) {
  200. drush_log(dt("Using 'minimal' install profile since --config-dir option was provided.", 'ok'));
  201. $profile = 'minimal';
  202. }
  203. drush_include_engine('drupal', 'site_install');
  204. drush_core_site_install_version($profile, $form_options);
  205. // Post installation, run the configuration import.
  206. if ($config = drush_get_option('config-dir')) {
  207. // Set the destination site UUID to match the source UUID, to bypass a core fail-safe.
  208. $source_storage = new FileStorage($config);
  209. $options = ['yes' => TRUE];
  210. drush_invoke_process('@self', 'config-set', array('system.site', 'uuid', $source_storage->read('system.site')['uuid']), $options);
  211. // Run a full configuration import.
  212. drush_invoke_process('@self', 'config-import', array(), array('source' => $config) + $options);
  213. }
  214. }