Files
CyberPanel/plogical/processUtilities.py

285 lines
8.8 KiB
Python
Raw Normal View History

2019-12-15 13:30:40 +05:00
from plogical.CyberCPLogFileWriter import CyberCPLogFileWriter as logging
2017-10-24 19:16:36 +05:00
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
2019-04-15 15:54:23 +05:00
import time
import getpass
2019-12-15 19:56:59 +05:00
import codecs
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
2019-12-18 15:34:33 +05:00
cent8 = 2
2018-11-09 22:01:28 +05:00
ubuntu = 0
2019-03-30 14:21:52 +05:00
server_address = '/usr/local/lscpd/admin/comm.sock'
2019-04-01 15:19:54 +05:00
token = "unset"
2019-03-30 14:21:52 +05:00
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()
2019-12-10 15:09:10 +05:00
except BaseException as msg:
2019-03-30 14:21:52 +05:00
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)
2019-12-10 15:09:10 +05:00
except BaseException as 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:
2019-12-15 13:30:40 +05:00
command = "systemctl restart lsws"
2018-11-09 22:01:28 +05:00
else:
2019-12-15 13:30:40 +05:00
command = "/usr/local/lsws/bin/lswsctrl restart"
2018-11-09 22:01:28 +05:00
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
2019-12-10 15:09:10 +05:00
except subprocess.CalledProcessError as 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:
2019-12-15 13:30:40 +05:00
command = "systemctl stop lsws"
2018-11-09 22:01:28 +05:00
else:
2019-12-15 13:30:40 +05:00
command = "/usr/local/lsws/bin/lswsctrl stop"
2018-11-09 22:01:28 +05:00
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
2019-12-10 15:09:10 +05:00
except subprocess.CalledProcessError as msg:
2017-10-24 19:16:36 +05:00
logging.writeToFile(str(msg) + "[stopLitespeed]")
2019-03-26 16:19:03 +05:00
@staticmethod
2019-12-16 13:05:50 +05:00
def normalExecutioner(command, shell=False):
2019-03-26 16:19:03 +05:00
try:
2019-12-16 13:05:50 +05:00
if shell == False:
res = subprocess.call(shlex.split(command))
else:
res = subprocess.call(command, shell=shell)
2019-03-26 16:19:03 +05:00
if res == 0:
return 1
else:
return 0
2019-12-10 15:09:10 +05:00
except subprocess.CalledProcessError as msg:
2019-10-20 23:33:19 +05:00
logging.writeToFile('%s. [ProcessUtilities.normalExecutioner]' % (str(msg)))
return 0
2019-12-10 15:09:10 +05:00
except BaseException as msg:
2019-10-20 23:33:19 +05:00
logging.writeToFile('%s. [ProcessUtilities.normalExecutioner.Base]' % (str(msg)))
2019-03-26 16:19:03 +05:00
return 0
2018-11-09 22:01:28 +05:00
@staticmethod
def killLiteSpeed():
2018-11-10 16:05:40 +05:00
try:
2019-12-15 13:30:40 +05:00
command = 'systemctl stop lsws'
2019-04-01 15:19:54 +05:00
ProcessUtilities.normalExecutioner(command)
2018-11-10 16:05:40 +05:00
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)
2019-04-01 15:19:54 +05:00
ProcessUtilities.normalExecutioner(command)
2018-11-09 22:01:28 +05:00
except:
pass
@staticmethod
def decideServer():
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:
2019-12-18 15:34:33 +05:00
if open('/etc/redhat-release', 'r').read().find('CentOS Linux release 8') > -1:
return ProcessUtilities.cent8
2018-11-09 22:01:28 +05:00
return ProcessUtilities.centos
2019-01-27 01:18:49 +05:00
@staticmethod
def containerCheck():
try:
2019-12-15 13:30:40 +05:00
command = 'cat /etc/cgrules.conf'
output = ProcessUtilities.outputExecutioner(command)
if output.find('No such') > -1:
2019-01-27 01:18:49 +05:00
return 0
else:
return 1
except BaseException:
return 0
2019-03-30 14:21:52 +05:00
@staticmethod
def setupUDSConnection():
2019-04-15 15:54:23 +05:00
count = 0
while 1:
try:
sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
sock.connect(ProcessUtilities.server_address)
return [sock, "None"]
2019-12-10 15:09:10 +05:00
except BaseException as msg:
2019-04-15 15:54:23 +05:00
if count == 3:
logging.writeToFile("Failed to connect to LSCPD socket, run 'systemctl restart lscpd' on command line to fix this issue.")
return [-1, str(msg)]
else:
count = count + 1
logging.writeToFile("Failed to connect to LSCPD UDS, error message:" + str(msg) + ". Attempt " + str(count) + ", we will attempt again in 2 seconds. [setupUDSConnection:138]")
time.sleep(2)
2019-03-30 14:21:52 +05:00
@staticmethod
def sendCommand(command, user=None):
2019-03-30 14:21:52 +05:00
try:
ret = ProcessUtilities.setupUDSConnection()
if ret[0] == -1:
return ret[0]
2019-04-01 15:19:54 +05:00
if ProcessUtilities.token == "unset":
ProcessUtilities.token = os.environ.get('TOKEN')
del os.environ['TOKEN']
2019-03-30 14:21:52 +05:00
sock = ret[0]
2019-04-01 15:19:54 +05:00
if user == None:
2019-12-13 12:06:08 +05:00
logging.writeToFile(ProcessUtilities.token + command)
sock.sendall((ProcessUtilities.token + command).encode('utf-8'))
else:
command = '%s-u %s %s' % (ProcessUtilities.token, user, command)
2019-12-16 20:43:35 +05:00
command = command.replace('sudo', '')
2019-12-13 12:06:08 +05:00
logging.writeToFile(ProcessUtilities.token + command)
sock.sendall(command.encode('utf-8'))
2019-03-30 14:21:52 +05:00
data = ""
while (1):
currentData = sock.recv(32)
if len(currentData) == 0 or currentData == None:
break
2019-12-15 19:56:59 +05:00
try:
2019-12-16 11:53:58 +05:00
data = data + currentData.decode(errors = 'ignore')
2019-12-15 19:56:59 +05:00
except BaseException as msg:
logging.writeToFile('Some data could not be decoded to str, error message: %s' % str(msg))
2019-03-30 14:21:52 +05:00
2019-12-15 19:56:59 +05:00
sock.close()
2019-12-24 16:51:38 +05:00
logging.writeToFile('Final data: %s.' % (str(data)))
2019-12-16 11:53:58 +05:00
return data
2019-12-10 15:09:10 +05:00
except BaseException as msg:
2019-12-16 11:53:58 +05:00
logging.writeToFile(str(msg) + " [hey:sendCommand]")
2019-03-30 14:21:52 +05:00
return "0" + str(msg)
2019-03-21 23:26:42 +05:00
@staticmethod
2019-12-16 13:05:50 +05:00
def executioner(command, user=None, shell=False):
2019-03-21 23:26:42 +05:00
try:
if getpass.getuser() == 'root':
2019-12-16 13:05:50 +05:00
ProcessUtilities.normalExecutioner(command, shell)
return 1
2019-12-16 11:53:58 +05:00
ret = ProcessUtilities.sendCommand(command, user)
2019-03-31 02:47:35 +05:00
2019-04-01 15:19:54 +05:00
exitCode = ret[len(ret) -1]
2019-12-15 19:56:59 +05:00
exitCode = int(codecs.encode(exitCode.encode(), 'hex'))
2019-04-01 15:19:54 +05:00
if exitCode == 0:
return 1
else:
return 0
2019-12-10 15:09:10 +05:00
except BaseException as 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, user=None):
2019-03-30 14:21:52 +05:00
try:
if getpass.getuser() == 'root':
2019-12-16 13:15:04 +05:00
return subprocess.check_output(command, shell=True).decode("utf-8")
2019-12-15 11:34:09 +05:00
if type(command) == list:
2019-03-30 14:21:52 +05:00
command = " ".join(command)
2019-12-16 11:53:58 +05:00
return ProcessUtilities.sendCommand(command, user)[:-1]
2019-12-10 15:09:10 +05:00
except BaseException as msg:
2019-03-30 14:21:52 +05:00
logging.writeToFile(str(msg) + "[outputExecutioner:188]")
def customPoen(self):
try:
2019-12-15 11:34:09 +05:00
if type(self.extraArgs['command']) == str or type(self.extraArgs['command']) == bytes:
2019-03-30 14:21:52 +05:00
command = self.extraArgs['command']
else:
command = " ".join(self.extraArgs['command'])
ProcessUtilities.sendCommand(command, self.extraArgs['user'])
2019-03-30 14:21:52 +05:00
return 1
2019-12-10 15:09:10 +05:00
except BaseException as msg:
2019-03-30 14:21:52 +05:00
logging.writeToFile(str(msg) + " [customPoen]")
2019-03-21 23:26:42 +05:00
@staticmethod
def popenExecutioner(command, user=None):
2019-03-30 14:21:52 +05:00
try:
extraArgs = {}
extraArgs['command'] = command
extraArgs['user'] = user
2019-03-30 14:21:52 +05:00
pu = ProcessUtilities("popen", extraArgs)
pu.start()
2019-12-10 15:09:10 +05:00
except BaseException as msg:
2019-03-30 14:21:52 +05:00
logging.writeToFile(str(msg) + " [popenExecutioner]")
2019-03-21 23:26:42 +05:00
2019-08-03 14:53:31 +05:00
@staticmethod
def BuildCommand(path, functionName, parameters):
2019-12-10 23:04:24 +05:00
execPath = "/usr/local/CyberCP/bin/python %s %s " % (path, functionName)
2019-12-10 15:09:10 +05:00
for key, value in parameters.items():
2019-08-03 14:53:31 +05:00
execPath = execPath + ' --%s %s' % (key, value)
return execPath
2017-10-24 19:16:36 +05:00