-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcamion
executable file
·410 lines (360 loc) · 8.76 KB
/
camion
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
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
#!/usr/bin/env bash
#
# camion.sh - Shell script to deploy web app to server.
# Released under the MIT License.
# https://github.com/webzhao/camion.sh
#
VERSION=1.0.0
CONFIG=./deploy.ini
TEMP_DIR=camion
REQUIRED_CONF="project user dir hosts files"
#
# text style control
#
_NORMAL=`tput sgr0`
UNDERLINE=`tput smul`
BOLD=`tput bold`
RED=`tput setaf 1`
GREEN=`tput setaf 2`
YELLOW=`tput setaf 3`
#
# variables, will be set after parsing config
#
env='' # server environment
sudo='' # switch to user
runOnServer='' # run commond on server
sync='' # rsync to server
bakDir='' # dir to backup history files
logFile='' # file to store deploy history
#
# entry point
#
main() {
local cmd=$1
local env=$2
# show usage if parameter isn't enough
if [ $# -lt 2 ] && [[ " init update " != *" $cmd "* ]]; then
usage
exit 1
fi
# init config for current project
if [ $cmd == "init" ]; then
init
exit $?
fi
# update camion.sh itself
if [ $cmd == "update" ]; then
update
exit $?
fi
# run deploy command
if [[ " deploy list revert " != *" $cmd "* ]]; then
error "Unknown command!"
usage
exit 1
fi
#parse config
parseConfig
# ensure all required configs are set
checkConfig $env
# set global variables
setGlobal $env
# run command
eval $cmd $env
}
#
# show error message
#
error() {
local msg=$1
echo "${RED}Error: $msg${_NORMAL}"
}
#
# show success message
#
success() {
local msg=$1
echo "${GREEN}✓ $msg${_NORMAL}"
}
#
# init project
#
init() {
if [ -f $CONFIG ]; then
error 'Config file exists!'
return 1
fi
set +e
read -d '' content <<-EOF
[common]
project = myapp
user = work
dir = /data/www/myapp
[test]
files = app src resource
hosts = 10.0.0.1
[production]
files = app pm2.json
hosts = 10.0.0.2 10.0.0.3
EOF
set -e
echo "$content" > $CONFIG
success "Done!${_NORMAL} Config file ${BOLD}${CONFIG}${_NORMAL} generated."
return 0
}
#
# parse config
#
parseConfig() {
local env=''
if [ ! -f "$CONFIG" ]; then
error "Can't find config file!"
exit
fi
while read line; do
# ignore empty line
[ -z "$line" ] && continue
# ignore inline starts with ';'
[[ $line == \;* ]] && continue
# check whether a new env start
e=$(echo $line | egrep "\[\w+\]" | sed 's/\W//g')
# start to set config for environment $e
if [ "$e" ]; then
env=$e
continue
fi
# split the line by '='
IFS='=' read -r key value <<< "$line"
# reset delimiter
IFS=$' \t\n'
# trim key and value
key=$(echo $key)
value=$(echo $value)
# set config variable
eval "config_${env}_${key}=\"$value\""
done < $CONFIG
}
#
# set global variables
#
setGlobal() {
env=$1
user=$(getConfig ${env} user)
project=$(getConfig ${env} project)
sudo="/usr/bin/sudo -u $user"
runOnServer="$sudo ssh"
sync="$sudo rsync -aqI"
bakDir="/home/$user/.camion/$project/$env"
logFile="$bakDir/history"
}
#
# check config
#
checkConfig() {
local env=$1
for key in $REQUIRED_CONF; do
value=$(getConfig ${env} ${key})
if [ -z "$value" ]; then
error "'$key' is not configured for $env environment!"
exit 1
fi
done
}
#
# get config by env and key
#
getConfig() {
local env=$1
local key=$2
local k1="config_${env}_${key}"
local k2="config_common_${key}"
local v1=${!k1}
local v2=${!k2}
echo ${v1:-$v2}
}
#
# list history
#
list() {
local logs=$($sudo tail -n 10 $logFile | sed '1!G;h;$!d')
if [ -z "$logs" ]; then
echo "No deploy history."
else
echo -e "Deploy history:\n"
echo "$logs" | cut -d $'\t' -f1,2,4
echo -e "\nSee ${UNDERLINE}$logFile${_NORMAL} for more.\n"
fi
}
#
# revert to previous version
#
revert() {
local count=$($sudo cat $logFile | wc -l)
if [ $count -lt 2 ]; then
error "No history to revert."
exit 1
fi
local previous=$($sudo tail -n 2 $logFile | head -n 1)
local current=$($sudo tail -n 1 $logFile)
echo "${GREEN}Current$_NORMAL $current" | cut -d $'\t' -f1,2,4
echo " ⬇ "
echo "${GREEN}Previous$_NORMAL $previous" | cut -d $'\t' -f1,2,4
echo -n "Are you sure to revert to previous version (y/n): "
read confirm
if [ $confirm != "y" ]; then
exit 1;
fi
bakFile=$(echo "$previous" | cut -d $'\t' -f3)
sudo tar -xf $bakFile
sudo chown $USER:$USER $TEMP_DIR
syncToServer
clean
$sudo sed -i '$ d' $logFile
success "Done!"
}
#
# upgrade camion.sh
#
update() {
sudo sh -c 'curl https://raw.githubusercontent.com/webzhao/camion.sh/master/camion > /usr/local/bin/camion'
success "Done!"
}
#
# deploy to server
#
deploy() {
local desc=""
# get description
while [ -z "$desc" ]; do
echo "Please input description for this deployment: "
read desc
done
# prepare
prepare=$(getConfig ${env} prepare)
if [ -n "$prepare" ]; then
echo "Running ${UNDERLINE}prepare${_NORMAL} hook: ${UNDERLINE}$prepare${_NORMAL}"
eval "$prepare"
success "Prepare hook finished."
fi
# make temp dir
[ ! -d $TEMP_DIR ] && mkdir $TEMP_DIR
# copy files to temp dir
files=$(getConfig ${env} files)
echo "Copying files to temporary directory..."
for file in $files; do
cp -r --parents $file $TEMP_DIR/
done
success "All files copied."
# run before srcipts
before=$(getConfig ${env} before)
if [ -n "$before" ]; then
echo "Running ${UNDERLINE}before-deploy${_NORMAL} hook: ${UNDERLINE}$before${_NORMAL}"
eval "$before"
success "Before-deploy hook finished."
fi
# deploy to server
syncToServer
# add deploy history
now=$(date "+%Y年%m月%d日 %H:%M:%S")
bakFile="$bakDir/$(date +%Y%m%d%H%M%S).tar.gz"
[ ! -d $bakDir ] && $sudo mkdir -p $bakDir
$sudo tar -czf $bakFile $TEMP_DIR/*
echo -e "$now\t$USER\t$bakFile\t$desc" | $sudo tee -a $logFile > /dev/null
# do the clean work
clean
success "Done!"
}
#
# update git refs and get revision
#
updateGitRefs() {
git remote update
local remote=$(git remote)
local branch=$(git rev-parse --abbrev-ref HEAD)
localRev=$(git rev-parse $branch)
remoteRev=$(git rev-parse $remote/$branch)
baseRev=$(git merge-base $branch $remote/$branch)
}
#
# assert git updated
#
assertGitUpdated() {
updateGitRefs
local outOfDate=false
if [[ $localRev != $remoteRev ]] && [[ $localRev == $baseRev ]]; then
outOfDate=true
elif [[ $remoteRev != $baseRev ]]; then
outOfDate=true
fi
if $outOfDate; then
error "Git: local out of date, please update"
exit 1
fi
}
#
# assert git clean
#
assertGitClean() {
updateGitRefs
if [[ -n $(git status -s) ]]; then
error "Git: please commit local changes and push to remote"
exit 1
fi
if [ $localRev != $baseRev ]; then
error "Git: please push local commits to remote"
exit 1
fi
}
#
# sync files to server
#
syncToServer() {
dir=$(getConfig ${env} dir)
hosts=$(getConfig ${env} hosts)
cd $TEMP_DIR
for host in $hosts; do
# check dir
$runOnServer $host "[ ! -d $dir ] && mkdir -p $dir" || true
# copy file to server
echo "Syncing files to server ${UNDERLINE}$host${_NORMAL}"
$sync --exclude=.* --exclude=.*/ --exclude=*.map * $host:$dir
success "Files synced."
# run after script on server
after=$(getConfig ${env} after)
if [ -n "$after" ]; then
echo "Running ${UNDERLINE}after-deploy${_NORMAL} hook on server: ${UNDERLINE}$after${_NORMAL}"
$runOnServer $host "cd $dir; $after"
success "After-deploy hook finished."
fi
done
cd ..
}
#
# do clean work after deploy
#
clean() {
rm -rf $TEMP_DIR
clean=$(getConfig ${env} clean)
if [ -n "$clean" ]; then
echo "Running ${UNDERLINE}clean${_NORMAL} hook: ${UNDERLINE}$clean${_NORMAL}"
eval "$before"
success "Clean hook finished."
fi
}
#
# show usage
#
usage() {
cat <<-EOF
${BOLD}Usage: camion <command> [env]${_NORMAL}
Commands:
${BOLD}init${_NORMAL} generate sample config file in current directory
${BOLD}deploy${_NORMAL} deploy current version to server
${BOLD}revert${_NORMAL} revert to previous deployed version
${BOLD}list${_NORMAL} show deploy history
${BOLD}update${_NORMAL} update camion.sh
Please refer ${UNDERLINE}https://github.com/webzhao/camion.sh$_NORMAL for more details.
EOF
}
set -e
main $*