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
2019-04-15 15:54:23 +05:00
import time
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 '
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 ( )
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 :
2017-11-09 00:35:52 +05:00
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 '
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 ( ) :
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-04-28 03:19:24 +05:00
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 " ]
except BaseException , msg :
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
2019-07-16 23:23:16 +05:00
def sendCommand ( command , user = None ) :
2019-03-30 14:21:52 +05:00
try :
2019-04-01 15:19:54 +05:00
2019-03-30 14:21:52 +05:00
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
2019-07-16 23:23:16 +05:00
# SplittedCommand = command.split(' ')
# if SplittedCommand[0] == 'sudo':
# finalCommand = SplittedCommand[1:]
# else:
# finalCommand = SplittedCommand
#
# CommandArgs = finalCommand[1:]
#
# finalCommand = finalCommand[0]
#
# for items in CommandArgs:
# finalCommand = '%s %s' % (finalCommand, items)
2019-10-04 15:56:53 +05:00
2019-07-16 23:23:16 +05:00
if user == None :
2019-10-04 15:56:53 +05:00
logging . writeToFile ( ProcessUtilities . token + command )
2019-07-16 23:23:16 +05:00
sock . sendall ( ProcessUtilities . token + command )
else :
command = ' %s -u %s %s ' % ( ProcessUtilities . token , user , command )
2019-10-04 15:56:53 +05:00
logging . writeToFile ( command )
2019-07-16 23:23:16 +05:00
command = command . replace ( ' sudo ' , ' ' )
sock . sendall ( command )
2019-03-30 14:21:52 +05:00
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
2019-07-16 23:23:16 +05:00
def executioner ( command , user = None ) :
2019-03-21 23:26:42 +05:00
try :
2019-07-16 23:23:16 +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 ]
exitCode = int ( exitCode . encode ( ' hex ' ) , 16 )
if exitCode == 0 :
#logging.writeToFile("Command: " + command + ", resturn code: " + str(exitCode) + ".")
return 1
else :
#logging.writeToFile("Command: " + command + ", resturn code: " + str(exitCode) + ".")
return 0
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
2019-07-16 23:23:16 +05:00
def outputExecutioner ( command , user = None ) :
2019-03-30 14:21:52 +05:00
try :
if type ( command ) == str or type ( command ) == unicode :
2019-04-01 15:19:54 +05:00
pass
2019-03-30 14:21:52 +05:00
else :
command = " " . join ( command )
2019-07-16 23:23:16 +05:00
return ProcessUtilities . sendCommand ( command , user ) [ : - 1 ]
2019-03-30 14:21:52 +05:00
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 ' ]
else :
command = " " . join ( self . extraArgs [ ' command ' ] )
2019-07-16 23:23:16 +05:00
ProcessUtilities . sendCommand ( command , self . extraArgs [ ' user ' ] )
2019-03-30 14:21:52 +05:00
return 1
except BaseException , msg :
logging . writeToFile ( str ( msg ) + " [customPoen] " )
2019-03-21 23:26:42 +05:00
@staticmethod
2019-07-16 23:23:16 +05:00
def popenExecutioner ( command , user = None ) :
2019-03-30 14:21:52 +05:00
try :
extraArgs = { }
extraArgs [ ' command ' ] = command
2019-07-16 23:23:16 +05:00
extraArgs [ ' user ' ] = user
2019-03-30 14:21:52 +05:00
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
2019-08-03 14:53:31 +05:00
@staticmethod
def BuildCommand ( path , functionName , parameters ) :
2019-08-27 14:40:02 +05:00
execPath = " /usr/local/CyberCP/bin/python2 %s %s " % ( path , functionName )
2019-08-03 14:53:31 +05:00
for key , value in parameters . iteritems ( ) :
execPath = execPath + ' -- %s %s ' % ( key , value )
return execPath
2017-10-24 19:16:36 +05:00