Files
CyberPanel/plogical/cronUtil.py

167 lines
5.3 KiB
Python
Raw Normal View History

2021-08-30 14:39:45 +05:00
import os
2019-12-24 16:51:38 +05:00
import sys
2021-08-30 14:39:45 +05:00
import time
2019-12-24 16:51:38 +05:00
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:
2020-05-24 10:22:37 +01:00
if ProcessUtilities.decideDistro() == ProcessUtilities.centos or ProcessUtilities.decideDistro() == ProcessUtilities.cent8:
2019-03-26 16:19:03 +05:00
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:
2020-05-24 10:22:37 +01:00
if ProcessUtilities.decideDistro() == ProcessUtilities.centos or ProcessUtilities.decideDistro() == ProcessUtilities.cent8:
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
2020-05-24 10:22:37 +01:00
if ProcessUtilities.decideDistro() == ProcessUtilities.centos or ProcessUtilities.decideDistro() == ProcessUtilities.cent8:
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')
2020-03-25 13:02:46 +05:00
for items in data:
if counter == line:
removedLine = items
2020-03-25 13:02:46 +05:00
counter = counter + 1
continue
else:
writeToFile.writelines(items)
2019-03-26 16:19:03 +05:00
counter = counter + 1
2019-03-26 16:19:03 +05:00
2020-03-25 12:39:36 +05:00
writeToFile.close()
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:
2021-08-30 14:39:45 +05:00
2020-05-24 10:22:37 +01:00
if ProcessUtilities.decideDistro() == ProcessUtilities.centos or ProcessUtilities.decideDistro() == ProcessUtilities.cent8:
2020-02-11 21:33:20 +05:00
cronPath = "/var/spool/cron/" + externalApp
else:
cronPath = "/var/spool/cron/crontabs/" + externalApp
2019-03-26 16:19:03 +05:00
2021-08-30 14:39:45 +05:00
from random import randint, seed
try:
seed(time.perf_counter())
except:
pass
TempFile = '/tmp/' + str(randint(1000, 9999))
if os.path.exists(cronPath):
FullCrons = open(cronPath, 'r').read()
finalCron = '%s%s\n' % (FullCrons, finalCron)
with open(TempFile, "w") as file:
file.write(finalCron)
else:
with open(TempFile, "w") as file:
file.write(finalCron + '\n')
command = 'cp %s %s' % (TempFile, cronPath)
ProcessUtilities.executioner(command)
os.read(TempFile)
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')
2020-02-11 21:51:35 +05:00
2020-05-15 01:07:04 +05:00
if ProcessUtilities.decideDistro() == ProcessUtilities.ubuntu or ProcessUtilities.decideDistro() == ProcessUtilities.ubuntu20:
2020-02-11 21:51:35 +05:00
command = 'chmod 755 /var/spool/cron/crontabs'
ProcessUtilities.outputExecutioner(command)
else:
cronParent = '/var/spool/cron'
commandT = 'chmod 700 %s' % (cronParent)
ProcessUtilities.executioner(commandT, 'root')
2020-05-15 01:07:04 +05:00
if ProcessUtilities.decideDistro() == ProcessUtilities.ubuntu or ProcessUtilities.decideDistro() == ProcessUtilities.ubuntu20:
2020-02-11 21:51:35 +05:00
command = 'chmod 1730 /var/spool/cron/crontabs'
ProcessUtilities.outputExecutioner(command)
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()