def command(self): "command" self.init() if acquire_lock('dbclean', self.conf): try: if self.options.days > 0: days = self.options.days else: days = int(self.conf.get('baruwa.messages.keep.days', 30)) if self.options.adays > 0: adays = self.options.adays else: adays = int(self.conf.get('baruwa.archive.keep.days', 90)) interval = datetime.timedelta(days=days) archive_interval = datetime.timedelta(days=adays) current_date = arrow.utcnow().datetime msgs_date = current_date - interval last_achive_date = current_date - archive_interval releases_date = current_date - datetime.timedelta(days=2) # process messages table process_messages(msgs_date) # process archive table prune_archive(last_achive_date) # process message status table prune_table('messagestatus', last_achive_date) # process releases table prune_table('releases', releases_date) # process awl table prune_table('awl', msgs_date) finally: Session.close() release_lock('dbclean', self.conf)
def command(self): "command" self.init() if asbool(self.conf.get('ms.quarantine.shared')): lock_name = 'cleanquarantine' else: lock_name = 'cleanquarantine-%s' % system_hostname() if acquire_lock(lock_name, self.conf): try: days_to_retain = int( self.conf.get('ms.quarantine.days_to_keep', 0)) quarantine_dir = get_config_option('QuarantineDir') if (quarantine_dir.startswith(('/etc', '/lib', '/home', '/bin', '/sbin', '..'))): return False if ((not os.path.exists(quarantine_dir)) or (days_to_retain == 0)): return False ignore_dirs = ['spam', 'mcp', 'nonspam'] def process_dir(dirs, process_path, direc): "process dirs" if os.path.exists(os.path.join(process_path, direc)): dirs.extend([f for f in os.listdir( os.path.join(process_path, direc))]) dirs = [f for f in os.listdir(quarantine_dir) if os.path.isdir(os.path.join(quarantine_dir, f)) and QDIR.match(f) and should_be_pruned(f, days_to_retain)] dirs.sort() for direc in dirs: process_path = os.path.join(quarantine_dir, direc) ids = [f for f in os.listdir(process_path) if f not in ignore_dirs] for ignore_dir in ignore_dirs: process_dir(ids, process_path, ignore_dir) year, month, day = (int(direc[:4]), int(direc[4:-2]), int(direc[6:])) startdate = datetime.datetime(year, month, day, 00, 00, 00) enddate = datetime.datetime(year, month, day, 23, 59, 59) localzone = make_tz(self.conf['baruwa.timezone']) startdate = localzone.localize(startdate) enddate = localzone.localize(enddate) startdate = pytz.utc.normalize( startdate.astimezone(pytz.utc)) enddate = pytz.utc.normalize(enddate.astimezone(pytz.utc)) sql = Message.__table__.update().where(and_( Message.messageid.in_(ids), Message.timestamp.between(startdate, enddate) )).values(isquarantined=0) Session.bind.execute(sql) if (os.path.isabs(process_path) and (not os.path.islink(process_path))): try: shutil.rmtree(process_path) except shutil.Error: print >> sys.stderr, ("Failed to remove %(path)s" % dict(path=process_path)) else: print >> sys.stderr, ("%(path)s is a symlink skipping" % dict(path=process_path)) finally: Session.close() release_lock(lock_name, self.conf)