/
functions
428 lines (344 loc) · 10.7 KB
/
functions
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
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
#!/bin/sh
set +o noclobber # Why yes, I did mean to clobber that file.
hook()
{
trace_filter "deploy" || set -o xtrace
local _hook
_hook="$1"
(( _hooks_flag )) || return 0
# System; Shared
if path exists "${shared_path}/deploy"
then
if path exists "${shared_path}/deploy" &&
file is executable "${shared_path}/deploy/$_hook"
then
log step "${_hook} ( ${shared_path##${project_path}/}/deploy/ )"
hook_source "${shared_path}/deploy/$_hook" &> >(tee -a "${deploy_log_path}/${_hook}.log")
log step success
fi
fi
# System; Shared, Environment
if variable is nonempty environment &&
file is executable "$shared_path/deploy/${environment}/${_hook}"
then
log step "${_hook} ( ${shared_path##${project_path}/}/deploy/${environment}/ )"
hook_source "$shared_path/deploy/${environment}/${_hook}" &> >(tee -a "${deploy_log_path}/${_hook}.log")
log step success
fi
# Project; Repository
if path exists "$shared_path/$project" &&
file is executable "$shared_path/$project/config/deploy/${_hook}"
then
log step "${_hook} ( ${shared_path##${project_path}/}/${project}/config/deploy/ )"
hook_source "$shared_path/$project/config/deploy/${_hook}" &> >(tee -a "${deploy_log_path}/${_hook}.log")
log step success
fi
# Project; Repository, Environment
if variable is nonempty environment &&
file is executable "$shared_path/$project/config/deploy/$environment/${_hook}"
then
log step "${_hook} ( ${shared_path##${project_path}/}/${project}/config/deploy/${environment}/ )"
hook_source "$shared_path/$project/config/deploy/$environment/${_hook}" &> >(tee -a "${deploy_log_path}/${_hook}.log")
log step success
fi
}
hook_source()
{
trace_filter "deploy" || set -o xtrace
( set -v ; source "$1" )
}
before_step()
{
trace_filter "deploy" || set -o xtrace
hook "before_$1" "$@"
}
after_step()
{
trace_filter "deploy" || set -o xtrace
hook "after_$1" "$@"
}
update()
{
trace_filter "deploy" || set -o xtrace
update_repository && release
}
update_repository()
{
trace_filter "deploy" || set -o xtrace
log step "update_repository ( ${shared_path##${project_path}/}/${project} )"
fetch_uri "$repository_url" "$shared_path/$project" "${branch:-}"
log step success
path enter "${shared_path}/${project}"
log " TODO: **** move revision handling to fetch_uri ****"
case "${scm_type}" in
(git|github)
if files exists ".gitmodules"
then
log step "Updating submodules"
git submodule init > "${deploy_log_path}/git-submodules.log" 2>&1
git submodule update >> "${deploy_log_path}/git-submodules.log" 2>&1
log step success
fi
if variable is nonempty revision
then
log step "Checking out revision '$revision'."
git checkout $revision > "${deploy_log_path}/git-checkout.log" 2>&1
log step success
else
revision=$( git log --no-color -1 | awk '/commit/{print $2}')
revision=${revision:0:7}
fi
if variable is nonempty branch
then
branch=$( git branch --no-color | awk '/^\* /{print $2}')
fi
;;
(hg)
if variable is nonempty revision
then
# TODO: Find out if this is how it's done for hg ;)
log step "Checking out revision '$revision'."
hg update $branch $revision > "${deploy_log_path}/hg-update.log" 2>&1
log step success
else
log step "Updating branch '$branch'."
hg update $branch > "${deploy_log_path}/hg-update.log" 2>&1
log step success
fi
;;
(svn)
if variable is nonempty revision && (( revision > 0 ))
then
log step "Checking out revision $revision"
svn checkout -r $revision > "${deploy_log_path}/svn-checkout.log" 2>&1
log step success
fi
;;
esac
}
stage()
{
trace_filter "deploy" || set -o xtrace
log step "stage ~ staging ${project} ${branch:-} ${revision:-} ( ${stage_path##${project_path}/} )"
rsync -avg --delete --exclude=".${scm_type}/" "${shared_path}/${project}/" "${stage_path}/" \
> "${deploy_log_path}/stage-rsync.log" 2>&1
log step success
path enter "$stage_path"
path create "$stage_path/public"
}
fail_stage()
{
trace_filter "deploy" || set -o xtrace
if path exists "${stage_path}"
then
log step "stage fail ~ Removing staged release ( $stage_path )" \
path move from "${stage_path}" to "${discard_path}/${timestamp}"
fi
}
release()
{
trace_filter "deploy" || set -o xtrace
typeset _prefix
_prefix="${shared_path%/*}"
#TODO: version detection failed !!!
versionedfs add \
path "${project_path}" \
name "${project}" \
version ${version:=$(date +"%Y-%m-%dT%H:%M:%S")} \
source "$stage_path"
versionedfs activate \
path "${project_path}" \
name "${project}" \
version ${version:=$(date +"%Y-%m-%dT%H:%M:%S")}
path remove "$stage_path"
path enter "$release_path"
}
configure()
{
trace_filter "deploy" || set -o xtrace
local _name _file _path _paths _prefix _shared_public_path
_prefix="${shared_path%/*}"
_shared_public_path="${shared_path}/public"
if ! path exists "${shared_path}/config"
then
return 0
fi
path enter "${shared_path}/config"
for _file in "${shared_path}"/config/*
do
_name="${_file##*/}"
log step " link ~ ${_name} ( ${shared_path##${project_path}/}/config/${_name} => $stage_path/config/${_name} )"
path remove "$stage_path/config/${_name}"
file link symbolic \
from "$shared_path/config/${_name}" \
to "$stage_path/config/${_name}"
log step success
done
if path exists "$shared_path/db"
then
log step " link ~ db path ( ${shared_path##${project_path}/}/db => ${stage_path##${project_path}/}/db )"
path remove "$stage_path/db"
file link symbolic from "$shared_path/db" to "$stage_path/db"
log step success
fi
for _path in tmp log pids #files
do
if path exists "${stage_path}/${_path}"
then
path remove "${stage_path}/${_path}"
fi
log step " link ~ ${_path} ( ${shared_path##${project_path}/}/${_path} => ${stage_path##${project_path}/}/${_path} )"
path remove "${stage_path}/${_path}"
file link symbolic \
from "${shared_path}/${_path}" \
to "${stage_path}/${_path}"
log step success
done
if ! path exists "$shared_path/public/"
then
log error "\nPlease run the folloing command first and then retry:\n\tsm rails setup"
fi
_paths=($( find "${shared_path}/public" -mindepth 1 -maxdepth 1 -type d ))
for _path in "${_paths[@]}"
do
_path="${_path#${_shared_public_path}/}"
log step " link ~ public ${_path} ( ${shared_path##${project_path}/}/public/${_path} => ${stage_path##${project_path}/}/public/${_path} )"
path remove "${stage_path}/public/${_path}"
file link symbolic \
from "${_shared_public_path}/${_path}" \
to "${stage_path}/public/${_path}"
log step success
done
}
record()
{ # TODO: What else could we record that would be useful?
trace_filter "deploy" || set -o xtrace
paths exist "${release_path}" || return 0 # Release failed, nothing to do...
if variable is nonempty revision
then
file write string "${revision}" to "${release_path}/revision"
fi
if variable is nonempty branch
then
file write string "${branch}" to "${release_path}/branch"
fi
if variable is nonempty repository_url # TODO: get this from the .uri file.
then
file write string "$repository_url" to "${release_path}/repository_url"
fi
if variable is nonempty timestamp
then
files write string "${timestamp}" to "${release_path}/timestamp"
fi
}
remove_releases()
{
trace_filter "deploy" || set -o xtrace
local _release
path enter "${versions_path}"
while (( $# > 0 ))
do
_release="${1#*:}"
shift
log "Removing old release ${_release}"
versionedfs remove \
path "${project_path}" \
name "${project}" \
version ${_release}
done
}
cleanup()
{
trace_filter "deploy" || set -o xtrace
if array is empty old_releases || ! path exists "${versions_path}"
then
return 0 # No old releases
fi
if ! remove_releases "${old_releases[@]}"
then
log warn "There was an error removing old releases (${old_releases[*]})."
fi
}
do_deploy()
{
trace_filter "deploy" || set -o xtrace
PATH="./bin:$PATH" # Account for project-specific command overrides.
# Logging (http://nakedape.cc/wiki/ShellHacks 1.6.1, 1.7, 1.11)
log_file="${deploy_log_path}/deploy.log"
touch "${log_file}"
#exec 3>>"${log_file}"
exec > >(tee -a "${log_file}") 2>&1
# Error handling
trap revert_on_error ERR
trap revert_on_error ZERR
# This will run any deploy hooks that are called
# (by hook file name)
# Hooks are sourced into a subshell IF they are executable.
# This means that users may disable hooks by changing their
# execute bit.
#
if ! command exists rsync
then
error "The 'rsync' command was not found, please install rsync before using deploy."\
"(Note that there is an rsync extension in the networking extension set)\n"\
"root# sm sets install networking ; sm rsync package install"
fi
if path exists "${release_path}"
then # If release path already exists, start inside it.
path enter "$release_path"
fi
log " Deploy started at ${timestamp}"
step deploy
log " Deploy completed at $(date +%m.%d.%YT%H:%M:%S)"
}
deploy()
{
trace_filter "deploy" || set -o xtrace
typeset _step
for _step in "${deploy_steps[@]}"
do
step "${_step}" &> >(tee -a "${log_file}")
done
}
revert_on_error()
{
trace_filter "deploy" || set -o xtrace
result=$?
trap "backtrace \"An errorr occured while reverting.\"" ERR
trap "backtrace \"An errorr occured while reverting.\"" ZERR
backtrace "A command has returned an not handled error code (${result})." no_exit 2>"${log_file}"
log "\nAn error occured, reverting...\n for details check the logfiles in ${deploy_log_path}/\n" >&2
retreat >&2
kill -s USR2 $APP_PID #to exit application not current subshell
}
revert()
{
retreat
}
do_revert()
{
trace_filter "deploy" || set -o xtrace
log " Reverting ..."
retreat "deploy" "${deploy_steps[@]}"
}
fail_release()
{
trace_filter "deploy" || set -o xtrace
if path exists "$stage_path"
then
log step "discard ~ Discarding stage ( $discard_path/$timestamp )"
path move from "$stage_path" to "$discard_path/$timestamp"
log step success
fi
if variable is nonempty initial_release
then
paths exist "$initial_release" || return 0
else
return 0
fi
paths create "$discard_path" "$deploy_log_path"
# TODO: versionedfs last # switch to last version
log step "Reverting to previous release $previous"
path move from "$previous" to "${release_path%/}"
log step success
}