Files
CyberPanel/plogical/processUtilities.py

233 lines
6.5 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
2019-03-30 14:21:52 +05:00
import socket
import threading as multi
2017-10-24 19:16:36 +05:00
2019-03-30 14:21:52 +05:00
class ProcessUtilities(multi.Thread):
2017-10-24 19:16:36 +05:00
litespeedProcess = "litespeed"
2018-11-09 22:01:28 +05:00
ent = 1
OLS = 0
centos = 1
ubuntu = 0
2019-03-30 14:21:52 +05:00
server_address = '/usr/local/lscpd/admin/comm.sock'
token = "2dboNyhseD7ro8rRUsJGy9AlLxJtSjHI"
def __init__(self, function, extraArgs):
multi.Thread.__init__(self)
self.function = function
self.extraArgs = extraArgs
def run(self):
try:
if self.function == 'popen':
self.customPoen()
except BaseException, msg:
logging.writeToFile( str(msg) + ' [ApplicationInstaller.run]')
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-30 14:21:52 +05:00
@staticmethod
def setupUDSConnection():
try:
sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
sock.connect(ProcessUtilities.server_address)
return [sock, "None"]
except BaseException, msg:
logging.writeToFile(str(msg) + ". [setupUDSConnection:138]")
return [-1, str(msg)]
@staticmethod
def sendCommand(command):
try:
ret = ProcessUtilities.setupUDSConnection()
if ret[0] == -1:
return ret[0]
token = os.environ.get('TOKEN')
sock = ret[0]
sock.sendall(token + command)
data = ""
while (1):
currentData = sock.recv(32)
if len(currentData) == 0 or currentData == None:
break
data = data + currentData
sock.close()
return data
except BaseException, msg:
logging.writeToFile(str(msg) + " [sendCommand]")
return "0" + str(msg)
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-31 02:47:35 +05:00
ret = ProcessUtilities.sendCommand(command)
2019-03-30 14:21:52 +05:00
return 1
2019-03-21 23:26:42 +05:00
except BaseException, msg:
2019-03-30 14:21:52 +05:00
logging.writeToFile(str(msg) + " [executioner]")
2019-03-21 23:26:42 +05:00
return 0
@staticmethod
def outputExecutioner(command):
2019-03-30 14:21:52 +05:00
try:
if type(command) == str or type(command) == unicode:
logging.writeToFile(command)
else:
command = " ".join(command)
logging.writeToFile(command)
return ProcessUtilities.sendCommand(command)
except BaseException, msg:
logging.writeToFile(str(msg) + "[outputExecutioner:188]")
def customPoen(self):
try:
if type(self.extraArgs['command']) == str or type(self.extraArgs['command']) == unicode:
command = self.extraArgs['command']
logging.writeToFile(self.extraArgs['command'])
else:
command = " ".join(self.extraArgs['command'])
logging.writeToFile(command)
ProcessUtilities.sendCommand(command)
return 1
except BaseException, msg:
logging.writeToFile(str(msg) + " [customPoen]")
2019-03-21 23:26:42 +05:00
@staticmethod
def popenExecutioner(command):
2019-03-30 14:21:52 +05:00
try:
extraArgs = {}
extraArgs['command'] = command
pu = ProcessUtilities("popen", extraArgs)
pu.start()
except BaseException, msg:
logging.writeToFile(str(msg) + " [popenExecutioner]")
2019-03-21 23:26:42 +05:00
2018-11-09 22:01:28 +05:00
2017-10-24 19:16:36 +05:00