forked from tinode/chat
-
Notifications
You must be signed in to change notification settings - Fork 0
/
build-all.sh
executable file
·233 lines (188 loc) · 6.61 KB
/
build-all.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
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
#!/bin/bash
# Cross-compiling script using https://github.com/mitchellh/gox
# I use this to compile the Linux version of the server on my Mac.
# Supported OSs: darwin windows linux
goplat=( darwin windows linux )
# Supported CPU architectures: amd64
goarc=( amd64 )
# Supported database tags
dbtags=( mysql rethinkdb )
DAEMON=$GOPATH/bin/server
DB_DAEMON = $GOPATH/bin/tinode_db
PROGRAM_CONFIG=$GOPATH/src/github.com/mudphilo/chat/server/tinode.conf
DB_CONFIG=$GOPATH/src/github.com/mudphilo/chat/tinode-db/tinode.conf
PROGRAM_DATA = $GOPATH/tinode-web
DB_DATA=$GOPATH/src/github.com/mudphilo/chat/tinode-db/data.json
LOGFILE=/var/log/somefile.log
RETHINK_DIR_DIRECTORY="$GOPATH/src/rethinkdb_data"
RETHINK_PROCESS="rethinkdb --bind all --daemon"
rethink_running=0
for line in $@; do
eval "$line"
done
version=${tag#?}
if [ -z "$version" ]; then
# Get last git tag as release version. Tag looks like 'v.1.2.3', so strip 'v'.
version=`git describe --tags`
version=${version#?}
fi
echo "Releasing $version"
GOSRC=${GOPATH}/src/github.com/mudphilo
install() {
echo -n "installing $DAEMON"
# check if rethinkdb daemon is running
pgrep -f "$RETHINK_PROCESS" |
{
while read -r pid ; do
echo "GOT PID $pid killing process"
kill $pid
(( rethink_running += 1 ))
done
echo "GOT rethinkdb daemon running instances $rethink_running"
# initialize application
if [ "$rethink_running" -gt "$zero" ]
then
# initialize database
echo " rethinkdb database not running"
echo "starting rethinkdb as daemon "
rethinkdb --bind all --daemon > $LOGFILE 2>&1
fi
}
pushd ${GOSRC}/chat > $LOGFILE 2>&1
# Prepare directory for the new release
rm -fR ./releases/${version}
mkdir ./releases/${version}
for plat in "${goplat[@]}"
do
for arc in "${goarc[@]}"
do
# Keygen is database-independent
# Remove previous build
rm -f $GOPATH/bin/keygen
# Build
~/go/bin/gox -osarch="${plat}/${arc}" -ldflags "-s -w" -output $GOPATH/bin/keygen ./keygen > $LOGFILE 2>&1
for dbtag in "${dbtags[@]}"
do
echo "Building ${dbtag}-${plat}/${arc}..."
# Remove previous builds
rm -f $GOPATH/bin/tinode
rm -f $GOPATH/bin/init-db
# Build tinode server and database initializer for RethinkDb and MySQL.
~/go/bin/gox -osarch="${plat}/${arc}" \
-ldflags "-s -w -X main.buildstamp=`git describe --tags`" \
-tags ${dbtag} -output $GOPATH/bin/tinode ./server > $LOGFILE 2>&1
~/go/bin/gox -osarch="${plat}/${arc}" \
-ldflags "-s -w" \
-tags ${dbtag} -output $GOPATH/bin/init-db ./tinode-db > $LOGFILE 2>&1
# Tar on Mac is inflexible about directories. Let's just copy release files to
# one directory.
rm -fR ./releases/tmp
mkdir -p ./releases/tmp/static
# Copy templates and database initialization files
cp ./server/tinode.conf ./releases/tmp
cp -R ./server/templ ./releases/tmp
cp -R ./server/static/img ./releases/tmp/static
cp -R ./server/static/audio ./releases/tmp/static
cp -R ./server/static/css ./releases/tmp/static
cp ./server/static/index.html ./releases/tmp/static
cp ./server/static/tinode.js ./releases/tmp/static
cp ./server/static/drafty.js ./releases/tmp/static
cp ./tinode-db/data.json ./releases/tmp
cp ./tinode-db/*.jpg ./releases/tmp
cp ./tinode-db/credentials.sh ./releases/tmp
# Build archive. All platforms but Windows use tar for archiving. Windows uses zip.
if [ "$plat" = "windows" ]; then
# Copy binaries
cp $GOPATH/bin/tinode.exe ./releases/tmp
cp $GOPATH/bin/init-db.exe ./releases/tmp
cp $GOPATH/bin/keygen.exe ./releases/tmp
# Remove possibly existing archive.
rm -f ./releases/${version}/tinode-${dbtag}."${plat}-${arc}".zip
# Generate a new one
pushd ./releases/tmp > $LOGFILE 2>&1
zip -q -r ../${version}/tinode-${dbtag}."${plat}-${arc}".zip ./*
popd > /dev/null
else
plat2=$plat
# Rename 'darwin' tp 'mac'
if [ "$plat" = "darwin" ]; then
plat2=mac
fi
# Copy binaries
cp $GOPATH/bin/tinode ./releases/tmp
cp $GOPATH/bin/init-db ./releases/tmp
cp $GOPATH/bin/keygen ./releases/tmp
# Remove possibly existing archive.
rm -f ./releases/${version}/tinode-${dbtag}."${plat2}-${arc}".tar.gz
# Generate a new one
tar -C ${GOSRC}/chat/releases/tmp -zcf ./releases/${version}/tinode-${dbtag}."${plat2}-${arc}".tar.gz .
fi
done
done
done
# Need to rebuild lthe inux-rethink binary without stripping debug info.
echo "Building the binary for the demo at api.tinode.co"
rm -f $GOPATH/bin/tinode
rm -f $GOPATH/bin/init-db
~/go/bin/gox -osarch=linux/amd64 \
-ldflags "-X main.buildstamp=`git describe --tags`" \
-tags rethinkdb -output $GOPATH/bin/tinode ./server > $LOGFILE 2>&1
~/go/bin/gox -osarch=linux/amd64 \
-tags rethinkdb -output $GOPATH/bin/init-db ./tinode-db > $LOGFILE 2>&1
# Build chatbot release
echo "Building chatbot..."
rm -fR ./releases/tmp
mkdir -p ./releases/tmp
cp ${GOSRC}/chat/chatbot/chatbot.py ./releases/tmp
cp ${GOSRC}/chat/chatbot/quotes.txt ./releases/tmp
cp ${GOSRC}/chat/pbx/model_pb2.py ./releases/tmp
cp ${GOSRC}/chat/pbx/model_pb2_grpc.py ./releases/tmp
tar -C ${GOSRC}/chat/releases/tmp -zcf ./releases/${version}/chatbot.tar.gz .
pushd ./releases/tmp > $LOGFILE 2>&1
zip -q -r ../${version}/chatbot.zip ./*
popd > $LOGFILE 2>&1
# Clean up temporary files
rm -fR ./releases/tmp
popd > $LOGFILE 2>&1
}
stop() {
killall $DAEMON
}
status() {
killall -0 $DAEMON
if [ "$?" -eq 0 ]; then
echo "Running."
else
echo "Not Running."
fi
}
start() {
echo -n "starting up $DAEMON"
echo -n "starting db"
RUN=`cd / && $DB_DAEMON --config=$DB_CONFIG --data=$DB_DATA > $LOGFILE 2>&1`
if [ "$?" -eq 0 ]; then
echo "Done."
else
echo "FAILED."
fi
}
case "$1" in
start)
start
;;
restart)
stop
sleep 2
start
;;
stop)
stop
;;
status)
status
;;
*)
echo "usage : $0 start|restart|stop|status"
;;
esac
exit 0