/
deploy
executable file
·261 lines (210 loc) · 4.44 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
#!/usr/bin/env bash
VERSION="0.0.1"
CONFIG=./deploy.conf
LOG=/tmp/deploy.log
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
-V, --version output program version
-h, --help output help information
Commands:
setup run remote setup commands
deploy [rev] deploy to [rev] or HEAD
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>
list list previous deploy commits
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
}
#
# Output version.
#
version() {
echo $VERSION
}
#
# Run the given remote <cmd>.
#
run() {
local url="`config_get user`@`config_get host`"
local key=`config_get key`
if test -n "$key"; then
local shell="ssh -i $key $url"
else
local shell="ssh $url"
fi
echo $shell $@ >> $LOG
$shell $@
}
#
# 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 && $cmd"
test $? -eq 0 || abort "$hook hook failed"
else
log "hook $hook does not exist"
fi
}
#
# Run setup.
#
setup() {
local path=`config_get path`
local repo=`config_get repo`
local branch=`config_get branch`
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"
log setup complete
}
#
# Deploy <ref>.
#
deploy() {
local ref=$1
local path=`config_get path`
log deploying $ref
hook pre-deploy
# fetch source
run "cd $path/source && git fetch origin && git reset --hard $ref"
test $? -eq 0 || abort 'fetch failed'
# link current
run "ln -sf $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
# done
log deploy complete
}
#
# 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.
#
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"
}
# 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 ;;
run|exec) require_env; run "cd `config_get path` && $@"; 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 ;;
config) config $@; exit ;;
*) ENV=$arg ;;
esac
done
require_env
# deploy
deploy `config_get branch`