i was straight-up goofin'

This commit is contained in:
brent s 2018-11-07 12:40:37 -05:00
parent b27ee82a9d
commit b17646ea4f

View File

@ -2,6 +2,7 @@


import argparse import argparse
import configparser import configparser
import copy
import datetime import datetime
import os import os
import pprint import pprint
@ -34,6 +35,8 @@ opts = [
] ]


def sync(args): def sync(args):
# TODO: this should be a class, probably, instead as there's a lot of shared data across what should be multiple
# functions.
with open(os.devnull, 'w') as devnull: with open(os.devnull, 'w') as devnull:
mntchk = subprocess.run(['findmnt', args['mount']], stdout = devnull, stderr = devnull) mntchk = subprocess.run(['findmnt', args['mount']], stdout = devnull, stderr = devnull)
if mntchk.returncode != 0: if mntchk.returncode != 0:
@ -52,8 +55,17 @@ def sync(args):
cmd = [rsync] # the path to the binary cmd = [rsync] # the path to the binary
cmd.extend(opts) # the arguments cmd.extend(opts) # the arguments
# TODO: implement repos here? # TODO: implement repos here?
cmd.append(os.path.join(args['mirror'], '.')) # the path on the remote mirror # end TODO
cmd.append(os.path.join(args['destination'], '.')) # the local destination # The https://git.server-speed.net/users/flo/bin/tree/syncrepo.sh script uses http(s). to check for lastupdate.
# I don't, because not all mirrors *have* http(s).
check_cmd = copy.deepcopy(cmd)
check_cmd.append(os.path.join(args['mirror'], 'lastupdate'))
check_cmd.append(os.path.join(args['destination'], 'lastupdate'))
update_cmd = copy.deepcopy(cmd)
update_cmd.append(os.path.join(args['mirror'], 'lastsync'))
update_cmd.append(os.path.join(args['destination'], 'lastsync'))
cmd.append(os.path.join(args['mirror'], '.')) # the path on the remote mirror (full sync)
cmd.append(os.path.join(args['destination'], '.')) # the local destination (full sync)
if os.path.isfile(args['lockfile']): if os.path.isfile(args['lockfile']):
with open(args['lockfile'], 'r') as f: with open(args['lockfile'], 'r') as f:
existingpid = f.read().strip() existingpid = f.read().strip()
@ -65,11 +77,30 @@ def sync(args):
else: else:
with open(args['lockfile'], 'w') as f: with open(args['lockfile'], 'w') as f:
f.write(str(os.getpid())) f.write(str(os.getpid()))
# determine if we need to do a full sync.
# TODO: clean this up. there's a lot of code duplication here, and it should really be a function.
with open(os.path.join(args['destination'], 'lastupdate'), 'r') as f:
oldupdate = datetime.datetime.utcfromtimestamp(int(f.read().strip()))
with open(os.devnull, 'wb') as devnull:
# TODO: when i clean this up, change this to do error detection
c = subprocess.run(check_cmd, stdout = devnull, stderr = devnull)
c2 = subprocess.run(update_cmd, stdout = devnull, stderr = devnull)
with open(os.path.join(args['destination'], 'lastupdate'), 'r') as f:
newupdate = datetime.datetime.utcfromtimestamp(int(f.read().strip()))
if newupdate > oldupdate:
with open(args['logfile'], 'a') as log: with open(args['logfile'], 'a') as log:
c = subprocess.run(cmd, stdout = log, stderr = subprocess.PIPE) c = subprocess.run(cmd, stdout = log, stderr = subprocess.PIPE)
now = int(datetime.datetime.timestamp(datetime.datetime.utcnow())) now = int(datetime.datetime.timestamp(datetime.datetime.utcnow()))
with open(os.path.join(args['destination'], 'lastsync'), 'w') as f: with open(os.path.join(args['destination'], 'lastsync'), 'w') as f:
f.write(str(now) + '\n') f.write(str(now) + '\n')
else:
# No-op. Stderr should be empty.
c = subprocess.run(['echo'], stdout = subprocess.PIPE, stderr = subprocess.PIPE)
now = int(datetime.datetime.timestamp(datetime.datetime.utcnow()))
# always write the "lastcheck" timestamp. This is something I personally do since other mirrors only update
# lastsync when syncs are needed.
with open(os.path.join(args['destination'], 'lastcheck'), 'w') as f:
f.write(str(now) + '\n')
os.remove(args['lockfile']) os.remove(args['lockfile'])
# Only report errors at the end of the run if we aren't running in cron. Otherwise, log them. # Only report errors at the end of the run if we aren't running in cron. Otherwise, log them.
errors = c.stderr.decode('utf-8').splitlines() errors = c.stderr.decode('utf-8').splitlines()
@ -80,7 +111,7 @@ def sync(args):
print('Broken upstream symlink: {0}'.format(e.split()[1].replace('"', ''))) print('Broken upstream symlink: {0}'.format(e.split()[1].replace('"', '')))
else: else:
print(e) print(e)
else: elif errors:
with open(args['logfile'], 'a') as f: with open(args['logfile'], 'a') as f:
for e in errors: for e in errors:
f.write('{0}\n'.format(e)) f.write('{0}\n'.format(e))