Simple Linux Panel
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
mdserver-web/route/plugins.py

180 lines
5.1 KiB

7 years ago
# coding:utf-8
from flask import Blueprint, render_template
7 years ago
from flask import jsonify
7 years ago
from flask import request
7 years ago
7 years ago
import psutil
import time
import sys
import os
7 years ago
import json
7 years ago
7 years ago
sys.path.append("class/core")
7 years ago
import public
7 years ago
import plugin_api
7 years ago
7 years ago
plugins = Blueprint('plugins', __name__, template_folder='templates')
7 years ago
__plugin_name = "plugins"
7 years ago
__row_num = 3
7 years ago
7 years ago
7 years ago
@plugins.route('/file', methods=['GET'])
def file():
7 years ago
name = request.args.get('name', '')
if name.strip() == '':
return ''
f = request.args.get('f', '')
if f.strip() == '':
return ''
7 years ago
file = __plugin_name + '/' + name + '/' + f
7 years ago
if not os.path.exists(file):
7 years ago
return ''
7 years ago
c = public.readFile(file)
return c
7 years ago
@plugins.route('/list', methods=['GET', 'POST'])
7 years ago
def list():
7 years ago
typeVal = request.args.get('type', '0')
7 years ago
data = plugin_api.plugin_api().getPluginList(typeVal, 1)
7 years ago
return public.getJson(data)
7 years ago
@plugins.route('/install', methods=['POST'])
def install():
7 years ago
7 years ago
rundir = public.getRunDir()
7 years ago
name = request.form.get('name', '')
7 years ago
version = request.form.get('version', '')
7 years ago
7 years ago
mmsg = '安装'
if hasattr(request.form, 'upgrade'):
mtype = 'update'
mmsg = 'upgrade'
7 years ago
if name.strip() == '':
7 years ago
return public.returnJson(False, '缺少插件名称!', ())
7 years ago
if version.strip() == '':
7 years ago
return public.returnJson(False, '缺少版本信息!', ())
7 years ago
7 years ago
infoJsonPos = __plugin_name + '/' + name + '/' + 'info.json'
7 years ago
7 years ago
if not os.path.exists(infoJsonPos):
7 years ago
return public.retJson(False, '配置文件不存在!', ())
7 years ago
7 years ago
pluginInfo = json.loads(public.readFile(infoJsonPos))
7 years ago
7 years ago
execstr = "cd " + os.getcwd() + "/plugins/" + \
7 years ago
name + " && /bin/bash " + pluginInfo["shell"] \
7 years ago
+ " install " + version
7 years ago
7 years ago
taskAdd = (None, mmsg + '[' + name + '-' + version + ']',
7 years ago
'execshell', '0', time.strftime('%Y-%m-%d %H:%M:%S'), execstr)
7 years ago
public.M('tasks').add('id,name,type,status,addtime, execstr', taskAdd)
7 years ago
return public.returnJson(True, '已将安装任务添加到队列!')
7 years ago
@plugins.route('/uninstall', methods=['POST'])
def uninstall():
7 years ago
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, '已将卸载任务添加到队列!')
7 years ago
@plugins.route('/installed', methods=['POST'])
def installed():
rundir = public.getRunDir()
name = request.form.get('name', '')
if name.strip() == '':
7 years ago
return public.retJson(-1, "缺少插件名称!", ())
7 years ago
infoJsonPos = __plugin_name + '/' + name + '/' + 'info.json'
if not os.path.exists(infoJsonPos):
7 years ago
return public.returnJson(-1, "配置文件不存在!", ())
7 years ago
pluginInfo = json.loads(public.readFile(infoJsonPos))
sh = __plugin_name + '/' + name + '/' + pluginInfo['shell']
os.system('/bin/bash ' + sh + ' install')
print request.args
return ''
7 years ago
7 years ago
@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"
7 years ago
7 years ago
@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)
7 years ago
7 years ago
@plugins.route('/setting', methods=['GET'])
7 years ago
def setting():
7 years ago
name = request.args.get('name', '')
html = __plugin_name + '/' + name + '/index.html'
return public.readFile(html)
7 years ago
@plugins.route('/run', methods=['POST', 'GET'])
7 years ago
def run():
7 years ago
name = request.form.get('name', '')
func = request.form.get('func', '')
args = request.form.get('args', '')
script = request.form.get('script', 'index')
7 years ago
py = 'python ' + public.getRunDir() + '/' + __plugin_name + '/' + name
if args == '':
py = py + '/' + script + '.py' + ' ' + func
7 years ago
else:
py = py + '/' + script + '.py' + ' ' + func + ' ' + args
7 years ago
7 years ago
print py
7 years ago
data = public.execShell(py)
7 years ago
if data[1].strip() == '':
7 years ago
return public.returnJson(True, "OK", data[0].strip())
return public.returnJson(False, data[1].strip())