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

187 lines
4.2 KiB

4 years ago
# coding:utf-8
import sys
import io
import os
import time
sys.path.append(os.getcwd() + "/class/core")
import mw
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
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()
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
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
systemDir = '/lib/systemd/system'
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()
3 years ago
service_path = mw.getServerDir()
se_content = mw.readFile(systemServiceTpl)
se_content = se_content.replace('{$SERVER_PATH}', service_path)
se_content = se_content.replace('{$SWAPON_BIN}', swapon_bin)
3 years ago
se_content = se_content.replace('{$SWAPOFF_BIN}', swapoff_bin)
3 years ago
mw.writeFile(systemService, se_content)
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'
4 years ago
data = mw.execShell(file + ' start')
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 swapOp('reload')
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"
data = mw.execShell('systemctl enable swap')
if data[0] == '':
return 'fail'
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
data = mw.execShell('systemctl disable swap')
if data[0] == '':
return 'fail'
4 years ago
return 'ok'
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())
4 years ago
else:
4 years ago
print('error')