Files
CyberPanel/plogical/processUtilities.py

163 lines
4.4 KiB
Python
Raw Normal View History

2017-10-24 19:16:36 +05:00
from CyberCPLogFileWriter import CyberCPLogFileWriter as logging
import subprocess
2017-12-09 22:30:10 +05:00
import shlex
2018-11-09 22:01:28 +05:00
import os
2017-10-24 19:16:36 +05:00
class ProcessUtilities:
litespeedProcess = "litespeed"
2018-11-09 22:01:28 +05:00
ent = 1
OLS = 0
centos = 1
ubuntu = 0
2017-10-24 19:16:36 +05:00
@staticmethod
def getLitespeedProcessNumber():
finalListOfProcesses = []
try:
import psutil
for proc in psutil.process_iter():
2018-11-09 22:01:28 +05:00
if proc.name().find(ProcessUtilities.litespeedProcess) > -1:
2017-10-24 19:16:36 +05:00
finalListOfProcesses.append(proc.pid)
except BaseException,msg:
logging.writeToFile(
2017-10-24 19:16:36 +05:00
str(msg) + " [getLitespeedProcessNumber]")
return 0
if len(finalListOfProcesses) > 0:
return finalListOfProcesses
else:
return 0
@staticmethod
def restartLitespeed():
try:
2018-11-09 22:01:28 +05:00
if ProcessUtilities.decideServer() == ProcessUtilities.OLS:
command = "sudo systemctl restart lsws"
else:
command = "sudo /usr/local/lsws/bin/lswsctrl restart"
2017-12-09 22:30:10 +05:00
cmd = shlex.split(command)
2019-03-26 16:19:03 +05:00
res = subprocess.call(cmd)
2017-10-24 19:16:36 +05:00
2017-12-09 22:30:10 +05:00
if res == 0:
return 1
else:
return 0
2017-10-24 19:16:36 +05:00
2017-12-09 22:30:10 +05:00
except subprocess.CalledProcessError,msg:
2017-10-24 19:16:36 +05:00
logging.writeToFile(str(msg) + "[restartLitespeed]")
@staticmethod
def stopLitespeed():
try:
2018-11-09 22:01:28 +05:00
if ProcessUtilities.decideServer() == ProcessUtilities.OLS:
command = "sudo systemctl stop lsws"
else:
command = "sudo /usr/local/lsws/bin/lswsctrl stop"
2017-12-09 22:30:10 +05:00
cmd = shlex.split(command)
2019-03-26 16:19:03 +05:00
res = subprocess.call(cmd)
2017-12-09 22:30:10 +05:00
if res == 0:
return 1
else:
return 0
2017-10-24 19:16:36 +05:00
except subprocess.CalledProcessError, msg:
logging.writeToFile(str(msg) + "[stopLitespeed]")
2019-03-26 16:19:03 +05:00
@staticmethod
def normalExecutioner(command):
try:
res = subprocess.call(shlex.split(command))
if res == 0:
return 1
else:
return 0
except BaseException, msg:
return 0
2018-11-09 22:01:28 +05:00
@staticmethod
def killLiteSpeed():
2018-11-10 16:05:40 +05:00
try:
command = 'sudo systemctl stop lsws'
ProcessUtilities.executioner(command)
except:
pass
2018-11-09 22:01:28 +05:00
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'
2018-11-10 02:37:45 +05:00
if os.path.exists('/usr/local/lsws/bin/openlitespeed'):
2018-11-09 22:01:28 +05:00
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
2019-01-27 01:18:49 +05:00
@staticmethod
def containerCheck():
try:
command = 'sudo cat /etc/cgrules.conf'
2019-03-26 16:19:03 +05:00
result = subprocess.call(shlex.split(command))
2019-01-27 01:18:49 +05:00
if result == 1:
return 0
else:
return 1
except BaseException:
return 0
2019-03-21 23:26:42 +05:00
@staticmethod
def executioner(command):
try:
2019-03-26 16:19:03 +05:00
logging.writeToFile(command)
2019-03-21 23:26:42 +05:00
res = subprocess.call(shlex.split(command))
if res == 0:
return 1
else:
return 0
except BaseException, msg:
return 0
@staticmethod
def outputExecutioner(command):
2019-03-26 16:19:03 +05:00
if type(command) == str or type(command) == unicode:
logging.writeToFile(command)
return subprocess.check_output(shlex.split(command))
else:
command = " ".join(command)
logging.writeToFile(command + " join")
return subprocess.check_output(shlex.split(command))
2019-03-21 23:26:42 +05:00
@staticmethod
def popenExecutioner(command):
2019-03-26 16:19:03 +05:00
if type(command) == str or type(command) == unicode:
logging.writeToFile(command)
return subprocess.Popen(shlex.split(command))
else:
command = " ".join(command)
logging.writeToFile(command)
return subprocess.Popen(shlex.split(command))
2019-03-21 23:26:42 +05:00
2018-11-09 22:01:28 +05:00
2017-10-24 19:16:36 +05:00