9ea3c3261389ce78965c14c9da23a2629ae7f66b
[drupal-init-tools.git] / libexec / bootstrap.sh
1 #!/bin/bash
2 # Bootstrap a Drupal project
3 #
4 # Copyright (C) 2017  Antonio Ospite <ao2@ao2.it>
5 #
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.
10 #
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.
15 #
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/>.
18
19 set -e
20
21 usage() {
22   cat <<EOF
23 usage: drin $(basename "$0" .sh) [--devel|--overwrite-profile|-h|--help]
24
25 Bootstrap a Drupal project, using settings from a 'bootstrap.conf' file.
26
27 Options:
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
31
32 EOF
33 }
34
35 while [ $# -gt 0 ];
36 do
37   case "$1" in
38     -h|--help)
39       usage
40       exit 0
41       ;;
42     --devel)
43       DEVEL_MODE="true"
44       ;;
45     --overwrite-profile)
46       OVERWRITE_PROFILE="true"
47       ;;
48     -*)
49       echo "Error: Unknown option '${1}'" 1>&2
50       ;;
51   esac
52   shift
53 done
54
55 [ -f "bootstrap.conf" ] || { echo "Aborting, run this command from the Drupal project directory." 1>&2; exit 1; }
56
57 # shellcheck disable=SC1091
58 . bootstrap.conf
59
60 declare -p DB_NAME
61 declare -p DB_USER
62 declare -p DB_PASS
63
64 declare -p ACCOUNT_NAME
65 declare -p ACCOUNT_PASS
66 declare -p ACCOUNT_MAIL
67
68 declare -p SITE_NAME
69 declare -p SITE_MAIL
70 declare -p SITE_BASE_PATH
71
72 declare -p TRUSTED_HOSTS
73
74 declare -p WEB_SERVER_GROUP
75
76 [ "x$INSTALLATION_PROFILE" = "x" ] && { echo "INSTALLATION_PROFILE not specified, using the \"standard\" profile!"; INSTALLATION_PROFILE="standard"; }
77
78 if [ "$MYSQL_PASSWORDLESS_ACCESS" = true ];
79 then
80   MYSQL_SU_USER="${USER}"
81 else
82   # XXX Deprecate MYSQL_ROOT_PASSWORD, it will be removed eventually,
83   # but for now add some backwards compatibility mapping.
84   if [ "x$MYSQL_ROOT_PASSWORD" != "x" ];
85   then
86     echo "WARN: MYSQL_ROOT_PASSWORD is deprecated, use MYSQL_SU_USER and MYSQL_SU_PASSWORD" 1>&2
87     MYSQL_SU_USER="root"
88     MYSQL_SU_PASSWORD="$MYSQL_ROOT_PASSWORD"
89   fi
90
91   if [ "x$MYSQL_SU_USER" = "x" ];
92   then
93     echo "Aborting, for password regulated access specify MYSQL_SU_USER in bootstrap.conf" 1>&2
94     exit 1
95   fi
96
97   if [ "x$MYSQL_SU_PASSWORD" = "x" ];
98   then
99     read -r -s -p "MySQL password for \"${MYSQL_SU_USER}\": " MYSQL_SU_PASSWORD
100     echo
101   fi
102
103   DRUSH_DB_SU_CREDENTIALS=(--db-su-pw'='"${MYSQL_SU_PASSWORD}")
104 fi
105
106 DRUSH_DB_SU_CREDENTIALS+=(--db-su'='"${MYSQL_SU_USER}")
107
108 WEB_ROOT="${PWD}/web"
109
110 command -v composer &> /dev/null || { echo "Aborting, 'composer' not available." 1>&2; exit 1; }
111 command -v git &> /dev/null || { echo "Aborting, 'git' not available." 1>&2; exit 1; }
112
113 [ -d "$WEB_ROOT" ] || composer install
114
115 DRUSH="${PWD}/vendor/bin/drush"
116 DRUPAL_CONSOLE="${PWD}/vendor/bin/drupal"
117
118 [ -x "$DRUSH" ] || { echo "Aborting, '$DRUSH' not available." 1>&2; exit 1; }
119 [ -x "$DRUPAL_CONSOLE" ] || { echo "Aborting, '$DRUPAL_CONSOLE' not available." 1>&2; exit 1; }
120
121 # This becomes unnecessary if the installation profile gets pulled in by
122 # composer.json, like suggested in
123 # https://github.com/drupal-composer/drupal-project/issues/249
124 if ! echo "$INSTALLATION_PROFILE" | grep -q -E "^(minimal|standard)$";
125 then
126   if [ -d "${WEB_ROOT}/profiles/${INSTALLATION_PROFILE}" ] && [ "$OVERWRITE_PROFILE" != "true" ];
127   then
128     echo "Installation profile '$INSTALLATION_PROFILE' already there." 1>&2
129     echo "Use --overwrite-profile to copy over it." 1>&2
130     exit 1
131   else
132     cp -a "$INSTALLATION_PROFILE" "${WEB_ROOT}/profiles"
133   fi
134 fi
135
136 pushd "$WEB_ROOT"
137
138 # Update the install_profile if it's already there
139 if grep -q "^\\\$settings\['install_profile'\] =" sites/default/settings.php;
140 then
141   chmod 755 sites/default
142   chmod 644 sites/default/settings.php
143   sed -i -e "s/^\(\$settings\['install_profile'\]\) = '[^']*';/\1 = '$INSTALLATION_PROFILE';/g" sites/default/settings.php
144   chmod 444 sites/default/settings.php
145   chmod 555 sites/default
146 fi
147
148 $DRUSH --verbose --yes \
149   site-install \
150   "${DRUSH_DB_SU_CREDENTIALS[@]}" \
151   --db-url="mysql://${DB_USER}:${DB_PASS}@localhost/${DB_NAME}" \
152   --site-name="$SITE_NAME" \
153   --site-mail="$SITE_MAIL" \
154   --account-name="$ACCOUNT_NAME" \
155   --account-pass="$ACCOUNT_PASS" \
156   --account-mail="$ACCOUNT_MAIL" \
157   "$INSTALLATION_PROFILE"
158
159 if $DRUSH pm-list --type=module --field=name --status=enabled --pipe | grep -q "^locale$";
160 then
161   # This is necessary for multi-language sites, it fixes some issues like:
162   #   "The Translation source field needs to be installed."
163   $DRUSH --yes entity-updates
164
165   # Update translations of contrib modules
166   $DRUSH --yes locale-update
167 fi
168
169 # This fixes permissions when installing under $HOME/public_html/
170 chmod 775 sites/default/files
171 sudo chgrp -R "$WEB_SERVER_GROUP" sites/default/files
172
173 [ -d ../config/sync ] && sudo chgrp -R "$WEB_SERVER_GROUP" ../config/sync
174
175 # Enable clean URLs
176 sed -i "s@\(# \)\{0,1\}RewriteBase .*\$@RewriteBase ${SITE_BASE_PATH}@" .htaccess
177
178 chmod 755 sites/default
179 chmod 644 sites/default/settings.php
180
181 # Add some basic settings to settings.php
182 if ! grep -q "^\\\$settings\['trusted_host_patterns'\] =" sites/default/settings.php;
183 then
184   echo "\$settings['trusted_host_patterns'] = [" >> sites/default/settings.php
185   for host in "${TRUSTED_HOSTS[@]}"
186   do
187     echo "  '^${host}\$'," >> sites/default/settings.php
188   done
189   echo "];" >> sites/default/settings.php
190 fi
191
192 if [ "$DEVEL_MODE" = "true" ];
193 then
194   # NOTE: don't run composer under web/ but in the project dir
195   composer --working-dir=../ require drupal/devel
196   $DRUSH --yes en devel
197
198   if [ ! -e sites/default/settings.local.php ];
199   then
200     cp sites/example.settings.local.php sites/default/settings.local.php
201
202     # Disable some overly permissive settings
203     sed -i -e "s/^\(\$settings\['rebuild_access'\]\).*$/\1 = FALSE;/g" sites/default/settings.local.php
204     sed -i -e "s/^\(\$settings\['skip_permissions_hardening'\]\).*$/\1 = FALSE;/g" sites/default/settings.local.php
205
206     chmod 444 sites/default/settings.local.php
207   fi
208
209   if ! grep -q "^include \$app_root . '/' . \$site_path . '/settings.local.php';" sites/default/settings.php;
210   then
211     echo "include \$app_root . '/' . \$site_path . '/settings.local.php';" >> sites/default/settings.php
212   fi
213 fi
214
215 chmod 444 sites/default/settings.php
216 chmod 555 sites/default
217
218 # If using a git checkout of Drupal core, set up a diff alias.
219 #
220 # This is useful because the Automated Test infrastructure of drupal.org does
221 # not expect patches to be created from a split core directory.
222 if [ -d core/.git ];
223 then
224   git -C core/ config --local alias.core-diff "diff --src-prefix=a/core/ --dst-prefix=b/core/"
225   echo "Added a 'git core-diff' command to the drupal/core repository clone."
226   echo "This command helps creating core patches ready for upstream."
227 fi
228
229 popd