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/plugins/swap/index.py

227 lines
5.1 KiB

4 years ago
# coding:utf-8
import sys
import io
import os
import time
web_dir = os.getcwd() + "/web"
if os.path.exists(web_dir):
sys.path.append(web_dir)
os.chdir(web_dir)
import core.mw as mw
4 years ago
app_debug = False
if mw.isAppleSystem():
app_debug = True
def getPluginName():
return 'swap'
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 getInitDTpl():
path = getPluginDir() + "/init.d/" + getPluginName() + ".tpl"
return path
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
3 years ago
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'))
4 years ago
def status():
4 years ago
data = mw.execShell("free -m|grep Swap|awk '{print $2}'")
4 years ago
if data[0].strip() == '0':
4 years ago
return 'stop'
return 'start'
4 years ago
def getInitDTpl():
path = getPluginDir() + "/init.d/" + getPluginName() + ".tpl"
return path
4 years ago
def initDreplace():
4 years ago
file_tpl = getInitDTpl()
5 months ago
service_path = mw.getServerDir()
4 years ago
initD_path = getServerDir() + '/init.d'
if not os.path.exists(initD_path):
os.mkdir(initD_path)
file_bin = initD_path + '/' + getPluginName()
# initd replace
3 years ago
if not os.path.exists(file_bin):
content = mw.readFile(file_tpl)
content = content.replace(
3 years ago
'{$SERVER_PATH}', getServerDir() + '/swapfile')
3 years ago
mw.writeFile(file_bin, content)
mw.execShell('chmod +x ' + file_bin)
# systemd
3 years ago
systemDir = mw.systemdCfgDir()
3 years ago
systemService = systemDir + '/swap.service'
systemServiceTpl = getPluginDir() + '/init.d/swap.service.tpl'
if os.path.exists(systemDir) and not os.path.exists(systemService):
swapon_bin = mw.execShell('which swapon')[0].strip()
3 years ago
swapoff_bin = mw.execShell('which swapoff')[0].strip()
5 months ago
content = mw.readFile(systemServiceTpl)
content = content.replace('{$SERVER_PATH}', service_path)
content = content.replace('{$SWAPON_BIN}', swapon_bin)
content = content.replace('{$SWAPOFF_BIN}', swapoff_bin)
mw.writeFile(systemService, content)
3 years ago
mw.execShell('systemctl daemon-reload')
4 years ago
return file_bin
4 years ago
3 years ago
def swapOp(method):
4 years ago
file = initDreplace()
3 years ago
if not mw.isAppleSystem():
data = mw.execShell('systemctl ' + method + ' swap')
if data[1] == '':
return 'ok'
return 'fail'
3 years ago
data = mw.execShell(file + ' ' + method)
4 years ago
if data[1] == '':
4 years ago
return 'ok'
return 'fail'
3 years ago
def start():
return swapOp('start')
4 years ago
def stop():
3 years ago
return swapOp('stop')
4 years ago
def restart():
3 years ago
return swapOp('restart')
4 years ago
def reload():
3 years ago
return 'ok'
4 years ago
def initdStatus():
3 years ago
if mw.isAppleSystem():
return "Apple Computer does not support"
shell_cmd = 'systemctl status swap | grep loaded | grep "enabled;"'
data = mw.execShell(shell_cmd)
if data[0] == '':
return 'fail'
return 'ok'
4 years ago
def initdInstall():
3 years ago
if mw.isAppleSystem():
return "Apple Computer does not support"
3 years ago
mw.execShell('systemctl enable swap')
4 years ago
return 'ok'
def initdUinstall():
3 years ago
if mw.isAppleSystem():
return "Apple Computer does not support"
4 years ago
3 years ago
mw.execShell('systemctl disable swap')
4 years ago
return 'ok'
3 years ago
def swapStatus():
sfile = getServerDir() + '/swapfile'
3 years ago
if os.path.exists(sfile):
size = os.path.getsize(sfile) / 1024 / 1024
else:
size = '218'
3 years ago
data = {'size': size}
return mw.returnJson(True, "ok", data)
def changeSwap():
args = getArgs()
data = checkArgs(args, ['size'])
if not data[0]:
return data[1]
size = args['size']
swapOp('stop')
3 years ago
3 years ago
gsdir = getServerDir()
cmd = 'dd if=/dev/zero of=' + gsdir + '/swapfile bs=1M count=' + size
cmd += ' && mkswap ' + gsdir + '/swapfile && chmod 600 ' + gsdir + '/swapfile'
3 years ago
msg = mw.execShell(cmd)
3 years ago
swapOp('start')
3 years ago
return mw.returnJson(True, "修改成功:\n" + msg[0])
3 years ago
4 years ago
if __name__ == "__main__":
func = sys.argv[1]
if func == 'status':
4 years ago
print(status())
4 years ago
elif func == 'start':
4 years ago
print(start())
4 years ago
elif func == 'stop':
4 years ago
print(stop())
4 years ago
elif func == 'restart':
4 years ago
print(restart())
4 years ago
elif func == 'reload':
4 years ago
print(reload())
4 years ago
elif func == 'initd_status':
4 years ago
print(initdStatus())
4 years ago
elif func == 'initd_install':
4 years ago
print(initdInstall())
4 years ago
elif func == 'initd_uninstall':
4 years ago
print(initdUinstall())
4 years ago
elif func == 'conf':
4 years ago
print(getConf())
3 years ago
elif func == "swap_status":
print(swapStatus())
elif func == "change_swap":
print(changeSwap())
4 years ago
else:
4 years ago
print('error')