mirror of https://github.com/r4sas/py-i2phosts
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
120 lines
4.2 KiB
120 lines
4.2 KiB
14 years ago
|
#!/usr/bin/python
|
||
14 years ago
|
# vim: set fileencoding=utf-8 :
|
||
14 years ago
|
|
||
|
import os
|
||
|
import sys
|
||
|
import datetime
|
||
|
import argparse
|
||
|
import logging
|
||
14 years ago
|
import configobj
|
||
14 years ago
|
|
||
|
# parse command line options
|
||
|
parser = argparse.ArgumentParser(
|
||
|
description='Hosts maintainer for py-i2phosts.',
|
||
|
epilog='Report bugs to http://zzz.i2p/topics/733')
|
||
|
parser.add_argument('-d', '--debug', action='store_true',
|
||
14 years ago
|
help='write debug messages to stdout')
|
||
14 years ago
|
parser.add_argument('-c', '--config', default='/etc/py-i2phosts/maintainer.conf', dest='config_file',
|
||
|
help='config file to use')
|
||
14 years ago
|
args = parser.parse_args()
|
||
|
|
||
14 years ago
|
# read config
|
||
14 years ago
|
spec = '''
|
||
14 years ago
|
log_file = string(default='/var/log/py-i2phosts/maintainer.log')
|
||
|
log_level = option('debug', 'info', 'warning', 'error', 'critical', default='info')
|
||
14 years ago
|
external_inactive_max = integer(default=365)
|
||
|
internal_inactive_max = integer(default=14)
|
||
|
external_expires = integer(default=30)
|
||
|
internal_expires = integer(default=30)
|
||
|
activate_min_delay = integer(default=3)
|
||
|
keep_expired = integer(default=730)
|
||
|
'''
|
||
|
spec = spec.split('\n')
|
||
|
config = configobj.ConfigObj(args.config_file, configspec=spec)
|
||
14 years ago
|
if 'include' in config:
|
||
|
config_included = configobj.ConfigObj(config['include'])
|
||
|
config.merge(config_included)
|
||
|
|
||
14 years ago
|
# django setup
|
||
|
DJANGO_SETTINGS_MODULE = 'settings'
|
||
|
if 'DJANGO_PROJECT_PATH' in config:
|
||
|
DJANGO_PROJECT_PATH = config['DJANGO_PROJECT_PATH']
|
||
14 years ago
|
else:
|
||
14 years ago
|
DJANGO_PROJECT_PATH = os.path.dirname(sys.argv[0]) + '/web'
|
||
|
sys.path.insert(1, DJANGO_PROJECT_PATH)
|
||
|
os.environ['DJANGO_SETTINGS_MODULE'] = DJANGO_SETTINGS_MODULE
|
||
14 years ago
|
from pyi2phosts.postkey.models import i2phost
|
||
|
from pyi2phosts.lib.utils import get_logger
|
||
|
from pyi2phosts.lib.utils import validate_config
|
||
14 years ago
|
|
||
|
# validate config
|
||
|
validate_config(config)
|
||
14 years ago
|
|
||
14 years ago
|
# configure logger
|
||
|
if args.debug == True:
|
||
|
log_level = 'debug'
|
||
|
log_file = None
|
||
|
else:
|
||
14 years ago
|
log_level = config['log_level']
|
||
|
log_file = config['log_file']
|
||
14 years ago
|
log = get_logger(filename=log_file, log_level=log_level)
|
||
14 years ago
|
|
||
|
all_hosts = i2phost.objects.all()
|
||
14 years ago
|
log.info('starting maintenance')
|
||
14 years ago
|
for host in all_hosts:
|
||
|
# how long host was added
|
||
14 years ago
|
dl = datetime.datetime.utcnow() - host.date_added
|
||
14 years ago
|
if host.last_seen == None:
|
||
14 years ago
|
# delete external hosts which we never seen after X days of inactivity
|
||
14 years ago
|
if host.external == True:
|
||
14 years ago
|
if dl > datetime.timedelta(days=config['external_inactive_max']):
|
||
|
log.info('deleting %s, reason: external host, never seen for %s days',
|
||
|
host.name, config['external_inactive_max'])
|
||
14 years ago
|
host.delete()
|
||
|
continue
|
||
14 years ago
|
# delete hosts added by us and never seen after X days of inactivity
|
||
14 years ago
|
else:
|
||
14 years ago
|
if dl > datetime.timedelta(days=config['internal_inactive_max']):
|
||
|
log.info('deleting %s, reason: internal host, never seen for %s days',
|
||
|
host.name, config['internal_inactive_max'])
|
||
14 years ago
|
host.delete()
|
||
|
continue
|
||
|
else:
|
||
|
# configure registration period for hosts
|
||
|
if host.external == True:
|
||
14 years ago
|
timedelta = datetime.timedelta(days=config['external_expires'])
|
||
14 years ago
|
else:
|
||
14 years ago
|
timedelta = datetime.timedelta(days=config['internal_expires'])
|
||
14 years ago
|
# get current host expiration date from database
|
||
14 years ago
|
if host.expires == None:
|
||
|
# workaround for situation when we updating expires first time
|
||
14 years ago
|
expires_current = datetime.datetime.utcnow().date()
|
||
14 years ago
|
else:
|
||
|
expires_current = host.expires
|
||
14 years ago
|
# calculate new expiration date
|
||
|
expires_new = host.last_seen + timedelta
|
||
|
expires_new = expires_new.date()
|
||
|
# update expiration date only if changed
|
||
|
if expires_new > expires_current:
|
||
|
log.debug('updating expires for %s', host.name)
|
||
|
host.expires = expires_new
|
||
|
# deactivate if expired
|
||
14 years ago
|
min_dl = datetime.timedelta(days=config['activate_min_delay'])
|
||
14 years ago
|
if host.expires < datetime.datetime.utcnow().date():
|
||
14 years ago
|
if host.activated == True:
|
||
|
log.info('deactivating %s, reason: expired', host.name)
|
||
|
host.activated = False
|
||
14 years ago
|
# if not expired and added more than X days ago and approved then activate
|
||
|
elif dl > min_dl and host.activated == False and host.approved == True:
|
||
|
log.info('activating %s, reason: host up and added more than %s days ago',
|
||
|
host.name, config['activate_min_delay'])
|
||
14 years ago
|
host.activated = True
|
||
14 years ago
|
# if expired X days ago then delete
|
||
14 years ago
|
dl_e = datetime.datetime.utcnow().date() - host.expires
|
||
14 years ago
|
if dl_e > datetime.timedelta(days=config['keep_expired']):
|
||
|
log.info('deleting %s, reason: expired % days ago',
|
||
|
host.name, config['keep_expired'])
|
||
14 years ago
|
host.delete()
|
||
|
continue
|
||
|
host.save()
|