1
0
mirror of https://github.com/r4sas/py-i2phosts synced 2025-08-26 21:52:40 +00:00

py-i2phosts-checker: remove daemonization

We will use separate master process.
This commit is contained in:
Hidden Z 2010-11-01 21:04:55 +00:00
parent 5957ee9bbe
commit 00f066fd2d

View File

@ -2,34 +2,75 @@
import os import os
import sys import sys
import pwd
import errno
import argparse import argparse
import datetime import datetime
import time
import hashlib import hashlib
import base64 import base64
import configobj import configobj
import subprocess
import daemon
import daemon.pidlockfile
from i2p import samclasses from i2p import samclasses
from i2p import socket from i2p import socket
def check(): # parse command line options
all_hosts = i2phost.objects.all() parser = argparse.ArgumentParser(
description='Hosts checker for py-i2phosts.',
epilog='Report bugs to http://zzz.i2p/topics/733')
parser.add_argument('-d', '--debug', action='store_true',
help='set loglevel to debug and write messages to stdout'),
parser.add_argument('-v', '--verbose', action='store_true',
help='set loglevel to info and write messages to stdout'),
parser.add_argument('-c', '--config', default='/etc/py-i2phosts/checker.conf', dest='config_file',
help='config file to use')
args = parser.parse_args()
# determine SAM interface address # read config
if 'sam_addr' in config: spec = '''
log_file = string(default='/var/log/py-i2phosts/master.log')
log_level = option('debug', 'info', 'warning', 'error', 'critical', default='info')
'''
spec = spec.split('\n')
config = configobj.ConfigObj(args.config_file, configspec=spec)
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
from web.lib.utils import validate_config
# validate config
validate_config(config)
# configure logger
if args.debug == True:
log_level = 'debug'
log_file = None
elif args.verbose == True:
log_level = 'info'
log_file = None
else:
log_file, log_level = check_logger_options(config)
log = get_logger(filename=log_file, log_level=log_level)
# determine SAM interface address
if 'sam_addr' in config:
sam_addr = config['sam_addr'] sam_addr = config['sam_addr']
else: else:
log.warning('SAM address isn\'t specified in config, falling back to localhost') log.warning('SAM address isn\'t specified in config, falling back to localhost')
sam_addr = '127.0.0.1:7656' sam_addr = '127.0.0.1:7656'
S = samclasses.BaseSession(sam_addr) S = samclasses.BaseSession(sam_addr)
log.info('starting check') all_hosts = i2phost.objects.all()
for host in all_hosts: log.info('starting check')
for host in all_hosts:
log.debug('testing %s', host.name) log.debug('testing %s', host.name)
# get b32 address from full dest key # get b32 address from full dest key
dest = host.b64hash dest = host.b64hash
@ -47,96 +88,5 @@ def check():
# update lastseen timestamp # update lastseen timestamp
host.last_seen = datetime.datetime.now() host.last_seen = datetime.datetime.now()
host.save() host.save()
S.close() S.close()
log.info('check finished') log.info('check finished')
def run_prog(prog):
try:
log.info('launching %s (see his log for details)', prog)
p = subprocess.Popen([prog], shell=False, stdin=None,
stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
except OSError, e:
log.error('failed to exec %s: %s', prog, e)
if e.errno == errno.ENOENT:
log.error('maybe it isn\'t in PATH')
else:
out = p.communicate()[0]
if out:
log.info('got output from %s: %s', prog, out)
def main():
while True:
if config.as_bool('run_fetcher'):
run_prog('py-i2phosts-fetcher')
check()
if config.as_bool('run_maint'):
run_prog('py-i2phosts-maint')
if config.as_bool('run_builder'):
run_prog('py-i2phosts-builder')
if args.debug or args.verbose:
sys.exit(0)
else:
time.sleep(float(config['check_interval']))
# parse command line options
parser = argparse.ArgumentParser(
description='Hosts checker for py-i2phosts.',
epilog='Report bugs to http://zzz.i2p/topics/733')
parser.add_argument('-d', '--debug', action='store_true',
help='set loglevel to debug and write messages to stdout'),
parser.add_argument('-v', '--verbose', action='store_true',
help='set loglevel to info and write messages to stdout'),
parser.add_argument('-c', '--config', default='/etc/py-i2phosts/checker.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
from web.lib.utils import check_logger_options
# configure logger
if args.debug == True:
log_level = 'debug'
log_file = None
elif args.verbose == True:
log_level = 'info'
log_file = None
else:
log_file, log_level = check_logger_options(config)
if not args.debug and not args.verbose:
# get pid object for daemon
if 'pid_file' in config:
pid = daemon.pidlockfile.TimeoutPIDLockFile(config['pid_file'], 10)
else:
sys.stderr.write('"pid_file" missing in config\n')
sys.exit(1)
# create daemon context
d = daemon.DaemonContext(pidfile=pid, umask=077)
# write stderr to logfile # FIXME: and how we will deal with log rotation?
logfile = open(config['log_file'], 'a')
d.stderr = logfile
d.stdout = logfile
# drop privileges when started as root
if os.getuid() == 0:
runas = '_pyi2phosts'
pw_entry = pwd.getpwnam(runas)
d.uid = pw_entry[2]
d.gid = pw_entry[3]
d.open() # become daemon
log = get_logger(filename=log_file, log_level=log_level)
main()