-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbdest.py
166 lines (124 loc) · 6.09 KB
/
bdest.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
# Copyright (c) 2012 Jakub Filipowicz <[email protected]>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
import logging as l
import time
import os
import os.path
import glob
from rsync import Rsync
# ------------------------------------------------------------------------
def dest_generator(job, cfg, name, bg):
dest_type = cfg.get("dest:" + name, 'type')
if dest_type == 'active':
return DestRsync(job, cfg, name, bg)
elif dest_type == 'passive':
return DestPassive(job, cfg, name, bg)
else:
raise SyntaxError("Unknown destination type '%s' for destination '%s'" % (dest_type, name))
# ------------------------------------------------------------------------
class Dest:
params_required = ['type']
params_allowed = []
# --------------------------------------------------------------------
def __init__(self, job, cfg, name, bg):
from bjob import Job
self.status = Job.COPY_STATUS_INIT
self.job = job
self.name = name
self.sname = "dest:" + name
self.bg = bg
self.cfg = cfg
self.type = cfg.get(self.sname, "type")
self.cfg.validate(self.sname, self.params_required, self.params_allowed)
# --------------------------------------------------------------------
def set_status(self, status):
self.status = status
for r in self.job.report:
r.update()
# --------------------------------------------------------------------
def copy(self):
raise NotImplementedError
# ------------------------------------------------------------------------
class DestRsync(Dest):
params_required = ['type', 'path']
params_allowed = ['verbosity', 'exclude']
# --------------------------------------------------------------------
def __init__(self, job, cfg, name, bg):
l.debug("Adding rsync destination '%s' (background: %s) to job '%s'" % (name, str(bg), job.real_name))
Dest.__init__(self, job, cfg, name, bg)
self.exclude = cfg.get_def(self.sname, "exclude", "")
self.verbosity = int(cfg.get_def(self.sname, "verbosity", "1"))
self.path = self.__prepare_path()
self.retries = int(cfg.get_def("global", "copy_retries", '3'))
self.retry_sleep = int(cfg.get_def("global", "copy_retries", '60'))
# --------------------------------------------------------------------
def __prepare_path(self):
path = self.cfg.get(self.sname, "path")
if not path.endswith("/"):
path += "/"
path += self.job.get_job_path()
return path
# --------------------------------------------------------------------
def copy(self):
excludes = self.job.exclude + " " + self.exclude
l.debug("Copying '%s' to '%s' on destination '%s' excluding: '%s'" % (self.job.include, self.path, self.name, excludes))
self.rsync = Rsync(self.name, self.job.include, self.path, excludes)
self.rsync.set_verbosity(self.verbosity)
self.retries -= 1
self.rsync.run()
# ------------------------------------------------------------------------
class DestPassive(Dest):
params_required = ['type', 'host', 'timeout']
params_allowed = []
# --------------------------------------------------------------------
def __init__(self, job, cfg, name, bg):
l.debug("Adding passive destination '%s' to job '%s'" % (name, job.real_name))
if not job.report_dir:
raise SyntaxError("'passive' destination needs a job with 'file' report configured")
Dest.__init__(self, job, cfg, name, bg)
self.host = cfg.get(self.sname, "host")
self.timeout = int(cfg.get("dest:" + name, "timeout"))
self.retries = -1 # we don't retry destinations that are pulled
self.path = 'pull://%s' % self.host # pulling side decides where to store data, we note here who should pull
# --------------------------------------------------------------------
def copy(self):
l.info("Waiting for host '%s' to pull job's data from destination '%s'" % (self.host, self.name))
start_time = time.strftime("%Y-%m-%d-%H-%M-%S", self.job.start_time)
status_files = self.job.report_dir + "/" + self.job.real_name + "-" + self.job.instance + "-" + start_time + ".b1k." + self.host + ".*"
l.debug("Waiting for files matching '%s' (with timeout of %i seconds)" % (status_files, self.timeout))
wait_started = time.time()
while True:
files = glob.glob(status_files)
if files:
for f in files:
# remove the status file, we don't need it anymore
try:
os.remove(f)
except Exception, e:
l.warning("Could not remove status file '%s'" % f)
# check the remote status
if f.endswith(".error"):
raise RuntimeError("Remote job returned 'ERROR'")
elif f.endswith(".done"):
l.debug("Remote job returned 'DONE'")
else:
l.warning("Remote job wrote file with unknown status: %s" % f)
break
if time.time() - wait_started > self.timeout:
raise RuntimeError("Timeout while waiting for host '%s' to pull data from destination '%s'" % (self.host, self.name))
time.sleep(1)
# vim: tabstop=4 expandtab shiftwidth=4 softtabstop=4