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