Simple Linux Panel
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
mdserver-web/plugins/data_query/nosql_mongodb.py

147 lines
3.4 KiB

# coding:utf-8
import sys
import io
import os
import time
import re
1 year ago
import pymongo
sys.path.append(os.getcwd() + "/class/core")
import mw
1 year ago
def singleton(cls):
_instance = {}
def inner():
if cls not in _instance:
_instance[cls] = cls()
return _instance[cls]
return inner
@singleton
class nosqlMongodb():
__DB_PASS = None
__DB_USER = None
__DB_PORT = 6379
__DB_HOST = '127.0.0.1'
__DB_CONN = None
__DB_ERR = None
__DB_LOCAL = None
def __init__(self):
self.__config = self.get_options(None)
1 year ago
def mgdb_conn(self):
1 year ago
if self.__DB_HOST in ['127.0.0.1', 'localhost']:
1 year ago
mgdb_path = "{}/mongodb".format(mw.getServerDir())
if not os.path.exists(mgdb_path): return False
1 year ago
if not self.__DB_LOCAL:
self.__DB_PORT = int(self.__config['port'])
# print(self.__DB_HOST,self.__DB_PORT, self.__DB_PASS)
try:
1 year ago
self.__DB_CONN = pymongo.MongoClient(host=self.__DB_HOST, port=self.__DB_PORT, maxPoolSize=10)
self.__DB_CONN.admin.command('ping')
1 year ago
return self.__DB_CONN
1 year ago
except pymongo.errors.ConnectionFailure:
1 year ago
return False
except Exception:
1 year ago
self.__DB_ERR = mw.get_error_info()
1 year ago
return False
# 获取配置项
def get_options(self, get=None):
result = {}
1 year ago
mgdb_content = mw.readFile("{}/mongodb/mongodb.conf".format(mw.getServerDir()))
if not mgdb_content: return False
keys = ["bind_ip", "port"]
result['host'] = '127.0.0.1'
rep = 'port\s*=\s*(.*)'
ip_re = re.search(rep, mgdb_content)
if ip_re:
result['port'] = int(ip_re.groups()[0].strip())
else:
result['port'] = 27017
1 year ago
return result
def set_host(self, host, port, name, username, password, prefix=''):
self.__DB_HOST = host
self.__DB_PORT = int(port)
self.__DB_NAME = name
if self.__DB_NAME: self.__DB_NAME = str(self.__DB_NAME)
self.__DB_USER = str(username)
self._USER = str(username)
self.__DB_PASS = str(password)
self.__DB_PREFIX = prefix
self.__DB_LOCAL = 1
return self
@singleton
class nosqlMongodbCtr():
def __init__(self):
pass
def getInstanceBySid(self, sid = 0):
1 year ago
instance = nosqlMongodb()
1 year ago
return instance
1 year ago
def getDbList(self, args):
1 year ago
sid = args['sid']
1 year ago
mgdb_instance = self.getInstanceBySid(sid).mgdb_conn()
if mgdb_instance is False:
1 year ago
return mw.returnData(False,'无法链接')
1 year ago
result = {}
result["dbs"] = mgdb_instance.list_database_names()
1 year ago
return mw.returnData(True,'ok', result)
# ---------------------------------- run ----------------------------------
# 获取 mongodb databases 列表
1 year ago
def get_db_list(args):
1 year ago
t = nosqlMongodbCtr()
1 year ago
return t.getDbList(args)
1 year ago
# 获取 redis key 列表
def get_dbkey_list(args):
t = nosqlMongodbCtr()
return t.getDbKeyList(args)
1 year ago
def set_kv(args):
t = nosqlMongodbCtr()
return t.setKv(args)
1 year ago
def del_val(args):
t = nosqlMongodbCtr()
return t.delVal(args)
def batch_del_val(args):
t = nosqlMongodbCtr()
return t.batchDelVal(args)
def clear_flushdb(args):
t = nosqlMongodbCtr()
return t.clearFlushDB(args)
1 year ago
# 测试
def test(args):
sid = args['sid']
t = nosqlMongodbCtr()
print(t.get_options())
print("test")
return 'ok'