configd.py 4.35 KB
Newer Older
Ad Schellevis's avatar
Ad Schellevis committed
1 2 3 4
#!/usr/local/bin/python2.7
"""
    Copyright (c) 2014 Ad Schellevis

Ad Schellevis's avatar
Ad Schellevis committed
5
    part of OPNsense (https://www.opnsense.org/)
Ad Schellevis's avatar
Ad Schellevis committed
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30

    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.

    --------------------------------------------------------------------------------------
31
    package : configd
Ad Schellevis's avatar
Ad Schellevis committed
32 33 34 35 36 37 38 39 40 41 42
    function: delivers a process coordinator to handle frontend functions



"""
__author__ = 'Ad Schellevis'

#
import os
import sys
import modules.processhandler
43
import modules.csconfigparser
Ad Schellevis's avatar
Ad Schellevis committed
44
from modules.daemonize import Daemonize
45
import cProfile, pstats
Ad Schellevis's avatar
Ad Schellevis committed
46 47 48 49 50 51 52 53 54 55 56 57

# 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
58
cnf = modules.csconfigparser.CSConfigParser()
59
cnf.read('conf/configd.conf')
Ad Schellevis's avatar
Ad Schellevis committed
60 61 62 63

# 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:
64
        print('configuration item main/%s not found in %s/conf/configd.conf'%(config_item,program_path))
Ad Schellevis's avatar
Ad Schellevis committed
65 66
        sys.exit(0)

67 68 69 70 71 72 73 74
# 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
75 76 77
# 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:]:
78 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,
                                                  simulation_mode=True)
Ad Schellevis's avatar
Ad Schellevis committed
82
else:
83 84 85
    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
86 87 88

if len(sys.argv) > 1 and 'console' in sys.argv[1:]:
    print('run %s in console mode'%sys.argv[0])
89 90 91 92 93 94 95
    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()
96
        profile.enable(subcalls=True)
97
        try:
98
            proc_handler.single_threaded = True
99 100 101 102 103 104 105 106 107
            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
108 109 110 111 112 113
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)