-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfigInit.py
145 lines (121 loc) · 7.24 KB
/
configInit.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
145
import yaml
from dataclasses import dataclass
import sys
from os import path
import util
@dataclass
class ConfSchedule:
day: str
time: str
@dataclass
class Config:
nextcloudContainerName: str
generateAll: bool
weeklySchedule: ConfSchedule
dailySchedule: ConfSchedule
lastDayOfMonthSchedule: ConfSchedule
conf = Config(None, True, None, None, None)
def checkMandatoryFields():
if conf.nextcloudContainerName is None:
print("ERROR: Name of the nextcloud container must be inputted! Now exiting!")
sys.exit(0)
if conf.weeklySchedule is None and conf.dailySchedule is None and conf.lastDayOfMonthSchedule is None:
print("ERROR: At least one backup schedule must be setup in order for the script to work! Now exiting!")
sys.exit(0)
if conf.weeklySchedule is not None and (conf.weeklySchedule.time is None or conf.weeklySchedule.day is None):
print("ERROR: Weekly Schedule must have TIME and DAY setup! Now exiting!")
sys.exit(0)
if conf.lastDayOfMonthSchedule is not None and (
conf.lastDayOfMonthSchedule.time is None or conf.lastDayOfMonthSchedule.day is None):
print("ERROR: Last Day Of Month Schedule must have TIME and DAY setup! Now exiting!")
sys.exit(0)
if conf.dailySchedule is not None and conf.dailySchedule.time is None:
print("ERROR: Daily Schedule must have TIME field setup! Now exiting!")
sys.exit(0)
def printSetConfig():
resultStr = "The following config params were set:\n"
resultStr += f"- nextcloud_container_name = {conf.nextcloudContainerName}\n"
resultStr += f"- generate_all = {conf.generateAll}\n"
resultStr += f"Backup Schedule:\n"
if conf.weeklySchedule is not None:
resultStr += f"- weekly.day = {conf.weeklySchedule.day}\n"
resultStr += f"- weekly.time = {conf.weeklySchedule.time}\n"
if conf.dailySchedule is not None:
resultStr += f"- daily.time = {conf.dailySchedule.time}\n"
if conf.lastDayOfMonthSchedule is not None:
resultStr += f"- last_day_of_month.day = {conf.lastDayOfMonthSchedule.day}\n"
resultStr += f"- last_day_of_month.time = {conf.lastDayOfMonthSchedule.time}\n"
print(resultStr)
def initConfig():
try:
if path.exists('/yaml/config.yml'):
with open('/yaml/config.yml') as f:
docs = yaml.load_all(f, Loader=yaml.FullLoader)
for doc in docs:
for k, v in doc.items():
if k == "general_settings" and v is not None:
for generalKey, generalVal in v.items():
if generalKey == "nextcloud_container_name" and generalVal != "<INSERT YOUR NEXTCLOUD CONTAINER NAME HERE>":
conf.nextcloudContainerName = generalVal
if generalKey == "generate_all" and generalVal != "<True or False>":
conf.generateAll = util.safeCastBool(generalVal, True)
# Backup Schedule
if k == "backup_schedule" and v is not None:
for backupKey, backupVal in v.items():
if backupKey == "weekly" and backupVal is not None:
weeklySchedule = ConfSchedule(None, None)
for weeklyKey, weeklyVal in backupVal.items():
if weeklyKey == "day":
if weeklyVal in ["MON", "TUE", "WED", "THU", "FRI", "SAT", "SUN"]:
weeklySchedule.day = weeklyVal
else:
print(
"ERROR: Weekly schedule's day is not set properly - Please use MON, TUE, WED, THU, FRI, SAT or SUN to specify the day. Now exiting!")
sys.exit(0)
if weeklyKey == "time":
if util.isTimeFormat(weeklyVal):
weeklySchedule.time = weeklyVal
else:
print(
"ERROR: Weekly time format is not valid! Please use HH:mm format! Now exiting!")
sys.exit(0)
conf.weeklySchedule = weeklySchedule
if backupKey == "daily" and backupVal is not None:
dailySchedule = ConfSchedule(None, None)
for dailyKey, dailyVal in backupVal.items():
if dailyKey == "time":
if util.isTimeFormat(dailyVal):
dailySchedule.time = dailyVal
else:
print(
"ERROR: Daily time format is not valid! Please use HH:mm format! Now exiting!")
sys.exit(0)
conf.dailySchedule = dailySchedule
if backupKey == "last_day_of_month" and backupVal is not None:
lastDaySchedule = ConfSchedule(None, None)
for lastDayKey, lastDayVal in backupVal.items():
if lastDayKey == "day":
if lastDayVal.upper() in ["MON", "TUE", "WED", "THU", "FRI", "SAT", "SUN"]:
lastDaySchedule.day = lastDayVal
else:
print(
"ERROR: Last Day Of Month schedule's day is not set properly - Please use MON, TUE, WED, THU, FRI, SAT or SUN to specify the day. Now exiting!")
sys.exit(0)
if lastDayKey == "time":
if util.isTimeFormat(lastDayVal):
lastDaySchedule.time = lastDayVal
else:
print(
"ERROR: Last Day Of Month time format is not valid! Please use HH:mm format! Now exiting!")
sys.exit(0)
conf.lastDayOfMonthSchedule = lastDaySchedule
checkMandatoryFields()
printSetConfig()
return conf
else:
print(
"ERROR: config.yml file not found (please bind the volume that contains the config.yml file) - now exiting!")
sys.exit(0)
except Exception as e:
print("ERROR: config.yml file is not a valid yml file - now exiting!", e)
sys.exit(0)