-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdatenfresser.py
executable file
·188 lines (140 loc) · 4.5 KB
/
datenfresser.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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
#!/usr/bin/python
# -*- coding: utf-8 -*-
############################################################################
# datenfresser is a backup daemon written by Sebastian Moors #
# It is licensed under the GPL v2. #
############################################################################
# You can configure datenfresser with /etc/datenfresser.conf .
# directory structure:
# $backupdir/container/ : represents a backup entity
# $backupdir/container/cur : holds current data
# $backupdir/container/archived : contains compressed or somehow archived versioned of "cur"
import os
import sys
import subprocess
import select
import getopt
import traceback
import smtplib
import signal
from threading import Lock
sys.path.append("/usr/lib/datenfresser/modules")
sys.path.append("/usr/lib/datenfresser")
from config import config
from config import CliArguments
from db import database
from db import monitorLog
from webserver import datenfresser_webserver
from core import *
from helper import *
from backupOperations import *
from monitor import datenfresserMonitorServer
from monitor import datenfresserMonitorClient
from udev import UdevListener
from udev import pushUdevNotification
from udev import getNextUdevNotification
udevNotificationLock = Lock()
udevNotificationList = []
def handler(signum, frame):
print 'Signal handler called with signal', signum
print os.getpid()
def shutdown():
cmd = "shutdown -h now"
subprocess.Popen(cmd,shell=True, stdout=subprocess.PIPE).wait()
def main( cliArguments ):
# Set the signal handler and a 5-second alarm
signal.signal(signal.SIGUSR1, handler)
log("Starting datenfresser server")
if cliArguments.daemon == True:
createDaemon()
c = config()
webserver = c.getWebserverEnabled()
webserver_port = c.getWebserverPort()
monitor = c.getMonitorServerEnabled()
if cliArguments.monitor: monitor = "True"
auto_shutdown = c.getAutomaticShutdown()
start_delay = c.getStartDelay()
debug = c.getDebug()
#udev = UdevListener()
#udev.run()
# if automatic shutdown is enabled, we ask the user to hit the "enter" key to
# disable automatic shutdown at startup
if int(auto_shutdown) > 0:
print "Press 'enter' to disable automatic shutdown"
rfds, wfds, efds = select.select( [sys.stdin], [], [], 5)
if rfds != []:
auto_shutdown = 0
if webserver == "True":
log("Starting builtin webserver on port " + str(webserver_port))
#start our own webserver to serve the webinterface
web = datenfresser_webserver( webserver_port )
web.startServer()
d = database()
d.cleanupZombieJobs()
#current time
cur_time = time()
if int(start_delay) > 0:
sleep( float ( start_delay ) )
syncMonitorData()
print os.getpid()
if monitor == "True" or monitor == "true":
#start our own monitor
log("Trying to start monitor service..")
monitorServer = datenfresserMonitorServer ()
monitorServer.startServer()
else:
log("Not starting monitor")
pidFileName = "/var/lib/datenfresser/datenfresser.pid"
if os.path.isfile( pidFileName ):
pidFile = open( pidFileName )
tmp = pidFile.readline()
if len(tmp) == 0:
pid = int( pidFile.readline() )
try:
os.getpgid( pid )
log("Another instance of datenfresser is already running. Quitting now..")
sys.exit( 0 )
except OSError, e:
pass
pidFile = open( pidFileName , "w" )
pidFile.write( str( os.getpid() ) )
pidFile.close()
#the user forced an backup of all containers
if cliArguments.forceAll:
for id in d.getAllIDs():
performBackup( id )
while 1:
#print getNextUdevNotification()
#checkSyncDirs()
for id in d.tickAction():
performBackup( id )
#print "before sleep"
#wait till we look for new ready-to-run jobs
#sleep( float( c.getPollInterval() ) )
sleep( 5 )
#print "after sleep"
if int(auto_shutdown) > 0 and (int(cur_time) + int (auto_shutdown)) - time() < 0:
print "shutdown"
shutdown()
if __name__ == "__main__":
cliArguments = CliArguments()
try:
opts, args = getopt.getopt(sys.argv[1:], "hmvdf", ["help", "monitor", "verbose","daemon","forceAll"])
except getopt.GetoptError, err:
print str(err)
#usage()
sys.exit(2)
for o, a in opts:
if o == "-d":
cliArguments.daemon = True
if o == "-v":
cliArguments.verbose = True
if o == "-m":
cliArguments.monitor = True
if o == "-f":
cliArguments.forceAll = True
elif o in ("-h", "--help"):
#usage()
sys.exit()
main( cliArguments )
sys.exit(0)