<commit_before>#!/usr/bin/env python

""" Module to handle attributes and configuration for RSV service """

import os, re, pwd, sys, shutil, ConfigParser, logging

from osg_configure.modules import exceptions
from osg_configure.modules import utilities
from osg_configure.modules import validation
from osg_configure.modules import configfile
from osg_configure.modules.configurationbase import BaseConfiguration

__all__ = ['RsvConfiguration']


class RsvConfiguration(BaseConfiguration):
  """Class to handle attributes and configuration related to osg-rsv services"""

  def __init__(self, *args, **kwargs):
    # pylint: disable-msg=W0142
    super(RsvConfiguration, self).__init__(*args, **kwargs)    
    self.log('RsvConfiguration.__init__ started')    
    self.options = {'enable_local_probes' : 
                      configfile.Option(name = 'enable_local_probes',
                                        required = configfile.Option.OPTIONAL,
                                        type = bool,
                                        default_value = True),
                    'gratia_probes' : 
                      configfile.Option(name = 'gratia_probes',
                                        default_value = '',
                                        required = configfile.Option.OPTIONAL),
                    'ce_hosts' : 
                      configfile.Option(name = 'ce_hosts',
                                        default_value = '',
                                        required = configfile.Option.OPTIONAL),
                    'gridftp_hosts' : 
                      configfile.Option(name = 'gridftp_hosts',
                                        default_value = None,
                                        required = configfile.Option.OPTIONAL),
                    'gridftp_dir' : 
                      configfile.Option(name = 'gridftp_dir',
                                        default_value = '/tmp'),
                    'gums_hosts' : 
                      configfile.Option(name = 'gums_hosts',
                                        default_value = '',
                                        required = configfile.Option.OPTIONAL),
                    'srm_hosts' : 
                      configfile.Option(name = 'srm_hosts',
                                        default_value = '',
                                        required = configfile.Option.OPTIONAL),
                    'srm_dir' : 
                      configfile.Option(name = 'srm_dir',
                                        required = configfile.Option.OPTIONAL),
                    'srm_webservice_path' : 
                      configfile.Option(name = 'srm_webservice_path',
                                        required = configfile.Option.OPTIONAL),
                    'service_cert' : 
                      configfile.Option(name = 'service_cert',
                                        required = configfile.Option.OPTIONAL,
                                        default_value = '/etc/grid-security/rsv/rsvcert.pem'),
                    'service_key' : 
                      configfile.Option(name = 'service_key',
                                        required = configfile.Option.OPTIONAL,
                                        default_value = '/etc/grid-security/rsv/rsvkey.pem'),
                    'service_proxy' : 
                      configfile.Option(name = 'service_proxy',
                                        required = configfile.Option.OPTIONAL,
                                        default_value = '/tmp/rsvproxy'),
                    'user_proxy' : 
                      configfile.Option(name = 'user_proxy',
                                        default_value = '',
                                        required = configfile.Option.OPTIONAL),
                    'legacy_proxy' : 
                      configfile.Option(name = 'legacy_proxy',
                                        required = configfile.Option.OPTIONAL,
                                        type = bool,
                                        default_value = False),
                    'enable_gratia' : 
                      configfile.Option(name = 'enable_gratia',
                                        type = bool),
                    'gratia_collector' : 
                      configfile.Option(name = 'gratia_collector',
                                        required = configfile.Option.OPTIONAL,
                                        default_value = 'rsv.grid.iu.edu:8880'),
                    'condor_location' : 
                      configfile.Option(name = 'condor_location',
                                        default_value = '',
                                        required = configfile.Option.OPTIONAL),
                    'enable_nagios' : 
                      configfile.Option(name = 'enable_nagios',
                                        type = bool),
                    'nagios_send_nsca' : 
                      configfile.Option(name = 'nagios_send_nsca',
                                        required = configfile.Option.OPTIONAL,
                                        type = bool,
                                        default_value = False)}

    self.__rsv_user = "rsv"
    self.__ce_hosts = []
    self.__gridftp_hosts = []
    self.__gums_hosts = []
    self.__srm_hosts = []
    self.__gratia_probes_2d = []
    self.__gratia_metric_map = {}
    self.__enable_rsv_downloads = False
    self.__meta = ConfigParser.RawConfigParser()
    self.use_service_cert = True
    self.grid_group = 'OSG'
    self.site_name = 'Generic Site'
    self.config_section = "RSV"
    self.rsv_control = os.path.join('/', 'usr', 'bin', 'rsv-control')
    self.rsv_meta_dir = os.path.join('/', 'etc', 'rsv', 'meta', 'metrics')
    self.uid = None
    self.gid = None
    self.log('RsvConfiguration.__init__ completed')

  def parseConfiguration(self, configuration):
    """
    Try to get configuration information from ConfigParser or 
    SafeConfigParser object given by configuration and write recognized settings 
    to attributes dict
    """
    self.log('RsvConfiguration.parseConfiguration started')    

    self.checkConfig(configuration)
    
    if not configuration.has_section(self.config_section):
      self.enabled = False
      self.log("%s section not in config file" % self.config_section)    
      self.log('RsvConfiguration.parseConfiguration completed')    
      return True

    if not utilities.rpm_installed('rsv-core'):
      self.enabled = False
      self.log('rsv-core rpm not installed, disabling RSV configuration')
      if configuration.has_section(self.config_section):        
        self.log('Your configuration has configuration information for RSV ' +
                 'but RSV is not installed. RSV configuration will be ' +
                 'ignored.',
                 level = logging.WARNING)
      return True

    if not self.setStatus(configuration):
      self.log('RsvConfiguration.parseConfiguration completed')    
      return True

    self.getOptions(configuration, ignore_options = ['enabled'])
    (self.uid, self.gid) = pwd.getpwnam(self.__rsv_user)[2:4]

    # If we're on a CE, get the grid group if possible
    if configuration.has_section('Site Information'): 
      if configuration.has_option('Site Information', 'group'):
        self.grid_group = configuration.get('Site Information', 'group')

      if configuration.has_option('Site Information', 'resource'):
        self.site_name = configuration.get('Site Information', 'resource')
      elif configuration.has_option('Site Information', 'site_name'):
        self.site_name = configuration.get('Site Information', 'site_name')


    # Parse lists
    self.__ce_hosts = split_list(self.options['ce_hosts'].value)
    self.__gums_hosts = split_list(self.options['gums_hosts'].value)
    self.__srm_hosts = split_list(self.options['srm_hosts'].value)

    # If the gridftp hosts are not defined then they default to the CE hosts
    if self.options['gridftp_hosts'].value is not None:
      self.__gridftp_hosts = split_list(self.options['gridftp_hosts'].value)
    else:
      self.__gridftp_hosts = self.__ce_hosts

    if self.options['gratia_probes'].value is not None:
      self.__gratia_probes_2d = self.split_2d_list(self.options['gratia_probes'].value)

    self.log('RsvConfiguration.parseConfiguration completed')    
  

# pylint: disable-msg=W0613
  def checkAttributes(self, attributes):
    """
    Check attributes currently stored and make sure that they are consistent
    """

    self.log('RsvConfiguration.checkAttributes started')
    attributes_ok = True

    if not self.enabled:
      self.log('Not enabled, returning True')
      self.log('RsvConfiguration.checkAttributes completed')    
      return attributes_ok

    if self.ignored:
      self.log('Ignored, returning True')
      self.log('RsvConfiguration.checkAttributes completed')    <commit_msg>Clean up handling of gridftp_hosts settings in RSV section<commit_after>#!/usr/bin/env python

""" Module to handle attributes and configuration for RSV service """

import os, re, pwd, sys, shutil, ConfigParser, logging

from osg_configure.modules import exceptions
from osg_configure.modules import utilities
from osg_configure.modules import validation
from osg_configure.modules import configfile
from osg_configure.modules.configurationbase import BaseConfiguration

__all__ = ['RsvConfiguration']


class RsvConfiguration(BaseConfiguration):
  """Class to handle attributes and configuration related to osg-rsv services"""

  def __init__(self, *args, **kwargs):
    # pylint: disable-msg=W0142
    super(RsvConfiguration, self).__init__(*args, **kwargs)    
    self.log('RsvConfiguration.__init__ started')    
    self.options = {'enable_local_probes' : 
                      configfile.Option(name = 'enable_local_probes',
                                        required = configfile.Option.OPTIONAL,
                                        type = bool,
                                        default_value = True),
                    'gratia_probes' : 
                      configfile.Option(name = 'gratia_probes',
                                        default_value = '',
                                        required = configfile.Option.OPTIONAL),
                    'ce_hosts' : 
                      configfile.Option(name = 'ce_hosts',
                                        default_value = '',
                                        required = configfile.Option.OPTIONAL),
                    'gridftp_hosts' : 
                      configfile.Option(name = 'gridftp_hosts',
                                        default_value = '',
                                        required = configfile.Option.OPTIONAL),
                    'gridftp_dir' : 
                      configfile.Option(name = 'gridftp_dir',
                                        default_value = '/tmp'),
                    'gums_hosts' : 
                      configfile.Option(name = 'gums_hosts',
                                        default_value = '',
                                        required = configfile.Option.OPTIONAL),
                    'srm_hosts' : 
                      configfile.Option(name = 'srm_hosts',
                                        default_value = '',
                                        required = configfile.Option.OPTIONAL),
                    'srm_dir' : 
                      configfile.Option(name = 'srm_dir',
                                        required = configfile.Option.OPTIONAL),
                    'srm_webservice_path' : 
                      configfile.Option(name = 'srm_webservice_path',
                                        required = configfile.Option.OPTIONAL),
                    'service_cert' : 
                      configfile.Option(name = 'service_cert',
                                        required = configfile.Option.OPTIONAL,
                                        default_value = '/etc/grid-security/rsv/rsvcert.pem'),
                    'service_key' : 
                      configfile.Option(name = 'service_key',
                                        required = configfile.Option.OPTIONAL,
                                        default_value = '/etc/grid-security/rsv/rsvkey.pem'),
                    'service_proxy' : 
                      configfile.Option(name = 'service_proxy',
                                        required = configfile.Option.OPTIONAL,
                                        default_value = '/tmp/rsvproxy'),
                    'user_proxy' : 
                      configfile.Option(name = 'user_proxy',
                                        default_value = '',
                                        required = configfile.Option.OPTIONAL),
                    'legacy_proxy' : 
                      configfile.Option(name = 'legacy_proxy',
                                        required = configfile.Option.OPTIONAL,
                                        type = bool,
                                        default_value = False),
                    'enable_gratia' : 
                      configfile.Option(name = 'enable_gratia',
                                        type = bool),
                    'gratia_collector' : 
                      configfile.Option(name = 'gratia_collector',
                                        required = configfile.Option.OPTIONAL,
                                        default_value = 'rsv.grid.iu.edu:8880'),
                    'condor_location' : 
                      configfile.Option(name = 'condor_location',
                                        default_value = '',
                                        required = configfile.Option.OPTIONAL),
                    'enable_nagios' : 
                      configfile.Option(name = 'enable_nagios',
                                        type = bool),
                    'nagios_send_nsca' : 
                      configfile.Option(name = 'nagios_send_nsca',
                                        required = configfile.Option.OPTIONAL,
                                        type = bool,
                                        default_value = False)}

    self.__rsv_user = "rsv"
    self.__ce_hosts = []
    self.__gridftp_hosts = []
    self.__gums_hosts = []
    self.__srm_hosts = []
    self.__gratia_probes_2d = []
    self.__gratia_metric_map = {}
    self.__enable_rsv_downloads = False
    self.__meta = ConfigParser.RawConfigParser()
    self.use_service_cert = True
    self.grid_group = 'OSG'
    self.site_name = 'Generic Site'
    self.config_section = "RSV"
    self.rsv_control = os.path.join('/', 'usr', 'bin', 'rsv-control')
    self.rsv_meta_dir = os.path.join('/', 'etc', 'rsv', 'meta', 'metrics')
    self.uid = None
    self.gid = None
    self.log('RsvConfiguration.__init__ completed')

  def parseConfiguration(self, configuration):
    """
    Try to get configuration information from ConfigParser or 
    SafeConfigParser object given by configuration and write recognized settings 
    to attributes dict
    """
    self.log('RsvConfiguration.parseConfiguration started')    

    self.checkConfig(configuration)
    
    if not configuration.has_section(self.config_section):
      self.enabled = False
      self.log("%s section not in config file" % self.config_section)    
      self.log('RsvConfiguration.parseConfiguration completed')    
      return True

    if not utilities.rpm_installed('rsv-core'):
      self.enabled = False
      self.log('rsv-core rpm not installed, disabling RSV configuration')
      if configuration.has_section(self.config_section):        
        self.log('Your configuration has configuration information for RSV ' +
                 'but RSV is not installed. RSV configuration will be ' +
                 'ignored.',
                 level = logging.WARNING)
      return True

    if not self.setStatus(configuration):
      self.log('RsvConfiguration.parseConfiguration completed')    
      return True

    self.getOptions(configuration, ignore_options = ['enabled'])
    (self.uid, self.gid) = pwd.getpwnam(self.__rsv_user)[2:4]

    # If we're on a CE, get the grid group if possible
    if configuration.has_section('Site Information'): 
      if configuration.has_option('Site Information', 'group'):
        self.grid_group = configuration.get('Site Information', 'group')

      if configuration.has_option('Site Information', 'resource'):
        self.site_name = configuration.get('Site Information', 'resource')
      elif configuration.has_option('Site Information', 'site_name'):
        self.site_name = configuration.get('Site Information', 'site_name')


    # Parse lists
    self.__ce_hosts = split_list(self.options['ce_hosts'].value)
    self.__gums_hosts = split_list(self.options['gums_hosts'].value)
    self.__srm_hosts = split_list(self.options['srm_hosts'].value)

    # If the gridftp hosts are not defined then they default to the CE hosts
    if self.options['gridftp_hosts'].value == '':
      # check to see if the setting is in the config file
      if configuration.has_option(self.config_section, 'gridftp_hosts'):
        # present and set to default so we don't want gridftp tests
        self.__gridftp_hosts = []
      else:
        # option is commented out, use ce_hosts setting
        self.__gridftp_hosts = self.__ce_hosts
    else:
      self.__gridftp_hosts = split_list(self.options['gridftp_hosts'].value)

    
    if self.options['gratia_probes'].value != '':
      self.__gratia_probes_2d = self.split_2d_list(self.options['gratia_probes'].value)

    self.log('RsvConfiguration.parseConfiguration completed')    
  

# pylint: disable-msg=W0613
  def checkAttributes(self, attributes):
    """
    Check attributes currently stored and make sure that they are consistent
    """

    self.log('RsvConfiguration.checkAttributes started')
    attributes_ok = True

    if not self.enabled:
      self.log('Not enabled, returning True')
      self.log('RsvConfiguration.checkAttributes completed')    
      return attributes_ok

    if self.ignored:
      self.log('Ignored, returning True')
      self.log('RsvConfiguration.checkAttributes completed')    
{'fraction_non_alphanumeric': 0.0894092022869589, 'fraction_numerical': 0.0018513476722025592, 'mean_word_length': 1.5918712955122778, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 84, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '916514', 'n_tokens_mistral': 4531, 'n_tokens_neox': 4258, 'n_words': 901}