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

180 lines
3.7 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 getConf():
path = getPluginDir() + "/config/redis.conf"
return path
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
content = mw.readFile(file_tpl)
content = content.replace('{$SERVER_PATH}', '/swapfile')
mw.writeFile(file_bin, content)
mw.execShell('chmod +x ' + file_bin)
return file_bin
4 years ago
def start():
file = initDreplace()
4 years ago
data = mw.execShell(file + ' start')
4 years ago
if data[1] == '':
4 years ago
return 'ok'
return 'fail'
def stop():
file = initDreplace()
4 years ago
data = mw.execShell(file + ' stop')
4 years ago
if data[1] == '':
4 years ago
return 'ok'
return 'fail'
def restart():
4 years ago
file = initDreplace()
data = mw.execShell(file + ' restart')
if data[1] == '':
return 'ok'
return 'fail'
4 years ago
def reload():
4 years ago
file = initDreplace()
data = mw.execShell(file + ' reload')
if data[1] == '':
return 'ok'
return 'fail'
4 years ago
def initdStatus():
if not app_debug:
if mw.isAppleSystem():
return "Apple Computer does not support"
initd_bin = getInitDFile()
if os.path.exists(initd_bin):
return 'ok'
return 'fail'
def initdInstall():
import shutil
if not app_debug:
if mw.isAppleSystem():
return "Apple Computer does not support"
source_bin = initDreplace()
initd_bin = getInitDFile()
shutil.copyfile(source_bin, initd_bin)
mw.execShell('chmod +x ' + initd_bin)
mw.execShell('chkconfig --add ' + getPluginName())
return 'ok'
def initdUinstall():
if not app_debug:
if mw.isAppleSystem():
return "Apple Computer does not support"
mw.execShell('chkconfig --del ' + getPluginName())
initd_bin = getInitDFile()
os.remove(initd_bin)
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')