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