2 # Bootstrap a Drupal project
4 # Copyright (C) 2017 Antonio Ospite <ao2@ao2.it>
6 # This program is free software: you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation, either version 2 of the License, or
9 # (at your option) any later version.
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
16 # You should have received a copy of the GNU General Public License
17 # along with this program. If not, see <http://www.gnu.org/licenses/>.
23 usage: drin $(basename $0 .sh) [--devel|-h|--help]
25 Bootstrap a Drupal project, using settings from a 'bootstrap.conf' file.
28 --devel install drupal/devel and use a settings.local.php file
29 --overwrite-profile allow overwriting the current installation profile
30 -h, --help display this usage message and exit
46 OVERWRITE_PROFILE="true"
49 echo "Error: Unknown option '${1}'" 1>&2
61 declare -p ACCOUNT_NAME
62 declare -p ACCOUNT_PASS
63 declare -p ACCOUNT_MAIL
67 declare -p SITE_BASE_PATH
69 declare -p TRUSTED_HOSTS
71 declare -p WEB_SERVER_GROUP
73 [ "x$INSTALLATION_PROFILE" = "x" ] && { echo "INSTALLATION_PROFILE not specified, using the \"standard\" profile!"; INSTALLATION_PROFILE="standard"; }
75 if [ "x$MYSQL_ROOT_PASSWORD" = "x" ];
77 read -s -p "MySQL root password: " MYSQL_ROOT_PASSWORD
81 SITE_LOCAL_PATH="${PWD}/web"
83 command -v composer &> /dev/null || { echo "Aborting, 'composer' not available." 1>&2; exit 1; }
84 command -v git &> /dev/null || { echo "Aborting, 'git' not available." 1>&2; exit 1; }
86 [ -d "$SITE_LOCAL_PATH" ] || composer install
88 DRUSH="${PWD}/vendor/bin/drush"
89 DRUPAL_CONSOLE="${PWD}/vendor/bin/drupal"
91 [ -x "$DRUSH" ] || { echo "Aborting, '$DRUSH' not available." 1>&2; exit 1; }
92 [ -x "$DRUPAL_CONSOLE" ] || { echo "Aborting, '$DRUPAL_CONSOLE' not available." 1>&2; exit 1; }
94 # This becomes unnecessary if the installation profile gets pulled in by
95 # composer.json, like suggested in
96 # https://github.com/drupal-composer/drupal-project/issues/249
97 if ! echo $INSTALLATION_PROFILE | egrep -q "^(minimal|standard)$";
99 if [ -d $SITE_LOCAL_PATH/profiles/$INSTALLATION_PROFILE -a "$OVERWRITE_PROFILE" != "true" ];
101 echo "Installation profile '$INSTALLATION_PROFILE' already there." 1>&2
102 echo "Use --overwrite-profile to copy over it." 1>&2
105 cp -a $INSTALLATION_PROFILE $SITE_LOCAL_PATH/profiles
109 pushd "$SITE_LOCAL_PATH"
111 # Update the install_profile if it's already there
112 if grep -q "^\\\$settings\['install_profile'\] =" sites/default/settings.php;
114 chmod 755 sites/default
115 chmod 644 sites/default/settings.php
116 sed -i -e "s/^\(\$settings\['install_profile'\]\) = '[^']*';/\1 = '$INSTALLATION_PROFILE';/g" sites/default/settings.php
117 chmod 444 sites/default/settings.php
118 chmod 555 sites/default
121 $DRUSH --verbose --yes \
124 --db-su-pw="$MYSQL_ROOT_PASSWORD" \
125 --db-url="mysql://${DB_USER}:${DB_PASS}@localhost/${DB_NAME}" \
126 --site-name="$SITE_NAME" \
127 --site-mail="$SITE_MAIL" \
128 --account-name="$ACCOUNT_NAME" \
129 --account-pass="$ACCOUNT_PASS" \
130 --account-mail="$ACCOUNT_MAIL" \
131 "$INSTALLATION_PROFILE"
133 if $DRUSH pm-info --fields=status locale | grep -q enabled;
135 # This is necessary for multi-language sites, it fixes some issues like:
136 # "The Translation source field needs to be installed."
137 $DRUSH --yes entity-updates
139 # Update translations of contrib modules
140 $DRUSH --yes locale-update
143 # This fixes permissions when installing under $HOME/public_html/
144 chmod 775 sites/default/files
145 sudo chgrp -R "$WEB_SERVER_GROUP" sites/default/files
147 [ -d ../config/sync ] && sudo chgrp -R "$WEB_SERVER_GROUP" ../config/sync
150 sed -i "s@# RewriteBase /drupal\$@RewriteBase ${SITE_BASE_PATH}@" .htaccess
152 chmod 755 sites/default
153 chmod 644 sites/default/settings.php
155 # Add some basic settings to settings.php
156 if ! grep -q "^\\\$settings\['trusted_host_patterns'\] =" sites/default/settings.php;
158 echo "\$settings['trusted_host_patterns'] = [" >> sites/default/settings.php
159 for host in "${TRUSTED_HOSTS[@]}"
161 echo " '^${host}\$'," >> sites/default/settings.php
163 echo "];" >> sites/default/settings.php
166 if [ "$DEVEL_MODE" = "true" ];
168 # NOTE: don't run composer under web/ but in the project dir
169 composer --working-dir=../ require drupal/devel
170 $DRUSH --yes en devel
172 if [ ! -e sites/default/settings.local.php ];
174 cp sites/example.settings.local.php sites/default/settings.local.php
176 # Disable some overly permissive settings
177 sed -i -e "s/^\(\$settings\['rebuild_access'\]\).*$/\1 = FALSE;/g" sites/default/settings.local.php
178 sed -i -e "s/^\(\$settings\['skip_permissions_hardening'\]\).*$/\1 = FALSE;/g" sites/default/settings.local.php
180 chmod 444 sites/default/settings.local.php
183 if ! grep -q "^include \$app_root . '/' . \$site_path . '/settings.local.php';" sites/default/settings.php;
185 echo "include \$app_root . '/' . \$site_path . '/settings.local.php';" >> sites/default/settings.php
189 chmod 444 sites/default/settings.php
190 chmod 555 sites/default
192 # If using a git checkout of Drupal core, set up a diff alias.
194 # This is useful because the Automated Test infrastructure of drupal.org does
195 # not expect patches to be created from a split core directory.
198 git -C core/ config --local alias.core-diff "diff --src-prefix=a/core/ --dst-prefix=b/core/"
199 echo "Added a 'git core-diff' to the drupal/core repository clone."
200 echo "This command helps creating core patches ready for upstream."