-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdrupal
320 lines (276 loc) · 10.4 KB
/
drupal
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
# Drupal plug-in for Joey Hess' excellent mr tool.
#
# This allows using git repositories checked out from git.drupal.org as a
# pseudo version control system with mr.
#
# Author: Wim Leers <[email protected]>
# INSTRUCTIONS
#
# To make mr use this file, add a line like this inside the [DEFAULT] section
# of your .mrconfig:
#
# include = cat /usr/share/mr/drupal
#
# and then inside each [repo] section of your .mrconfig for which you want to
# use the Drupal pseudo VCS:
#
# project = <project name>
# version = <tag or branch>
#
# A concrete example would be:
#
# project = cdn
# version = 7.x-2.5
#
# The biggest benefit is that this enables you to use a declarative syntax:
# say which project and which version (branch name or tag), and this will do
# all the work for you: just call `mr update`, and new Drupal projects will
# be checked out, existing ones will be updated to match the new version.
#
# It's also recommended to use the included "drupal_set_permissions" function
# to automatically set the right permissions on your Drupal site.
#
# Note that by including this, a default "checkout" action is defined for each
# repository. This is what allows us to use a declarative syntax. However, you
# can still mix in any other repository by setting a checkout command.
# EXAMPLE .mrconfig FILE
#
# # Drupal itself, along with automatic permissions configuration.
# [www]
# project = drupal
# version = 7.19
# fixups = drupal_rm_unnecessary_txt
# fixups_append = drupal_symlink_druplicon_favicon
#
# # Same as [www], but with a different key. Dummy target to run fixups after
# # EVERYTHING else: fixups that must be applied to ALL modules/themes.
# [www/../www]
# # Set the proper file system permissions on all Drupal files. The owner and
# # group are calculated by the user-defined functions above.
# # See http://drupal.org/node/244924.
# fixups = drupal_set_permissions `whoami` 'www-data'
# checkout = :
# status = :
# update = :
# order = 100
#
# # A branch of a Drupal module.
# [www/sites/all/modules/cdn]
# project = cdn
# version = 7.x-2.x
#
# # A tag of a Drupal module.
# [www/sites/all/modules/comment_notify]
# project = comment_notify
# version = 7.x-1.1
#
# # Custom themes live in a non-git.drupal.org repository. `mr` is smart enough
# # to automatically know this is a git repository, and knows how to update it.
# [www/sites/wimleers.com/themes]
# checkout = git clone git://github.com/wimleers/wimleers.com-themes.git themes
# REFERENCES
#
# - drupal_set_permissions() is heavily based on the fix-permissions.sh script
# at http://drupal.org/node/244924, and has contributed upstream to it.
# - on() is included in mr's complex .mrconfig example, see
# https://github.com/joeyh/mr/blob/master/mrconfig.complex
# TODO:
#
# - support for shallow cloning, but only for tags
#
# CORE of Drupal plug-in for mr.
#
lib =
gdo_checkout() {
# Look up variables.
section=$(echo "$MR_REPO" | sed -e "s|${MR_CONFIG%.*}||")
project=`mr config "$section" project`
version=`mr config "$section" version`
target=${MR_REPO##*/}
# Perform checkout.
echo " |- Checking out version $version."
git clone --recursive http://git.drupal.org/project/$project.git $target
cd $target
git checkout -q $version
}
gdo_update() {
# Look up variables.
section=$(echo "$MR_REPO" | sed -e "s|${MR_CONFIG%.*}||")
project=`mr config "$section" project`
version=`mr config "$section" version`
# Perform update.
current_branch=`git rev-parse --abbrev-ref HEAD`
current_tag=`git describe --tags --always HEAD`
if [ $current_tag = $version ]; then
echo " |- Already at tag $version."
elif [ $current_branch = $version ]; then
echo " |- Already on branch $version, but pulling in updates."
git pull -q
else
# Determining old version: was it a tag or a branch?
old_version=""
branch_to_pull=""
if [ $current_branch = "HEAD" ]; then
old_version=$current_tag
branch_to_pull=${current_tag%.*}.x # Strip all characters after the last period, replace with "x".
else
old_version=$current_branch
branch_to_pull=$current_branch
fi
echo " |- Updating '$project' to version $version (from $old_version)..."
git checkout -- .
git checkout -q $branch_to_pull
git pull -q
git checkout -q $version
echo "Done."
fi
}
# Create a "gdo" (git.drupal.org) pseudo-VCS, which allows us to use a custom,
# declarative per-Drupal project syntax.
git_test = perl: -e "$ENV{MR_REPO}/.git" && `git config --file "$ENV{MR_REPO}/.git/config" --get remote.origin.url` !~ /git.drupal.org/
gdo_test = perl: -e "$ENV{MR_REPO}/.git" && `git config --file "$ENV{MR_REPO}/.git/config" --get remote.origin.url` =~ /git.drupal.org/
gdo_update = gdo_update
gdo_status = git status -s "$@" || true
gdo_commit = echo "Committing to git.drupal.org from within 'mr' unsupported; go into the project directory and use 'git' directly."
gdo_record = git commit -a "$@"
gdo_push = echo "Pushing to git.drupal.org from within 'mr' unsupported; go into the project directory and use 'git' directly."
gdo_diff = git diff "$@"
gdo_log = git log "$@"
gdo_grep = git grep "$@"
gdo_register =
url="`LC_ALL=C git config --get remote.origin.url`" || true
if [ -z "$url" ]; then
error "Cannot determine git URL."
fi
project=`echo $url | cut -d/ -f5 | cut -d. -f1`
current_branch=`git rev-parse --abbrev-ref HEAD`
current_tag=`git describe --tags --always HEAD`
version=""
if [ $current_branch = "HEAD" ]; then
version=$current_tag
else
version=$current_branch
fi
echo "Registering Drupal project '$project', version $version in $MR_CONFIG"
mr -c "$MR_CONFIG" config "`pwd`" project=$project
mr -c "$MR_CONFIG" config "`pwd`" version=$version
# Default to the "gdo" pseudo-VCS, which allows us to use declarative syntax
# to automatically download (git checkout) and update Drupal projects.
checkout = gdo_checkout
#
# EXTRAS of Drupal plug-in for mr.
#
lib =
hostname="$(hostname)"
whoami="$(whoami)"
# Checks whether the given host basename is one of the listed values. A
# value can also have a username in it, e.g. "user@host".
# This can be used to apply http://en.wikipedia.org/wiki/Development,_testing,_acceptance_and_production
on() {
for host in $@; do
if [ "${host%@*}" != "${host#*@}" ]; then
if [ "$whoami" != "${host%@*}" ]; then
continue
fi
host="${host#*@}"
fi
if [ "$hostname" = "$host" ]; then
return 0
fi
done
return 1
}
# Create (or update) a symlink.
create_symlink() {
printf " |- Symlinking $2 to $1..."
rm -f $2
ln -s $1 $2
printf " symlinked.\n"
}
# Apply a patch with `git apply`, and check whether it's already applied.
# Patch file must live in the same directory as the `.mrconfig` file (you
# can of course `wget` the patch there too).
#
# Example:
# [www]
# project = drupal
# version = 7.19
# fixups = git_apply_patch only-js-settings-dont-add-jquery_15.patch
git_apply_patch() {
printf " |- Applying patch '$1'..."
MR_CONFIG_PATH=${MR_CONFIG%/*}
cd $MR_REPO
cp $MR_CONFIG_PATH/$1 .
if ! git apply --check $1 >/dev/null 2>&1
then
if git apply -R --check $1 >/dev/null 2>&1
then
printf " already applied.\n"
rm $1
else
printf " FAILED. Patch not already applied, and failed.\n"
rm $1
return 1
fi
else
git apply $1
rm $1
printf " applied.\n"
fi
}
# As per http://drupal.org/node/244924. `pwd` must already be a Drupal
# root. Ensure the fix-permissions.sh script from that page is available.
drupal_set_permissions() {
drupal_path=${MR_REPO%/}
drupal_user=$1
httpd_group="${2:-www-data}"
print_help() {
printf "\n\tThis script is used to fix permissions of a Drupal installation\n"
printf "\tyou need to provide the following arguments:\n\n"
printf "\t1) Username of the user that you want to give files/directories ownership.\n"
printf "\t2) HTTPD group name (defaults to www-data for Apache).\n\n"
printf "\tNote: it is applied to the current MR repository, so that must be the root of a Drupal site.\n\n"
exit 0
}
if [ -z "${drupal_path}" ] || [ ! -d "${drupal_path}/sites" ] || [ ! -f "${drupal_path}/core/modules/system/system.module" ] && [ ! -f "${drupal_path}/modules/system/system.module" ]; then
printf "\tERROR: Please provide a valid Drupal path.\n"
print_help
exit 1
fi
if [ -z "${drupal_user}" ] || [ $(id -un ${drupal_user} 2> /dev/null) != "${drupal_user}" ]; then
printf "\tERROR: Please provide a valid user.\n"
print_help
exit 1
fi
cd $drupal_path
printf " |+ Securing Drupal files by setting optimal permissions.\n"
printf " \\ \n"
printf " |- Drupal root: \"${drupal_path}\".\n"
printf " |- Owner user: \"${drupal_user}\".\n"
printf " |- Owner group: \"${httpd_group}\".\n"
printf " |- Changing ownership of all contents of Drupal root to aforementioned user & group.\n"
chown -R ${drupal_user}:${httpd_group} .
printf " |- Changing permissions of all directories inside Drupal root to \"rwxr-x---\"...\n"
find . -type d -exec chmod u=rwx,g=rx,o= '{}' \;
printf " |- Changing permissions of all files inside Drupal root to \"rw-r-----\"...\n"
find . -type f -exec chmod u=rw,g=r,o= '{}' \;
printf " |- Changing permissions of \"files\" directories inside \"sites\" directory to \"rwxrwx---\"...\n"
cd ${drupal_path}/sites
find . -type d -name files -exec chmod ug=rwx,o= '{}' \;
printf " |- Changing permissions of all files inside all \"files\" directories to \"rw-rw-r--\"...\n"
printf " |- Changing permissions of all directories inside all \"files\" directories to \"rwxrwx--x\"...\n"
for x in ./*/files; do
find ${x} -type d -exec chmod ug=rwx,o=x '{}' \;
find ${x} -type f -exec chmod ug=rw,o=r '{}' \;
done
printf " /\n"
}
# Removes all .txt files from the Drupal root, except for robots.txt.
drupal_rm_unnecessary_txt() {
ls $MR_REPO/*.txt | grep -v robots.txt | xargs -I{} rm {}
}
# Symlinks Drupal's default Druplicon favicon from misc/favicon.ico to the
# Drupal root because most browsers still request the favicon from there.
drupal_symlink_druplicon_favicon() {
create_symlink misc/favicon.ico $MR_REPO/favicon.ico
}