configd.py 4.3 KB
Newer Older
1
#!/usr/local/bin/python2.7
2

Ad Schellevis's avatar
Ad Schellevis committed
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
"""
    Copyright (c) 2014 Ad Schellevis
    All rights reserved.

    Redistribution and use in source and binary forms, with or without
    modification, are permitted provided that the following conditions are met:

    1. Redistributions of source code must retain the above copyright notice,
     this list of conditions and the following disclaimer.

    2. Redistributions in binary form must reproduce the above copyright
     notice, this list of conditions and the following disclaimer in the
     documentation and/or other materials provided with the distribution.

    THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
    INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
    AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
    AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
    OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
    SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
    INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
    CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
    ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
    POSSIBILITY OF SUCH DAMAGE.

    --------------------------------------------------------------------------------------
29

30
    package : configd
Ad Schellevis's avatar
Ad Schellevis committed
31 32
    function: delivers a process coordinator to handle frontend functions
"""
33

Ad Schellevis's avatar
Ad Schellevis committed
34 35 36 37 38
__author__ = 'Ad Schellevis'

import os
import sys
import modules.processhandler
39
import modules.csconfigparser
Ad Schellevis's avatar
Ad Schellevis committed
40
from modules.daemonize import Daemonize
41
import cProfile, pstats
Ad Schellevis's avatar
Ad Schellevis committed
42 43 44 45 46 47 48 49 50 51 52 53

# find program path
if len(__file__.split('/')[:-1]) >0 :
    program_path = '/'.join(__file__.split('/')[:-1])
else:
    program_path = os.getcwd()

# set working directory to program_path
sys.path.append(program_path)
os.chdir(program_path)

# open configuration
54
cnf = modules.csconfigparser.CSConfigParser()
55
cnf.read('conf/configd.conf')
Ad Schellevis's avatar
Ad Schellevis committed
56 57 58 59

# validate configuration, exit on missing item
for config_item in  ['socket_filename','pid_filename']:
    if cnf.has_section('main') == False or cnf.has_option('main',config_item) == False:
60
        print('configuration item main/%s not found in %s/conf/configd.conf'%(config_item,program_path))
Ad Schellevis's avatar
Ad Schellevis committed
61 62
        sys.exit(0)

63 64 65 66 67 68 69 70
# setup configd environment to use for all configured actions
if not cnf.has_section('environment'):
    config_environment = os.environ.copy()
else:
    config_environment={}
    for envKey in cnf.items('environment'):
        config_environment[envKey[0]] = envKey[1]

Ad Schellevis's avatar
Ad Schellevis committed
71 72 73
# run process coordinator ( on console or as daemon )
# if command-line arguments contain "emulate",  start in emulation mode
if len(sys.argv) > 1 and 'simulate' in sys.argv[1:]:
74 75 76 77
    proc_handler = modules.processhandler.Handler(socket_filename=cnf.get('main','socket_filename'),
                                                  config_path='%s/conf'%program_path,
                                                  config_environment=config_environment,
                                                  simulation_mode=True)
Ad Schellevis's avatar
Ad Schellevis committed
78
else:
79 80 81
    proc_handler = modules.processhandler.Handler(socket_filename=cnf.get('main','socket_filename'),
                                                  config_path='%s/conf'%program_path,
                                                  config_environment=config_environment)
Ad Schellevis's avatar
Ad Schellevis committed
82 83 84

if len(sys.argv) > 1 and 'console' in sys.argv[1:]:
    print('run %s in console mode'%sys.argv[0])
85 86 87 88 89 90 91
    if 'profile' in sys.argv[1:]:
        # profile configd
        # for graphical output use gprof2dot:
        #   gprof2dot -f pstats /tmp/configd.profile  -o /tmp/callingGraph.dot
        # (https://code.google.com/p/jrfonseca/wiki/Gprof2Dot)
        print ("...<ctrl><c> to stop profiling")
        profile = cProfile.Profile()
92
        profile.enable(subcalls=True)
93
        try:
94
            proc_handler.single_threaded = True
95 96 97 98 99 100 101 102 103
            proc_handler.run()
        except KeyboardInterrupt:
            pass
        except:
            raise
        profile.disable()
        profile.dump_stats('/tmp/configd.profile')
    else:
        proc_handler.run()
Ad Schellevis's avatar
Ad Schellevis committed
104 105 106 107 108 109
else:
    # daemonize process
    daemon = Daemonize(app=__file__.split('/')[-1].split('.py')[0], pid=cnf.get('main','pid_filename'), action=proc_handler.run)
    daemon.start()

sys.exit(0)