/
cronUtil.py
executable file
·144 lines (105 loc) · 4.42 KB
/
cronUtil.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
import sys
sys.path.append('/usr/local/CyberCP')
import argparse
from plogical.processUtilities import ProcessUtilities
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()
print(f)
except BaseException as msg:
print("0,CyberPanel," + str(msg))
return 1
except BaseException as msg:
print("0,CyberPanel," + str(msg))
@staticmethod
def saveCronChanges(externalApp, finalCron, line):
try:
if ProcessUtilities.decideDistro() == ProcessUtilities.centos:
cronPath = "/var/spool/cron/" + externalApp
else:
cronPath = "/var/spool/cron/crontabs/" + externalApp
with open(cronPath, 'r') as file:
data = file.readlines()
data[line] = finalCron + '\n'
with open(cronPath, 'w') as file:
file.writelines(data)
print("1,None")
except BaseException as msg:
print("0," + str(msg))
@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
data = open(cronPath, 'r').readlines()
counter = 0
writeToFile = open(cronPath, 'w')
for items in data:
if counter == line:
removedLine = items
continue
else:
writeToFile.writelines(items)
counter = counter + 1
print("1," + removedLine)
except BaseException as msg:
print("0," + str(msg))
@staticmethod
def addNewCron(externalApp, finalCron):
try:
if ProcessUtilities.decideDistro() == ProcessUtilities.centos:
cronPath = "/var/spool/cron/" + externalApp
else:
cronPath = "/var/spool/cron/crontabs/" + externalApp
with open(cronPath, "a") as file:
file.write(finalCron + "\n")
if ProcessUtilities.decideDistro() == ProcessUtilities.ubuntu:
command = 'chmod 600 %s' % (cronPath)
ProcessUtilities.executioner(command)
print("1,None")
except BaseException as msg:
print("0," + str(msg))
@staticmethod
def CronPrem(mode):
if mode:
cronParent = '/var/spool/cron'
commandT = 'chmod 755 %s' % (cronParent)
ProcessUtilities.executioner(commandT, 'root')
if ProcessUtilities.decideDistro() == ProcessUtilities.ubuntu:
command = 'chmod 755 /var/spool/cron/crontabs'
ProcessUtilities.outputExecutioner(command)
else:
cronParent = '/var/spool/cron'
commandT = 'chmod 700 %s' % (cronParent)
ProcessUtilities.executioner(commandT, 'root')
if ProcessUtilities.decideDistro() == ProcessUtilities.ubuntu:
command = 'chmod 1730 /var/spool/cron/crontabs'
ProcessUtilities.outputExecutioner(command)
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()