From 42b25be20448c62747c212816b6aefbd79d24cce Mon Sep 17 00:00:00 2001 From: midoks Date: Tue, 23 Nov 2021 05:03:55 +0800 Subject: [PATCH] mongodb --- class/core/mw.py | 6 +- plugins/mongodb/config/mongodb.conf | 12 ++ plugins/mongodb/ico.png | Bin 0 -> 642 bytes plugins/mongodb/index.html | 44 ++++++ plugins/mongodb/index.py | 225 ++++++++++++++++++++++++++++ plugins/mongodb/info.json | 18 +++ plugins/mongodb/init.d/mongodb.tpl | 70 +++++++++ plugins/mongodb/install.sh | 73 +++++++++ 8 files changed, 445 insertions(+), 3 deletions(-) create mode 100644 plugins/mongodb/config/mongodb.conf create mode 100644 plugins/mongodb/ico.png create mode 100755 plugins/mongodb/index.html create mode 100755 plugins/mongodb/index.py create mode 100755 plugins/mongodb/info.json create mode 100644 plugins/mongodb/init.d/mongodb.tpl create mode 100755 plugins/mongodb/install.sh diff --git a/class/core/mw.py b/class/core/mw.py index 880fadc9d..389966ebb 100755 --- a/class/core/mw.py +++ b/class/core/mw.py @@ -482,7 +482,7 @@ def getLastLine(inputfile, lineNum): def getNumLines(path, num, p=1): pyVersion = sys.version_info[0] try: - import cgi + import html if not os.path.exists(path): return "" start_line = (p - 1) * num @@ -503,8 +503,8 @@ def getNumLines(path, num, p=1): if n >= start_line: line = buf[newline_pos + 1:] try: - data.insert(0, cgi.escape(line)) - except: + data.insert(0, html.escape(line)) + except Exception as e: pass buf = buf[:newline_pos] n += 1 diff --git a/plugins/mongodb/config/mongodb.conf b/plugins/mongodb/config/mongodb.conf new file mode 100644 index 000000000..018376398 --- /dev/null +++ b/plugins/mongodb/config/mongodb.conf @@ -0,0 +1,12 @@ +dbpath = {$SERVER_PATH}/mongodb/data +logpath = {$SERVER_PATH}/mongodb/logs/mongodb.log +logappend = true +bind_ip = 127.0.0.1 +port = 27017 +fork = true +auth = false + +# 5.0.2 - Master/slave replication is no longer supported +#master = true + +pidfilepath = {$SERVER_PATH}/mongodb/mongodb.pid diff --git a/plugins/mongodb/ico.png b/plugins/mongodb/ico.png new file mode 100644 index 0000000000000000000000000000000000000000..2b8a9f6ac83c6cbf8a64a8ee11e1277b48193ce6 GIT binary patch literal 642 zcmV-|0)737P)Px%JV``BR9Hvt*Nu@4Q4j^-vk@QyHUdO|2*_Z`Kn93_3=jb#SRz0KhyW2#U)61O z&Ca};@#d+gN>@$I`{~<#?wQlwcTREyc5(z5@=t-do#a-M8%ZvA_nydaNj@a0``aY% zmN31G35m~=+;71I?UP8>TMrJ{0{$Y&%Ouv1+87AIUL`qA@;J%sw!0;p8v2NJuf=(v zV5 zupT9Ov!^ERHchCMRUm)^LS+yZQI%FarY}u5JyYoR%-vnq-)zFhg7qHtVkU-;0=g;z z)n6YxtB;K_AYGCU%W^LeTWMn!L}|eNQ0*KD>^$jN>XUK!oAxXBQG-C2mQ7&hg$hBV zA-0vxG6BT1b^!r^@2Ueh=DWG#e_#RC2du4b`>^?1qoSU7J6HrlaOSRG5Uj+KnV^7~ zmi`yq!1Ej5^VTEuhhov^r)xp*x3C%y5O!wwc4~KiV>+vy0pa%o0W=??*Lii%vPDc3 z3wFx{Gl0;IT8TBN9)x5yB>}YwfU5OdzZgX4yXvFF1|K#&7R+enV>Zjgs>}oDYS-23 zr(%tQDCg1Ex(}*u?QGlbL0s8md{5z}qpwS+C^`w~5QmzowBLYE9dwl;K@r$ihZ>bc ztix0K&S_bP8eOo&R%eB!hZ-@Q(&|vNyG&+;S4!_+el88xd>?uFnS9si*B;M1& literal 0 HcmV?d00001 diff --git a/plugins/mongodb/index.html b/plugins/mongodb/index.html new file mode 100755 index 000000000..7f148ee1f --- /dev/null +++ b/plugins/mongodb/index.html @@ -0,0 +1,44 @@ +
+
+
+

服务

+

自启动

+

配置修改

+

负载状态

+

日志

+
+
+
+
+
+
+ \ No newline at end of file diff --git a/plugins/mongodb/index.py b/plugins/mongodb/index.py new file mode 100755 index 000000000..f4727707e --- /dev/null +++ b/plugins/mongodb/index.py @@ -0,0 +1,225 @@ +# coding:utf-8 + +import sys +import io +import os +import time + +sys.path.append(os.getcwd() + "/class/core") +import mw + +app_debug = False +if mw.isAppleSystem(): + app_debug = True + + +def getPluginName(): + return 'mongodb' + + +def getPluginDir(): + return mw.getPluginDir() + '/' + getPluginName() + + +def getServerDir(): + return mw.getServerDir() + '/' + getPluginName() + + +def getInitDFile(): + if app_debug: + return '/tmp/' + getPluginName() + return '/etc/init.d/' + getPluginName() + + +def getConf(): + if mw.isAppleSystem(): + path = getServerDir() + "/mongodb.conf" + return path + return "/etc/mongod.conf" + + +def getConfTpl(): + path = getPluginDir() + "/config/mongodb.conf" + return path + + +def getInitDTpl(): + path = getPluginDir() + "/init.d/" + getPluginName() + ".tpl" + return path + + +def getArgs(): + args = sys.argv[2:] + tmp = {} + args_len = len(args) + + if args_len == 1: + t = args[0].strip('{').strip('}') + t = t.split(':') + tmp[t[0]] = t[1] + elif args_len > 1: + for i in range(len(args)): + t = args[i].split(':') + tmp[t[0]] = t[1] + + return tmp + + +def status(): + data = mw.execShell( + "ps -ef|grep mongod |grep -v grep | grep -v python | grep -v mdserver-web | awk '{print $2}'") + + if data[0] == '': + return 'stop' + return 'start' + + +def initDreplace(): + + file_tpl = getInitDTpl() + service_path = os.path.dirname(os.getcwd()) + + initD_path = getServerDir() + '/init.d' + if not os.path.exists(initD_path): + os.mkdir(initD_path) + file_bin = initD_path + '/' + getPluginName() + + # initd replace + content = mw.readFile(file_tpl) + content = content.replace('{$SERVER_PATH}', service_path) + mw.writeFile(file_bin, content) + mw.execShell('chmod +x ' + file_bin) + + # config replace + conf_content = mw.readFile(getConfTpl()) + conf_content = conf_content.replace('{$SERVER_PATH}', service_path) + mw.writeFile(getServerDir() + '/mongodb.conf', conf_content) + + return file_bin + + +def start(): + if mw.isAppleSystem(): + file = initDreplace() + data = mw.execShell(file + ' start') + if data[1] == '': + return 'ok' + return 'fail' + + data = mw.execShell('systemctl start mongod') + if data[1] == '': + return 'ok' + return 'fail' + + +def stop(): + if mw.isAppleSystem(): + file = initDreplace() + data = mw.execShell(file + ' stop') + if data[1] == '': + return 'ok' + return 'fail' + + data = mw.execShell('systemctl stop mongod') + if data[1] == '': + return 'ok' + return 'fail' + + +def reload(): + if mw.isAppleSystem(): + file = initDreplace() + data = mw.execShell(file + ' reload') + if data[1] == '': + return 'ok' + return 'fail' + + data = mw.execShell('systemctl reload mongod') + if data[1] == '': + return 'ok' + return 'fail' + + +def runInfo(): + import pymongo + client = pymongo.MongoClient(host='127.0.0.1', port=27017) + db = client.admin + serverStatus = db.command('serverStatus') + # print(serverStatus) + # for key, value in serverStatus.items(): + # print(key, value) + result = {} + result["version"] = serverStatus['version'] + result["uptime"] = serverStatus['uptime'] + result["connections"] = serverStatus['connections']['current'] + result["collections"] = serverStatus['catalogStats']['collections'] + + return mw.getJson(result) + + +def initdStatus(): + if not app_debug: + if mw.isAppleSystem(): + return "Apple Computer does not support" + initd_bin = getInitDFile() + if os.path.exists(initd_bin): + return 'ok' + return 'fail' + + +def initdInstall(): + import shutil + if not app_debug: + if mw.isAppleSystem(): + return "Apple Computer does not support" + + source_bin = initDreplace() + initd_bin = getInitDFile() + shutil.copyfile(source_bin, initd_bin) + mw.execShell('chmod +x ' + initd_bin) + mw.execShell('chkconfig --add ' + getPluginName()) + return 'ok' + + +def initdUinstall(): + if not app_debug: + if mw.isAppleSystem(): + return "Apple Computer does not support" + + mw.execShell('chkconfig --del ' + getPluginName()) + initd_bin = getInitDFile() + os.remove(initd_bin) + return 'ok' + + +def runLog(): + if mw.isAppleSystem(): + return getServerDir() + '/logs/mongodb.log' + return "/var/log/mongodb/mongod.log" + +if __name__ == "__main__": + func = sys.argv[1] + if func == 'status': + print(status()) + elif func == 'start': + print(start()) + elif func == 'stop': + print(stop()) + elif func == 'restart': + print(restart()) + elif func == 'reload': + print(reload()) + elif func == 'initd_status': + print(initdStatus()) + elif func == 'initd_install': + print(initdInstall()) + elif func == 'initd_uninstall': + print(initdUinstall()) + elif func == 'run_info': + print(runInfo()) + elif func == 'conf': + print(getConf()) + elif func == 'run_log': + print(runLog()) + else: + print('error') diff --git a/plugins/mongodb/info.json b/plugins/mongodb/info.json new file mode 100755 index 000000000..1a41cbf19 --- /dev/null +++ b/plugins/mongodb/info.json @@ -0,0 +1,18 @@ +{ + "sort": 7, + "ps": "一个高性能的NOSQL数据库", + "name": "mongodb", + "title": "Mongodb", + "shell": "install.sh", + "versions":["5.0.4"], + "updates":["5.0.4"], + "tip": "soft", + "checks": "server/mongodb", + "path": "server/mongodb", + "display": 1, + "author": "mongodb", + "date": "2021-11-23", + "home": "https://www.mongodb.com/", + "type": 0, + "pid": "2" +} \ No newline at end of file diff --git a/plugins/mongodb/init.d/mongodb.tpl b/plugins/mongodb/init.d/mongodb.tpl new file mode 100644 index 000000000..2781240b5 --- /dev/null +++ b/plugins/mongodb/init.d/mongodb.tpl @@ -0,0 +1,70 @@ +#!/bin/sh +# chkconfig: 2345 55 25 +# description: Mongodb Service + +### BEGIN INIT INFO +# Provides: Mongodb +# Required-Start: $all +# Required-Stop: $all +# Default-Start: 2 3 4 5 +# Default-Stop: 0 1 6 +# Short-Description: starts Mongodb +# Description: starts the MDW-Web +### END INIT INFO + +# Simple Mongodb init.d script conceived to work on Linux systems +# as it does use of the /proc filesystem. + +CONF="{$SERVER_PATH}/mongodb/mongodb.conf" +EXEC={$SERVER_PATH}/mongodb/bin/mongod + +PIDFILE={$SERVER_PATH}/mongodb/mongodb.pid + +mkdir -p {$SERVER_PATH}/mongodb/data +mkdir -p {$SERVER_PATH}/mongodb/logs + +app_start(){ + if [ -f $PIDFILE ];then + kill -9 `cat $PIDFILE` + fi + + echo "Starting mongodb server..." + echo $EXEC -f $CONF + $EXEC -f $CONF >> {$SERVER_PATH}/mongodb/logs.pl 2>&1 & +} +app_stop(){ + if [ ! -f $PIDFILE ] + then + echo "$PIDFILE does not exist, process is not running" + else + PID=$(cat $PIDFILE) + echo "Stopping ..." + $CLIEXEC shutdown + while [ -x /proc/${PID} ] + do + echo "Waiting for Redis to shutdown ..." + sleep 1 + done + echo "Redis stopped" + rm -rf $PIDFILE + fi +} + + +case "$1" in + start) + app_start + ;; + stop) + app_stop + ;; + restart|reload) + app_stop + sleep 0.3 + app_start + ;; + *) + echo "Please use start or stop as first argument" + ;; +esac + diff --git a/plugins/mongodb/install.sh b/plugins/mongodb/install.sh new file mode 100755 index 000000000..f526bbdf1 --- /dev/null +++ b/plugins/mongodb/install.sh @@ -0,0 +1,73 @@ +#!/bin/bash +PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin +export PATH + +# cd /Users/midoks/Desktop/mwdev/server/mdserver-web/plugins/mongodb && /bin/bash install.sh install 5.0.4 +# cd /www/server/mdserver-web/plugins/mongodb && /bin/bash install.sh install 5.0.4 + +curPath=`pwd` +rootPath=$(dirname "$curPath") +rootPath=$(dirname "$rootPath") +serverPath=$(dirname "$rootPath") + +install_tmp=${rootPath}/tmp/mw_install.pl +VERSION=$2 +sysName=`uname` + +Install_app_mac() +{ + + if [ ! -f $serverPath/source/mongodb-macos-x86_64-${VERSION}.tgz ];then + wget -O $serverPath/source/mongodb-macos-x86_64-${VERSION}.tgz https://fastdl.mongodb.org/osx/mongodb-macos-x86_64-${VERSION}.tgz + fi + + cd $serverPath/source && tar -zxvf mongodb-macos-x86_64-${VERSION}.tgz + mkdir -p $serverPath/mongodb + cd mongodb-macos-x86_64-${VERSION} && mv ./* $serverPath/mongodb +} + + +# https://repo.mongodb.org/yum/redhat/7/mongodb-org/5.0/x86_64/RPMS/mongodb-org-server-5.0.4-1.el7.x86_64.rpm +Install_app_linux(){ + if [ ! -f $serverPath/source/mongodb-org-server-${VERSION}-1.el7.x86_64.rpm ];then + wget -O $serverPath/source/mongodb-org-server-${VERSION}-1.el7.x86_64.rpm https://repo.mongodb.org/yum/redhat/7/mongodb-org/5.0/x86_64/RPMS/mongodb-org-server-${VERSION}-1.el7.x86_64.rpm + fi + + rpm -ivh $serverPath/source/mongodb-org-server-${VERSION}-1.el7.x86_64.rpm +} + + +Install_app() +{ + pip3 install pymongo + + echo "sys:$sysName" + echo '正在安装脚本文件...' > $install_tmp + mkdir -p $serverPath/source + # echo $sysName + if [ "Darwin" == "$sysName" ];then + Install_app_mac + else + Install_app_linux + fi + + # cd $serverPath/source && tar -zxvf mongodb-${VERSION}.tar.gz + + + + echo "${VERSION}" > $serverPath/mongodb/version.pl + echo '安装完成' > $install_tmp +} + +Uninstall_app() +{ + rm -rf $serverPath/mongodb + echo "Uninstall_mongodb" > $install_tmp +} + +action=$1 +if [ "${1}" == 'install' ];then + Install_app +else + Uninstall_app +fi