-
Notifications
You must be signed in to change notification settings - Fork 0
/
appserver
executable file
·144 lines (112 loc) · 4.83 KB
/
appserver
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
#!/bin/bash
PARENT_PATH=$(dirname $0)
MXPROTOCOL_SERVER_PATH=$PARENT_PATH/../mxprotocol-server
CONFIG=$PARENT_PATH/config
SERVICENAME=appserver
if [ -f "$CONFIG" ]; then
supernode_mode="development"
else
echo "Config file does not exist. Nothing is done. "
exit
fi
if [ "$1" = "-h" ]; then
echo "Usage : appserver [local|build]"
echo ""
echo "Commands : "
echo " local Start docker container with local database and mqtt service"
echo " build Command for building the code, only run in docker container"
echo ""
exit
fi
build_image()
{
VERSION=$(git describe --tags --always --long |sed -e "s/^v//")
echo "build image with version $VERSION"
rm -f out.txt error.txt
# build image
DOCKER_HUB_USERNAME=$(cat "./.dockerhub" | awk -F '=' '/docker_hub_username/{print $NF}')
DOCKER_HUB_PASSWORD=$(cat "./.dockerhub" | awk -F '=' '/docker_hub_password/{print $NF}')
DOCKER_HUB_REPOSITORY="mxcdocker/appserver"
docker logout
docker login -u $DOCKER_HUB_USERNAME -p $DOCKER_HUB_PASSWORD docker.io
docker build --pull -t $DOCKER_HUB_REPOSITORY:$VERSION . 2>>error.txt 1>>out.txt|tee
# it error.txt contains error, exit
ERROR=$(cat error.txt| awk '/^Building /{next}{print "error occurs"; exit}')
if [ "$ERROR"x = "error occurs"x ]; then
echo "Failed to build image, error occurs..."
exit
fi
IMAGE_ID=$(cat out.txt |awk '/ *Successfully built */{print $NF}')
if [ "$IMAGE_ID"x = x ]; then
echo "Failed to build image, unable to get image_id..."
exit
else
echo "Successfully built image $IMAGE_ID"
fi
echo "re-login docker.io"
# push image to docker hub
echo "docker push image"
docker tag $IMAGE_ID $DOCKER_HUB_REPOSITORY:$VERSION && \
docker push $DOCKER_HUB_REPOSITORY:$VERSION && \
docker image rm $IMAGE_ID
echo "Build image successfully, rm error.txt"
# delete error message only when everything is done successfully
rm error.txt
}
obtain_certificate()
{
[ -d "$PARENT_PATH"/configuration/ecc ] && echo "$PARENT_PATH/configuration/ecc already exists, skip generate certificates" && return
[ -f "$PARENT_PATH"/configuration/pass.txt ] || ( echo "$PARENT_PATH/configuration/pass.txt is required, failed to obtain certificates" && return )
echo "$PARENT_PATH/configuration/pass.txt exists"
mkdir -p "$PARENT_PATH"/configuration/ecc || ( echo "Failed to do mkdir -p $PARENT_PATH/configuration/ecc" && return )
SERVER_HOSTNAME=$(hostname -f)
git clone git@gitlab.com:MXCFoundation/cloud/testing-framework.git
mv "$PARENT_PATH"/configuration/pass.txt "$PARENT_PATH"/testing-framework/gRPC-client/certificates/ps-staging.matchx.io/
"$PARENT_PATH"/testing-framework/gRPC-client/certificates/gen-ecc-server-certificates.sh ps-staging.matchx.io "$SERVER_HOSTNAME" || return
cp "$PARENT_PATH"/testing-framework/gRPC-client/certificates/ps-staging.matchx.io/"$SERVER_HOSTNAME"/*.crt "$PARENT_PATH"/configuration/ecc/ecc_client.crt
cp "$PARENT_PATH"/testing-framework/gRPC-client/certificates/ps-staging.matchx.io/"$SERVER_HOSTNAME"/*.key "$PARENT_PATH"/configuration/ecc/ecc_client.key
cp "$PARENT_PATH"/testing-framework/gRPC-client/certificates/ps-staging.matchx.io/ecc_rootCA.pem "$PARENT_PATH"/configuration/ecc/ecc_rootCA.pem
}
if [ "$*" = "local" ]; then
if [ -f "/.dockerenv" ]; then
echo "Command '$0 $*' only runs outside of a docker container, nothing is executed. "
exit
fi
python "$PARENT_PATH"/scripts/docker/docker-compose-template.py \
"env_file"=$CONFIG \
"supernode_mode"=$supernode_mode |tee tmp
[ "$(cat tmp)" = "invalid argument" ] && exit
if [ -f "$PARENT_PATH"/docker-compose.yml ]; then
mv "$PARENT_PATH"/docker-compose.yml "$PARENT_PATH"/docker-compose.yml.bak
fi
mv tmp "$PARENT_PATH"/docker-compose.yml
# obtain certificate
obtain_certificate
[ -d "$PARENT_PATH"/testing-framework/ ] && rm -rf "$PARENT_PATH"/testing-framework/
docker-compose -f "$PARENT_PATH"/docker-compose.yml up -d --remove-orphans
if [ "$supernode_mode" = "development" ]; then
# build mxprotocol-server
if [ -d "$MXPROTOCOL_SERVER_PATH" ]; then
docker-compose exec -d mxprotocol-server bash --init-file "$MXPROTOCOL_SERVER_PATH"/scripts/compile/develop-compile
fi
# build appserver
docker-compose exec appserver bash --init-file "$PARENT_PATH"/scripts/compile/develop-compile
fi
elif [ "$*" = "create_image" ]; then
if [ -f "/.dockerenv" ]; then
echo "Command '$0 $*' only runs outside of a docker container, nothing is executed. "
exit
fi
build_image
echo "Before exit, rm out.txt"
rm out.txt
elif [ "$*" = "build" ]; then
if ! [ -f "/.dockerenv" ]; then
echo "Command '$0 $*' only runs inside of a docker container to rebuild service, nothing is executed. "
exit
fi
"$PARENT_PATH"/scripts/compile/develop-compile
else
"$PARENT_PATH"/appserver -h
exit
fi