init
This commit is contained in:
parent
6c40e92a15
commit
dc5670fc39
|
|
@ -0,0 +1,43 @@
|
|||
CREATE TABLE IF NOT EXISTS `config` (
|
||||
`id` INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
`mysql_root` TEXT
|
||||
);
|
||||
|
||||
INSERT INTO `config` (`id`, `mysql_root`) VALUES (1, 'admin');
|
||||
|
||||
CREATE TABLE IF NOT EXISTS `databases` (
|
||||
`id` INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
`pid` INTEGER,
|
||||
`name` TEXT,
|
||||
`username` TEXT,
|
||||
`password` TEXT,
|
||||
`accept` TEXT,
|
||||
`rw` TEXT DEFAULT 'rw',
|
||||
`ps` TEXT,
|
||||
`addtime` TEXT
|
||||
);
|
||||
-- ALTER TABLE `databases` ADD COLUMN `rw` TEXT DEFAULT 'rw';
|
||||
|
||||
CREATE TABLE IF NOT EXISTS `master_replication_user` (
|
||||
`id` INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
`username` TEXT,
|
||||
`password` TEXT,
|
||||
`accept` TEXT,
|
||||
`ps` TEXT,
|
||||
`addtime` TEXT
|
||||
);
|
||||
|
||||
-- 从库配置主库的[ssh private key]
|
||||
-- drop table `slave_id_rsa`;
|
||||
CREATE TABLE IF NOT EXISTS `slave_id_rsa` (
|
||||
`id` INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
`ip` TEXT,
|
||||
`port` TEXT,
|
||||
`user` TEXT,
|
||||
`db_user` TEXT,
|
||||
`id_rsa` TEXT,
|
||||
`ps` TEXT,
|
||||
`addtime` TEXT
|
||||
);
|
||||
|
||||
|
||||
Binary file not shown.
|
After Width: | Height: | Size: 2.5 KiB |
|
|
@ -0,0 +1,32 @@
|
|||
<div class="bt-form">
|
||||
<div class='plugin_version'></div>
|
||||
<div class="bt-w-main">
|
||||
<div class="bt-w-menu">
|
||||
<!-- <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="myDbPos();">存储位置</p>
|
||||
<p onclick="myPort();">端口</p>
|
||||
<p onclick="runInfo();">当前状态</p>
|
||||
<p onclick="myPerfOpt();">性能优化</p>
|
||||
<p onclick="myLogs();">日志</p>
|
||||
<p onclick="pluginLogs('postgresql',$('.plugin_version').attr('version'),'show_log');">慢日志</p>
|
||||
<p onclick="dbList()">管理列表</p>
|
||||
<p onclick="masterOrSlaveConf($('.plugin_version').attr('version'))">主从配置</p> -->
|
||||
</div>
|
||||
<div class="bt-w-con pd15">
|
||||
<div class="soft-man-con" style="height: 530px; overflow: auto;"></div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
</div>
|
||||
|
||||
<script type="text/javascript">
|
||||
resetPluginWinWidth(800);
|
||||
var loading = layer.msg("资源加载中...", { icon: 16, time: 0, shade: 0.3 });
|
||||
$.getScript( "/plugins/file?name=postgresql&f=js/postgresql.js", function(){
|
||||
layer.close(loading);
|
||||
pluginService('postgresql',$('.plugin_version').attr('version'));
|
||||
});
|
||||
</script>
|
||||
|
|
@ -0,0 +1,745 @@
|
|||
# coding:utf-8
|
||||
|
||||
import sys
|
||||
import io
|
||||
import os
|
||||
import time
|
||||
import subprocess
|
||||
import re
|
||||
import json
|
||||
|
||||
|
||||
# reload(sys)
|
||||
# sys.setdefaultencoding('utf-8')
|
||||
|
||||
sys.path.append(os.getcwd() + "/class/core")
|
||||
import mw
|
||||
|
||||
|
||||
if mw.isAppleSystem():
|
||||
cmd = 'ls /usr/local/lib/ | grep python | cut -d \\ -f 1 | awk \'END {print}\''
|
||||
info = mw.execShell(cmd)
|
||||
p = "/usr/local/lib/" + info[0].strip() + "/site-packages"
|
||||
sys.path.append(p)
|
||||
|
||||
|
||||
app_debug = False
|
||||
if mw.isAppleSystem():
|
||||
app_debug = True
|
||||
|
||||
|
||||
def getPluginName():
|
||||
return 'postgresql'
|
||||
|
||||
|
||||
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 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 checkArgs(data, ck=[]):
|
||||
for i in range(len(ck)):
|
||||
if not ck[i] in data:
|
||||
return (False, mw.returnJson(False, '参数:(' + ck[i] + ')没有!'))
|
||||
return (True, mw.returnJson(True, 'ok'))
|
||||
|
||||
|
||||
def getConf():
|
||||
path = getServerDir() + '/data/postgresql.conf'
|
||||
return path
|
||||
|
||||
|
||||
def getDbPort():
|
||||
file = getConf()
|
||||
content = mw.readFile(file)
|
||||
rep = 'port\s*=\s*(.*)'
|
||||
tmp = re.search(rep, content)
|
||||
return tmp.groups()[0].strip()
|
||||
|
||||
|
||||
def getSocketFile():
|
||||
file = getConf()
|
||||
content = mw.readFile(file)
|
||||
rep = 'socket\s*=\s*(.*)'
|
||||
tmp = re.search(rep, content)
|
||||
return tmp.groups()[0].strip()
|
||||
|
||||
|
||||
def getInitdTpl(version=''):
|
||||
path = getPluginDir() + '/init.d/postgresql.tpl'
|
||||
if not os.path.exists(path):
|
||||
path = getPluginDir() + '/init.d/postgresql.tpl'
|
||||
return path
|
||||
|
||||
|
||||
def contentReplace(content):
|
||||
service_path = mw.getServerDir()
|
||||
content = content.replace('{$ROOT_PATH}', mw.getRootDir())
|
||||
content = content.replace('{$SERVER_PATH}', service_path)
|
||||
content = content.replace('{$APP_PATH}', service_path + '/postgresql')
|
||||
return content
|
||||
|
||||
|
||||
def pSqliteDb(dbname='databases'):
|
||||
file = getServerDir() + '/pgsql.db'
|
||||
name = 'pgsql'
|
||||
if not os.path.exists(file):
|
||||
conn = mw.M(dbname).dbPos(getServerDir(), name)
|
||||
csql = mw.readFile(getPluginDir() + '/conf/pgsql.sql')
|
||||
csql_list = csql.split(';')
|
||||
for index in range(len(csql_list)):
|
||||
conn.execute(csql_list[index], ())
|
||||
else:
|
||||
# 现有run
|
||||
# conn = mw.M(dbname).dbPos(getServerDir(), name)
|
||||
# csql = mw.readFile(getPluginDir() + '/conf/mysql.sql')
|
||||
# csql_list = csql.split(';')
|
||||
# for index in range(len(csql_list)):
|
||||
# conn.execute(csql_list[index], ())
|
||||
conn = mw.M(dbname).dbPos(getServerDir(), name)
|
||||
return conn
|
||||
|
||||
|
||||
def pMysqlDb():
|
||||
# pymysql
|
||||
db = mw.getMyORM()
|
||||
# MySQLdb |
|
||||
# db = mw.getMyORMDb()
|
||||
|
||||
db.setPort(getDbPort())
|
||||
db.setSocket(getSocketFile())
|
||||
# db.setCharset("utf8")
|
||||
db.setPwd(pSqliteDb('config').where('id=?', (1,)).getField('mysql_root'))
|
||||
return db
|
||||
|
||||
|
||||
def initDreplace(version=''):
|
||||
|
||||
conf_dir = getServerDir() + '/etc'
|
||||
log_dir = getServerDir() + "/logs"
|
||||
conf_list = [
|
||||
conf_dir,
|
||||
log_dir
|
||||
]
|
||||
for conf in conf_list:
|
||||
if not os.path.exists(conf):
|
||||
os.mkdir(conf)
|
||||
|
||||
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)
|
||||
|
||||
# systemd
|
||||
system_dir = mw.systemdCfgDir()
|
||||
service = system_dir + '/postgresql.service'
|
||||
if os.path.exists(system_dir) and not os.path.exists(service):
|
||||
tpl = getPluginDir() + '/init.d/postgresql.service.tpl'
|
||||
service_path = mw.getServerDir()
|
||||
content = mw.readFile(tpl)
|
||||
content = content.replace('{$SERVER_PATH}', service_path)
|
||||
mw.writeFile(service, content)
|
||||
mw.execShell('systemctl daemon-reload')
|
||||
|
||||
if not mw.isAppleSystem():
|
||||
mw.execShell('chown -R postgresql:postgresql ' + getServerDir())
|
||||
|
||||
initd_path = getServerDir() + '/init.d'
|
||||
if not os.path.exists(initd_path):
|
||||
os.mkdir(initd_path)
|
||||
|
||||
file_bin = initd_path + '/' + getPluginName()
|
||||
if not os.path.exists(file_bin):
|
||||
initd_tpl = getInitdTpl(version)
|
||||
content = mw.readFile(initd_tpl)
|
||||
content = contentReplace(content)
|
||||
mw.writeFile(file_bin, content)
|
||||
mw.execShell('chmod +x ' + file_bin)
|
||||
return file_bin
|
||||
|
||||
|
||||
def status(version=''):
|
||||
data = mw.execShell(
|
||||
"ps -ef|grep postgres |grep -v grep | grep -v python | grep -v mdserver-web | awk '{print $2}'")
|
||||
if data[0] == '':
|
||||
return 'stop'
|
||||
return 'start'
|
||||
|
||||
|
||||
def getDataDir():
|
||||
file = getConf()
|
||||
content = mw.readFile(file)
|
||||
rep = 'datadir\s*=\s*(.*)'
|
||||
tmp = re.search(rep, content)
|
||||
return tmp.groups()[0].strip()
|
||||
|
||||
|
||||
def getPidFile():
|
||||
file = getConf()
|
||||
content = mw.readFile(file)
|
||||
rep = 'pid-file\s*=\s*(.*)'
|
||||
tmp = re.search(rep, content)
|
||||
return tmp.groups()[0].strip()
|
||||
|
||||
|
||||
def getErrorLog():
|
||||
args = getArgs()
|
||||
path = getDataDir()
|
||||
filename = ''
|
||||
for n in os.listdir(path):
|
||||
if len(n) < 5:
|
||||
continue
|
||||
if n == 'error.log':
|
||||
filename = path + '/' + n
|
||||
break
|
||||
# print filename
|
||||
if not os.path.exists(filename):
|
||||
return mw.returnJson(False, '指定文件不存在!')
|
||||
if 'close' in args:
|
||||
mw.writeFile(filename, '')
|
||||
return mw.returnJson(False, '日志已清空')
|
||||
info = mw.getNumLines(filename, 18)
|
||||
return mw.returnJson(True, 'OK', info)
|
||||
|
||||
|
||||
def getShowLogFile():
|
||||
file = getConf()
|
||||
content = mw.readFile(file)
|
||||
rep = 'slow-query-log-file\s*=\s*(.*)'
|
||||
tmp = re.search(rep, content)
|
||||
return tmp.groups()[0].strip()
|
||||
|
||||
|
||||
def pGetDbUser():
|
||||
if mw.isAppleSystem():
|
||||
user = mw.execShell(
|
||||
"who | sed -n '2, 1p' |awk '{print $1}'")[0].strip()
|
||||
return user
|
||||
return 'postgresql'
|
||||
|
||||
|
||||
def initPgData():
|
||||
datadir = getDataDir()
|
||||
if not os.path.exists(datadir + '/postgresql'):
|
||||
serverdir = getServerDir()
|
||||
myconf = serverdir + "/etc/my.cnf"
|
||||
user = pGetDbUser()
|
||||
cmd = 'cd ' + serverdir + ' && ./bin/initdb -D ' + serverdir + "/data"
|
||||
mw.execShell(cmd)
|
||||
return False
|
||||
return True
|
||||
|
||||
|
||||
def initPgPwd():
|
||||
|
||||
serverdir = getServerDir()
|
||||
pwd = mw.getRandomString(16)
|
||||
|
||||
cmd_pass = serverdir + '/bin/mysql -uroot -e'
|
||||
cmd_pass = cmd_pass + "\"UPDATE mysql.user SET password=PASSWORD('" + \
|
||||
pwd + "') WHERE user='root';"
|
||||
cmd_pass = cmd_pass + "flush privileges;\""
|
||||
data = mw.execShell(cmd_pass)
|
||||
# print(cmd_pass)
|
||||
# print(data)
|
||||
|
||||
# 删除测试数据库
|
||||
drop_test_db = serverdir + '/bin/mysql -uroot -p' + \
|
||||
pwd + ' -e "drop database test";'
|
||||
mw.execShell(drop_test_db)
|
||||
|
||||
pSqliteDb('config').where('id=?', (1,)).save('mysql_root', (pwd,))
|
||||
return True
|
||||
|
||||
|
||||
def myOp(version, method):
|
||||
# import commands
|
||||
init_file = initDreplace()
|
||||
cmd = init_file + ' ' + method
|
||||
try:
|
||||
isInited = initPgData()
|
||||
if not isInited:
|
||||
if mw.isAppleSystem():
|
||||
cmd_init_start = init_file + ' start'
|
||||
subprocess.Popen(cmd_init_start, stdout=subprocess.PIPE, shell=True,
|
||||
bufsize=4096, stderr=subprocess.PIPE)
|
||||
|
||||
time.sleep(6)
|
||||
else:
|
||||
mw.execShell('systemctl start postgresql')
|
||||
|
||||
initPgPwd()
|
||||
|
||||
if mw.isAppleSystem():
|
||||
cmd_init_stop = init_file + ' stop'
|
||||
subprocess.Popen(cmd_init_stop, stdout=subprocess.PIPE, shell=True,
|
||||
bufsize=4096, stderr=subprocess.PIPE)
|
||||
time.sleep(3)
|
||||
else:
|
||||
mw.execShell('systemctl stop postgresql')
|
||||
|
||||
if mw.isAppleSystem():
|
||||
sub = subprocess.Popen(cmd, stdout=subprocess.PIPE, shell=True,
|
||||
bufsize=4096, stderr=subprocess.PIPE)
|
||||
sub.wait(5)
|
||||
else:
|
||||
mw.execShell('systemctl ' + method + ' postgresql')
|
||||
return 'ok'
|
||||
except Exception as e:
|
||||
return str(e)
|
||||
|
||||
|
||||
def appCMD(version, action):
|
||||
return myOp(version, action)
|
||||
|
||||
|
||||
def start(version=''):
|
||||
return appCMD(version, 'start')
|
||||
|
||||
|
||||
def stop(version=''):
|
||||
return appCMD(version, 'stop')
|
||||
|
||||
|
||||
def restart(version=''):
|
||||
return appCMD(version, 'restart')
|
||||
|
||||
|
||||
def reload(version=''):
|
||||
return appCMD(version, 'reload')
|
||||
|
||||
|
||||
def initdStatus():
|
||||
if mw.isAppleSystem():
|
||||
return "Apple Computer does not support"
|
||||
|
||||
shell_cmd = 'systemctl status mysql | grep loaded | grep "enabled;"'
|
||||
data = mw.execShell(shell_cmd)
|
||||
if data[0] == '':
|
||||
return 'fail'
|
||||
return 'ok'
|
||||
|
||||
|
||||
def initdInstall():
|
||||
if mw.isAppleSystem():
|
||||
return "Apple Computer does not support"
|
||||
|
||||
mw.execShell('systemctl enable mysql')
|
||||
return 'ok'
|
||||
|
||||
|
||||
def initdUinstall():
|
||||
if mw.isAppleSystem():
|
||||
return "Apple Computer does not support"
|
||||
|
||||
mw.execShell('systemctl disable mysql')
|
||||
return 'ok'
|
||||
|
||||
|
||||
def getMyDbPos():
|
||||
file = getConf()
|
||||
content = mw.readFile(file)
|
||||
rep = 'datadir\s*=\s*(.*)'
|
||||
tmp = re.search(rep, content)
|
||||
return tmp.groups()[0].strip()
|
||||
|
||||
|
||||
def setMyDbPos():
|
||||
args = getArgs()
|
||||
data = checkArgs(args, ['datadir'])
|
||||
if not data[0]:
|
||||
return data[1]
|
||||
|
||||
s_datadir = getMyDbPos()
|
||||
t_datadir = args['datadir']
|
||||
if t_datadir == s_datadir:
|
||||
return mw.returnJson(False, '与当前存储目录相同,无法迁移文件!')
|
||||
|
||||
if not os.path.exists(t_datadir):
|
||||
mw.execShell('mkdir -p ' + t_datadir)
|
||||
|
||||
# mw.execShell('/etc/init.d/mysqld stop')
|
||||
stop()
|
||||
mw.execShell('cp -rf ' + s_datadir + '/* ' + t_datadir + '/')
|
||||
mw.execShell('chown -R mysql mysql ' + t_datadir)
|
||||
mw.execShell('chmod -R 755 ' + t_datadir)
|
||||
mw.execShell('rm -f ' + t_datadir + '/*.pid')
|
||||
mw.execShell('rm -f ' + t_datadir + '/*.err')
|
||||
|
||||
path = getServerDir()
|
||||
myfile = path + '/etc/my.cnf'
|
||||
mycnf = mw.readFile(myfile)
|
||||
mw.writeFile(path + '/etc/my_backup.cnf', mycnf)
|
||||
|
||||
mycnf = mycnf.replace(s_datadir, t_datadir)
|
||||
mw.writeFile(myfile, mycnf)
|
||||
start()
|
||||
|
||||
result = mw.execShell(
|
||||
'ps aux|grep mysqld| grep -v grep|grep -v python')
|
||||
if len(result[0]) > 10:
|
||||
mw.writeFile('data/datadir.pl', t_datadir)
|
||||
return mw.returnJson(True, '存储目录迁移成功!')
|
||||
else:
|
||||
mw.execShell('pkill -9 mysqld')
|
||||
mw.writeFile(myfile, mw.readFile(path + '/etc/my_backup.cnf'))
|
||||
start()
|
||||
return mw.returnJson(False, '文件迁移失败!')
|
||||
|
||||
|
||||
def getMyPort():
|
||||
file = getConf()
|
||||
content = mw.readFile(file)
|
||||
rep = 'port\s*=\s*(.*)'
|
||||
tmp = re.search(rep, content)
|
||||
return tmp.groups()[0].strip()
|
||||
|
||||
|
||||
def setMyPort():
|
||||
args = getArgs()
|
||||
data = checkArgs(args, ['port'])
|
||||
if not data[0]:
|
||||
return data[1]
|
||||
|
||||
port = args['port']
|
||||
file = getConf()
|
||||
content = mw.readFile(file)
|
||||
rep = "port\s*=\s*([0-9]+)\s*\n"
|
||||
content = re.sub(rep, 'port = ' + port + '\n', content)
|
||||
mw.writeFile(file, content)
|
||||
restart()
|
||||
return mw.returnJson(True, '编辑成功!')
|
||||
|
||||
|
||||
def runInfo():
|
||||
|
||||
if status(version) == 'stop':
|
||||
return mw.returnJson(False, 'PG未启动', [])
|
||||
|
||||
db = pMysqlDb()
|
||||
data = db.query('show global status')
|
||||
gets = ['Max_used_connections', 'Com_commit', 'Com_rollback', 'Questions', 'Innodb_buffer_pool_reads', 'Innodb_buffer_pool_read_requests', 'Key_reads', 'Key_read_requests', 'Key_writes',
|
||||
'Key_write_requests', 'Qcache_hits', 'Qcache_inserts', 'Bytes_received', 'Bytes_sent', 'Aborted_clients', 'Aborted_connects',
|
||||
'Created_tmp_disk_tables', 'Created_tmp_tables', 'Innodb_buffer_pool_pages_dirty', 'Opened_files', 'Open_tables', 'Opened_tables', 'Select_full_join',
|
||||
'Select_range_check', 'Sort_merge_passes', 'Table_locks_waited', 'Threads_cached', 'Threads_connected', 'Threads_created', 'Threads_running', 'Connections', 'Uptime']
|
||||
|
||||
result = {}
|
||||
# print(data)
|
||||
for d in data:
|
||||
vname = d["Variable_name"]
|
||||
for g in gets:
|
||||
if vname == g:
|
||||
result[g] = d["Value"]
|
||||
|
||||
# print(result, int(result['Uptime']))
|
||||
result['Run'] = int(time.time()) - int(result['Uptime'])
|
||||
tmp = db.query('show master status')
|
||||
try:
|
||||
result['File'] = tmp[0]["File"]
|
||||
result['Position'] = tmp[0]["Position"]
|
||||
except:
|
||||
result['File'] = 'OFF'
|
||||
result['Position'] = 'OFF'
|
||||
return mw.getJson(result)
|
||||
|
||||
|
||||
def myDbStatus():
|
||||
result = {}
|
||||
db = pMysqlDb()
|
||||
data = db.query('show variables')
|
||||
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']
|
||||
result['mem'] = {}
|
||||
for d in data:
|
||||
vname = d['Variable_name']
|
||||
for g in gets:
|
||||
# print(g)
|
||||
if vname == g:
|
||||
result['mem'][g] = d["Value"]
|
||||
return mw.getJson(result)
|
||||
|
||||
|
||||
def setDbStatus():
|
||||
gets = ['key_buffer_size', 'tmp_table_size', 'max_heap_table_size', 'innodb_buffer_pool_size', 'innodb_log_buffer_size', 'max_connections',
|
||||
'table_open_cache', 'thread_cache_size', 'sort_buffer_size', 'read_buffer_size', 'read_rnd_buffer_size', 'join_buffer_size', 'thread_stack', 'binlog_cache_size']
|
||||
emptys = ['max_connections', 'thread_cache_size', 'table_open_cache']
|
||||
args = getArgs()
|
||||
conFile = getConf()
|
||||
content = mw.readFile(conFile)
|
||||
n = 0
|
||||
for g in gets:
|
||||
s = 'M'
|
||||
if n > 5:
|
||||
s = 'K'
|
||||
if g in emptys:
|
||||
s = ''
|
||||
rep = '\s*' + g + '\s*=\s*\d+(M|K|k|m|G)?\n'
|
||||
c = g + ' = ' + args[g] + s + '\n'
|
||||
if content.find(g) != -1:
|
||||
content = re.sub(rep, '\n' + c, content, 1)
|
||||
else:
|
||||
content = content.replace('[mysqld]\n', '[mysqld]\n' + c)
|
||||
n += 1
|
||||
mw.writeFile(conFile, content)
|
||||
return mw.returnJson(True, '设置成功!')
|
||||
|
||||
|
||||
def isSqlError(mysqlMsg):
|
||||
# 检测数据库执行错误
|
||||
mysqlMsg = str(mysqlMsg)
|
||||
if "MySQLdb" in mysqlMsg:
|
||||
return mw.returnJson(False, 'MySQLdb组件缺失! <br>进入SSH命令行输入: pip install mysql-python | pip install mysqlclient==2.0.3')
|
||||
if "2002," in mysqlMsg:
|
||||
return mw.returnJson(False, '数据库连接失败,请检查数据库服务是否启动!')
|
||||
if "2003," in mysqlMsg:
|
||||
return mw.returnJson(False, "Can't connect to MySQL server on '127.0.0.1' (61)")
|
||||
if "using password:" in mysqlMsg:
|
||||
return mw.returnJson(False, '数据库管理密码错误!')
|
||||
if "1045" in mysqlMsg:
|
||||
return mw.returnJson(False, '连接错误!')
|
||||
if "SQL syntax" in mysqlMsg:
|
||||
return mw.returnJson(False, 'SQL语法错误!')
|
||||
if "Connection refused" in mysqlMsg:
|
||||
return mw.returnJson(False, '数据库连接失败,请检查数据库服务是否启动!')
|
||||
if "1133" in mysqlMsg:
|
||||
return mw.returnJson(False, '数据库用户不存在!')
|
||||
if "1007" in mysqlMsg:
|
||||
return mw.returnJson(False, '数据库已经存在!')
|
||||
return None
|
||||
|
||||
|
||||
def __createUser(dbname, username, password, address):
|
||||
pdb = pMysqlDb()
|
||||
|
||||
if username == 'root':
|
||||
dbname = '*'
|
||||
|
||||
pdb.execute(
|
||||
"CREATE USER `%s`@`localhost` IDENTIFIED BY '%s'" % (username, password))
|
||||
pdb.execute(
|
||||
"grant all privileges on %s.* to `%s`@`localhost`" % (dbname, username))
|
||||
for a in address.split(','):
|
||||
pdb.execute(
|
||||
"CREATE USER `%s`@`%s` IDENTIFIED BY '%s'" % (username, a, password))
|
||||
pdb.execute(
|
||||
"grant all privileges on %s.* to `%s`@`%s`" % (dbname, username, a))
|
||||
pdb.execute("flush privileges")
|
||||
|
||||
|
||||
def getDbBackupListFunc(dbname=''):
|
||||
bkDir = mw.getRootDir() + '/backup/database'
|
||||
blist = os.listdir(bkDir)
|
||||
r = []
|
||||
|
||||
bname = 'db_' + dbname
|
||||
blen = len(bname)
|
||||
for x in blist:
|
||||
fbstr = x[0:blen]
|
||||
if fbstr == bname:
|
||||
r.append(x)
|
||||
return r
|
||||
|
||||
|
||||
def setDbBackup():
|
||||
args = getArgs()
|
||||
data = checkArgs(args, ['name'])
|
||||
if not data[0]:
|
||||
return data[1]
|
||||
|
||||
scDir = mw.getRunDir() + '/scripts/backup.py'
|
||||
|
||||
cmd = 'python ' + scDir + ' database ' + args['name'] + ' 3'
|
||||
os.system(cmd)
|
||||
return mw.returnJson(True, 'ok')
|
||||
|
||||
|
||||
def importDbBackup():
|
||||
args = getArgs()
|
||||
data = checkArgs(args, ['file', 'name'])
|
||||
if not data[0]:
|
||||
return data[1]
|
||||
|
||||
file = args['file']
|
||||
name = args['name']
|
||||
|
||||
file_path = mw.getRootDir() + '/backup/database/' + file
|
||||
file_path_sql = mw.getRootDir() + '/backup/database/' + file.replace('.gz', '')
|
||||
|
||||
if not os.path.exists(file_path_sql):
|
||||
cmd = 'cd ' + mw.getRootDir() + '/backup/database && gzip -d ' + file
|
||||
mw.execShell(cmd)
|
||||
|
||||
pwd = pSqliteDb('config').where('id=?', (1,)).getField('mysql_root')
|
||||
|
||||
mysql_cmd = mw.getRootDir() + '/server/mysql/bin/mysql -uroot -p' + pwd + \
|
||||
' ' + name + ' < ' + file_path_sql
|
||||
|
||||
# print(mysql_cmd)
|
||||
os.system(mysql_cmd)
|
||||
return mw.returnJson(True, 'ok')
|
||||
|
||||
|
||||
def deleteDbBackup():
|
||||
args = getArgs()
|
||||
data = checkArgs(args, ['filename'])
|
||||
if not data[0]:
|
||||
return data[1]
|
||||
|
||||
bkDir = mw.getRootDir() + '/backup/database'
|
||||
|
||||
os.remove(bkDir + '/' + args['filename'])
|
||||
return mw.returnJson(True, 'ok')
|
||||
|
||||
|
||||
def getDbBackupList():
|
||||
args = getArgs()
|
||||
data = checkArgs(args, ['name'])
|
||||
if not data[0]:
|
||||
return data[1]
|
||||
|
||||
r = getDbBackupListFunc(args['name'])
|
||||
bkDir = mw.getRootDir() + '/backup/database'
|
||||
rr = []
|
||||
for x in range(0, len(r)):
|
||||
p = bkDir + '/' + r[x]
|
||||
data = {}
|
||||
data['name'] = r[x]
|
||||
|
||||
rsize = os.path.getsize(p)
|
||||
data['size'] = mw.toSize(rsize)
|
||||
|
||||
t = os.path.getctime(p)
|
||||
t = time.localtime(t)
|
||||
|
||||
data['time'] = time.strftime('%Y-%m-%d %H:%M:%S', t)
|
||||
rr.append(data)
|
||||
|
||||
data['file'] = p
|
||||
|
||||
return mw.returnJson(True, 'ok', rr)
|
||||
|
||||
|
||||
def getDbList():
|
||||
args = getArgs()
|
||||
page = 1
|
||||
page_size = 10
|
||||
search = ''
|
||||
data = {}
|
||||
if 'page' in args:
|
||||
page = int(args['page'])
|
||||
|
||||
if 'page_size' in args:
|
||||
page_size = int(args['page_size'])
|
||||
|
||||
if 'search' in args:
|
||||
search = args['search']
|
||||
|
||||
conn = pSqliteDb('databases')
|
||||
limit = str((page - 1) * page_size) + ',' + str(page_size)
|
||||
condition = ''
|
||||
if not search == '':
|
||||
condition = "name like '%" + search + "%'"
|
||||
field = 'id,pid,name,username,password,accept,rw,ps,addtime'
|
||||
clist = conn.where(condition, ()).field(
|
||||
field).limit(limit).order('id desc').select()
|
||||
|
||||
for x in range(0, len(clist)):
|
||||
dbname = clist[x]['name']
|
||||
blist = getDbBackupListFunc(dbname)
|
||||
# print(blist)
|
||||
clist[x]['is_backup'] = False
|
||||
if len(blist) > 0:
|
||||
clist[x]['is_backup'] = True
|
||||
|
||||
count = conn.where(condition, ()).count()
|
||||
_page = {}
|
||||
_page['count'] = count
|
||||
_page['p'] = page
|
||||
_page['row'] = page_size
|
||||
_page['tojs'] = 'dbList'
|
||||
data['page'] = mw.getPage(_page)
|
||||
data['data'] = clist
|
||||
|
||||
info = {}
|
||||
info['root_pwd'] = pSqliteDb('config').where(
|
||||
'id=?', (1,)).getField('mysql_root')
|
||||
data['info'] = info
|
||||
|
||||
return mw.getJson(data)
|
||||
|
||||
|
||||
def installPreInspection(version):
|
||||
return 'ok'
|
||||
|
||||
|
||||
def uninstallPreInspection(version):
|
||||
# return "请手动删除MySQL[{}]".format(version)
|
||||
return 'ok'
|
||||
|
||||
if __name__ == "__main__":
|
||||
func = sys.argv[1]
|
||||
|
||||
version = "14.4"
|
||||
version_pl = getServerDir() + "/version.pl"
|
||||
if os.path.exists(version_pl):
|
||||
version = mw.readFile(version_pl).strip()
|
||||
|
||||
if func == 'status':
|
||||
print(status(version))
|
||||
elif func == 'start':
|
||||
print(start(version))
|
||||
elif func == 'stop':
|
||||
print(stop(version))
|
||||
elif func == 'restart':
|
||||
print(restart(version))
|
||||
elif func == 'reload':
|
||||
print(reload(version))
|
||||
elif func == 'initd_status':
|
||||
print(initdStatus())
|
||||
elif func == 'initd_install':
|
||||
print(initdInstall())
|
||||
elif func == 'initd_uninstall':
|
||||
print(initdUinstall())
|
||||
elif func == 'install_pre_inspection':
|
||||
print(installPreInspection(version))
|
||||
elif func == 'uninstall_pre_inspection':
|
||||
print(uninstallPreInspection(version))
|
||||
elif func == 'conf':
|
||||
print(getConf())
|
||||
elif func == 'run_info':
|
||||
print(runInfo())
|
||||
else:
|
||||
print('error')
|
||||
|
|
@ -0,0 +1,18 @@
|
|||
{
|
||||
"title":"PostgreSQL",
|
||||
"tip":"soft",
|
||||
"name":"postgresql",
|
||||
"type":"运行环境",
|
||||
"ps":"功能强大的开源数据库",
|
||||
"coexist": false,
|
||||
"install_pre_inspection":true,
|
||||
"uninstall_pre_inspection":true,
|
||||
"versions":["14.4"],
|
||||
"shell":"install.sh",
|
||||
"checks":"server/postgresql",
|
||||
"path":"server/postgresql",
|
||||
"author":"postgresql",
|
||||
"home":"https://www.postgresql.org/",
|
||||
"date":"2022-08-07",
|
||||
"pid": "2"
|
||||
}
|
||||
|
|
@ -0,0 +1,16 @@
|
|||
[Unit]
|
||||
Description=PostgreSQL: a powerful open source database
|
||||
After=network.target
|
||||
|
||||
[Service]
|
||||
Type=forking
|
||||
User=postgres
|
||||
Group=postgres
|
||||
WorkingDirectory={$APP_PATH}
|
||||
ExecStart={$APP_PATH}/pg_ctl start -D {$APP_PATH}/data
|
||||
ExecReload={$APP_PATH}/pg_ctl restart -D {$APP_PATH}/data
|
||||
ExecStop={$APP_PATH}/pg_ctl stop -D {$APP_PATH}/data
|
||||
PrivateTmp=false
|
||||
|
||||
[Install]
|
||||
WantedBy=multi-user.target
|
||||
|
|
@ -0,0 +1,60 @@
|
|||
#!/bin/bash
|
||||
# chkconfig: 2345 55 25
|
||||
# description: PostgreSQL Service
|
||||
|
||||
### BEGIN INIT INFO
|
||||
# Provides: Midoks
|
||||
# Required-Start: $all
|
||||
# Required-Stop: $all
|
||||
# Default-Start: 2 3 4 5
|
||||
# Default-Stop: 0 1 6
|
||||
# Short-Description: starts PostgreSQL
|
||||
# Description: starts the PostgreSQL
|
||||
### END INIT INFO
|
||||
|
||||
|
||||
PATH=/usr/local/bin:/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
|
||||
export LC_ALL="en_US.UTF-8"
|
||||
|
||||
MW_PATH={$SERVER_PATH}
|
||||
PATH=$PATH:$MW_PATH/bin
|
||||
|
||||
if [ -f $MW_PATH/bin/activate ];then
|
||||
source $MW_PATH/bin/activate
|
||||
fi
|
||||
|
||||
pg_start()
|
||||
{
|
||||
touch {$APP_PATH}/logs/server.log
|
||||
{$APP_PATH}/bin/pg_ctl -D {$APP_PATH}/data -l {$APP_PATH}/logs/server.log start
|
||||
}
|
||||
|
||||
|
||||
pg_stop()
|
||||
{
|
||||
{$APP_PATH}/bin/pg_ctl -D {$APP_PATH}/data -l {$APP_PATH}/logs/server.log stop
|
||||
}
|
||||
|
||||
|
||||
|
||||
pg_status()
|
||||
{
|
||||
echo "123123"
|
||||
}
|
||||
|
||||
|
||||
pg_reload()
|
||||
{
|
||||
echo "123"
|
||||
}
|
||||
|
||||
|
||||
|
||||
case "$1" in
|
||||
'start') pg_start;;
|
||||
'stop') pg_stop;;
|
||||
'reload') pg_reload;;
|
||||
'restart')
|
||||
pg_stop
|
||||
pg_start;;
|
||||
esac
|
||||
|
|
@ -0,0 +1,42 @@
|
|||
#!/bin/bash
|
||||
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
|
||||
export PATH
|
||||
|
||||
curPath=`pwd`
|
||||
rootPath=$(dirname "$curPath")
|
||||
rootPath=$(dirname "$rootPath")
|
||||
serverPath=$(dirname "$rootPath")
|
||||
|
||||
|
||||
install_tmp=${rootPath}/tmp/mw_install.pl
|
||||
|
||||
action=$1
|
||||
type=$2
|
||||
|
||||
if [ "${2}" == "" ];then
|
||||
echo '缺少安装脚本...' > $install_tmp
|
||||
exit 0
|
||||
fi
|
||||
|
||||
if [ ! -d $curPath/versions/$2 ];then
|
||||
echo '缺少安装脚本2...' > $install_tmp
|
||||
exit 0
|
||||
fi
|
||||
|
||||
# if [ "${action}" == "uninstall" ];then
|
||||
# if [ -f /usr/lib/systemd/system/postgresql.service ] || [ -f /lib/systemd/system/postgresql.service ];then
|
||||
# systemctl stop postgresql
|
||||
# systemctl disable postgresql
|
||||
# rm -rf /usr/lib/systemd/system/postgresql.service
|
||||
# rm -rf /lib/systemd/system/postgresql.service
|
||||
# systemctl daemon-reload
|
||||
# fi
|
||||
# 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
|
||||
|
|
@ -0,0 +1,97 @@
|
|||
#!/bin/bash
|
||||
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
|
||||
export PATH
|
||||
|
||||
#https://www.postgresql.org/ftp/source/
|
||||
|
||||
curPath=`pwd`
|
||||
rootPath=$(dirname "$curPath")
|
||||
rootPath=$(dirname "$rootPath")
|
||||
serverPath=$(dirname "$rootPath")
|
||||
sysName=`uname`
|
||||
|
||||
install_tmp=${rootPath}/tmp/mw_install.pl
|
||||
postgreDir=${serverPath}/source/postgresql
|
||||
|
||||
VERSION=14.4
|
||||
|
||||
Install_App()
|
||||
{
|
||||
mkdir -p ${postgreDir}
|
||||
echo '正在安装脚本文件...' > $install_tmp
|
||||
|
||||
if id postgres &> /dev/null ;then
|
||||
echo "postgres UID is `id -u postgres`"
|
||||
echo "postgres Shell is `grep "^postgres:" /etc/passwd |cut -d':' -f7 `"
|
||||
else
|
||||
groupadd postgres
|
||||
useradd -g postgres postgres
|
||||
fi
|
||||
|
||||
if [ "$sysName" != "Darwin" ];then
|
||||
mkdir -p /var/log/mariadb
|
||||
touch /var/log/mariadb/mariadb.log
|
||||
fi
|
||||
|
||||
# ----- cpu start ------
|
||||
if [ -z "${cpuCore}" ]; then
|
||||
cpuCore="1"
|
||||
fi
|
||||
|
||||
if [ -f /proc/cpuinfo ];then
|
||||
cpuCore=`cat /proc/cpuinfo | grep "processor" | wc -l`
|
||||
fi
|
||||
|
||||
MEM_INFO=$(free -m|grep Mem|awk '{printf("%.f",($2)/1024)}')
|
||||
if [ "${cpuCore}" != "1" ] && [ "${MEM_INFO}" != "0" ];then
|
||||
if [ "${cpuCore}" -gt "${MEM_INFO}" ];then
|
||||
cpuCore="${MEM_INFO}"
|
||||
fi
|
||||
else
|
||||
cpuCore="1"
|
||||
fi
|
||||
# ----- cpu end ------
|
||||
|
||||
if [ ! -f ${postgreDir}/postgresql-${VERSION}.tar.bz2 ];then
|
||||
wget --no-check-certificate -O ${postgreDir}/postgresql-${VERSION}.tar.bz2 --tries=3 https://ftp.postgresql.org/pub/source/v${VERSION}/postgresql-${VERSION}.tar.bz2
|
||||
fi
|
||||
|
||||
if [ ! -d ${postgreDir}/postgresql-${VERSION} ];then
|
||||
cd ${postgreDir} && tar -zxvf ${postgreDir}/postgresql-${VERSION}.tar.bz2
|
||||
fi
|
||||
|
||||
|
||||
if [ ! -d $serverPath/postgresql ];then
|
||||
cd ${postgreDir}/postgresql-${VERSION} && ./configure \
|
||||
--prefix=$serverPath/postgresql
|
||||
# --with-openssl \
|
||||
# --with-pgport=33206
|
||||
|
||||
echo "cd ${postgreDir}/postgresql-${VERSION} && ./configure \
|
||||
--prefix=$serverPath/postgresql"
|
||||
# --with-openssl \
|
||||
# --with-pgport=33206
|
||||
make -j${cpuCore} && make install && make clean
|
||||
fi
|
||||
|
||||
if [ -d $serverPath/postgresql ];then
|
||||
echo "{$VERSION}" > $serverPath/postgresql/version.pl
|
||||
echo 'install successful' > $install_tmp
|
||||
else
|
||||
echo 'install fail' > $install_tmp
|
||||
echo 'install fail'>&2
|
||||
fi
|
||||
}
|
||||
|
||||
Uninstall_App()
|
||||
{
|
||||
rm -rf $serverPath/postgresql
|
||||
echo '卸载完成' > $install_tmp
|
||||
}
|
||||
|
||||
action=$1
|
||||
if [ "${1}" == "install" ];then
|
||||
Install_App
|
||||
else
|
||||
Uninstall_App
|
||||
fi
|
||||
Loading…
Reference in New Issue