mdserver-web/route/plugins.py

197 lines
5.5 KiB
Python
Raw Normal View History

2018-10-02 04:59:56 -04:00
# coding:utf-8
2018-10-02 09:50:10 -04:00
import time
import sys
import os
2018-10-14 05:37:43 -04:00
import json
2018-12-01 04:20:10 -05:00
import psutil
from flask import Blueprint, render_template
from flask import jsonify
from flask import request
2018-10-02 09:50:10 -04:00
2018-11-12 01:38:35 -05:00
sys.path.append("class/core")
2018-10-02 09:50:10 -04:00
import public
2018-11-27 10:29:28 -05:00
import plugin_api
2018-10-02 09:50:10 -04:00
2018-10-02 04:59:56 -04:00
plugins = Blueprint('plugins', __name__, template_folder='templates')
2018-10-02 09:50:10 -04:00
__plugin_name = "plugins"
2018-10-26 01:17:19 -04:00
__row_num = 3
2018-10-02 09:50:10 -04:00
2018-10-02 04:59:56 -04:00
2018-11-27 01:37:24 -05:00
@plugins.route('/file', methods=['GET'])
2018-10-08 08:10:44 -04:00
def file():
2018-10-10 21:55:27 -04:00
name = request.args.get('name', '')
if name.strip() == '':
return ''
f = request.args.get('f', '')
if f.strip() == '':
return ''
2018-11-27 01:37:24 -05:00
file = __plugin_name + '/' + name + '/' + f
2018-10-10 21:55:27 -04:00
if not os.path.exists(file):
2018-11-27 01:37:24 -05:00
return ''
2018-10-10 21:55:27 -04:00
c = public.readFile(file)
return c
2018-10-08 08:10:44 -04:00
2018-11-29 06:16:12 -05:00
@plugins.route('/list', methods=['GET'])
2018-10-02 05:24:26 -04:00
def list():
2018-11-21 05:18:17 -05:00
typeVal = request.args.get('type', '0')
2018-11-27 10:29:28 -05:00
data = plugin_api.plugin_api().getPluginList(typeVal, 1)
2018-11-21 05:18:17 -05:00
return public.getJson(data)
2018-10-09 07:53:03 -04:00
2018-11-29 06:16:12 -05:00
@plugins.route('/index_list', methods=['GET'])
def indexList():
data = plugin_api.plugin_api().getIndexList()
return public.getJson(data)
2018-11-30 01:54:06 -05:00
@plugins.route('/index_sort', methods=['POST'])
def indexSort():
2018-11-30 03:58:50 -05:00
sort = request.form.get('ssort', '')
if sort.strip() == '':
return public.returnJson(False, '排序数据不能为空!')
data = plugin_api.plugin_api().setIndexSort(sort)
if data:
return public.returnJson(True, '成功!')
return public.returnJson(False, '失败!')
2018-11-30 01:54:06 -05:00
2018-10-13 10:54:11 -04:00
@plugins.route('/install', methods=['POST'])
2018-10-09 07:53:03 -04:00
def install():
2018-10-11 22:35:29 -04:00
2018-10-12 07:09:54 -04:00
rundir = public.getRunDir()
2018-10-13 10:54:11 -04:00
name = request.form.get('name', '')
2018-11-04 10:02:45 -05:00
version = request.form.get('version', '')
2018-10-11 07:26:06 -04:00
2018-10-25 07:32:21 -04:00
mmsg = '安装'
if hasattr(request.form, 'upgrade'):
mtype = 'update'
mmsg = 'upgrade'
2018-10-13 10:54:11 -04:00
if name.strip() == '':
2018-11-27 01:37:24 -05:00
return public.returnJson(False, '缺少插件名称!', ())
2018-11-04 10:02:45 -05:00
if version.strip() == '':
2018-11-27 01:37:24 -05:00
return public.returnJson(False, '缺少版本信息!', ())
2018-10-12 07:09:54 -04:00
2018-10-13 10:54:11 -04:00
infoJsonPos = __plugin_name + '/' + name + '/' + 'info.json'
2018-10-12 07:09:54 -04:00
2018-10-13 10:54:11 -04:00
if not os.path.exists(infoJsonPos):
2018-11-27 01:37:24 -05:00
return public.retJson(False, '配置文件不存在!', ())
2018-10-13 23:49:24 -04:00
2018-10-13 10:54:11 -04:00
pluginInfo = json.loads(public.readFile(infoJsonPos))
2018-10-12 07:09:54 -04:00
2018-11-02 01:38:20 -04:00
execstr = "cd " + os.getcwd() + "/plugins/" + \
2018-11-13 01:18:06 -05:00
name + " && /bin/bash " + pluginInfo["shell"] \
2018-11-27 01:37:24 -05:00
+ " install " + version
2018-10-29 08:19:59 -04:00
2018-11-04 10:02:45 -05:00
taskAdd = (None, mmsg + '[' + name + '-' + version + ']',
2018-10-29 08:19:59 -04:00
'execshell', '0', time.strftime('%Y-%m-%d %H:%M:%S'), execstr)
2018-10-25 07:32:21 -04:00
public.M('tasks').add('id,name,type,status,addtime, execstr', taskAdd)
2018-11-03 06:19:47 -04:00
return public.returnJson(True, '已将安装任务添加到队列!')
2018-10-13 10:54:11 -04:00
@plugins.route('/uninstall', methods=['POST'])
def uninstall():
2018-11-26 06:18:50 -05:00
rundir = public.getRunDir()
name = request.form.get('name', '')
version = request.form.get('version', '')
if name.strip() == '':
return public.returnJson(False, "缺少插件名称!", ())
if version.strip() == '':
return public.returnJson(False, "缺少版本信息!", ())
infoJsonPos = __plugin_name + '/' + name + '/' + 'info.json'
if not os.path.exists(infoJsonPos):
return public.retJson(False, "配置文件不存在!", ())
pluginInfo = json.loads(public.readFile(infoJsonPos))
execstr = "cd " + os.getcwd() + "/plugins/" + \
name + " && /bin/bash " + pluginInfo["shell"] \
+ " uninstall " + version
taskAdd = (None, '卸载[' + name + '-' + version + ']',
'execshell', '0', time.strftime('%Y-%m-%d %H:%M:%S'), execstr)
public.M('tasks').add('id,name,type,status,addtime, execstr', taskAdd)
return public.returnJson(True, '已将卸载任务添加到队列!')
2018-10-14 05:37:43 -04:00
@plugins.route('/installed', methods=['POST'])
def installed():
rundir = public.getRunDir()
name = request.form.get('name', '')
if name.strip() == '':
2018-11-13 01:18:06 -05:00
return public.retJson(-1, "缺少插件名称!", ())
2018-10-14 05:37:43 -04:00
infoJsonPos = __plugin_name + '/' + name + '/' + 'info.json'
if not os.path.exists(infoJsonPos):
2018-11-13 01:18:06 -05:00
return public.returnJson(-1, "配置文件不存在!", ())
2018-10-14 05:37:43 -04:00
pluginInfo = json.loads(public.readFile(infoJsonPos))
sh = __plugin_name + '/' + name + '/' + pluginInfo['shell']
os.system('/bin/bash ' + sh + ' install')
print request.args
return ''
2018-10-26 01:17:19 -04:00
2018-11-12 01:38:35 -05:00
@plugins.route('/check_installed', methods=['POST'])
def checkInstalled():
checks = ['nginx', 'apache', 'php', 'mysql']
for name in checks:
filename = public.getRootDir() + "/server/" + name
if os.path.exists(filename):
return "True"
return "False"
2018-11-21 06:15:37 -05:00
2018-11-29 05:19:18 -05:00
@plugins.route('/set_index', methods=['POST'])
def setIndex():
name = request.form.get('name', '')
status = request.form.get('status', '0')
version = request.form.get('version', '')
if status == '1':
return plugin_api.plugin_api().addIndex(name, version)
return plugin_api.plugin_api().removeIndex(name, version)
2018-11-28 06:11:55 -05:00
2018-11-22 04:08:26 -05:00
@plugins.route('/setting', methods=['GET'])
2018-11-21 06:15:37 -05:00
def setting():
2018-11-21 06:27:26 -05:00
name = request.args.get('name', '')
html = __plugin_name + '/' + name + '/index.html'
return public.readFile(html)
2018-11-22 04:08:26 -05:00
@plugins.route('/run', methods=['POST', 'GET'])
2018-11-21 06:27:26 -05:00
def run():
2018-11-22 04:08:26 -05:00
name = request.form.get('name', '')
func = request.form.get('func', '')
args = request.form.get('args', '')
2018-11-22 05:44:19 -05:00
script = request.form.get('script', 'index')
2018-11-22 04:08:26 -05:00
py = 'python ' + public.getRunDir() + '/' + __plugin_name + '/' + name
if args == '':
2018-11-22 05:44:19 -05:00
py = py + '/' + script + '.py' + ' ' + func
2018-11-22 04:08:26 -05:00
else:
2018-11-22 05:44:19 -05:00
py = py + '/' + script + '.py' + ' ' + func + ' ' + args
2018-11-22 04:08:26 -05:00
2018-11-25 03:13:59 -05:00
print py
2018-11-22 04:08:26 -05:00
data = public.execShell(py)
2018-11-22 05:44:19 -05:00
2018-11-22 04:08:26 -05:00
if data[1].strip() == '':
2018-11-22 04:47:19 -05:00
return public.returnJson(True, "OK", data[0].strip())
return public.returnJson(False, data[1].strip())