|
|
|
#!/usr/bin/python
|
|
|
|
# vim: set fileencoding=utf-8 :
|
|
|
|
|
|
|
|
import os
|
|
|
|
import sys
|
|
|
|
import datetime
|
|
|
|
import argparse
|
|
|
|
import logging
|
|
|
|
import configobj
|
|
|
|
|
|
|
|
# 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',
|
|
|
|
help='write debug messages to stdout')
|
|
|
|
parser.add_argument('-c', '--config', default='/etc/py-i2phosts/maintainer.conf', dest='config_file',
|
|
|
|
help='config file to use')
|
|
|
|
args = parser.parse_args()
|
|
|
|
|
|
|
|
# read config
|
|
|
|
config = configobj.ConfigObj(args.config_file)
|
|
|
|
if 'include' in config:
|
|
|
|
config_included = configobj.ConfigObj(config['include'])
|
|
|
|
config.merge(config_included)
|
|
|
|
|
|
|
|
# django setup
|
|
|
|
DJANGO_SETTINGS_MODULE = 'settings'
|
|
|
|
if 'DJANGO_PROJECT_PATH' in config:
|
|
|
|
DJANGO_PROJECT_PATH = config['DJANGO_PROJECT_PATH']
|
|
|
|
else:
|
|
|
|
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
|
|
|
|
from web.postkey.models import i2phost
|
|
|
|
from web.lib.utils import get_logger
|
|
|
|
|
|
|
|
# configure logger
|
|
|
|
if args.debug == True:
|
|
|
|
log_level = 'debug'
|
|
|
|
log_file = None
|
|
|
|
else:
|
|
|
|
log_level = config['log_level']
|
|
|
|
log_file = config['log_file']
|
|
|
|
log = get_logger(filename=log_file, log_level=log_level)
|
|
|
|
|
|
|
|
all_hosts = i2phost.objects.all()
|
|
|
|
for host in all_hosts:
|
|
|
|
# how long host was added
|
|
|
|
dl = datetime.datetime.now() - host.date_added
|
|
|
|
if host.last_seen == None:
|
|
|
|
# delete external hosts which we never seen after 365 days of inactivity
|
|
|
|
if host.external == True:
|
|
|
|
if dl > datetime.timedelta(days=365):
|
|
|
|
log.info('deleting %s, reason: external host, never seen for 365 days', host.name)
|
|
|
|
host.delete()
|
|
|
|
continue
|
|
|
|
# delete hosts added by us and never seen after 14 days of inactivity
|
|
|
|
else:
|
|
|
|
if dl > datetime.timedelta(days=14):
|
|
|
|
log.info('deleting %s, reason: internal host, never seen for 14 days', host.name)
|
|
|
|
host.delete()
|
|
|
|
continue
|
|
|
|
else:
|
|
|
|
# configure registration period for hosts
|
|
|
|
# FIXME: make intervals configurable via config or/and commandline
|
|
|
|
if host.external == True:
|
|
|
|
timedelta = datetime.timedelta(days=30)
|
|
|
|
else:
|
|
|
|
timedelta = datetime.timedelta(days=60)
|
|
|
|
# get current host expiration date from database
|
|
|
|
if host.expires == None:
|
|
|
|
# workaround for situation when we updating expires first time
|
|
|
|
expires_current = datetime.datetime.now().date()
|
|
|
|
else:
|
|
|
|
expires_current = host.expires
|
|
|
|
# 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
|
|
|
|
if host.expires < datetime.datetime.now().date():
|
|
|
|
log.info('deactivating %s, reason: expired', host.name)
|
|
|
|
host.activated = False
|
|
|
|
# if not expired and added more than 3 days ago and approved then activate
|
|
|
|
elif dl > datetime.timedelta(days=3) and host.activated == False and host.approved == True:
|
|
|
|
log.info('activating %s, reason: host up and added more than 3 days ago', host.name)
|
|
|
|
host.activated = True
|
|
|
|
# if expired two years ago then delete
|
|
|
|
dl_e = datetime.datetime.now().date() - host.expires
|
|
|
|
if dl_e > datetime.timedelta(days=730):
|
|
|
|
log.info('deleting %s, reason: expired 2 years ago', host.name)
|
|
|
|
host.delete()
|
|
|
|
continue
|
|
|
|
host.save()
|