Files
CyberPanel/plogical/processUtilities.py
2019-03-21 23:26:42 +05:00

139 lines
3.6 KiB
Python

from CyberCPLogFileWriter import CyberCPLogFileWriter as logging
import subprocess
import shlex
import os
class ProcessUtilities:
litespeedProcess = "litespeed"
ent = 1
OLS = 0
centos = 1
ubuntu = 0
@staticmethod
def getLitespeedProcessNumber():
finalListOfProcesses = []
try:
import psutil
for proc in psutil.process_iter():
if proc.name().find(ProcessUtilities.litespeedProcess) > -1:
finalListOfProcesses.append(proc.pid)
except BaseException,msg:
logging.writeToFile(
str(msg) + " [getLitespeedProcessNumber]")
return 0
if len(finalListOfProcesses) > 0:
return finalListOfProcesses
else:
return 0
@staticmethod
def restartLitespeed():
try:
if ProcessUtilities.decideServer() == ProcessUtilities.OLS:
command = "sudo systemctl restart lsws"
else:
command = "sudo /usr/local/lsws/bin/lswsctrl restart"
cmd = shlex.split(command)
res = ProcessUtilities.executioner(cmd)
if res == 0:
return 1
else:
return 0
except subprocess.CalledProcessError,msg:
logging.writeToFile(str(msg) + "[restartLitespeed]")
@staticmethod
def stopLitespeed():
try:
if ProcessUtilities.decideServer() == ProcessUtilities.OLS:
command = "sudo systemctl stop lsws"
else:
command = "sudo /usr/local/lsws/bin/lswsctrl stop"
cmd = shlex.split(command)
res = ProcessUtilities.executioner(cmd)
if res == 0:
return 1
else:
return 0
except subprocess.CalledProcessError, msg:
logging.writeToFile(str(msg) + "[stopLitespeed]")
@staticmethod
def killLiteSpeed():
try:
command = 'sudo systemctl stop lsws'
ProcessUtilities.executioner(command)
except:
pass
pids = ProcessUtilities.getLitespeedProcessNumber()
if pids !=0:
for items in pids:
try:
command = 'sudo kill -9 ' + str(items)
ProcessUtilities.executioner(command)
except:
pass
@staticmethod
def decideServer():
entPath = '/usr/local/lsws/bin/lshttpd'
if os.path.exists('/usr/local/lsws/bin/openlitespeed'):
return ProcessUtilities.OLS
else:
return ProcessUtilities.ent
@staticmethod
def decideDistro():
distroPath = '/etc/lsb-release'
if os.path.exists(distroPath):
return ProcessUtilities.ubuntu
else:
return ProcessUtilities.centos
@staticmethod
def containerCheck():
try:
command = 'sudo cat /etc/cgrules.conf'
result = ProcessUtilities.executioner(shlex.split(command))
if result == 1:
return 0
else:
return 1
except BaseException:
return 0
@staticmethod
def executioner(command):
try:
res = subprocess.call(shlex.split(command))
if res == 0:
return 1
else:
return 0
except BaseException, msg:
return 0
@staticmethod
def outputExecutioner(command):
return subprocess.check_output(shlex.split(command))
@staticmethod
def popenExecutioner(command):
return subprocess.Popen(shlex.split(command))