forked from nko4/macambiras
-
Notifications
You must be signed in to change notification settings - Fork 1
/
deploy
executable file
·371 lines (305 loc) · 7.11 KB
/
deploy
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
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
#!/usr/bin/env bash
#
# deploy(1) - Minimalistic shell script to deploy Git repositories.
# Released under the MIT License.
#
# https://github.com/visionmedia/deploy
#
VERSION="0.6.0"
CONFIG=./deploy.conf
LOG=/tmp/deploy.log
TEST=1
REF=
ENV=
#
# Output usage information.
#
usage() {
cat <<-EOF
Usage: deploy [options] <env> [command]
Options:
-C, --chdir <path> change the working directory to <path>
-c, --config <path> set config path. defaults to ./deploy.conf
-T, --no-tests ignore test hook
-V, --version output program version
-h, --help output help information
Commands:
setup run remote setup commands
update update deploy to the latest release
revert [n] revert to [n]th last deployment or 1
config [key] output config file or [key]
curr[ent] output current release commit
prev[ious] output previous release commit
exec|run <cmd> execute the given <cmd>
console open an ssh session to the host
list list previous deploy commits
[ref] deploy to [ref], the 'ref' setting, or latest tag
EOF
}
#
# Abort with <msg>
#
abort() {
echo
echo " $@" 1>&2
echo
exit 1
}
#
# Log <msg>.
#
log() {
echo " ○ $@"
}
#
# Set configuration file <path>.
#
set_config_path() {
test -f $1 || abort invalid --config path
CONFIG=$1
}
#
# Check if config <section> exists.
#
config_section() {
grep "^\[$1" $CONFIG &> /dev/null
}
#
# Get config value by <key>.
#
config_get() {
local key=$1
test -n "$key" \
&& grep "^\[$ENV" -A 20 $CONFIG \
| grep "^$key" \
| head -n 1 \
| cut -d ' ' -f 2-999
}
#
# Output version.
#
version() {
echo $VERSION
}
#
# Return the ssh command to run.
#
ssh_command() {
local url="`config_get user`@`config_get host`"
local key="`config_get key`"
local forward_agent="`config_get forward-agent`"
local port="`config_get port`"
test -n "$forward_agent" && local agent="-A"
test -n "$key" && local identity="-i $key"
test -n "$port" && local port="-p $port"
echo "ssh $agent $port $identity $url"
}
#
# Run the given remote <cmd>.
#
run() {
local shell="`ssh_command`"
echo $shell "\"$@\"" >> $LOG
$shell $@
}
#
# Launch an interactive ssh console session.
#
console() {
local path="`config_get path`/current"
local shell="`ssh_command`"
echo $shell
exec $shell -t "cd $path; \$SHELL --login"
}
#
# Output config or [key].
#
config() {
if test $# -eq 0; then
cat $CONFIG
else
config_get $1
fi
}
#
# Execute hook <name> relative to the path configured.
#
hook() {
test -n "$1" || abort hook name required
local hook=$1
local path=`config_get path`
local cmd=`config_get $hook`
if test -n "$cmd"; then
log "executing $hook \`$cmd\`"
run "cd $path/current; \
SHARED=\"$path/shared\" \
$cmd 2>&1 | tee -a $LOG; \
exit \${PIPESTATUS[0]}"
test $? -eq 0
else
log hook $hook
fi
}
#
# Run setup.
#
setup() {
local path=`config_get path`
local repo=`config_get repo`
run "mkdir -p $path/{shared/{logs,pids},source}"
test $? -eq 0 || abort setup paths failed
log running setup
log cloning $repo
run "git clone $repo $path/source"
test $? -eq 0 || abort failed to clone
run "ln -sfn $path/source $path/current"
test $? -eq 0 || abort symlink failed
log setup complete
}
#
# Deploy [ref].
#
deploy() {
local ref=$1
local path=`config_get path`
log deploying
hook pre-deploy || abort pre-deploy hook failed
# fetch source
log fetching updates
run "cd $path/source && git fetch --all"
test $? -eq 0 || abort fetch failed
# latest tag
if test -z "$ref"; then
log fetching latest tag
ref=`run "cd $path/source && git for-each-ref refs/tags \
--sort=-authordate \
--format='%(refname)' \
--count=1 | cut -d '/' -f 3"`
test $? -eq 0 || abort failed to determine latest tag
fi
# reset HEAD
log resetting HEAD to $ref
run "cd $path/source && git reset --hard $ref"
test $? -eq 0 || abort git reset failed
# link current
run "ln -sfn $path/source $path/current"
test $? -eq 0 || abort symlink failed
# deploy log
run "cd $path/source && \
echo \`git rev-parse --short HEAD\` \
>> $path/.deploys"
test $? -eq 0 || abort deploy log append failed
hook post-deploy || abort post-deploy hook failed
if test $TEST -eq 1; then
hook test
if test $? -ne 0; then
log tests failed, reverting deploy
quickly_revert_to 1 && log "revert complete" && exit
fi
else
log ignoring tests
fi
# done
log successfully deployed $ref
}
#
# Get current commit.
#
current_commit() {
local path=`config_get path`
run "cd $path/source && \
git rev-parse --short HEAD"
}
#
# Get <n>th deploy commit.
#
nth_deploy_commit() {
local n=$1
local path=`config_get path`
run "cat $path/.deploys | tail -n $n | head -n 1 | cut -d ' ' -f 1"
}
#
# List deploys.
#
list_deploys() {
local path=`config_get path`
run "cat $path/.deploys"
}
#
# Revert to the <n>th last deployment, ignoring tests.
#
quickly_revert_to() {
local n=$1
log "quickly reverting $n deploy(s)"
local commit=`nth_deploy_commit $((n + 1))`
TEST=0 deploy "$commit"
}
#
# Revert to the <n>th last deployment.
#
revert_to() {
local n=$1
log "reverting $n deploy(s)"
local commit=`nth_deploy_commit $((n + 1))`
deploy "$commit"
}
#
# Require environment arg.
#
require_env() {
config_section $ENV || abort "[$ENV] config section not defined"
test -z "$ENV" && abort "<env> required"
}
#
# Ensure all changes are committed and pushed before deploying.
#
check_for_local_changes() {
git --no-pager diff --exit-code --quiet || abort "commit or stash your changes before deploying"
git --no-pager diff --exit-code --quiet --cached || abort "commit your staged changes before deploying"
[ -z "`git rev-list @{upstream}.. -n 1`" ] || abort "push your changes before deploying"
}
#
# Update deploy.
#
update() {
log "updating deploy(1)"
rm -fr /tmp/deploy
git clone git://github.com/visionmedia/deploy.git \
--depth 0 \
/tmp/deploy \
&> /tmp/deploy.log \
&& cd /tmp/deploy \
&& make install \
&& log "updated $VERSION -> `./bin/deploy --version`"
}
# parse argv
while test $# -ne 0; do
arg=$1; shift
case $arg in
-h|--help) usage; exit ;;
-V|--version) version; exit ;;
-c|--config) set_config_path $1; shift ;;
-C|--chdir) log cd $1; cd $1; shift ;;
-T|--no-tests) TEST=0 ;;
run|exec) require_env; run "cd `config_get path`/current && $@"; exit ;;
console) require_env; console; exit ;;
curr|current) require_env; current_commit; exit ;;
prev|previous) require_env; nth_deploy_commit 2; exit ;;
revert) require_env; revert_to ${1-1}; exit ;;
setup) require_env; setup $@; exit ;;
list) require_env; list_deploys; exit ;;
update) update; exit ;;
config) config $@; exit ;;
*)
if test -z "$ENV"; then
ENV=$arg;
else
REF="$REF $arg";
fi
;;
esac
done
require_env
check_for_local_changes
# deploy
deploy "${REF:-`config_get ref`}"