forked from WilliamFei/gaga
-
Notifications
You must be signed in to change notification settings - Fork 0
/
duckchat.sh
251 lines (223 loc) · 5.65 KB
/
duckchat.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
#!/bin/bash
LANG="en_US.UTF-8"
LANG="zh_CN.UTF-8"
echo "\
+-------------------------------------------+
| Duckchat - 一个安全的私有聊天软件 |
+-------------------------------------------+
| Website: https://duckchat.akaxin.com |
+-------------------------------------------+
| Github : https://github.com/duckchat/gaga |
+-------------------------------------------+
"
originDirName=""
httpPort=80
zalyPort=2021
wsPort=2031
sysOS=`uname -s`
operation=""
duckchatDockerName="ccr.ccs.tencentyun.com/duckchat/gaga"
dockerImageExist=false
dockerContainerExist=false
stopDockerContainerId=""
stopDockerName=""
duckchatName="duckchat"
isExit=false
dockerContainerIdExists=false
# check permission
if [ "$sysOS" = "Linux" ];then
uid=`id -u`
if [ $uid -ne 0 ]; then
echo "[DuckChat] 需要 root 权限,请使用: " sudo sh $0
exit 1
fi
fi
checkDockerImageExist () {
imageCounts=`docker images | grep $duckchatDockerName | wc -l`
if [ $imageCounts -gt 0 ];then
dockerImageExist=true
fi
if [ "$dockerImageExist" = false ];then
echo
echo "[DuckChat] duckchat镜像不存在"
echo "[DuckChat] 开始拉取duckchat镜像"
docker pull $duckchatDockerName
if [ $? != 0 ];then
echo "[DuckChat] 拉取duckchat镜像失败 "
exit
else
dockerImageExist=true
fi
fi
}
getStopDockerContainerId () {
containers=` docker ps -af "name=$stopDockerName" | awk '{print \$1}'`
OLD_IFS="$IFS"
IFS=" "
IFS="$OLD_IFS"
for container in $containers
do
if [ "$container" != "CONTAINER" ];then
stopDockerContainerId=$container
fi
done
if [ "$stopDockerContainerId" = "" ];then
echo "[duckchat] 镜像已是停止状态,不需要再次停止"
exit
fi
}
checkDockerContainerIdExist () {
containers=` docker ps -af "name=$duckchatName" | awk '{print \$1}'`
OLD_IFS="$IFS"
IFS=" "
IFS="$OLD_IFS"
for container in $containers
do
if [ "$container" != "CONTAINER" ];then
dockerContainerIdExists=true
fi
done
}
for arg in "$@"
do
name=`echo $arg | cut -d \= -f 1`
value=`echo $arg | cut -d \= -f 2`
case "$name" in
-h|--help )
echo "\
-http=[port]
指定http服务端口号,默认为 80
-zaly=[port]
指定zaly服务器的监听地址与端口,默认为 “:2021”
-ws=[port]
指定websocket服务器的监听地址与端口,默认为:”:2031”
start
启动duckchat镜像
stop [-i stopDockerName]
终止duckchat镜像
ls
列出所有的镜像
"
exit 0;;
-base)
originDirName=$value
continue
;;
-http)
httpPort=$value
continue
;;
-zaly)
zalyPort=$value
continue
;;
-ws)
wsPort=$value
continue
;;
start)
if [ -z "$operation" ];then
operation="start"
else
isExit=true
echo [DuckChat] $operation , start 命令冲突
exit
fi
;;
stop)
if [ -z "$operation" ];then
operation="stop"
else
isExit=true
echo [DuckChat] $operation , stop 命令冲突
exit
fi
;;
ls)
if [ -z "$operation" ];then
operation="ls"
else
isExit=true
echo [DuckChat] $operation , ls 命令冲突
exit
fi
;;
-i)
duckchatName=$value
esac
done
if [ "$isExit" = true ];then
exit
fi
if [ -z "$operation" ];then
operation="start"
fi
case $operation in
start)
if [ "$sysOS" = "Linux" ] || [ "$sysOS" = "Darwin" ];then
echo "[Command] docker -v";
docker -v
if [ $? != 0 ];then
echo "[DuckChat] 请根据文档安装Docker服务器集成环境: https://duckchat.akaxin.com/wiki/server/docker.md"
echo
exit
fi
fi
checkDockerImageExist
if [ "$dockerImageExist" = true ] ; then
echo "[DuckChat] 启动duckchat镜像"
if [ $originDirName = ""];then
originDirName=$(cd $(dirname ${BASH_SOURCE:-$0});pwd)
fi
checkDockerContainerIdExist
if [ $dockerContainerIdExists = true ]; then
echo "[DuckChat] duckchat镜像已经启动,请使用stop命令,停止运行镜像"
exit
fi
echo
echo "#" docker run -itd -p $httpPort:80 -p $wsPort:2031 -p $zalyPort:2021 --name $duckchatName -v $originDirName:/home/gaga $duckchatDockerName
su -c "setenforce 0" 2>/dev/null
docker run -itd -p $httpPort:80 -p $wsPort:2031 -p $zalyPort:2021 --name $duckchatName -v $originDirName:/home/gaga $duckchatDockerName
if [ $? != 0 ];then
echo "[DuckChat] 启动duckchat镜像失败"
exit
fi
chmod -R 777 $originDirName/src
if [ -f $originDirName/.git/config ]; then
if [ "$sysOS" = "Linux" ]; then
sed -i 's/remote\"/remote \"/g' $originDirName/.git/config
sed -i 's/branch\"/branch \"/g' $originDirName/.git/config
sed -i 's/filemode = true/filemode = false/g' $originDirName/.git/config
elif [ "$sysOS" = "Darwin" ]; then
sed -i 'config' 's/remote\"/remote \"/g' $originDirName/.git/config
sed -i 'config' 's/branch\"/branch \"/g' $originDirName/.git/config
sed -i 'config' 's/filemode = true/filemode = false/g' $originDirName/.git/config
fi
fi
echo "[DuckChat] 请稍后片刻"
sleep 9
echo "[DuckChat] 启动duckchat镜像成功"
fi
;;
stop)
stopDockerName=$duckchatName
getStopDockerContainerId
echo "开始终止$stopDockerName 实例: $stopDockerContainerId"
docker stop $stopDockerContainerId
if [ $? != 0 ];then
echo "[DuckChat] 镜像终止失败"
exit
fi
echo "[DuckChat] 终止 $stopDockerName 镜像成功"
echo "[DuckChat] 开始删除容器 : $stopDockerName"
docker rm $stopDockerName
if [ $? != 0 ];then
echo "[DuckChat] 删除镜像失败"
exit
fi
echo "[DuckChat] 删除容器 $stopDockerName 成功"
;;
ls)
docker ps -a
;;
esac