Esempio n. 1
0
def loadDaemon():
    ''' Load the daemon configuration file.
    '''
    args = hlm_args.args()
    configFile = args.daemonConfig
    if configFile == None:
        configFile = hlm_paths.daemonConfig()
    config = configparser.SafeConfigParser()
    try:
        config.read(configFile)
    except SystemExit:
        raise
    except BaseException as exc:
        raise FatalError(_('Can\'t load the daemon configuration file {0}: {1}').format(quote(configFile), exc))

    try:
        if config.sections() != ['daemon']:
            raise Exception(_('only the section {0} is allowed in this file.').format('[daemon]'))
        options = config.options('daemon')
        _checkAlienDirectives(['credentials', 'user', 'group', 'ping_site', 'ping_interval'], options, 'daemon')
        # "credentials" accepts a credentials.conf file path
        if args.daemonCredentials == None:
            credentials = None
            if config.has_option('daemon', 'credentials'):
                credentials = config.get('daemon', 'credentials')
            else:
                credentials = hlm_paths.credentialsConfig()
            args.daemonCredentials = credentials
        # "user" accepts an account name
        user = None
        if config.has_option('daemon', 'user'):
            user = config.get('daemon', 'user')
        # "group" accepts a group name
        group = None
        if config.has_option('daemon', 'group'):
            group = config.get('daemon', 'group')
        # "pingSite" requires an http:// website URL
        pingSite = _mandatoryDirective('ping_site', options, 'daemon', config.get)
        if not pingSite.startswith('http://'):
            raise Exception(_('ping_site {0} is is incorrect (must start with {1}), exiting.').format(quote(pingSite), quote('http://')))
        pingInterval = _mandatoryDirective('ping_interval', options, 'daemon', config.getint)
        if pingInterval < _minimumPingInterval:
            if __WARNING__: logWarning(_('Daemon configuration file {0}: ping_interval is way too low ({1} seconds), forcing it to {2} seconds.').format(quote(configFile), pingInterval, _minimumPingInterval))
            pingInterval = _minimumPingInterval

        # Wrap configuration into a single Values() object
        configCredentials = Values()
        configCredentials.user = user
        configCredentials.group = group
        configCredentials.pingSite = pingSite
        configCredentials.pingInterval = pingInterval

        if __DEBUG__: logDebug('Daemon configuration has been loaded from {0}.'.format(configFile))
        return configCredentials
    except SystemExit:
        raise
    except BaseException as exc:
        raise FatalError(_('Incorrect daemon configuration file {0}: {1}').format(quote(configFile), exc))
Esempio n. 2
0
def _parse():
    ''' Perform the actual arguments parsing.
    '''
    (parser, options) = _parseArgs()

    # Handle --help and --version and exit immediately
    if options.displayHelp or options.displayVersion:
        print('Hotspot Login Manager {0}'.format(hlm_application.getVersion()))
        if options.displayHelp:
            print()
            parser.print_help()
        sys.exit(0)
    # We don't need the parser anymore
    parser.destroy()

    # Do not accept any additional options
    if options.strayArgs != []:
        exitWithError(_N('Unknown option:',
                         'Unknown options:',
                         len(options.strayArgs))
                      + ' ' + ' '.join(options.strayArgs))

    # Mutually exclusive options
    mainCommands = quote(['--reauth', '--status', '--providers', '--pid', '--notifier', '--daemon'])
    mainCommandsCount = sum([options.runReauth, options.runStatus, options.runProviders, options.runPID, options.runNotifier, options.runDaemon])
    if mainCommandsCount == 0:
        exitWithError(_('Missing option: one of {0} must be used.').format(mainCommands))
    if mainCommandsCount > 1:
        exitWithError(_('Incompatible options: the options {0} are mutually exclusive.').format(mainCommands))
    if (not options.runDaemon) and ((options.daemonConfig != None) or (options.daemonCredentials != None)):
        optionNames = []
        if options.daemonConfig != None:
            optionNames.append('--config')
        if options.daemonCredentials != None:
            optionNames.append('--credentials')
        exitWithError(_N('Incompatible options: {0} can only be used in combination with {1}.',
                         'Incompatible options: {0} can only be used in combination with {1}.',
                         len(optionNames))
                        .format(quote(optionNames), quote('--daemon')))
    if options.runNotifier and not hlm_notifierbackend.isAvailable():
        exitWithError(_('{0} is not available on your system, you cannot run a notifier daemon.').format(quote('notify-send')))

    # Apply default values
    if options.logLevel == None:
        options.logLevel = hlm_globals.defaultLogLevel
    if options.runDaemon:
        # We'll handle daemonCredentials later on because it could be defined in daemon.conf
        if options.daemonConfig == None:
            options.daemonConfig = hlm_paths.daemonConfig()

    return options
Esempio n. 3
0
def _parse():
    ''' Perform the actual arguments parsing.
    '''
    (parser, options) = _parseArgs()

    # Handle --help and --version and exit immediately
    if options.displayHelp or options.displayVersion:
        print('Hotspot Login Manager {0}'.format(hlm_application.getVersion()))
        if options.displayHelp:
            print()
            parser.print_help()
        sys.exit(0)
    # We don't need the parser anymore
    parser.destroy()

    # Do not accept any additional options
    if options.strayArgs != []:
        exitWithError(
            _N('Unknown option:', 'Unknown options:', len(options.strayArgs)) +
            ' ' + ' '.join(options.strayArgs))

    # Mutually exclusive options
    mainCommands = quote([
        '--reauth', '--status', '--providers', '--pid', '--notifier',
        '--daemon'
    ])
    mainCommandsCount = sum([
        options.runReauth, options.runStatus, options.runProviders,
        options.runPID, options.runNotifier, options.runDaemon
    ])
    if mainCommandsCount == 0:
        exitWithError(
            _('Missing option: one of {0} must be used.').format(mainCommands))
    if mainCommandsCount > 1:
        exitWithError(
            _('Incompatible options: the options {0} are mutually exclusive.').
            format(mainCommands))
    if (not options.runDaemon) and ((options.daemonConfig != None) or
                                    (options.daemonCredentials != None)):
        optionNames = []
        if options.daemonConfig != None:
            optionNames.append('--config')
        if options.daemonCredentials != None:
            optionNames.append('--credentials')
        exitWithError(
            _N(
                'Incompatible options: {0} can only be used in combination with {1}.',
                'Incompatible options: {0} can only be used in combination with {1}.',
                len(optionNames)).format(quote(optionNames),
                                         quote('--daemon')))
    if options.runNotifier and not hlm_notifierbackend.isAvailable():
        exitWithError(
            _('{0} is not available on your system, you cannot run a notifier daemon.'
              ).format(quote('notify-send')))

    # Apply default values
    if options.logLevel == None:
        options.logLevel = hlm_globals.defaultLogLevel
    if options.runDaemon:
        # We'll handle daemonCredentials later on because it could be defined in daemon.conf
        if options.daemonConfig == None:
            options.daemonConfig = hlm_paths.daemonConfig()

    return options