-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
137 lines (112 loc) · 6.16 KB
/
main.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
import docker
import schedule
import time
import configInit
import calendar
from datetime import datetime
import backupServices
import sys
import watchtowerService
import util
def checkBinds(dockerClient):
appDataBind = "/appData"
backupBind = "/backup"
for container in dockerClient.containers.list():
if "dokeraj/maintainer" in container.attrs["Config"]["Image"]:
listHasBinds = []
for bind in container.attrs["HostConfig"]["Binds"]:
if backupBind in bind.split(":")[-1]:
listHasBinds.append(True)
if appDataBind in bind.split(":")[-1]:
listHasBinds.append(True)
filteredBinds = [hasBind for hasBind in listHasBinds if hasBind]
return len(filteredBinds) == 2
else:
return False
def checkIfContainersExist(config, dockerClient):
for service in config.dockerServices:
try:
print(f"Trying to get container ${service.name}!")
dockerClient.containers.get(service.name)
except Exception as e:
util.notifyUser(15539485, config, True,
f"Error while checking available Docker containers",
f"Cannot find the container `{service.name}` in docker server. \nThis means that the selected container: `{service.name}` cannot be backed up. **Maintainer** will now shut down!")
## this will not invoke deathwatch
sys.exit()
def mainLastDayOfMonth(config, client):
# check to see if it is in fact the last day of the month - and if so, then allow the schedule to take place
today = datetime.today()
curDay = today.day
daysInCurMonth = calendar.monthrange(today.year, today.month)[1]
if (daysInCurMonth - 7) < curDay:
startMainProcess(config, client)
def startMainProcess(config, dockerClient):
cNamesSet = set()
for service in config.dockerServices:
cNamesSet.add(f"- *{service.name}*")
containerNames = "\n".join(cNamesSet)
util.notifyUser(4967467, config, True, ":checkered_flag: Starting scheduled docker update process", f"The following services will be backed up:\n{containerNames}")
checkIfContainersExist(config, dockerClient)
## backup All Specified Services
backupServices.createBackup(config, dockerClient)
## run watchtower
watchtowerService.update(config, dockerClient)
## start all stopped services!!
backupServices.startContainers(config, dockerClient)
util.notifyUser(4967467, config, True, "Updating finished successfully!",
"See you at the next scheduled time :wink:")
def handleScheduling(config, dockerClient):
if config.dailySchedule is not None:
schedule.every().day.at(config.dailySchedule.time).do(startMainProcess, config, dockerClient)
if config.weeklySchedule is not None:
if config.weeklySchedule.day == "MON":
schedule.every().monday.at(config.weeklySchedule.time).do(startMainProcess, config, dockerClient)
if config.weeklySchedule.day == "TUE":
schedule.every().tuesday.at(config.weeklySchedule.time).do(startMainProcess, config, dockerClient)
if config.weeklySchedule.day == "WED":
schedule.every().wednesday.at(config.weeklySchedule.time).do(startMainProcess, config, dockerClient)
if config.weeklySchedule.day == "THU":
schedule.every().thursday.at(config.weeklySchedule.time).do(startMainProcess, config, dockerClient)
if config.weeklySchedule.day == "FRI":
schedule.every().friday.at(config.weeklySchedule.time).do(startMainProcess, config, dockerClient)
if config.weeklySchedule.day == "SAT":
schedule.every().saturday.at(config.weeklySchedule.time).do(startMainProcess, config, dockerClient)
if config.weeklySchedule.day == "SUN":
schedule.every().sunday.at(config.weeklySchedule.time).do(startMainProcess, config, dockerClient)
if config.lastDayOfMonthSchedule is not None:
if config.lastDayOfMonthSchedule.day == "MON":
schedule.every().monday.at(config.lastDayOfMonthSchedule.time).do(mainLastDayOfMonth, config, dockerClient)
if config.lastDayOfMonthSchedule.day == "TUE":
schedule.every().tuesday.at(config.lastDayOfMonthSchedule.time).do(mainLastDayOfMonth, config, dockerClient)
if config.lastDayOfMonthSchedule.day == "WED":
schedule.every().wednesday.at(config.lastDayOfMonthSchedule.time).do(mainLastDayOfMonth, config,
dockerClient)
if config.lastDayOfMonthSchedule.day == "THU":
schedule.every().thursday.at(config.lastDayOfMonthSchedule.time).do(mainLastDayOfMonth, config,
dockerClient)
if config.lastDayOfMonthSchedule.day == "FRI":
schedule.every().friday.at(config.lastDayOfMonthSchedule.time).do(mainLastDayOfMonth, config, dockerClient)
if config.lastDayOfMonthSchedule.day == "SAT":
schedule.every().saturday.at(config.lastDayOfMonthSchedule.time).do(mainLastDayOfMonth, config,
dockerClient)
if config.lastDayOfMonthSchedule.day == "SUN":
schedule.every().sunday.at(config.lastDayOfMonthSchedule.time).do(mainLastDayOfMonth, config, dockerClient)
if __name__ == '__main__':
print("STARTING SCRIPT!")
try:
client = docker.DockerClient(base_url='unix://var/run/docker.sock')
except Exception as e:
print("ERROR: cannot find docker server! now exiting!")
sys.exit(0)
# Check if /appData and /backup bind has been setup properly
if checkBinds(client):
config = configInit.initConfig(client)
backupServices.checkForInitialBackup(config, client)
handleScheduling(config, client)
while True:
schedule.run_pending()
time.sleep(1)
else:
print("ERROR: The mounts /appData or /backup were not bound! Please use the `-v` docker param to bind these two folders! Now exiting!")
sys.exit(0)