-
Notifications
You must be signed in to change notification settings - Fork 4
/
build.sh
executable file
·339 lines (306 loc) · 6.74 KB
/
build.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
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
#!/bin/bash
usage="usage: build.sh [OPTION...] [COMMAND] \n
Available commands:\n
proto - creates required protobuf code. Available options:\n
\t -d|--docker - set USE_DOCKER=true
mock - creates required mocks for testing\n
test - run test\n
compile - build binary files\n
clean - remove all generated files\n
reset - stop processes and remove all generated files\n
start - run michman (launcher and rest api). Available options:\n
\t -c|--config - config path\n
\t -l|--launcher-port - launcher port (default value is 5000)\n
\t -r|--rest-port|--http-port - http port (default value is 8081)\n
status - check launch and http processes\n
help - show this message"
LAUNCHER_BIN=launch
REST_BIN=http
LAUNCHER_START_LOG=.launch_start.log
REST_START_LOG=.http_start.log
CONFIG=./configs/config.yaml
LAUNCHER_PORT=5000
HTTP_PORT=8081
PROTO_CODE=./internal/protobuf/launcher.pb.go
MOCK_DATABASE=mock_database.go
MOCK_GRPC=mock_grpcclient.go
MOCK_VAULT=mock_vault.go
while [[ $# -gt 0 ]]
do
key=$1
case $key in
-c|--config)
CONFIG=$2
shift
shift
;;
-l|--launcher-port)
LAUNCHER_PORT=$2
shift
shift
;;
-r|--rest-port|--http-port)
HTTP_PORT=$2
shift
shift
;;
-d|--docker)
USE_DOCKER=true
shift
shift
;;
-h|--help)
COMMAND=help
break
;;
proto|mock|test|compile|start|restart|reset|status|stop|clean|help)
if [[ -n "$COMMAND" ]]
then
echo $usage
break
fi
COMMAND=$key
shift
;;
*)
echo -e $usage
break
;;
esac
done
function generate_proto() {
if [ "$USE_DOCKER" == true ]
then
if ! command -v docker &> /dev/null
then
echo "ERROR: docker is not installed or not running"
exit 1
else
echo "generate protobuf code... (docker)"
cd internal/protobuf
docker run --platform linux/amd64\
--rm \
-v $(pwd)/../../:$(pwd)/../../ \
-w $(pwd) znly/protoc \
--go_out=paths=source_relative,plugins=grpc:. \
-I. \
launcher.proto || result=$?
cd ../..
if [ "$result" == 1 ]
then
exit 1
fi
fi
else
if ! command -v protoc &> /dev/null
then
echo "ERROR: protoc is not installed. Use -d (--docker) flag to use protoc in docker"
exit 1
else
echo "generate protobuf code..."
cd internal/protobuf
protoc \
--go_out=paths=source_relative,plugins=grpc:. \
--go_opt=Mlauncher.proto=github.com/ispras/michman/internal/protobuf \
launcher.proto
cd ../..
fi
fi
}
function generate_mock() {
if [ -z $( 2>/dev/null ls $PROTO_CODE ) ]
then
generate_proto
fi
echo "generate mocks..."
if ! command -v mockgen &> /dev/null
then
echo "ERROR: mockgen is not installed"
exit 1
else
cd ./internal/database
mockgen --destination=../mock/$MOCK_DATABASE -package=mock . Database
cd ../..
cd ./internal/rest/handler
mockgen --destination=../../mock/$MOCK_GRPC -package=mock . GrpcClient
cd ../../..
cd ./internal/utils
mockgen --destination=../mock/$MOCK_VAULT -package=mock . SecretStorage
cd ../..
fi
}
function run_tests() {
if [ -z $( 2>/dev/null ls ./internal/mocks/$MOCK_DATABASE ./internal/mocks/$MOCK_GRPC ./internal/mocks/$MOCK_VAULT ) ]
then
generate_mock
fi
#TODO: all tests
echo "run tests..."
cd ./test/handlers
go test
}
function compile() {
if [ -z $( 2>/dev/null ls $PROTO_CODE ) ]
then
generate_proto
fi
echo "build launcher..."
go build -o $LAUNCHER_BIN ./cmd/launcher
echo "build rest api server..."
go build -o $REST_BIN ./cmd/rest
}
function start() {
if [[ -f ".$LAUNCHER_BIN.pid" ]] || [[ -f ".$REST_BIN.pid" ]]
then
echo "Michman is already running"
exit 0
fi
if [ -z $( 2>/dev/null ls $PROTO_CODE ) ]
then
generate_proto
fi
if [ -z $(test -f "$LAUNCHER_BIN") ] || [ -z $(test -f "$REST_BIN") ]
then
compile
fi
echo "run launcher..."
./$LAUNCHER_BIN --config $CONFIG --port $LAUNCHER_PORT 1>/dev/null 2>$LAUNCHER_START_LOG &
echo $! > .$LAUNCHER_BIN.pid
sleep 1
if [[ -z $(ps -eo pid | grep -w $(cat .$LAUNCHER_BIN.pid)) ]]
then
echo "ERROR: launcher didn't start, check launch_start.log file for more information"
rm .$LAUNCHER_BIN.pid
exit
fi
echo "run rest api server..."
./$REST_BIN --config $CONFIG --port $HTTP_PORT --launcher localhost:$LAUNCHER_PORT 1>/dev/null 2>$REST_START_LOG &
echo $! > .$REST_BIN.pid
sleep 1
if [[ -z $(ps -eo pid | grep -w $(cat .$REST_BIN.pid)) ]]
then
echo "ERROR: rest api didn't start, check http_start.log file for more information"
rm .$REST_BIN.pid
exit
fi
}
function restart() {
stop
start
}
function stop() {
if [[ -f ".$LAUNCHER_BIN.pid" ]]
then
echo "kill launcher..."
if ps -p $( cat .$LAUNCHER_BIN.pid ) > /dev/null
then
kill $( cat .$LAUNCHER_BIN.pid )
fi
rm .$LAUNCHER_BIN.pid
fi
if [[ -f ".$REST_BIN.pid" ]]
then
echo "kill rest api server..."
if ps -p $( cat .$REST_BIN.pid ) > /dev/null
then
kill $( cat .$REST_BIN.pid )
fi
rm .$REST_BIN.pid
fi
}
function clean() {
echo "remove all generated and binary files:"
if test -f ./$LAUNCHER_BIN; then
rm -rf ./$LAUNCHER_BIN
echo "rm launch"
fi
if test -f ./$REST_BIN; then
rm -rf ./$REST_BIN
echo "rm http"
fi
if test -f $PROTO_CODE; then
rm -rf $PROTO_CODE
echo "rm proto files"
fi
if test -f ./internal/mock/$MOCK_DATABASE; then
rm -rf ./internal/mock/mock_*
echo "rm mock files"
fi
if test -f ./$LAUNCHER_START_LOG; then
rm -rf ./$LAUNCHER_START_LOG
echo "rm launch_start.log"
fi
if test -f ./$REST_START_LOG; then
rm -rf ./$REST_START_LOG
echo "rm http_start.log"
fi
}
function reset() {
stop
clean
}
function status() {
launch_running=0
launch_running=0
if [[ -f ".$LAUNCHER_BIN.pid" ]]
then
if ps -p $( cat .$LAUNCHER_BIN.pid ) > /dev/null
then
echo "launch process is alive (pid: $( cat .$LAUNCHER_BIN.pid ))"
else
echo "launch process with pid $( cat .$LAUNCHER_BIN.pid ) does not exist any more"
fi
else
launch_running=1
fi
if [[ -f ".$REST_BIN.pid" ]]
then
if ps -p $( cat .$REST_BIN.pid ) > /dev/null
then
echo "http process is alive (pid: $( cat .$REST_BIN.pid ))"
else
echo "http process with pid $( cat .$REST_BIN.pid ) does not exist any more"
fi
else
http_running=1
fi
if [[ $(( launch_running + http_running )) == 2 ]]
then
echo "michman is not running"
fi
}
case $COMMAND in
proto)
generate_proto
;;
mock)
generate_mock
;;
test)
run_tests
;;
compile)
compile
;;
start)
start
;;
restart)
restart
;;
status)
status
;;
stop)
stop
;;
clean)
clean
;;
reset)
reset
;;
help)
echo -e $usage
;;
esac