git-hacks

git-hacks Git Source Tree

Root/gitdeploy

1# This script is expected to be sourced from git hooks.
2#
3# USAGE:
4# source gitdeploy WEBDIR [FRONTEND]
5#
6# where WEBDIR is the (usually absolute) directory where the public
7# website is contained. FRONTEND specifies the web framework to know
8# how the data must be handled. If not specified, FRONTEND falls
9# back to "dummy" (that is no-op).
10#
11# EXAMPLE:
12# In the remote repository, create two hooks:
13#
14# #! /bin/sh
15# # pre-receive hook
16# source /path/to/gitdeploy /var/vhosts/mysite silverstripe
17# public2remote
18#
19# #! /bin/sh
20# # post-receive hook
21# source /path/to/gitdeploy /var/vhosts/mysite silverstripe
22# remote2public
23
24
25error() {
26 echo "** $@" 1>&2;
27}
28
29
30[ -z "$1" ] && error 'gitdeploy requires a WEBDIR as first argument' && exit 1
31webdir="$1"
32frontend="$2"
33[ -z "$frontend" ] && frontend=dummy
34
35
36# Ovveride git to explicitely set its working directory to $webdir.
37git() {
38 GIT_WORK_TREE="$webdir" command git "$@"
39}
40
41
42remote2public() {
43 echo "Syncing public with remote"
44 git checkout -qf
45
46 if [ ! -r "$webdir/bootstrap.cfg" ]; then return 0; fi
47
48 # Default bootstrap hook: $webdir/bootstrap can override it
49 bootstrap() {
50 }
51
52 source $webdir/bootstrap.cfg
53
54 if [ -n "${writables[*]}" ]; then
55chmod 0777 ${writables[*]/#/$webdir/}
56 fi
57
58 bootstrap
59}
60
61# Get the value part of a key => value assignment from a PHP source.
62# The approach using regex is quite flaky but way better than loading
63# a whole PHP framework to get a freaking value.
64lookup() {
65 sed -n "s/\s*['\"]$1['\"]\s*=>\s*['\"]\(.*\)['\"].*/\1/p" $2
66}
67
68refresh_dummy() {
69 return 0
70}
71
72refresh_silverstripe() {
73 local path="$1"
74 local cfg database server username password
75
76 for cfg in "$path"/*/_config.php; do
77database="$(lookup database $cfg)"
78[ -n "$database" ] && break
79 done
80
81 if [ -z "$database" ]; then return 1; fi
82
83 server=$(lookup server "$cfg")
84 [ -z "$server" ] && server=localhost
85 username=$(lookup username "$cfg")
86 password=$(lookup password "$cfg")
87
88 mysqldump -h"$server" -u"$username" -p"$password" \
89--skip-lock-tables --skip-dump-date \
90$database > $(dirname "$path")/"$database".sql
91
92 return 0
93}
94
95refresh_public() {
96 local dir subdomain
97 echo Refreshing MySQL dumps in public:
98
99 for dir in "$webdir"/*; do
100if [ ! -d $dir ]; then continue; fi
101
102subdomain="$(basename $dir)"
103echo -ne "\t$subdomain... "
104
105if refresh_$frontend "$dir" "$webdir/$subdomain.data"; then
106 echo done
107else
108 echo not a subdomain
109fi
110 done
111}
112
113public2remote() {
114 refresh_public
115
116 echo -n "Syncing remote with public... "
117
118 git add -A
119 if git diff --quiet --cached; then
120echo not needed
121return 0
122 else
123git commit -qm 'Automatically updated by gitdeploy'
124echo done
125 fi
126}
127
128
129
130[ -z "$1" ] && error No virtual host directory given
131base=$1
132export GIT_WORK_TREE="$base"
133
134# Source the configuration file, if found in the virtual host
135# directory. Define optional stuff, such as the "writables" array.
136[ -r $base/post-receive.silverstripe.cfg ] && source $base/bootstrap.cfg
137
138if [ -n "${writables[*]}" ]; then
139 echo Changing permissions of the writable directories
140 chmod 0777 ${writables[*]/#/$base/}
141fi
142
143echo Copying the current repository in the virtual host directory
144git checkout -qf
145
146echo Backing up MySQL data for every subdomain:
147for dir in $base/*; do
148 if [ ! -d $dir ]; then
149continue
150 fi
151
152 echo -ne "\t$(basename $dir)... "
153 database=
154
155 for file in $dir/*/_config.php; do
156database=$(lookup database $file)
157[ -n "$database" ] && break
158 done
159
160 if [ -z $database ]; then
161echo not a subdomain
162continue
163 fi
164
165 server=$(lookup server $file)
166 [ -z "$server" ] && server=localhost
167 username=$(lookup username $file)
168 password=$(lookup password $file)
169
170 mysqldump -h $server -u"$username" -p"$password" --skip-lock-tables --skip-dump-date $database > $dir.sql
171
172 echo $database
173done
174
175echo Committing changes on the remote side
176git add $base/*.sql
177git commit -qm 'Automatic backup'
178
179remote2public
180
181# Source the configuration file, if found in the virtual host
182# directory. Define optional stuff, such as the "writables" array.
183[ -r $base/post-receive.silverstripe.cfg ] && source $base/bootstrap.cfg
184
185if [ -n "${writables[*]}" ]; then
186 echo Changing permissions of the writable directories
187 chmod 0777 ${writables[*]/#/$base/}
188fi
189

Archive Download this file

Branches