-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathosg-mirror.py
executable file
·254 lines (214 loc) · 8.54 KB
/
osg-mirror.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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
#!/usr/bin/env python
import logging
import pwd
import os
from os.path import join as opj
from socket import getfqdn
import shutil
import signal
import subprocess
import sys
from time import clock, ctime, sleep, time
REPOS_ROOT = "/p/vdt/public/html/repos"
#REPOS_ROOT = "/scratch/matyas/repos"
GOC_ROOT = "rsync://repo.opensciencegrid.org/osg"
#GOC_ROOT = "rsync://repo-itb.opensciencegrid.org/osg"
LOCK_RETRY_MAX = 60 * 20
GLOBAL_TIMEOUT = 60 * 119 # 2 hours
DEBUG = False
REPO_MAP = {}
SYMLINK_MAP = {}
vdtver = "3.0"
for distro in ['el5', 'el6']:
for level in ['development', 'contrib', 'testing', 'release']:
key = "%(vdtver)s-%(distro)s-%(level)s" % locals()
from_loc = opj(GOC_ROOT, vdtver, distro, "osg-%s/" % level)
# For historical reasons, the dir for our release repos is
# called 'production'
if level == 'release':
locallevel = 'production'
else:
locallevel = level
to_loc = opj(REPOS_ROOT, vdtver, distro, locallevel)
REPO_MAP[key] = [from_loc, to_loc]
SYMLINK_MAP[key] = [os.path.basename(to_loc), opj(REPOS_ROOT, vdtver, distro, "osg-%s" % level)]
class Alarm(Exception): pass
class RsyncFailure(Exception): pass
def alarm_handler(signum, frame):
if signum == signal.SIGALRM:
raise Alarm()
def safe_makedirs(directory, mode=0777):
"""A wrapper around os.makedirs that does not raise an exception if the
directory already exists.
"""
if not os.path.isdir(directory):
os.makedirs(directory, mode)
def obtain_lock(lock_file):
"""Check for a lock on the repository, and obtain it if the repo is unlocked."""
# Check for lockfile. Lockfiles should be in AFS and should contain the
# hostname, the uid, and the pid of the process.
logging.debug("Obtaining lock %s", lock_file)
retry = 60
while retry < LOCK_RETRY_MAX and os.path.exists(lock_file):
# If on local machine, see if that pid still exists. If not, continue.
fh = open(lock_file, 'r')
try:
line = fh.readline().strip()
try:
their_user, their_fqdn, their_pid = line.split(":")
except ValueError:
logging.warning("Unrecognized lockfile. Retrying in %d seconds." % retry)
sleep(retry)
retry *= 2
continue
if their_fqdn == getfqdn():
if os.path.exists(os.path.join("/proc", their_pid)):
logging.info("Lockfile exists and owned by running "
"process. Retrying in %d seconds." % retry)
sleep(retry)
retry *= 2
continue
else:
logging.warning("Removing stale lockfile " + lock_file)
os.unlink(lock_file)
break
else:
logging.info("Lockfile exists and owned by process on "
"remote machine. Retrying in %d seconds." % retry)
sleep(retry)
retry *= 2
continue
finally:
if fh:
fh.close()
if retry >= LOCK_RETRY_MAX:
if not their_user or not their_fqdn or not their_pid:
raise Exception("Lockfile " + lock_file + " exists but its contents"
" are not recognized.")
else:
raise Exception("Lockfile " + lock_file + " exists.\n"
"Lock created by " + their_user + " on " +
their_fqdn + " with pid " + str(their_pid))
# Create our lockfile.
safe_makedirs(os.path.dirname(lock_file))
fh = open(lock_file, 'w')
try:
lock_contents = ":".join([pwd.getpwuid(os.getuid())[0],
getfqdn(),
str(os.getpid())])
logging.debug("Lock contents: %s", lock_contents)
print >> fh, lock_contents
finally:
if fh:
fh.close()
def release_lock(lock_file):
"""Release the lock (if there is one)"""
logging.debug("Releasing lock %s", lock_file)
if os.path.exists(lock_file):
os.unlink(lock_file)
def do_mirror(goc_repo, live_repo, ip_repo, old_repo):
if os.path.exists(ip_repo):
shutil.rmtree(ip_repo)
rsync_cmd = ["/usr/bin/rsync", "-arvt", goc_repo, "--exclude=debug/", ip_repo]
if os.path.exists(live_repo):
logging.debug("Live repo exists. Passing --copy-dest=%s to rsync", live_repo)
rsync_cmd += ["--copy-dest=" + live_repo]
rsync_outerr = ""
try:
rsync_proc = subprocess.Popen(
rsync_cmd, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
# communicate() returns nothing if we kill the process via a signal,
# but we still want the output, so we're not using it.
while rsync_proc.poll() is None:
rsync_outerr += rsync_proc.stdout.readline()
rsync_ret = rsync_proc.returncode
except Alarm:
logging.critical("Global timeout exceeded")
logging.critical("Last rsync command: " + str(rsync_cmd))
logging.critical("rsync output follows:\n%s", rsync_outerr)
rsync_proc.send_signal(signal.SIGALRM)
raise
if rsync_ret:
logging.error("Last rsync command: " + str(rsync_cmd))
logging.error(rsync_outerr)
logging.error("Died with code %d", rsync_ret)
raise RsyncFailure("rsync had problems!")
else:
logging.debug("rsync succeeded, output:\n%s", rsync_outerr)
if os.path.exists(live_repo):
logging.debug("Saving live repository %s to %s", live_repo, old_repo)
shutil.move(live_repo, old_repo)
logging.debug("Making in-progress repository %s live", ip_repo)
shutil.move(ip_repo, live_repo)
if os.path.exists(old_repo):
logging.debug("Removing old repo %s", old_repo)
shutil.rmtree(old_repo)
def make_symlink(repo):
# For historical reasons, our paths are different than the GOC's.
# Make symlinks so that their paths work as well.
symlink = SYMLINK_MAP[repo]
if not os.path.exists(symlink[1]):
logging.debug("Making symlink: %s -> %s" % (symlink[1], symlink[0]))
os.symlink(symlink[0], symlink[1])
else:
logging.debug("Not making symlink: %s already exists" % symlink[1])
#
# SCRIPT BEGINS HERE
#
if DEBUG:
level = logging.DEBUG
else:
level = logging.WARNING
logging.basicConfig(format="%(levelname)s:" + os.path.basename(sys.argv[0]) + ":%(message)s",
level=level)
if len(sys.argv) < 2:
print >> sys.stderr, ("Usage: %s REPO..." % sys.argv[0])
print >> sys.stderr, ("Valid repositories are: ALL," +
",".join(REPO_MAP.keys()))
sys.exit(2)
# validate arguments
if 'ALL' not in sys.argv:
for a in sys.argv[1:]:
if a not in REPO_MAP:
print >> sys.stderr, ("%s is not a valid repository name. Valid "
"repositories are: ALL,%s" %
(a, ",".join(REPO_MAP.keys())))
sys.exit(2)
repos_to_sync = sys.argv[1:]
else:
repos_to_sync = REPO_MAP.keys()
logging.debug("Setting alarm for %d minutes", (GLOBAL_TIMEOUT / 60))
signal.signal(signal.SIGALRM, alarm_handler)
signal.alarm(GLOBAL_TIMEOUT)
for repository in repos_to_sync:
goc_repo, live_repo = REPO_MAP[repository]
logging.debug("*** Updating repository %s via rsync ***"
"\nfrom: %s\nto : %s\n\n" %
(repository, goc_repo, live_repo))
repo_parent = os.path.dirname(live_repo)
repo_bn = os.path.basename(live_repo)
ip_repo = opj(repo_parent, ".in_progress." + repo_bn)
old_repo = opj(repo_parent, ".old." + repo_bn)
lock_file = opj(repo_parent, ".lock." + repo_bn)
if os.path.exists(old_repo):
logging.debug("Removing old repo %s", old_repo)
shutil.rmtree(old_repo)
obtain_lock(lock_file)
try:
try:
start_time = time()
logging.debug("Started at " + ctime())
do_mirror(goc_repo, live_repo, ip_repo, old_repo)
make_symlink(repository)
end_time = time()
logging.debug("Finished at " + ctime())
elapsed_time = end_time - start_time
logging.debug("Elapsed time: %f seconds", elapsed_time)
finally:
release_lock(lock_file)
except Alarm:
sys.exit(14)
except RsyncFailure:
# in case of rsync failure, try the next repository instead of aborting
# everything
continue