Files
CyberPanel/plogical/cronUtil.py

128 lines
3.7 KiB
Python
Raw Normal View History

2019-12-24 16:51:38 +05:00
import sys
sys.path.append('/usr/local/CyberCP')
2019-03-26 16:19:03 +05:00
import argparse
2019-12-15 13:30:40 +05:00
from plogical.processUtilities import ProcessUtilities
2019-03-26 16:19:03 +05:00
class CronUtil:
@staticmethod
def getWebsiteCron(externalApp):
try:
if ProcessUtilities.decideDistro() == ProcessUtilities.centos:
cronPath = "/var/spool/cron/" + externalApp
else:
cronPath = "/var/spool/cron/crontabs/" + externalApp
try:
f = open(cronPath, 'r').read()
2019-12-10 15:09:10 +05:00
print(f)
except BaseException as msg:
print("0,CyberPanel," + str(msg))
2019-03-26 16:19:03 +05:00
return 1
2019-12-10 15:09:10 +05:00
except BaseException as msg:
print("0,CyberPanel," + str(msg))
2019-03-26 16:19:03 +05:00
@staticmethod
def saveCronChanges(externalApp, finalCron, line):
try:
if ProcessUtilities.decideDistro() == ProcessUtilities.centos:
cronPath = "/var/spool/cron/" + externalApp
else:
cronPath = "/var/spool/cron/crontabs/" + externalApp
2019-03-26 16:19:03 +05:00
with open(cronPath, 'r') as file:
2019-03-26 16:19:03 +05:00
data = file.readlines()
data[line] = finalCron + '\n'
with open(cronPath, 'w') as file:
2019-03-26 16:19:03 +05:00
file.writelines(data)
2019-12-10 15:09:10 +05:00
print("1,None")
except BaseException as msg:
print("0," + str(msg))
2019-03-26 16:19:03 +05:00
@staticmethod
def remCronbyLine(externalApp, line):
try:
line -= 1
if ProcessUtilities.decideDistro() == ProcessUtilities.centos:
cronPath = "/var/spool/cron/" + externalApp
else:
cronPath = "/var/spool/cron/crontabs/" + externalApp
2019-03-26 16:19:03 +05:00
data = open(cronPath, 'r').readlines()
2019-03-26 16:19:03 +05:00
counter = 0
2019-03-26 16:19:03 +05:00
writeToFile = open(cronPath, 'w')
for items in data:
if counter == line:
removedLine = items
continue
else:
writeToFile.writelines(items)
2019-03-26 16:19:03 +05:00
counter = counter + 1
2019-03-26 16:19:03 +05:00
2019-12-10 15:09:10 +05:00
print("1," + removedLine)
except BaseException as msg:
print("0," + str(msg))
2019-03-26 16:19:03 +05:00
@staticmethod
def addNewCron(externalApp, finalCron):
try:
CronPath = '/var/spool/cron/%s' % (externalApp)
2019-03-26 16:19:03 +05:00
with open(CronPath, "a") as file:
file.write(finalCron + "\n")
2019-03-26 16:19:03 +05:00
2019-12-10 15:09:10 +05:00
print("1,None")
except BaseException as msg:
print("0," + str(msg))
2019-03-26 16:19:03 +05:00
@staticmethod
def CronPrem(mode):
if mode:
cronParent = '/var/spool/cron'
commandT = 'chmod 755 %s' % (cronParent)
ProcessUtilities.executioner(commandT, 'root')
else:
cronParent = '/var/spool/cron'
commandT = 'chmod 700 %s' % (cronParent)
ProcessUtilities.executioner(commandT, 'root')
2019-03-26 16:19:03 +05:00
def main():
parser = argparse.ArgumentParser(description='CyberPanel Installer')
parser.add_argument('function', help='Specific a function to call!')
parser.add_argument("--externalApp", help="externalApp")
parser.add_argument("--line", help="")
parser.add_argument("--finalCron", help="")
parser.add_argument("--tempPath", help="Temporary path to file where PHP is storing data!")
args = parser.parse_args()
if args.function == "getWebsiteCron":
CronUtil.getWebsiteCron(args.externalApp)
elif args.function == "saveCronChanges":
CronUtil.saveCronChanges(args.externalApp, args.finalCron, int(args.line))
elif args.function == "remCronbyLine":
CronUtil.remCronbyLine(args.externalApp, int(args.line))
elif args.function == "addNewCron":
CronUtil.addNewCron(args.externalApp, args.finalCron)
if __name__ == "__main__":
main()