From 3a81e3392b4fe126caecc215b1e7e4445e0b0277 Mon Sep 17 00:00:00 2001 From: midoks Date: Thu, 28 Feb 2019 01:59:56 +0800 Subject: [PATCH] =?UTF-8?q?=E5=9F=BA=E6=9C=AC=E5=8A=9F=E8=83=BDOK?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- plugins/pm2/index.html | 2 +- plugins/pm2/index.py | 79 +++++++++++++++++++++++++++++-- plugins/pm2/install.sh | 6 +-- plugins/pm2/js/pm2.js | 105 +++++++++++++++++++++++++++++++++++++++-- 4 files changed, 180 insertions(+), 12 deletions(-) diff --git a/plugins/pm2/index.html b/plugins/pm2/index.html index bcec2236a..9f0707e74 100755 --- a/plugins/pm2/index.html +++ b/plugins/pm2/index.html @@ -3,7 +3,7 @@

项目列表

Node版本

-

模版管理

+

模版管理

日志管理

diff --git a/plugins/pm2/index.py b/plugins/pm2/index.py index 5d2dc1ea8..32f2b354b 100755 --- a/plugins/pm2/index.py +++ b/plugins/pm2/index.py @@ -75,8 +75,8 @@ def pm2NVMDir(): __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() +export HOME=%s +source %s/nvm.sh && ''' % (getServerDir(), pm2NVMDir()) __path = getServerDir() + '/list' @@ -230,11 +230,76 @@ def pm2VerList(): # print cmd tmp = public.execShell(cmd) result['list'] = re.findall(rep, tmp[0]) - tmp = public.execShell("nvm version") + tmp = public.execShell(__SR + "nvm version") result['version'] = tmp[0].strip() return public.returnJson(True, 'ok', result) +def setNodeVersion(): + args = getArgs() + data = checkArgs(args, ['version']) + if not data[0]: + return data[1] + # 切换Node版本 + version = args['version'].replace('v', '') + estr = ''' +export NVM_NODEJS_ORG_MIRROR=http://npm.taobao.org/mirrors/node && nvm install %s +nvm use %s +nvm alias default %s +oldreg=`npm get registry` +npm config set registry http://registry.npm.taobao.org/ +npm install pm2 -g +npm config set registry $oldreg +''' % (version, version, version) + cmd = __SR + estr + public.execShell(cmd) + return public.returnJson(True, '已切换至[' + version + ']') + + +def getMod(): + cmd = __SR + "npm list --depth=0 -global" + tmp = public.execShell(cmd) + modList = tmp[0].replace("│", "").replace("└", "").replace( + "─", "").replace("┴", "").replace("┘", "").strip().split() + result = [] + for m in modList: + mod = {} + tmp = m.split('@') + if len(tmp) < 2: + continue + mod['name'] = tmp[0] + mod['version'] = tmp[1] + result.append(mod) + + return public.returnJson(True, 'OK', result) + + +# 安装库 +def installMod(): + args = getArgs() + data = checkArgs(args, ['mname']) + if not data[0]: + return data[1] + + mname = args['mname'] + public.execShell(__SR + 'npm install ' + mname + ' -g') + return public.returnJson(True, '安装成功!') + + +def uninstallMod(): + args = getArgs() + data = checkArgs(args, ['mname']) + if not data[0]: + return data[1] + + mname = args['mname'] + myNot = ['pm2', 'npm'] + if mname in myNot: + return public.returnJson(False, '不能卸载[' + mname + ']') + public.execShell(__SR + 'npm uninstall ' + mname + ' -g') + return public.returnJson(True, '卸载成功!') + + if __name__ == "__main__": func = sys.argv[1] if func == 'status': @@ -257,5 +322,13 @@ if __name__ == "__main__": print pm2Log() elif func == 'versions': print pm2VerList() + elif func == 'set_node_version': + print setNodeVersion() + elif func == 'mod_list': + print getMod() + elif func == 'install_mod': + print installMod() + elif func == 'uninstall_mod': + print uninstallMod() else: print 'error' diff --git a/plugins/pm2/install.sh b/plugins/pm2/install.sh index fbdc0bb8a..19fefa068 100755 --- a/plugins/pm2/install.sh +++ b/plugins/pm2/install.sh @@ -11,12 +11,12 @@ 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() { echo '正在安装脚本文件...' > $install_tmp + npm install pm2 -g + curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.34.0/install.sh | bash + mkdir -p $serverPath/pm2 echo '1.0' > $serverPath/pm2/version.pl echo '安装完成' > $install_tmp diff --git a/plugins/pm2/js/pm2.js b/plugins/pm2/js/pm2.js index 62c4d0e4b..fd7ee68a4 100755 --- a/plugins/pm2/js/pm2.js +++ b/plugins/pm2/js/pm2.js @@ -117,8 +117,10 @@ function getNodeVersions(){ }); pm2Post('versions', '', function(data){ - console.log(data); - return; + + var rdata = $.parseJSON(data.data); + console.log(rdata); + var versions = rdata.data; var opt = ''; for(var i=0;i\ - \ + \
    \
  • 当前版本为['+versions.version+']
  • \
  • 版本切换是全局的,切换版本后可能影响您正在运行的项目
  • \
\
'; - // $("#webEdit-con").html(con); - $(".soft-man-con").html(con); }); } + +//切换版本 +function setNodeVersion(){ + var version = $("select[name='versions']").val(); + var data = "version="+version; + pm2Post('set_node_version', data, function(data){ + var rdata = $.parseJSON(data.data); + layer.msg(rdata.msg,{icon:rdata.status?1:2}); + if(rdata.status) { + getNodeVersions(); + } + }); +} + + +//取模块列表 +function getModList(){ + var con = '
\ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ +
名称版本操作
\ + \ +
'; + + $(".soft-man-con").html(con); + + pm2Post('mod_list', '', function(data){ + var rdata = $.parseJSON(data.data); + var tbody = ''; + var tmp = rdata['data']; + for(var i=0;i\ + '+tmp[i].version+'\ + \ + 卸载\ + \ + '; + } + $("#modlist").html(tbody); + }); +} + + +//安装模块 +function installMod(){ + var mname = $("input[name='mname']").val(); + if(!mname){ + layer.msg('模块名称不能为空!',{icon:2}); + return; + } + + // var loadT = layer.msg('正在安装模块['+mname+']...',{icon:16,time:0,shade: [0.3, '#000']}); + // $.post('/plugin?action=a&s=InstallMod&name=pm2',data,function(rdata){ + // layer.close(loadT); + // layer.msg(rdata.msg,{icon:rdata.status?1:2}); + // if(rdata.status) GetModList(); + // }); + + var data = {mname:mname}; + pm2Post('install_mod', data, function(data){ + var rdata = $.parseJSON(data.data); + layer.msg(rdata.msg,{icon:rdata.status?1:2}); + if(rdata.status) { + getModList(); + } + }); +} + + +function uninstallMod(mname){ + safeMessage('卸载模块['+mname+']','卸载['+mname+']模块后,可能影响现有项目,继续吗?',function(){ + var data = "mname="+mname; + pm2Post('uninstall_mod', data, function(data){ + var rdata = $.parseJSON(data.data); + layer.msg(rdata.msg,{icon:rdata.status?1:2}); + if(rdata.status) { + getModList(); + } + }); + }); +} + 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){