pull/153/head
midoks 3 years ago
parent a42f15dae4
commit 5b441d5cb4
  1. 88
      plugins/postgresql/class/pg.py
  2. 4
      plugins/postgresql/conf/pgsql.sql
  3. 11
      plugins/postgresql/conf/postgresql.conf
  4. 2
      plugins/postgresql/index.html
  5. 131
      plugins/postgresql/index.py
  6. 6
      plugins/postgresql/init.d/postgresql.tpl
  7. 17
      plugins/postgresql/install.sh

@ -0,0 +1,88 @@
# coding: utf-8
import re
import os
import sys
import psycopg2
class ORM:
__DB_PASS = None
__DB_USER = 'root'
__DB_PORT = 5432
__DB_HOST = 'localhost'
__DB_CONN = None
__DB_CUR = None
__DB_ERR = None
__DB_CNF = '/etc/my.cnf'
__DB_SOCKET = '/www/server/postgresql/mysql.sock'
__DB_CHARSET = "utf8"
def __Conn(self):
'''连接数据库'''
try:
try:
self.__DB_CONN = psycopg2.connect(database='postgres', user=self.__DB_USER, password=self.__DB_PASS,
host=self.__DB_HOST, port=int(self.__DB_PORT))
except Exception as e:
self.__DB_HOST = '127.0.0.1'
self.__DB_CONN = psycopg2.connect(database='postgres', user=self.__DB_USER, password=self.__DB_PASS,
host=self.__DB_HOST, port=int(self.__DB_PORT))
self.__DB_CUR = self.__DB_CONN.cursor()
return True
except Exception as e:
self.__DB_ERR = e
return False
def setDbConf(self, conf):
self.__DB_CNF = conf
def setSocket(self, sock):
self.__DB_SOCKET = sock
def setCharset(self, charset):
self.__DB_CHARSET = charset
def setPort(self, port):
self.__DB_PORT = port
def setPwd(self, pwd):
self.__DB_PASS = pwd
def getPwd(self):
return self.__DB_PASS
def execute(self, sql):
# 执行SQL语句返回受影响行
if not self.__Conn():
return self.__DB_ERR
try:
result = self.__DB_CUR.execute(sql)
self.__DB_CONN.commit()
self.__Close()
return result
except Exception as ex:
return ex
def query(self, sql):
# 执行SQL语句返回数据集
if not self.__Conn():
return self.__DB_ERR
try:
self.__DB_CUR.execute(sql)
result = self.__DB_CUR.fetchall()
# print(result)
# 将元组转换成列表
# data = map(list, result)
self.__Close()
return result
except Exception as ex:
return ex
def __Close(self):
# 关闭连接
self.__DB_CUR.close()
self.__DB_CONN.close()

@ -1,9 +1,9 @@
CREATE TABLE IF NOT EXISTS `config` (
`id` INTEGER PRIMARY KEY AUTOINCREMENT,
`mysql_root` TEXT
`pg_root` TEXT
);
INSERT INTO `config` (`id`, `mysql_root`) VALUES (1, 'admin');
INSERT INTO `config` (`id`, `pg_root`) VALUES (1, 'admin');
CREATE TABLE IF NOT EXISTS `databases` (
`id` INTEGER PRIMARY KEY AUTOINCREMENT,

@ -0,0 +1,11 @@
port = 5432
listen_addresses='*'
unix_socket_directories='{$APP_PATH}/tmp'
max_wal_size = 1GB
min_wal_size = 80MB
log_timezone = 'Asia/Shanghai'
datestyle = 'iso, ymd'
timezone = 'Asia/Shanghai'
default_text_search_config = 'pg_catalog.simple'

@ -5,7 +5,7 @@
<!-- <script type="text/javascript">console.log($('.plugin_version').attr('version'));</script> -->
<p class="bgw" onclick="pluginService('postgresql', $('.plugin_version').attr('version'));">服务</p>
<p onclick="pluginInitD('postgresql', $('.plugin_version').attr('version'));">自启动</p>
<p onclick="pluginConfig('postgresql', $('.plugin_version').attr('version'));">配置文件</p>
<p onclick="pluginConfigTpl('postgresql');">配置文件</p>
<!--<p onclick="myDbPos();">存储位置</p> -->
<p onclick="pgPort();">端口</p>
<!-- <p onclick="runInfo();">当前状态</p>

@ -76,10 +76,31 @@ def getConf():
return path
def configTpl():
clist = []
app_dir = getServerDir()
clist.append(app_dir + "/data/postgresql.conf")
clist.append(app_dir + "/data/pg_hba.conf")
return mw.getJson(clist)
def readConfigTpl():
args = getArgs()
data = checkArgs(args, ['file'])
if not data[0]:
return data[1]
content = mw.readFile(args['file'])
return mw.returnJson(True, 'ok', content)
def getDbPort():
file = getConf()
content = mw.readFile(file)
rep = 'port\s*=\s*(.*)'
rep = 'port\s*=\s*(\d*)?'
tmp = re.search(rep, content)
return tmp.groups()[0].strip()
@ -127,37 +148,38 @@ def pSqliteDb(dbname='databases'):
return conn
def pMysqlDb():
# pymysql
db = mw.getMyORM()
# MySQLdb |
# db = mw.getMyORMDb()
def pgDb():
sys.path.append(getPluginDir() + "/class")
import pg
db = pg.ORM()
db.setPort(getDbPort())
db.setSocket(getSocketFile())
# db.setCharset("utf8")
db.setPwd(pSqliteDb('config').where('id=?', (1,)).getField('mysql_root'))
# db.setSocket(getSocketFile())
db.setPwd(pSqliteDb('config').where('id=?', (1,)).getField('pg_root'))
return db
def initDreplace(version=''):
conf_dir = getServerDir() + '/etc'
log_dir = getServerDir() + "/logs"
conf_dir = getServerDir()
log_dir = conf_dir + "/logs"
conf_list = [
conf_dir,
log_dir
]
for c in conf_list:
if not os.path.exists(c):
os.mkdir(c)
# my_conf = conf_dir + '/my.cnf'
# if not os.path.exists(my_conf):
# tpl = getPluginDir() + '/conf/my.cnf'
# content = mw.readFile(tpl)
# content = contentReplace(content)
# mw.writeFile(my_conf, content)
init_pl = conf_dir + "/init.pl"
if not os.path.exists(init_pl):
# mw.writeFile(init_pl, 'ok')
pg_conf = conf_dir + '/data/postgresql.conf'
tpl = getPluginDir() + '/conf/postgresql.conf'
content = mw.readFile(tpl)
content = contentReplace(content)
mw.writeFile(pg_conf, content)
# systemd
system_dir = mw.systemdCfgDir()
@ -179,8 +201,8 @@ def initDreplace(version=''):
file_bin = initd_path + '/' + getPluginName()
if not os.path.exists(file_bin):
initd_tpl = getInitdTpl(version)
content = mw.readFile(initd_tpl)
tpl = getInitdTpl(version)
content = mw.readFile(tpl)
content = contentReplace(content)
mw.writeFile(file_bin, content)
mw.execShell('chmod +x ' + file_bin)
@ -250,7 +272,11 @@ def pGetDbUser():
def initPgData():
serverdir = getServerDir()
if not os.path.exists(serverdir + '/data'):
cmd = 'cd ' + serverdir + ' && ./bin/initdb -D ' + serverdir + "/data"
cmd = serverdir + '/bin/initdb -D ' + serverdir + "/data"
if not mw.isAppleSystem():
cmd = "echo \"" + serverdir + "/bin/initdb -D " + \
serverdir + "/data\" | su - postgresql"
# print(cmd)
mw.execShell(cmd)
return False
return True
@ -264,14 +290,14 @@ def initPgPwd():
cmd_pass = "echo \"create user root with superuser password '" + pwd + "'\" | "
cmd_pass = cmd_pass + serverdir + '/bin/psql -d postgres'
data = mw.execShell(cmd_pass)
print(cmd_pass)
print(data)
# print(cmd_pass)
# print(data)
pSqliteDb('config').where('id=?', (1,)).save('pg_root', (pwd,))
return True
def myOp(version, method):
def pgOp(version, method):
# import commands
init_file = initDreplace()
cmd = init_file + ' ' + method
@ -311,7 +337,7 @@ def myOp(version, method):
def appCMD(version, action):
return myOp(version, action)
return pgOp(version, action)
def start(version=''):
@ -472,9 +498,9 @@ def myDbStatus():
result = {}
db = pMysqlDb()
data = db.query('show variables')
isError = isSqlError(data)
if isError != None:
return isError
# isError = isSqlError(data)
# if isError != None:
# return isError
gets = ['table_open_cache', 'thread_cache_size', 'key_buffer_size', 'tmp_table_size', 'max_heap_table_size', 'innodb_buffer_pool_size',
'innodb_additional_mem_pool_size', 'innodb_log_buffer_size', 'max_connections', 'sort_buffer_size', 'read_buffer_size', 'read_rnd_buffer_size', 'join_buffer_size', 'thread_stack', 'binlog_cache_size']
@ -672,6 +698,49 @@ def getDbList():
return mw.getJson(data)
def syncGetDatabases():
pdb = pgDb()
psdb = pSqliteDb('databases')
data = pdb.query('show databases')
print(data)
return
# isError = isSqlError(data)
# if isError != None:
# return isError
users = pdb.query(
"select User,Host from mysql.user where User!='root' AND Host!='localhost' AND Host!=''")
nameArr = ['information_schema', 'performance_schema', 'mysql', 'sys']
n = 0
# print(users)
for value in data:
vdb_name = value["Database"]
b = False
for key in nameArr:
if vdb_name == key:
b = True
break
if b:
continue
if psdb.where("name=?", (vdb_name,)).count() > 0:
continue
host = '127.0.0.1'
for user in users:
if vdb_name == user["User"]:
host = user["Host"]
break
ps = mw.getMsg('INPUT_PS')
if vdb_name == 'test':
ps = mw.getMsg('DATABASE_TEST')
addTime = time.strftime('%Y-%m-%d %X', time.localtime())
if psdb.add('name,username,password,accept,ps,addtime', (vdb_name, vdb_name, '', host, ps, addTime)):
n += 1
msg = mw.getInfo('本次共从服务器获取了{1}个数据库!', (str(n),))
return mw.returnJson(True, msg)
def installPreInspection(version):
return 'ok'
@ -710,6 +779,10 @@ if __name__ == "__main__":
print(uninstallPreInspection(version))
elif func == 'conf':
print(getConf())
elif func == 'config_tpl':
print(configTpl())
elif func == 'read_config_tpl':
print(readConfigTpl())
elif func == 'run_info':
print(runInfo())
elif func == 'run_log':
@ -720,5 +793,7 @@ if __name__ == "__main__":
print(setPgPort())
elif func == 'get_db_list':
print(getDbList())
elif func == 'sync_get_databases':
print(syncGetDatabases())
else:
print('error')

@ -36,10 +36,9 @@ pg_stop()
}
pg_status()
{
isStart=$(ps aux |grep 'postgres'|grep -v grep|awk '{print $2}')
isStart=$(ps aux | grep 'postgres'| grep -v grep | grep -v 'postgresql status' | awk '{print $2}')
if [ "$isStart" != '' ];then
echo -e "\033[32mPostgreSQL (pid $isStart) already running\033[0m"
else
@ -50,6 +49,7 @@ pg_status()
pg_reload()
{
echo '' > {$APP_PATH}/logs/server.log
{$APP_PATH}/bin/pg_ctl -D {$APP_PATH}/data -l {$APP_PATH}/logs/server.log reload
}
@ -62,4 +62,6 @@ case "$1" in
'restart')
pg_stop
pg_start;;
'status')
pg_status;;
esac

@ -13,6 +13,13 @@ install_tmp=${rootPath}/tmp/mw_install.pl
action=$1
type=$2
pip install psycopg2
if [ -f ${rootPath}/bin/activate ];then
source ${rootPath}/bin/activate
pip install psycopg2
fi
if [ "${2}" == "" ];then
echo '缺少安装脚本...' > $install_tmp
exit 0
@ -35,8 +42,8 @@ fi
sh -x $curPath/versions/$2/install.sh $1
# if [ "${action}" == "install" ] && [ -d $serverPath/postgresql ];then
# #初始化
# cd ${rootPath} && python3 ${rootPath}/plugins/postgresql/index.py start ${type}
# cd ${rootPath} && python3 ${rootPath}/plugins/postgresql/index.py initd_install ${type}
# fi
if [ "${action}" == "install" ] && [ -d $serverPath/postgresql ];then
#初始化
cd ${rootPath} && python3 ${rootPath}/plugins/postgresql/index.py start ${type}
cd ${rootPath} && python3 ${rootPath}/plugins/postgresql/index.py initd_install ${type}
fi

Loading…
Cancel
Save