From 287de962774e02d46f5341fd9995e0980fd28c29 Mon Sep 17 00:00:00 2001 From: Mr Chen Date: Wed, 27 Feb 2019 19:58:32 +0800 Subject: [PATCH] update --- plugins/pm2/index.html | 15 +- plugins/pm2/index.py | 535 ++++++++++++-------------------------- plugins/pm2/install.sh | 1 + plugins/pm2/js/pm2.js | 190 ++++++++++++++ plugins/simdht/index.py | 2 +- route/static/app/files.js | 4 +- 6 files changed, 365 insertions(+), 382 deletions(-) create mode 100755 plugins/pm2/js/pm2.js diff --git a/plugins/pm2/index.html b/plugins/pm2/index.html index 834706386..bcec2236a 100755 --- a/plugins/pm2/index.html +++ b/plugins/pm2/index.html @@ -1,11 +1,10 @@
-

服务

-

项目列表

-

Node版本

+

项目列表

+

Node版本

模版管理

-

日志管理

+

日志管理

@@ -13,4 +12,10 @@
\ No newline at end of file + +resetPluginWinWidth(760); +$.getScript( "/plugins/file?name=pm2&f=js/pm2.js", function(){ + pm2List(); +}); + + diff --git a/plugins/pm2/index.py b/plugins/pm2/index.py index 461db59c8..5d2dc1ea8 100755 --- a/plugins/pm2/index.py +++ b/plugins/pm2/index.py @@ -15,7 +15,7 @@ if public.isAppleSystem(): def getPluginName(): - return 'pureftp' + return 'pm2' def getPluginDir(): @@ -32,16 +32,6 @@ def getInitDFile(): return '/etc/init.d/' + getPluginName() -def getConf(): - path = getServerDir() + "/etc/pure-ftpd.conf" - return path - - -def getInitDTpl(): - path = getPluginDir() + "/init.d/pure-ftpd.tpl" - return path - - def getArgs(): args = sys.argv[2:] tmp = {} @@ -59,416 +49,213 @@ def getArgs(): return tmp +def checkArgs(data, ck=[]): + for i in range(len(ck)): + if not ck[i] in data: + return (False, public.returnJson(False, '参数:(' + ck[i] + ')没有!')) + return (True, public.returnJson(True, 'ok')) + + def status(): - cmd = "ps -ef|grep pure-ftpd |grep -v grep | grep -v python | awk '{print $2}'" + cmd = "ps -ef|grep pm2 |grep -v grep | grep -v python | awk '{print $2}'" data = public.execShell(cmd) if data[0] == '': return 'stop' return 'start' -def contentReplace(content): - service_path = public.getServerDir() - content = content.replace('{$ROOT_PATH}', public.getRootDir()) - content = content.replace('{$SERVER_PATH}', service_path) - return content - - -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 - if not os.path.exists(file_bin): - content = public.readFile(file_tpl) - content = contentReplace(content) - public.writeFile(file_bin, content) - public.execShell('chmod +x ' + file_bin) - - pureSbinConfig = getServerDir() + "/sbin/pure-config.pl" - if not os.path.exists(pureSbinConfig): - pureTplConfig = getPluginDir() + "/init.d/pure-config.pl" - content = public.readFile(pureTplConfig) - content = contentReplace(content) - public.writeFile(pureSbinConfig, content) - public.execShell('chmod +x ' + pureSbinConfig) - - pureFtpdConfig = getServerDir() + "/etc/pure-ftpd.conf" - pureFtpdConfigBak = getServerDir() + "/etc/pure-ftpd.bak.conf" - pureFtpdConfigTpl = getPluginDir() + "/conf/pure-ftpd.conf" - - if not os.path.exists(pureFtpdConfigBak): - shutil.copyfile(pureFtpdConfig, pureFtpdConfigBak) - content = public.readFile(pureFtpdConfigTpl) - content = contentReplace(content) - public.writeFile(pureFtpdConfig, content) - - return file_bin - - -def start(): - file = initDreplace() - data = public.execShell(file + ' start') - if data[1] == '': - return 'ok' - return data[1] - - -def stop(): - file = initDreplace() - data = public.execShell(file + ' stop') - if data[1] == '': - return 'ok' - return data[1] - - -def restart(): - file = initDreplace() - data = public.execShell(file + ' restart') - if data[1] == '': - return 'ok' - return 'fail' - - -def reload(): - file = initDreplace() - data = public.execShell(file + ' reload') - if data[1] == '': - return 'ok' - return data[1] - - -def initdStatus(): - if not app_debug: - os_name = public.getOs() - if os_name == 'darwin': - 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: - os_name = public.getOs() - if os_name == 'darwin': - return "Apple Computer does not support" - - source_bin = initDreplace() - initd_bin = getInitDFile() - shutil.copyfile(source_bin, initd_bin) - public.execShell('chmod +x ' + initd_bin) - return 'ok' - - -def initdUinstall(): - if not app_debug: - os_name = public.getOs() - if os_name == 'darwin': - return "Apple Computer does not support" - initd_bin = getInitDFile() - os.remove(initd_bin) - return 'ok' - - -def pftpDB(): - file = getServerDir() + '/ftps.db' - if not os.path.exists(file): - conn = public.M('ftps').dbPos(getServerDir(), 'ftps') - csql = public.readFile(getPluginDir() + '/conf/ftps.sql') - csql_list = csql.split(';') - for index in range(len(csql_list)): - conn.execute(csql_list[index], ()) - else: - conn = public.M('ftps').dbPos(getServerDir(), 'ftps') - return conn - - -def pftpUser(): +def pm2NVMDir(): + path = '/root/.nvm' if public.isAppleSystem(): user = public.execShell( "who | sed -n '2, 1p' |awk '{print $1}'")[0].strip() - return user - return 'www' + path = '/Users/' + user + '/.nvm' + return path + +__SR = '''#!/bin/bash +PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin +export PATH +export HOME=/root +source %s/nvm.sh && ''' % pm2NVMDir() +__path = getServerDir() + '/list' -def pftpAdd(username, password, path): - user = pftpUser() - - if not os.path.exists(path): - os.makedirs(path) - if public.isAppleSystem(): - os.system('chown ' + user + '.staff ' + path) - else: - os.system('chown www.www ' + path) - - cmd = getServerDir() + '/bin/pure-pw useradd ' + username + ' -u ' + user + ' -d ' + \ - path + '<')[0] + if os.path.exists(__path + '/' + appInfo['name']): + appInfo['path'] = public.readFile( + __path + '/' + appInfo['name']) + result.append(appInfo) + return result + except Exception as e: + return [] -def getFtpList(): +def pm2List(): + result = pm2GetList() + return public.returnJson(True, 'ok', result) + + +def pm2Add(): args = getArgs() - page = 1 - page_size = 10 - search = '' - if 'page' in args: - page = int(args['page']) + data = checkArgs(args, ['path', 'run', 'pname']) + if not data[0]: + return data[1] - if 'page_size' in args: - page_size = int(args['page_size']) + path = args['path'] + run = args['run'] + pname = args['pname'] - if 'search' in args: - search = args['search'] + runFile = (path + '/' + run).replace('//', '/') + if not os.path.exists(runFile): + return public.returnJson(False, '指定文件不存在!') - data = {} - conn = pftpDB() - limit = str((page - 1) * page_size) + ',' + str(page_size) - # print limit, search - condition = '' - if not search == '': - condition = "name like '%" + search + "%'" - field = 'id,pid,name,password,path,status,ps,addtime' - clist = conn.where(condition, ()).field( - field).limit(limit).order('id desc').select() - - count = conn.where(condition, ()).count() - _page = {} - _page['count'] = count - _page['p'] = page - _page['row'] = page_size - _page['tojs'] = 'ftpList' - data['page'] = public.getPage(_page) - - info = {} - info['ip'] = public.getLocalIp() - info['port'] = getFtpPort() - data['info'] = info - data['data'] = clist - - return public.getJson(data) + nlist = pm2GetList() + for node in nlist: + if pname == node['name']: + return public.returnJson(False, '指定项目名称已经存在!') + if os.path.exists(path + '/package.json') and not os.path.exists(path + '/package-lock.json'): + public.execShell("cd " + path + ' && npm install -s') + public.execShell('cd ' + path + ' && pm2 start ' + + runFile + ' --name "' + pname + '"|grep ' + pname) + public.execShell('pm2 save && pm2 startup') + if not os.path.exists(__path): + public.execShell('mkdir -p ' + __path) + public.writeFile(__path + '/' + pname, path) + return public.returnJson(True, '添加成功!') -def addFtp(): - import urllib +def pm2Delete(): args = getArgs() - if not 'ftp_username' in args: - return 'ftp_username missing' + data = checkArgs(args, ['pname']) + if not data[0]: + return data[1] - if not 'ftp_password' in args: - return 'ftp_password missing' - - if not 'path' in args: - return 'path missing' - - if not 'ps' in args: - return 'ps missing' - - path = urllib.unquote(args['path']) - user = args['ftp_username'] - pwd = args['ftp_password'] - ps = args['ps'] - - addtime = time.strftime('%Y-%m-%d %X', time.localtime()) - - data = pftpAdd(user, pwd, path) - conn = pftpDB() - conn.add('pid,name,password,path,status,ps,addtime', - (0, user, pwd, path, 1, ps, addtime)) - pftpReload() - if data[1] == '': - return 'ok' - return data[0] + pname = args['pname'] + cmd = 'pm2 stop "' + pname + '" && pm2 delete "' + \ + pname + '" | grep "' + pname + '"' + result = public.execShell(cmd)[0] + if result.find('✓') != -1: + public.execShell('pm2 save && pm2 startup') + if os.path.exists(__path + '/' + pname): + os.remove(__path + '/' + pname) + return public.returnJson(True, '删除成功!') + return public.returnJson(False, '删除失败!') -def delFtp(): +def pm2Stop(): args = getArgs() - if not 'id' in args: - return 'ftp_username missing' + data = checkArgs(args, ['pname']) + if not data[0]: + return data[1] - if not 'username' in args: - return 'username missing' - - public.execShell(getServerDir() + - '/bin/pure-pw userdel ' + args['username']) - pftpReload() - conn = pftpDB() - conn.where("id=?", (args['id'],)).delete() - public.writeLog('TYPE_FTP', 'FTP_DEL_SUCCESS', (args['username'],)) - return 'ok' + pname = args['pname'] + result = public.execShell('pm2 stop "' + pname + '"|grep ' + pname)[0] + if result.find('stoped') != -1: + return public.returnJson(True, '项目[' + pname + ']已停止!') + return public.returnJson(True, '项目[' + pname + ']停止失败!') -def modFtp(): +def pm2Start(): args = getArgs() - if not 'id' in args: - return 'id missing' + data = checkArgs(args, ['pname']) + if not data[0]: + return data[1] - if not 'name' in args: - return 'name missing' - - if not 'password' in args: - return 'password missing' - - conn = pftpDB() - data = pftpMod(args['name'], args['password']) - pftpReload() - - conn.where('id=?', (int(args['id']),)).save( - 'password', (args['password'],)) - # print data - if data[1] == '': - return 'ok' - return data[0] + pname = args['pname'] + result = public.execShell('pm2 start "' + pname + '"|grep ' + pname)[0] + if result.find('online') != -1: + return public.returnJson(True, '项目[' + pname + ']已启动!') + return public.returnJson(False, '项目[' + pname + ']启动失败!') -def modFtpPort(): +def pm2VerList(): + # 获取Node版本列表 import re - args = getArgs() - if not 'port' in args: - return 'port missing' - try: - port = args['port'] - if int(port) < 1 or int(port) > 65535: - return '端口范围不正确!' - file = file = getServerDir() + '/etc/pure-ftpd.conf' - conf = public.readFile(file) - rep = u"\n#?\s*Bind\s+[0-9]+\.[0-9]+\.[0-9]+\.+[0-9]+,([0-9]+)" - # preg_match(rep,conf,tmp) - conf = re.sub( - rep, "\nBind 0.0.0.0," + port, conf) - public.writeFile(file, conf) - restart() - return 'ok' - except Exception as ex: - return str(ex) + result = {} + rep = 'v\d+\.\d+\.\d+' - -def stopPort(): - args = getArgs() - if not 'id' in args: - return 'id missing' - - if not 'username' in args: - return 'username missing' - - if not 'status' in args: - return 'status missing' - - data = pftpStop(args['username']) - pftpReload() - conn = pftpDB() - conn.where('id=?', (int(args['id']),)).save( - 'status', (args['status'],)) - - if data[1] == '': - return 'ok' - return data[0] - - -def startPort(): - args = getArgs() - if not 'id' in args: - return 'id missing' - - if not 'username' in args: - return 'username missing' - - if not 'status' in args: - return 'status missing' - - data = pftpStart(args['username']) - pftpReload() - conn = pftpDB() - conn.where('id=?', (int(args['id']),)).save( - 'status', (args['status'],)) - - if data[1] == '': - return 'ok' - return data[0] + cmd = __SR + ' nvm ls-remote|grep -v v0|grep -v iojs' + # print cmd + tmp = public.execShell(cmd) + result['list'] = re.findall(rep, tmp[0]) + tmp = public.execShell("nvm version") + result['version'] = tmp[0].strip() + return public.returnJson(True, 'ok', result) 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 == 'conf': - print getConf() - elif func == 'get_www_dir': - print getWwwDir() - elif func == 'get_ftp_list': - print getFtpList() - elif func == 'add_ftp': - print addFtp() - elif func == 'del_ftp': - print delFtp() - elif func == 'mod_ftp': - print modFtp() - elif func == 'mod_ftp_port': - print modFtpPort() - elif func == 'stop_ftp': - print stopPort() - elif func == 'start_ftp': - print startPort() + elif func == 'list': + print pm2List() + elif func == 'add': + print pm2Add() + elif func == 'delete': + print pm2Delete() + elif func == 'stop': + print pm2Stop() + elif func == 'start': + print pm2Start() + elif func == 'get_logs': + print pm2Log() + elif func == 'versions': + print pm2VerList() else: print 'error' diff --git a/plugins/pm2/install.sh b/plugins/pm2/install.sh index 6f16b3bf5..fbdc0bb8a 100755 --- a/plugins/pm2/install.sh +++ b/plugins/pm2/install.sh @@ -12,6 +12,7 @@ serverPath=$(dirname "$rootPath") install_tmp=${rootPath}/tmp/bt_install.pl npm install pm2 -g +curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.34.0/install.sh | bash Install_pm2() { diff --git a/plugins/pm2/js/pm2.js b/plugins/pm2/js/pm2.js new file mode 100755 index 000000000..62c4d0e4b --- /dev/null +++ b/plugins/pm2/js/pm2.js @@ -0,0 +1,190 @@ + +function str2Obj(str){ + var data = {}; + kv = str.split('&'); + for(i in kv){ + v = kv[i].split('='); + data[v[0]] = v[1]; + } + return data; +} + + +function pm2Post(method,args,callback){ + + var _args = null; + if (typeof(args) == 'string'){ + _args = JSON.stringify(str2Obj(args)); + } else { + _args = JSON.stringify(args); + } + + var loadT = layer.msg('正在获取中...', { icon: 16, time: 0, shade: 0.3 }); + $.post('/plugins/run', {name:'pm2', func:method, args:_args}, function(data) { + layer.close(loadT); + if (!data.status){ + layer.msg(data.msg,{icon:0,time:2000,shade: [0.3, '#000']}); + return; + } + + if(typeof(callback) == 'function'){ + callback(data); + } + },'json'); +} + + + +function pm2List() { + var con = '
\ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ +
名称模式端口状态重启时间CPU内存目录操作
\ +
'; + + $(".soft-man-con").html(con); + + + pm2Post('list','', function(data){ + var rdata = $.parseJSON(data.data); + console.log(rdata); + if (!rdata['status']){ + layer.msg(rdata['msg'],{icon:2,time:2000,shade: [0.3, '#000']}); + return; + } + + var tbody = ''; + var tmp = rdata['data']; + for(var i=0;i启动 | '; + }else{ + var opt = '停止 | 重启 | 映射 | '; + } + tmp[i].path = tmp[i].path.replace('//',''); + + var status = ''; + if(tmp[i].status != 'online'){ + status = ''; + } + + tbody += '\ + '+tmp[i].name+'\ + '+tmp[i].mode+'\ + '+tmp[i].port+'\ + '+status+'\ + '+tmp[i].restart+'\ + '+tmp[i].uptime+'\ + '+tmp[i].cpu+'\ + '+tmp[i].mem+'\ + 打开\ + \ + '+opt+'删除\ + \ + '; + } + + $("#pmlist").html(tbody); + }); +} + + + +//取版本号列表 +function getNodeVersions(){ + var loadT = layer.msg('正在获取版本列表...',{icon:16,time:0,shade: [0.3, '#000']}); + $.get('/plugin?action=a&s=Versions&name=pm2',function(versions){ + layer.close(loadT); + + + }); + + pm2Post('versions', '', function(data){ + console.log(data); + return; + + var opt = ''; + for(var i=0;i'+versions.list[i]+''; + }else{ + opt += ''; + } + } + var con = '
\ + 当前版本\ + \ +
    \ +
  • 当前版本为['+versions.version+']
  • \ +
  • 版本切换是全局的,切换版本后可能影响您正在运行的项目
  • \ +
\ +
'; + // $("#webEdit-con").html(con); + + $(".soft-man-con").html(con); + }); +} + +function addNode(){ + var data = {path:$("input[name='path']").val(),pname:$("input[name='pname']").val(),run:$("input[name='run']").val()} + if(!data.path || !data.pname || !data.run){ + layer.msg('表单不完整,请检查!',{icon:2}); + return; + } + + pm2Post('add', data, function(data){ + var rdata = $.parseJSON(data.data); + layer.msg(rdata.msg,{icon:rdata.status?1:2}); + pm2List(); + }); +} + +function delNode(pname){ + safeMessage('删除项目['+pname+']','删除['+pname+']项目后,该项目将无法被访问,继续吗?',function(){ + var data = "pname="+pname; + pm2Post('delete', data, function(data){ + var rdata = $.parseJSON(data.data); + layer.msg(rdata.msg,{icon:rdata.status?1:2}); + pm2List(); + }); + }); +} + +function nodeStop(pname){ + var data = 'pname=' + pname; + pm2Post('stop', data, function(data){ + var rdata = $.parseJSON(data.data); + layer.msg(rdata.msg,{icon:rdata.status?1:2}); + pm2List(); + }); +} + +function nodeStart(pname){ + var data = 'pname=' + pname; + pm2Post('start', data, function(data){ + var rdata = $.parseJSON(data.data); + layer.msg(rdata.msg,{icon:rdata.status?1:2}); + pm2List(); + }); +} + + +function onekeyCodeSite(codename, port, path){ +} diff --git a/plugins/simdht/index.py b/plugins/simdht/index.py index cea652e73..dbabe8a67 100755 --- a/plugins/simdht/index.py +++ b/plugins/simdht/index.py @@ -113,7 +113,7 @@ def initDreplace(): def status(): data = public.execShell( - "ps -ef|grep \"python simdht_worker.py\" | grep -v grep | awk '{print $2}'") + "ps -ef|grep \"simdht_worker.py\" | grep -v grep | awk '{print $2}'") if data[0] == '': return 'stop' return 'start' diff --git a/route/static/app/files.js b/route/static/app/files.js index dd5171a26..4c8dcb7ca 100755 --- a/route/static/app/files.js +++ b/route/static/app/files.js @@ -465,14 +465,14 @@ function getFiles(Path) { var cutName = getCookie('cutFileName'); var isPaste = (copyName == 'null') ? cutName : copyName; if (isPaste != 'null' && isPaste != undefined) { - BarTools += ' '; + BarTools += ' '; } $("#Batch").html(''); var BatchTools = ''; var isBatch = getCookie('BatchSelected'); if (isBatch == 1 || isBatch == '1') { - BatchTools += ' '; + BatchTools += ' '; } $("#Batch").html(BatchTools); $("#setBox").prop("checked", false);