forked from SciCatProject/localdeploy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
upd-svc.sh
executable file
·124 lines (114 loc) · 3.98 KB
/
upd-svc.sh
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
#!/bin/sh
# upd-svc.sh
# Build script for running regularily in a crontab for example.
# This script rebuilds all SciCat services from source and pushes the resulting
# images to the registry as defined in $SC_SITECONFIG/general.rc
#
# Add this script to a crontab like this for building images repeatedly:
# cd $HOME/scicat; export SC_SITECONFIG=$(pwd)/<sitecfg>; ./deploy/upd-svc.sh update buildlog/readme.md; ./deploy/upd-svc.sh build buildlog/readme.md
# Add this script to a crontab like this for restarting regularly:
# cd $HOME/scicat; export SC_SITECONFIG=$(pwd)/<sitecfg>; ./deploy/upd-svc.sh update buildlog/readme.md; ./deploy/upd-svc.sh restart buildlog/readme.md
# - Assuming the following directory structure:
# - `$HOME/scicat`
# - `<sitecfg>` ($SC_SITECONFIG directory, file 'general.rc' is needed only)
# - `deploy` (git repo containing this script and SciCat deploy scripts)
# - `buildlog` (gitlab snippet or gist repo to share the build log file)
# - do not forget to
# - clone the deploy script repo
# - clone the buildlog repo, set user&pwd, upload ssh keys
# - add the building user to the docker group
# - copy the $SC_SITECONFIG/general.rc from elsewhere
# get the script directory before creating any files
scriptdir="$(dirname "$(readlink -f "$0")")"
. "$scriptdir/services/deploytools"
# get given command line flags
update="$(getScriptFlags update "$@")"
build="$(getScriptFlags build "$@")"
restart="$(getScriptFlags restart "$@")"
[ -z "$update" ] || action=update
[ -z "$build" ] || action=build
[ -z "$restart" ] || action=restart
# log file can be provided as 1st or 2nd arg
logfn="$(readlink -f "$1")"
[ -f "$logfn" ] || logfn="$(readlink -f "$2")"
loadSiteConfig
checkVars SC_NAMESPACE || exit 1
ts() {
date +%s
}
timeFmt() {
local secs="$1"
if [ "$secs" -lt 60 ]; then
echo "$secs s"
else
echo "$((secs/60)) m, $((secs-60*(secs/60))) s"
fi
}
update() {
export SC_TIMESUM=0
echo "# Updating the deploy script"
echo '```'
cd "$scriptdir"
git stash save && git pull --rebase && git stash pop
echo '```'
}
datestr () { TZ='Europe/Berlin' date; }
foreachsvc()
{
local start
local tocfn="$1"
(echo "# $(datestr)"; echo) > "$tocfn"
echo " * [Updating the deploy script](#updating-the-deploy-script)" >> "$tocfn"
local descr; local cmd
if [ "$action" = "build" ]; then
cmd="buildonly"; descr="Building"
elif [ "$action" = "restart" ]; then
cmd="nobuild"; descr="Restarting"
fi
local descr_low; descr_low="$(echo $descr | tr '[:upper:]' '[:lower:]')"
for svc in catamel catanie landing scichat-loopback;
do
start=$(ts)
echo "# $descr $svc"
datestr
echo '```'
if "$scriptdir/services/$svc"/*.sh $cmd;
then
echo " * [{+ $svc +}](#$descr_low-$svc)" >> "$tocfn"
else
echo " * [{- $svc -}](#$descr_low-$svc)" >> "$tocfn"
fi
echo '```'
timeDelta=$(($(ts)-start))
SC_TIMESUM=$((SC_TIMESUM+timeDelta))
echo "Completed in $(timeFmt $timeDelta)."
echo
done
if [ "$action" = "build" ]; then
# remove all containers built
$DOCKER_CMD rmi -f $($DOCKER_CMD images -a -q)
fi
echo >> "$tocfn"
echo "Overall time for $descr_low: $(timeFmt $SC_TIMESUM)."
}
if [ ! -f "$logfn" ]; then
echo "No log file provided, giving up!"
elif [ ! -z "$update" ]; then
#rm -f "$logfn"
update > "$logfn" 2>&1
elif [ ! -z "$build" ] || [ ! -z "$restart" ]; then
# assumes *update* ran before
tocfn="$(mktemp)"
foreachsvc "$tocfn" >> "$logfn" 2>&1
cat "$logfn" >> "$tocfn"
cat "$tocfn" > "$logfn"
#chmod g+rw "$logfn"
branch="${SC_NAMESPACE}-$action"
cd "$(dirname "$logfn")" \
&& git checkout -B "$branch" \
&& git commit -m "latest $action" "$(basename "$logfn")" \
&& git push -u origin "$branch"
else
echo "Usage: $0 (update|build|restart) <log file>"
fi
# vim: set ts=4 sw=4 sts=4 tw=0 et: