Commit 135a8615 authored by Ad Schellevis's avatar Ad Schellevis

(configd) small style fixes

parent abcb4cce
...@@ -32,8 +32,6 @@ ...@@ -32,8 +32,6 @@
function: delivers a process coordinator to handle frontend functions function: delivers a process coordinator to handle frontend functions
""" """
__author__ = 'Ad Schellevis'
import os import os
import sys import sys
import logging import logging
...@@ -44,10 +42,10 @@ import subprocess ...@@ -44,10 +42,10 @@ import subprocess
import modules.processhandler import modules.processhandler
import modules.csconfigparser import modules.csconfigparser
from modules.daemonize import Daemonize from modules.daemonize import Daemonize
import cProfile, pstats import cProfile
# find program path # find program path
if len(__file__.split('/')[:-1]) >0 : if len(__file__.split('/')[:-1]) > 0:
program_path = '/'.join(__file__.split('/')[:-1]) program_path = '/'.join(__file__.split('/')[:-1])
else: else:
program_path = os.getcwd() program_path = os.getcwd()
...@@ -56,6 +54,7 @@ else: ...@@ -56,6 +54,7 @@ else:
sys.path.append(program_path) sys.path.append(program_path)
os.chdir(program_path) os.chdir(program_path)
def get_config(): def get_config():
""" open configuration """ open configuration
""" """
...@@ -63,43 +62,51 @@ def get_config(): ...@@ -63,43 +62,51 @@ def get_config():
cnf.read('conf/configd.conf') cnf.read('conf/configd.conf')
return cnf return cnf
def validate_config(cnf): def validate_config(cnf):
""" validate configuration, exit on missing item """ validate configuration, exit on missing item
:param cnf: config handle
""" """
for config_item in ['socket_filename','pid_filename']: for config_item in ['socket_filename', 'pid_filename']:
if cnf.has_section('main') == False or cnf.has_option('main',config_item) == False: if cnf.has_section('main') == False or cnf.has_option('main', config_item) == False:
print('configuration item main/%s not found in %s/conf/configd.conf'%(config_item,program_path)) print('configuration item main/%s not found in %s/conf/configd.conf' % (config_item, program_path))
sys.exit(0) sys.exit(0)
def main(cnf, simulate=False, single_threaded=False): def main(cnf, simulate=False, single_threaded=False):
""" configd startup """ configd startup
:param cnf: config handle
:param simulate: simulate only
:param single_threaded: start single threaded
""" """
# setup configd environment to use for all configured actions # setup configd environment to use for all configured actions
if not cnf.has_section('environment'): if not cnf.has_section('environment'):
config_environment = os.environ.copy() config_environment = os.environ.copy()
else: else:
config_environment={} config_environment = dict()
for envKey in cnf.items('environment'): for envKey in cnf.items('environment'):
config_environment[envKey[0]] = envKey[1] config_environment[envKey[0]] = envKey[1]
# run process coordinator ( on console or as daemon ) # run process coordinator ( on console or as daemon )
# if command-line arguments contain "emulate", start in emulation mode # if command-line arguments contain "emulate", start in emulation mode
if simulate: if simulate:
proc_handler = modules.processhandler.Handler(socket_filename=cnf.get('main','socket_filename'), proc_handler = modules.processhandler.Handler(socket_filename=cnf.get('main', 'socket_filename'),
config_path='%s/conf'%program_path, config_path='%s/conf' % program_path,
config_environment=config_environment, config_environment=config_environment,
simulation_mode=True) simulation_mode=True)
else: else:
proc_handler = modules.processhandler.Handler(socket_filename=cnf.get('main','socket_filename'), proc_handler = modules.processhandler.Handler(socket_filename=cnf.get('main', 'socket_filename'),
config_path='%s/conf'%program_path, config_path='%s/conf' % program_path,
config_environment=config_environment) config_environment=config_environment)
proc_handler.single_threaded = single_threaded proc_handler.single_threaded = single_threaded
proc_handler.run() proc_handler.run()
def run_watch(): def run_watch():
""" start configd process and restart if it dies unexpected """ start configd process and restart if it dies unexpected
""" """
current_child_pid = None current_child_pid = None
def signal_handler(sig, frame): def signal_handler(sig, frame):
print current_child_pid print current_child_pid
if current_child_pid is not None: if current_child_pid is not None:
...@@ -118,7 +125,7 @@ def run_watch(): ...@@ -118,7 +125,7 @@ def run_watch():
this_config = get_config() this_config = get_config()
validate_config(this_config) validate_config(this_config)
if len(sys.argv) > 1 and 'console' in sys.argv[1:]: if len(sys.argv) > 1 and 'console' in sys.argv[1:]:
print('run %s in console mode'%sys.argv[0]) print('run %s in console mode' % sys.argv[0])
if 'profile' in sys.argv[1:]: if 'profile' in sys.argv[1:]:
# profile configd # profile configd
# for graphical output use gprof2dot: # for graphical output use gprof2dot:
...@@ -160,7 +167,7 @@ else: ...@@ -160,7 +167,7 @@ else:
loghandle = None loghandle = None
# daemonize process # daemonize process
daemon = Daemonize(app=__file__.split('/')[-1].split('.py')[0], daemon = Daemonize(app=__file__.split('/')[-1].split('.py')[0],
pid=this_config.get('main','pid_filename'), pid=this_config.get('main', 'pid_filename'),
action=run_watch, action=run_watch,
logger=loghandle logger=loghandle
) )
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment