def test_2(self):
        print('Settings fixed (2)')

        # Get configuration from only one file ...
        print("read configuration")
        cfg = Settings(
            os.path.join(os.path.abspath(os.path.dirname(__file__)),
                         'settings.fr'))
        found_cfg_files = cfg.read('Alignak-WebUI')
        assert found_cfg_files
        alignak_webui.set_app_config(cfg)

        # Application configuration is loaded
        self.config = alignak_webui.get_app_config()
        assert self.config
        # Not defined in settings.fr
        assert self.config.get('alignak_backend',
                               'default_value') == 'default_value'
        # Defined in settings.fr
        assert self.config.get('locale', 'en_US') == 'fr_FR'
        # Variable defined in the settings.cfg test file ...
        assert self.config.get('test_mode') == '1'

        # Alignak-WebUI object is initialized
        self.alignak_webui = get_app_webui()
        assert self.alignak_webui
Beispiel #2
0
def setup_module():
    print("")

    # Get configuration from only one file ...
    print("read configuration")
    cfg = Settings("settings.cfg")
    found_cfg_files = cfg.read('Alignak-WebUI')
    assert found_cfg_files
    set_app_config(cfg)
Beispiel #3
0
def setup_module(module):
    print("")

    # Get configuration from only one file ...
    print("read configuration")
    cfg = Settings(
        os.path.join(os.path.abspath(os.path.dirname(__file__)),
                     'settings.cfg'))
    found_cfg_files = cfg.read('Alignak-WebUI')
    assert found_cfg_files
    set_app_config(cfg)
Beispiel #4
0
    def test_2_found(self):
        # Relative file path
        cfg_file = "settings.cfg"
        print('Required configuration file:', cfg_file)
        # Normal use case ... default files and application name ...
        # And get config object ...
        app_config = Settings(cfg_file)
        found_cfg_files = app_config.read('Alignak-WebUI')
        print('Found:', found_cfg_files)
        assert found_cfg_files

        print(app_config)
        self.assert_('about_name' in app_config)
        self.assert_(app_config['about_name'] == 'Alignak-WebUI')
Beispiel #5
0
    def test_1_found_not_found(self):
        print('test configuration file')

        # Default configuration - not found because of test directory location !
        print('Search default configuration file ...')
        found_cfg_files = Settings().read(None)
        print('Found:', found_cfg_files)
        assert found_cfg_files is None
        # Normal use case ... default files and application name ...
        # But ! do not get config object ...
        found_cfg_files = Settings().read('Alignak-WebUI')
        print('Found:', found_cfg_files)
        assert found_cfg_files

        # Relative file path
        cfg_file = "settings.cfg"
        print('Required configuration file:', cfg_file)
        found_cfg_files = Settings(cfg_file).read(None)
        print('Found:', found_cfg_files)
        assert found_cfg_files is None
        found_cfg_files = Settings(cfg_file).read('Alignak-WebUI')
        print('Found:', found_cfg_files)
        assert found_cfg_files

        # Absolute file path
        cfg_file = os.path.dirname(os.path.abspath(__file__)) + "/settings.cfg"
        print('Required configuration file:', cfg_file)
        found_cfg_files = Settings(cfg_file).read(None)
        print('Found:', found_cfg_files)
        assert found_cfg_files is None
        found_cfg_files = Settings(cfg_file).read('Alignak-WebUI')
        print('Found:', found_cfg_files)
        assert found_cfg_files

        # Absolute file path - bad formed file
        cfg_file = os.path.dirname(
            os.path.abspath(__file__)) + "/test_bad_settings.txt"
        print('Required configuration file:', cfg_file)
        found_cfg_files = Settings(cfg_file).read(None)
        print('Found:', found_cfg_files)
        assert found_cfg_files is None
        found_cfg_files = Settings(cfg_file).read('Alignak-WebUI')
        print('Found:', found_cfg_files)
        assert found_cfg_files is None
Beispiel #6
0
def load_config(app=None, cfg_filenames=None):
    # pylint: disable=unused-argument
    """
    Load plugin configuration
    """
    global hosts_parameters, hosts_filenames

    if not cfg_filenames:
        cfg_filenames = hosts_filenames
    else:
        hosts_filenames = cfg_filenames

    logger.info("Read plugin configuration file: %s", cfg_filenames)

    # Read configuration file
    hosts_parameters = Settings(cfg_filenames)
    config_file = hosts_parameters.read('hosts')
    logger.info("Plugin configuration read from: %s", config_file)
    if not hosts_parameters:
        return False
    logger.info("Plugin configuration: %s", hosts_parameters)
    return True
Beispiel #7
0
def main():  # pragma: no cover, not mesured by coverage!
    # pylint: disable=redefined-variable-type, global-statement
    """
        Called when this module is started from shell
    """
    global cfg_file, app_config, app_webui

    # ----------------------------------------------------------------------------------------------
    # Command line parameters
    args = {
        '--debug': False,
        '--backend': None,
        '--hostname': None,
        '--port': None,
        '--exit': False
    }

    if __name__ == "__main__":  # pragma: no cover, not mesured by coverage!
        try:
            args = docopt(__doc__, version=manifest['version'])
        except DocoptExit:
            print("Command line parsing error")
            exit(64)
    # Application settings
    # ----------------------------------------------------------------------------------------------
    # Configuration file path in command line parameters
    if '<cfg_file>' in args:
        cfg_file = args['<cfg_file>']

        if cfg_file and isinstance(cfg_file, list):
            cfg_file = cfg_file[0]

        # Read configuration file
        app_config = Settings(cfg_file)
        new_config_file = app_config.read(manifest['name'])
        print("Configuration read from: %s" % new_config_file)
        if not app_config:  # pragma: no cover, should never happen
            print("Required configuration file not found.")
            exit(1)

    # Store application name in the configuration
    app_config['name'] = manifest['name']

    if '--debug' in args and args['--debug']:  # pragma: no cover, not mesured by coverage!
        app_config['debug'] = '1'
        print("Application is in debug mode from command line")

    if os.environ.get('WEBUI_DEBUG'):  # pragma: no cover, not mesured by coverage!
        app_config['debug'] = '1'
        print("Application is in debug mode from environment")

    # Applications backend URL
    if args['--backend']:  # pragma: no cover, not mesured by coverage!
        app_config['alignak_backend'] = args['--backend']

    # WebUI server configuration
    if args['--hostname']:  # pragma: no cover, not mesured by coverage!
        app_config['host'] = args['--hostname']
    if args['--port']:  # pragma: no cover, not mesured by coverage!
        app_config['port'] = args['--port']

    # Make the configuration available globally for the package
    set_app_config(app_config)

    # Make the application available globally for the package
    app_webui = set_app_webui(WebUI(app_config))

    try:
        if args['--exit']:
            print("Application exit because of command line parameter")
            exit(99)

        # Run application server...
        run(
            app=webapp,
            host=app_config.get('host', '127.0.0.1'),
            port=int(app_config.get('port', 5001)),
            debug=(app_config.get('debug', '0') == '1'),
            server=app_config.get('http_backend', 'cherrypy')
        )
    except Exception as e:
        logger.error("Application run failed, exception: %s / %s", type(e), str(e))
        logger.info("Backtrace: %s", traceback.format_exc())
        logger.info("stopping backend livestate thread...")
        exit(2)
Beispiel #8
0
logger = getLogger(__pkg_name__)

cfg_file = None

# Test mode for the application
if os.environ.get('TEST_WEBUI'):
    print("Application is in test mode")
else:  # pragma: no cover - tests are run in test mode...
    print("Application is in production mode")

if os.environ.get('ALIGNAK_WEBUI_CONFIGURATION_FILE'):
    cfg_file = os.environ.get('ALIGNAK_WEBUI_CONFIGURATION_FILE')
    print("Application configuration file name from environment: %s" % cfg_file)

# Read configuration file
app_config = Settings(cfg_file)
config_file = app_config.read(manifest['name'])
print("Configuration read from: %s" % config_file)
if not app_config:  # pragma: no cover, should never happen
    print("Required configuration file not found.")
    exit(1)

# Store application name in the configuration
app_config['name'] = manifest['name']

# Debug mode for the application (run Bottle in debug mode)
app_config['debug'] = (app_config.get('debug', '0') == '1')
print("Application debug mode: %s" % app_config['debug'])

if __name__ != "__main__":
    # Make the configuration available globally for the package