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. function site_install_drush_command() {
  4. $items['site-install'] = array(
  5. 'description' => 'Install Drupal along with modules/themes/configuration using the specified install profile.',
  6. 'arguments' => array(
  7. // In Drupal 7 installation profiles can be marked as exclusive by placing
  8. // a
  9. // @code
  10. // exclusive: true
  11. // @endcode
  12. // line in the profile's info file.
  13. // See https://www.drupal.org/node/1022020 for more information.
  14. //
  15. // In Drupal 8 you can turn your installation profile into a distribution
  16. // by providing a
  17. // @code
  18. // distribution:
  19. // name: 'Distribution name'
  20. // @endcode
  21. // block in the profile's info YAML file.
  22. // See https://www.drupal.org/node/2210443 for more information.
  23. '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.',
  24. 'key=value...' => 'Any additional settings you wish to pass to the profile. Fully supported on D7+, partially supported on D6 (single step configure forms only). The key is in the form [form name].[parameter name] on D7 or just [parameter name] on D6.',
  25. ),
  26. 'options' => array(
  27. 'db-url' => array(
  28. 'description' => 'A Drupal 6 style database URL. Only required for initial install - not re-install.',
  29. 'example-value' => 'mysql://root:pass@host/db',
  30. ),
  31. '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).',
  32. 'db-su' => array(
  33. 'description' => 'Account to use when creating a new database. Must have Grant permission (mysql only). Optional.',
  34. 'example-value' => 'root',
  35. ),
  36. 'db-su-pw' => array(
  37. 'description' => 'Password for the "db-su" account. Optional.',
  38. 'example-value' => 'pass',
  39. ),
  40. 'account-name' => 'uid1 name. Defaults to admin',
  41. 'account-pass' => 'uid1 pass. Defaults to a randomly generated password. If desired, set a fixed password in drushrc.php.',
  42. 'account-mail' => 'uid1 email. Defaults to admin@example.com',
  43. 'locale' => array(
  44. 'description' => 'A short language code. Sets the default site language. Language files must already be present. You may use download command to get them.',
  45. 'example-value' => 'en-GB',
  46. ),
  47. 'clean-url'=> 'Defaults to clean; use --no-clean-url to disable. Note that Drupal 8 and later requires clean.',
  48. 'site-name' => 'Defaults to Site-Install',
  49. 'site-mail' => 'From: for system mailings. Defaults to admin@example.com',
  50. 'sites-subdir' => array(
  51. 'description' => "Name of directory under 'sites' which should be created. Only needed when the subdirectory does not already exist. Defaults to 'default'",
  52. 'value' => 'required',
  53. 'example-value' => 'directory_name',
  54. ),
  55. 'writable' => 'Make CMI and other dirs writable by both web and CLI users. Suitable for non Prod environments.',
  56. 'keep-config' => 'Keep CMI directories untouched. This preserves existing configuration.'
  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 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']['writable']);
  82. unset($command['options']['keep-config']);
  83. }
  84. }
  85. }
  86. /**
  87. * Command validate.
  88. */
  89. function drush_core_site_install_validate() {
  90. if ($sites_subdir = drush_get_option('sites-subdir')) {
  91. $lower = strtolower($sites_subdir);
  92. if ($sites_subdir != $lower) {
  93. drush_log(dt('Only lowercase sites-subdir are valid. Switching to !lower.', array('!lower' => $lower)), LogLevel::WARNING);
  94. drush_set_option('sites-subdir', $lower);
  95. }
  96. // Make sure that we will bootstrap to the 'sites-subdir' site.
  97. drush_set_context('DRUSH_SELECTED_URI', 'http://' . $sites_subdir);
  98. }
  99. }
  100. /**
  101. * Perform setup tasks for installation.
  102. */
  103. function drush_core_pre_site_install($profile = NULL) {
  104. $sql = drush_sql_get_class();
  105. if (!$db_spec = $sql->db_spec()) {
  106. drush_set_error(dt('Could not determine database connection parameters. Pass --db-url option.'));
  107. return;
  108. }
  109. // Make sure URI is set so we get back a proper $alias_record. Needed for quick-drupal.
  110. _drush_bootstrap_selected_uri();
  111. $alias_record = drush_sitealias_get_record('@self');
  112. $sites_subdir = drush_sitealias_local_site_path($alias_record);
  113. // Override with sites-subdir if specified.
  114. if ($dir = drush_get_option('sites-subdir')) {
  115. $sites_subdir = "sites/$dir";
  116. }
  117. $conf_path = $sites_subdir;
  118. // Handle the case where someuse uses --variables to set the file public path. Won't work on D8+.
  119. $files = !empty($GLOBALS['conf']['files_public_path']) ? $GLOBALS['conf']['files_public_path'] : "$conf_path/files";
  120. $settingsfile = "$conf_path/settings.php";
  121. $sitesfile = "sites/sites.php";
  122. $default = realpath($alias_record['root'] . '/sites/default');
  123. $sitesfile_write = drush_drupal_major_version() >= 8 && $conf_path != $default && !file_exists($sitesfile);
  124. if (!file_exists($settingsfile)) {
  125. $msg[] = dt('create a @settingsfile file', array('@settingsfile' => $settingsfile));
  126. }
  127. if ($sitesfile_write) {
  128. $msg[] = dt('create a @sitesfile file', array('@sitesfile' => $sitesfile));
  129. }
  130. if ($sql->db_exists()) {
  131. $msg[] = dt("DROP all tables in your '@db' database.", array('@db' => $db_spec['database']));
  132. }
  133. else {
  134. $msg[] = dt("CREATE the '@db' database.", array('@db' => $db_spec['database']));
  135. }
  136. if (!drush_confirm(dt('You are about to ') . implode(dt(' and '), $msg) . ' Do you want to continue?')) {
  137. return drush_user_abort();
  138. }
  139. // Can't install without sites subdirectory and settings.php.
  140. if (!file_exists($conf_path)) {
  141. if (!drush_mkdir($conf_path) && !drush_get_context('DRUSH_SIMULATE')) {
  142. drush_set_error(dt('Failed to create directory @conf_path', array('@conf_path' => $conf_path)));
  143. return;
  144. }
  145. }
  146. else {
  147. drush_log(dt('Sites directory @subdir already exists - proceeding.', array('@subdir' => $conf_path)));
  148. }
  149. if (!drush_file_not_empty($settingsfile)) {
  150. if (!drush_op('copy', 'sites/default/default.settings.php', $settingsfile) && !drush_get_context('DRUSH_SIMULATE')) {
  151. return drush_set_error(dt('Failed to copy sites/default/default.settings.php to @settingsfile', array('@settingsfile' => $settingsfile)));
  152. }
  153. if (drush_drupal_major_version() == 6) {
  154. // On D6, we have to write $db_url ourselves. In D7+, the installer does it.
  155. file_put_contents($settingsfile, "\n" . '$db_url = \'' . drush_get_option('db-url') . "';\n", FILE_APPEND);
  156. // Instead of parsing and performing string replacement on the configuration file,
  157. // the options are appended and override the defaults.
  158. // Database table prefix
  159. if (!empty($db_spec['db_prefix'])) {
  160. if (is_array($db_spec['db_prefix'])) {
  161. // Write db_prefix configuration as an array
  162. $db_prefix_config = '$db_prefix = ' . var_export($db_spec['db_prefix'], TRUE) . ';';
  163. }
  164. else {
  165. // Write db_prefix configuration as a string
  166. $db_prefix_config = '$db_prefix = \'' . $db_spec['db_prefix'] . '\';';
  167. }
  168. file_put_contents($settingsfile, "\n" . $db_prefix_config . "\n", FILE_APPEND);
  169. }
  170. }
  171. }
  172. // Write an empty sites.php if we are on D8 and using multi-site.
  173. if ($sitesfile_write) {
  174. if (!drush_op('copy', 'sites/example.sites.php', $sitesfile) && !drush_get_context('DRUSH_SIMULATE')) {
  175. return drush_set_error(dt('Failed to copy sites/sites.php to @sitesfile', array('@sitesfile' => $sitesfile)));
  176. }
  177. }
  178. // We need to be at least at DRUSH_BOOTSTRAP_DRUPAL_SITE to select the site uri to install to
  179. define('MAINTENANCE_MODE', 'install');
  180. if (drush_drupal_major_version() == 6) {
  181. // The Drupal 6 installer needs to bootstrap up to the specified site.
  182. drush_bootstrap(DRUSH_BOOTSTRAP_DRUPAL_CONFIGURATION);
  183. }
  184. else {
  185. drush_bootstrap(DRUSH_BOOTSTRAP_DRUPAL_SITE);
  186. }
  187. $sql->drop_or_create($db_spec);
  188. if (drush_drupal_major_version() >= 8) {
  189. // Empty out any existing config directories.
  190. $directories = array();
  191. // Check for alternative format:
  192. // @code
  193. // $config_directories = array(
  194. // 'active' => 'config/active',
  195. // 'staging' => 'config/staging',
  196. // );
  197. // @endcode
  198. // @todo update this regex to omit path.
  199. // preg_match('/\n\$config_directories[^;]*\'path\' => \'([^\']*)\'.*\'path\' => \'([^\']*)\'/s', file_get_contents($settingsfile), $directories);
  200. if (empty($directories)) {
  201. // Check for canonical format:
  202. // @code
  203. // $config_directories['active'] = 'config/active';
  204. // $config_directories['staging'] = 'config/staging';
  205. // @endcode
  206. foreach (array('active', 'staging') as $type) {
  207. preg_match('/\n\$config_directories[^;]*\'' . $type . '\'\] = \'([^\']*)\'/', file_get_contents($settingsfile), $directory);
  208. if (isset($directory[1])) {
  209. $directories[$type] = $directory[1];
  210. }
  211. }
  212. }
  213. else {
  214. $directories['active'] = $files . '/' . $directories[1];
  215. $directories['staging'] = $files . '/' . $directories[2];
  216. }
  217. if (drush_get_option('keep-config', FALSE) == FALSE) {
  218. foreach ($directories as $directory) {
  219. if (file_exists($directory)) {
  220. drush_delete_dir_contents($directory, TRUE);
  221. }
  222. }
  223. }
  224. if (drush_get_option('writable')) {
  225. if (is_writable($settingsfile)) {
  226. $chmod = <<<'EOD'
  227. // Appended by Drush site-install.
  228. $settings['file_chmod_directory'] = 0777;
  229. $settings['file_chmod_file'] = 0777;
  230. EOD;
  231. drush_file_append_data($settingsfile, "\n$chmod\n");
  232. }
  233. else {
  234. drush_log(dt('Not appending file_chmod_* to @settingsfile as it is not writable.', array('@settingsfile' => $settingsfile)), LogLevel::NOTICE);
  235. }
  236. }
  237. // Remove files/php if needed.
  238. drush_delete_dir("$files/php", TRUE);
  239. }
  240. return TRUE;
  241. }
  242. /**
  243. * Command callback.
  244. */
  245. function drush_core_site_install($profile = NULL) {
  246. $args = func_get_args();
  247. $form_options = array();
  248. if ($args) {
  249. // The first argument is the profile.
  250. $profile = array_shift($args);
  251. // Subsequent arguments are additional form values.
  252. foreach ($args as $arg) {
  253. list($key, $value) = explode('=', $arg);
  254. // Allow for numeric and NULL values to be passed in.
  255. if (is_numeric($value)) {
  256. $value = intval($value);
  257. }
  258. elseif ($value == 'NULL') {
  259. $value = NULL;
  260. }
  261. $form_options[$key] = $value;
  262. }
  263. }
  264. drush_include_engine('drupal', 'site_install');
  265. drush_core_site_install_version($profile, $form_options);
  266. }