-
Notifications
You must be signed in to change notification settings - Fork 4
/
mrdoc.sh
517 lines (482 loc) · 16.4 KB
/
mrdoc.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
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
#!/bin/bash
# DATE: 2021-4-15 11:55:47
# Author: create by jonnyan404
# Blog:https://www.mrdoc.fun
# Description:This script is auto install mrdoc project
# Version:1.4
SYSTEMCTL_CMD=$(command -v systemctl 2>/dev/null)
WORK_PATH=$(cd "$(dirname "$0")";pwd)
#SERVICE_CMD=$(command -v service 2>/dev/null)
SOFTWARE_UPDATED=0
SCR_VERSION="2022.01.14"
#######color code########
RED="31m" # Error message
GREEN="32m" # Success message
YELLOW="33m" # Warning message
BLUE="36m" # Info message
colorEcho(){
COLOR=$1
echo -e "\033[${COLOR}${*:2}\033[0m"
}
installdepend(){
if [[ -n $(command -v apt-get) ]];then
if [[ $SOFTWARE_UPDATED -eq 0 ]]; then
colorEcho ${BLUE} "Debian/Ubuntu Updating software repo"
apt-get update
SOFTWARE_UPDATED=1
fi
apt-get -y install git python3 python3-dev python3-pip gcc python3-wheel python3-setuptools python3-venv libldap2-dev libsasl2-dev libmariadb-dev
elif [[ -n $(command -v yum) ]]; then
if [[ $SOFTWARE_UPDATED -eq 0 ]]; then
colorEcho ${BLUE} "Centos Updating software repo"
yum -q makecache
SOFTWARE_UPDATED=1
fi
yum -y install epel-release git python3 python3-devel python3-pip gcc openldap openldap-devel openssl-devel
yum -y insatll mariadb-devel
yum -y insatll mysql-devel
sqliteversion=$(sqlite3 -version|awk '{print $1}')
function version_ge() { test "$(echo "$@" | tr " " "\n" | sort -rV | head -n 1)" == "$1"; }
if version_ge $sqliteversion "3.9.0"; then
colorEcho ${BLUE} "Sqlite3 Version ${sqliteversion} Support!"
else
colorEcho ${YELLOW} "Sqlite3 Version ${sqliteversion} Not Support,Install New Version..."
tar -zxvf "$WORK_PATH"/sqlite-autoconf-3340000.tar.gz -C /tmp
cd /tmp/sqlite-autoconf-3340000 || exit \
&& ./configure --prefix=/usr/local \
&& make && make install
mv /usr/bin/sqlite3 /usr/bin/sqlite3_backup
ln -s /usr/local/bin/sqlite3 /usr/bin/sqlite3
echo "/usr/local/lib" > /etc/ld.so.conf.d/sqlite3.conf
ldconfig
sqlite3 -version
fi
else
colorEcho ${RED} "The system package manager tool isn't APT or YUM, please install depend manually."
return 1
fi
return 0
}
installmrdoc(){
MRDOCDIR=/opt/jonnyan404
USER="admin"
MM=$(cat /proc/sys/kernel/random/uuid| cut -f1 -d "-")
mkdir -p ${MRDOCDIR}
touch ${MRDOCDIR}/"${GIT_DIR}"pwdinfo.log
cd ${MRDOCDIR} && git clone ${GIT_LINK}
pip3 install uwsgi
python3 -m venv ${MRDOCDIR}/"${GIT_DIR}"_env
source ${MRDOCDIR}/"${GIT_DIR}"_env/bin/activate \
&& pip3 install --upgrade pip \
&& cd ${MRDOCDIR}/"${GIT_DIR}" \
&& pip3 install -r requirements.txt \
&& python3 manage.py makemigrations \
&& python3 manage.py migrate \
&& if echo "from django.contrib.auth import get_user_model; User = get_user_model(); User.objects.create_superuser('${USER}', 'www@mrdoc.fun', '${MM}')" | python manage.py shell;then printf "$(date) user:%s pwd:%s\n" "$USER" "$MM" >>/opt/jonnyan404/"${GIT_DIR}"pwdinfo.log; fi \
&& deactivate
}
initconfig(){
### Generate uwsgi configuration file ###
if [[ ! -f "/opt/jonnyan404/${GIT_DIR}_uwsgi.ini" ]]; then
cat >"/opt/jonnyan404/${GIT_DIR}_uwsgi.ini"<<EOF
[uwsgi]
# Django-related settings
http = :${mrdocport:-10086}
# the base directory (full path)
chdir = /opt/jonnyan404/${GIT_DIR}
# Django's wsgi file
module = ${GIT_DIR}.wsgi:application
wsgi-file = ${GIT_DIR}/wsgi.py
# the virtualenv (full path)
home = /opt/jonnyan404/${GIT_DIR}_env
logto = /opt/jonnyan404/mrdoc_uwsgi_log.log
# process-related settings
# master
master = true
# maximum number of worker processes
processes = 5
# the socket (use the full path to be safe
socket = /opt/jonnyan404/${GIT_DIR}.sock
# ... with appropriate permissions - may be needed
chmod-socket = 666
# clear environment on exit
vacuum = true
EOF
fi
### Generate nginx configuration file ###
if [[ ! -f "/opt/jonnyan404/${GIT_DIR}_nginx_jonnyan404.conf" ]]; then
cat >"/opt/jonnyan404/${GIT_DIR}_nginx_jonnyan404.conf"<<EOF
server {
# the port your site will be served on
listen 80;
# the domain name it will serve for
server_name _; # substitute your machine's IP address or FQDN
charset utf-8;
access_log /var/log/nginx/${GIT_DIR}-nginx-access.log;
error_log /var/log/nginx/${GIT_DIR}-nginx-error.log;
# max upload size
client_max_body_size 75M; # adjust to taste
# Django media
location /media {
alias /opt/jonnyan404/${GIT_DIR}/media; # your Django project's media files - amend as required
}
location /static {
alias /opt/jonnyan404/${GIT_DIR}/static; # your Django project's static files - amend as required
}
# Finally, send all non-media requests to the Django server.
location / {
uwsgi_pass unix:///opt/jonnyan404/${GIT_DIR}.sock;
include /etc/nginx/uwsgi_params; # the uwsgi_params file you installed
}
}
EOF
fi
### Generate mrdoc sample configuration file ###
if [[ ! -f "/opt/jonnyan404/${GIT_DIR}/config/config_sample.ini" ]]; then
cat >"/opt/jonnyan404/${GIT_DIR}/config/config_sample.ini"<<EOF
[site]
# True表示开启站点调试模式,False表示关闭站点调试模式
debug = False
[database]
# engine,指定数据库类型,接受sqlite、mysql、oracle、postgresql
engine = mysql
# name表示数据库的名称
name = db_name
# user表示数据库用户名
user = db_user
# password表示数据库用户密码
password = db_pwd
# host表示数据库主机地址
host = db_host
# port表示数据库端口
port = db_port
[selenium]
# PDF相关配置项
# 在Windows环境下测试或使用,请配置driver = Chrome,否则不用配置 driver 参数
driver = Chrome
# 如果系统无法正确安装或识别chromedriver,请指定chromedriver在计算机上的绝对路径
driver_path = driver_path
[cors_origin]
allow = http://localhost,capacitor://localhost
# 专业版更多配置请查看文档 "https://doc.mrdoc.pro/doc/3445/"
EOF
fi
### Generate systemd configuration file ###
if [[ -n "${SYSTEMCTL_CMD}" ]];then
cat>"/opt/jonnyan404/${GIT_DIR}fun.service"<<EOF
[Unit]
Description=mrdoc service by jonnyan404
Requires=network.target
After=network.target
After=syslog.target
[Service]
TimeoutStartSec=0
RestartSec=3
Restart=always
KillSignal=SIGQUIT
Type=notify
NotifyAccess=all
StandardError=syslog
RuntimeDirectory=uwsgi
# Main call: Virtual env is activated and uwsgi is started with INI file as argument
ExecStart=/bin/bash -c 'cd /opt/jonnyan404/; source ${GIT_DIR}_env/bin/activate; uwsgi --ini /opt/jonnyan404/${GIT_DIR}_uwsgi.ini'
[Install]
WantedBy=multi-user.target
EOF
else
colorEcho ${RED} "systemd配置生成失败,请确认系统支持并存在systemd服务!"
fi
return
}
start(){
systemctl daemon-reload
if systemctl start "${GIT_DIR}"fun;then
colorEcho ${GREEN} "启动${GIT_DIR}成功"
else
systemctl status "${GIT_DIR}"fun -l
colorEcho ${RED} "启动${GIT_DIR}失败"
fi
}
stop(){
systemctl daemon-reload
if systemctl stop "${GIT_DIR}"fun;then
colorEcho ${GREEN} "停止${GIT_DIR}成功"
else
systemctl status "${GIT_DIR}"fun -l
colorEcho ${RED} "停止${GIT_DIR}失败"
fi
}
status(){
systemctl daemon-reload
systemctl status "${GIT_DIR}"fun -l
}
showlog(){
cat /opt/jonnyan404/mrdoc_uwsgi_log.log
}
restart(){
systemctl daemon-reload
if systemctl restart "${GIT_DIR}"fun;then
colorEcho ${GREEN} "重启${GIT_DIR}成功"
else
systemctl status "${GIT_DIR}"fun -l
colorEcho ${RED} "重启${GIT_DIR}失败"
fi
}
enable(){
systemctl daemon-reload
if systemctl enable "${GIT_DIR}"fun;then
colorEcho ${GREEN} "设置${GIT_DIR}开机自启成功"
else
colorEcho ${RED} "设置${GIT_DIR}开机自启失败"
fi
}
remove(){
echo "执行卸载中(卸载采用软删除,彻底删除请到/tmp目录手动删除!)"
systemctl stop "${GIT_DIR}"fun
cp -rf /opt/jonnyan404 /tmp
rm -rf /opt/jonnyan404
rm -f /etc/systemd/system/"${GIT_DIR}"fun.service
systemctl daemon-reload
colorEcho ${GREEN} "卸载完成!"
}
update(){
MRDOCDIR=/opt/jonnyan404
cd ${MRDOCDIR}/"${GIT_DIR}" && git pull
source ${MRDOCDIR}/"${GIT_DIR}"_env/bin/activate \
&& cd ${MRDOCDIR}/"${GIT_DIR}" \
&& pip3 install -r requirements.txt \
&& python3 manage.py makemigrations \
&& python3 manage.py migrate \
&& deactivate
colorEcho ${YELLOW} "如果此步有报错,请查看官网关于升级的要求(一般是缺少系统依赖)或者群内咨询."
}
changepwd(){
if [[ $GIT_DIR == "MrDocPro" ]] ;then
colorEcho ${BLUE} "###修改专业版密码中...当前用户是:${cuser}###"
else
colorEcho ${BLUE} "###修改开源版密码中...当前用户是:${cuser}###"
fi
MRDOCDIR=/opt/jonnyan404
source ${MRDOCDIR}/"${GIT_DIR}"_env/bin/activate \
&& python3 ${MRDOCDIR}/"${GIT_DIR}"/manage.py changepassword ${cuser}
}
createsu(){
if [[ $GIT_DIR == "MrDocPro" ]] ;then
colorEcho ${BLUE} "###创建 专业版管理员 用户中...###"
else
colorEcho ${BLUE} "###创建 开源版管理员 用户中...###"
fi
MRDOCDIR=/opt/jonnyan404
source ${MRDOCDIR}/"${GIT_DIR}"_env/bin/activate \
&& python3 ${MRDOCDIR}/"${GIT_DIR}"/manage.py createsuperuser
}
via(){
MRDOCDIR=/opt/jonnyan404
cd ${MRDOCDIR}/"${GIT_DIR}"
if source ${MRDOCDIR}/"${GIT_DIR}"_env/bin/activate;then
cd ${MRDOCDIR}/"${GIT_DIR}"
colorEcho ${BLUE} "当前已进入虚拟环境内,如需推出虚拟环境,可直接关闭shell窗口或者当前窗口执行 deactivate 命令."
else
colorEcho ${RED} "进入虚拟环境失败,请检查你的命令是否输入正确."
fi
}
initdb(){
MRDOCDIR=/opt/jonnyan404
USER="admin"
MM=$(cat /proc/sys/kernel/random/uuid| cut -f1 -d "-")
mkdir -p ${MRDOCDIR}
touch ${MRDOCDIR}/"${GIT_DIR}"pwdinfo.log
source ${MRDOCDIR}/"${GIT_DIR}"_env/bin/activate \
&& pip3 install --upgrade pip \
&& cd ${MRDOCDIR}/"${GIT_DIR}" \
&& python3 manage.py makemigrations \
&& python3 manage.py migrate \
&& if echo "from django.contrib.auth import get_user_model; User = get_user_model(); User.objects.create_superuser('${USER}', 'www@mrdoc.fun', '${MM}')" | python manage.py shell;then printf "$(date) user:%s pwd:%s\n" "$USER" "$MM" >>/opt/jonnyan404/"${GIT_DIR}"pwdinfo.log; fi \
&& deactivate
colorEcho ${YELLOW} "如果此步有报错,一般是数据库版本和数据库配置问题."
colorEcho ${GREEN} "执行完毕,当前账号密码为:${USER}和${MM}"
}
Help(){
colorEcho ${BLUE} "如需修改 mrdoc 配置文件,请到 /opt/jonnyan404/ 目录下"
echo "-------"
echo "./mrdoc.sh [-h] [-i link] [-start pro] [-stop pro] [-status pro] [-restart pro] [-u pro] [-c] [--remove pro] [-v] [--changepwd user pro] [--createsu pro] [--initdb pro]"
echo " -h, --help Show help | 展示帮助选项"
echo " -i, --install To install mrdoc | 安装 mrdoc"
echo " -start, --start Start mrdoc | 启动 mrdoc"
echo " -stop, --stop Stop mrdoc | 停止 mrdoc"
echo " -status, --status mrdoc status | 查看 mrdoc 当前运行状态"
echo " -showlog, --showlog Show uwsgi log | 查看 uwsgi 日志"
echo " -restart, --restart Restart mrdoc | 重启 mrdoc"
echo " -u, --update Update mrdoc version | 更新 mrdoc 源码"
echo " --via Enter the virtual environment. | 进入 mrdoc 虚拟环境"
echo " --remove Remove installed mrdoc | 卸载 mrdoc"
echo " -c, --check Check for update | 检查mrdoc安装脚本是否可更新"
echo " -v, --version Look script version | 查看脚本版本号"
echo " --changepwd Changepassword | 修改用户密码"
echo " --createsu Createsuperuser | 创建新的管理员用户"
echo " --initdb Initialize database | 初始化数据库,更换数据库时需要执行."
return 0
}
main(){
if installdepend ;then
if [[ $(echo $GIT_LINK | grep "git.mrdoc.pro/MrDoc/MrDocPro") != "" ]];then
GIT_DIR=$(echo $GIT_LINK|grep -e "MrDocPro" -o)
echo $GIT_DIR
colorEcho ${BLUE} "###安装 mrdoc 专业版中...###"
else
GIT_LINK="https://gitee.com/zmister/MrDoc.git"
GIT_DIR=$(echo $GIT_LINK|grep -e "MrDoc" -o)
colorEcho ${BLUE} "###安装 mrdoc 开源版中...###"
fi
installmrdoc
colorEcho ${BLUE} "###初始化配置中...###"
initconfig
ln -sf /opt/jonnyan404/"${GIT_DIR}"fun.service /etc/systemd/system/"${GIT_DIR}"fun.service
colorEcho ${BLUE} "###写入环境变量...###"
chmod a+x "${WORK_PATH}"/mrdoc.sh
ln -sf "${WORK_PATH}"/mrdoc.sh /bin/mrdoc
colorEcho ${BLUE} "###启动mrdoc...###"
if start;then
# systemctl status "${GIT_DIR}"fun -l
colorEcho ${GREEN} "$(cat /opt/jonnyan404/${GIT_DIR}pwdinfo.log),Password is saved in /opt/jonnyan404/${GIT_DIR}pwdinfo.log"
colorEcho ${GREEN} "如果上方没显示账号密码,就是部署失败,请反馈至https://gitee.com/jonnyan404/oh-my-mrdoc/issues"
else
colorEcho ${RED} "部署失败,请反馈至https://gitee.com/jonnyan404/oh-my-mrdoc/issues"
systemctl status "${GIT_DIR}"fun -l
fi
fi
}
#########################
while [[ $# -gt 0 ]];do
key="$1"
case $key in
-h|--help)
Help
;;
-i|--install)
GIT_LINK=$2
main
shift # past argument
;;
-start|--start)
if [[ "$2" == "pro" ]] ;then
GIT_DIR=MrDocPro
shift
else
GIT_DIR=MrDoc
fi
start
;;
-stop|--stop)
if [[ "$2" == "pro" ]] ;then
GIT_DIR=MrDocPro
shift
else
GIT_DIR=MrDoc
fi
stop
;;
-status|--status)
if [[ "$2" == "pro" ]] ;then
GIT_DIR=MrDocPro
shift
else
GIT_DIR=MrDoc
fi
status
;;
-showlog|--showlog)
showlog
;;
-restart|--restart)
if [[ "$2" == "pro" ]] ;then
GIT_DIR=MrDocPro
shift
else
GIT_DIR=MrDoc
fi
restart
;;
--enable)
if [[ "$2" == "pro" ]] ;then
GIT_DIR=MrDocPro
shift
else
GIT_DIR=MrDoc
fi
enable
;;
-v|--version)
echo "当前版本号:${SCR_VERSION}"
#shift
;;
-u|--update)
if [[ "$2" == "pro" ]] ;then
GIT_DIR=MrDocPro
shift
else
GIT_DIR=MrDoc
fi
update
;;
--via)
if [[ "$2" == "pro" ]] ;then
GIT_DIR=MrDocPro
shift
else
GIT_DIR=MrDoc
fi
via
;;
--remove)
if [[ "$2" == "pro" ]] ;then
GIT_DIR=MrDocPro
shift
else
GIT_DIR=MrDoc
fi
remove
;;
-c|--check)
colorEcho ${YELLOW} "暂未实现"
;;
--changepwd)
if [[ "$2" != "" ]] ;then
cuser=$2
if [[ "$3" == "pro" ]] ;then
GIT_DIR=MrDocPro
shift
else
GIT_DIR=MrDoc
fi
changepwd
shift
else
colorEcho ${RED} "请指定用户名..."
fi
;;
--createsu)
if [[ "$2" == "pro" ]] ;then
GIT_DIR=MrDocPro
shift
else
GIT_DIR=MrDoc
fi
createsu
;;
--initdb)
if [[ "$2" == "pro" ]] ;then
GIT_DIR=MrDocPro
shift
else
GIT_DIR=MrDoc
fi
initdb
;;
*)
colorEcho ${RED} "指令错误,请重新输入..." # unknown option
;;
esac
shift # past argument or value
done
###############################