Check that the "bootstrap" and "clean" subcommands are run in the project dir
[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|-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 [ "x$MYSQL_ROOT_PASSWORD" = "x" ];
79 then
80   read -r -s -p "MySQL root password: " MYSQL_ROOT_PASSWORD
81   echo
82 fi
83
84 WEB_ROOT="${PWD}/web"
85
86 command -v composer &> /dev/null || { echo "Aborting, 'composer' not available." 1>&2; exit 1; }
87 command -v git &> /dev/null || { echo "Aborting, 'git' not available." 1>&2; exit 1; }
88
89 [ -d "$WEB_ROOT" ] || composer install
90
91 DRUSH="${PWD}/vendor/bin/drush"
92 DRUPAL_CONSOLE="${PWD}/vendor/bin/drupal"
93
94 [ -x "$DRUSH" ] || { echo "Aborting, '$DRUSH' not available." 1>&2; exit 1; }
95 [ -x "$DRUPAL_CONSOLE" ] || { echo "Aborting, '$DRUPAL_CONSOLE' not available." 1>&2; exit 1; }
96
97 # This becomes unnecessary if the installation profile gets pulled in by
98 # composer.json, like suggested in
99 # https://github.com/drupal-composer/drupal-project/issues/249
100 if ! echo "$INSTALLATION_PROFILE" | grep -q -E "^(minimal|standard)$";
101 then
102   if [ -d "${WEB_ROOT}/profiles/${INSTALLATION_PROFILE}" ] && [ "$OVERWRITE_PROFILE" != "true" ];
103   then
104     echo "Installation profile '$INSTALLATION_PROFILE' already there." 1>&2
105     echo "Use --overwrite-profile to copy over it." 1>&2
106     exit 1
107   else
108     cp -a "$INSTALLATION_PROFILE" "${WEB_ROOT}/profiles"
109   fi
110 fi
111
112 pushd "$WEB_ROOT"
113
114 # Update the install_profile if it's already there
115 if grep -q "^\\\$settings\['install_profile'\] =" sites/default/settings.php;
116 then
117   chmod 755 sites/default
118   chmod 644 sites/default/settings.php
119   sed -i -e "s/^\(\$settings\['install_profile'\]\) = '[^']*';/\1 = '$INSTALLATION_PROFILE';/g" sites/default/settings.php
120   chmod 444 sites/default/settings.php
121   chmod 555 sites/default
122 fi
123
124 $DRUSH --verbose --yes \
125   site-install \
126   --db-su=root \
127   --db-su-pw="$MYSQL_ROOT_PASSWORD" \
128   --db-url="mysql://${DB_USER}:${DB_PASS}@localhost/${DB_NAME}" \
129   --site-name="$SITE_NAME" \
130   --site-mail="$SITE_MAIL" \
131   --account-name="$ACCOUNT_NAME" \
132   --account-pass="$ACCOUNT_PASS" \
133   --account-mail="$ACCOUNT_MAIL" \
134   "$INSTALLATION_PROFILE"
135
136 if $DRUSH pm-info --fields=status locale | grep -q enabled;
137 then
138   # This is necessary for multi-language sites, it fixes some issues like:
139   #   "The Translation source field needs to be installed."
140   $DRUSH --yes entity-updates
141
142   # Update translations of contrib modules
143   $DRUSH --yes locale-update
144 fi
145
146 # This fixes permissions when installing under $HOME/public_html/
147 chmod 775 sites/default/files
148 sudo chgrp -R "$WEB_SERVER_GROUP" sites/default/files
149
150 [ -d ../config/sync ] && sudo chgrp -R "$WEB_SERVER_GROUP" ../config/sync
151
152 # Enables clean URLs
153 sed -i "s@# RewriteBase /drupal\$@RewriteBase ${SITE_BASE_PATH}@" .htaccess
154
155 chmod 755 sites/default
156 chmod 644 sites/default/settings.php
157
158 # Add some basic settings to settings.php
159 if ! grep -q "^\\\$settings\['trusted_host_patterns'\] =" sites/default/settings.php;
160 then
161   echo "\$settings['trusted_host_patterns'] = [" >> sites/default/settings.php
162   for host in "${TRUSTED_HOSTS[@]}"
163   do
164     echo "  '^${host}\$'," >> sites/default/settings.php
165   done
166   echo "];" >> sites/default/settings.php
167 fi
168
169 if [ "$DEVEL_MODE" = "true" ];
170 then
171   # NOTE: don't run composer under web/ but in the project dir
172   composer --working-dir=../ require drupal/devel
173   $DRUSH --yes en devel
174
175   if [ ! -e sites/default/settings.local.php ];
176   then
177     cp sites/example.settings.local.php sites/default/settings.local.php
178
179     # Disable some overly permissive settings
180     sed -i -e "s/^\(\$settings\['rebuild_access'\]\).*$/\1 = FALSE;/g" sites/default/settings.local.php
181     sed -i -e "s/^\(\$settings\['skip_permissions_hardening'\]\).*$/\1 = FALSE;/g" sites/default/settings.local.php
182
183     chmod 444 sites/default/settings.local.php
184   fi
185
186   if ! grep -q "^include \$app_root . '/' . \$site_path . '/settings.local.php';" sites/default/settings.php;
187   then
188     echo "include \$app_root . '/' . \$site_path . '/settings.local.php';" >> sites/default/settings.php
189   fi
190 fi
191
192 chmod 444 sites/default/settings.php
193 chmod 555 sites/default
194
195 # If using a git checkout of Drupal core, set up a diff alias.
196 #
197 # This is useful because the Automated Test infrastructure of drupal.org does
198 # not expect patches to be created from a split core directory.
199 if [ -d core/.git ];
200 then
201   git -C core/ config --local alias.core-diff "diff --src-prefix=a/core/ --dst-prefix=b/core/"
202   echo "Added a 'git core-diff' to the drupal/core repository clone."
203   echo "This command helps creating core patches ready for upstream."
204 fi
205
206 popd