-
Notifications
You must be signed in to change notification settings - Fork 2
/
manage.sh
executable file
·154 lines (140 loc) · 5.59 KB
/
manage.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
#!/bin/bash
# Script to manage the Aigents SNET service.
BASE_DIR=$(readlink -f $(dirname ${BASH_SOURCE[0]}))
B_TAG=$(date +%b%d)
C_PORT=9999
H_PORT=9999
if [ ! -z $2 ] ; then
B_TAG=$2
fi
if [ ! -z $3 ] ; then
H_PORT=$3
fi
I_NAME="snet_aigents:$B_TAG"
C_NAME="aigents_server-$B_TAG"
build_docker() {
docker build -t $I_NAME $BASE_DIR
}
run_service() {
if [ -f /.dockerenv ] ; then
printf "${BAD_COLOR}Ignoring! Running in a docker container${NORMAL_COLOR}\n"
else
docker inspect $C_NAME > /dev/null 2>&1
if [ $? -eq 1 ] ; then
printf "${GOOD_COLOR}Starting new container...${NORMAL_COLOR}\n"
docker run -d --name $C_NAME \
-p $H_PORT:$C_PORT \
$I_NAME \
python3 aigents_service.py
else
if [ "$(docker inspect -f '{{.State.Running}}' $C_NAME)" = "true"] ; then
printf "${OKAY_COLOR}Container already running. ${NORMAL_COLOR}\n"
else
printf "${OKAY_COLOR}Container exited. Starting... ${NORMAL_COLOR}\n"
docker start $C_NAME
fi
fi
fi
}
run_service_with_daemon() {
H_PORT=7110
if [ -f /.dockerenv ] ; then
printf "${BAD_COLOR}Ignoring! Running in a docker container${NORMAL_COLOR}\n"
else
docker inspect $C_NAME > /dev/null 2>&1
if [ $? -eq 1 ] ; then
printf "${GOOD_COLOR}Starting new container...${NORMAL_COLOR}\n"
docker run --name $C_NAME \
-p $H_PORT:$C_PORT \
-p 7113:7113 \
-v /home/$USER/.certs/:/opt/singnet/.certs/ \
$I_NAME \
supervisord -c supervisord-inside-dev.conf -n
else
if [ "$(docker inspect -f '{{.State.Running}}' $C_NAME)" = "true"] ; then
printf "${OKAY_COLOR}Container already running. ${NORMAL_COLOR}\n"
else
printf "${OKAY_COLOR}Container exited. Starting... ${NORMAL_COLOR}\n"
docker start $C_NAME
fi
fi
fi
}
run_service_with_daemon_prod() {
if [ -f /.dockerenv ] ; then
printf "${BAD_COLOR}Ignoring! Running in a docker container${NORMAL_COLOR}\n"
else
docker inspect $C_NAME > /dev/null 2>&1
if [ $? -eq 1 ] ; then
printf "${GOOD_COLOR}Starting new container...${NORMAL_COLOR}\n"
export AIGENTS_PROD_ENV=1
docker run --name $C_NAME \
-p 6003:$C_PORT \
-p 6002:6002 \
-e AIGENTS_PROD_ENV \
-v /home/$USER/.certs/:/opt/singnet/.certs/ \
$I_NAME \
supervisord -c supervisord-inside-prod.conf -n
else
if [ "$(docker inspect -f '{{.State.Running}}' $C_NAME)" = "true"] ; then
printf "${OKAY_COLOR}Container already running. ${NORMAL_COLOR}\n"
else
printf "${OKAY_COLOR}Container exited. Starting... ${NORMAL_COLOR}\n"
docker start $C_NAME
fi
fi
fi
}
stop_service() {
if [ "$(docker inspect -f '{{.State.Running}}' $C_NAME)" = "true" ]; then
docker stop $C_NAME
else
printf "${OKAY_COLOR}Container \"$C_NAME\" not running currently ${NORMAL_COLOR}\n"
fi
}
build_proto() {
docker inspect $I_NAME > /dev/null 2>&1
if [ $? -eq 1 ] ; then
printf "${BAD_COLOR}Error: Image \"$I_NAME\" doesn't exist! ${NORMAL_COLOR}\n"
else
export HOST_UID=$UID
docker run --rm \
-v $BASE_DIR/service_spec:/singnet/aigents/service_spec \
-e "HOST_UID" \
$I_NAME \
sh -c 'protoc --proto_path=/singnet/aigents/service_spec \
--php_out=/singnet/aigents/service_spec \
--grpc_out=/singnet/aigents/service_spec \
--plugin=protoc-gen-grpc=$(which grpc_php_plugin) \
/singnet/aigents/service_spec/aigents.proto \
&& \
protoc --proto_path=/singnet/aigents/service_spec \
--python_out=/singnet/aigents/service_spec \
--grpc_out=/singnet/aigents/service_spec \
--plugin=protoc-gen-grpc=$(which grpc_python_plugin) \
/singnet/aigents/service_spec/aigents.proto \
&& \
chown -R $HOST_UID:$HOST_UID /singnet/aigents/service_spec'
fi
}
help () {
echo "Usage: bash manage.sh OPTION [LABEL] [SERVICE_PORT]"
echo " Options:"
echo " build build the docker image"
echo " run run the snet service with out daemon (start container)"
echo " run-with-daemon-dev run the snet service dev with daemon"
echo " run-with-daemon-prod run the snet service prod with daemon"
echo " stop stop the snet service (stop container)"
echo " build-proto build the proto files"
echo -e "\n LABEL : image and container label (optional - default=date)"
echo -e "\n SERVICE_PORT : port to expose the service through (optional - default=9999)"
}
case $1 in
build) build_docker ;;
run) run_service ;;
run-with-daemon-dev) run_service_with_daemon ;;
run-with-daemon-prod) run_service_with_daemon_prod ;;
stop) stop_service ;;
build-proto) build_proto ;;
*) help ;;
esac