Package pulp :: Package server :: Module config
[hide private]
[frames] | no frames]

Source Code for Module pulp.server.config

  1  #!/usr/bin/env python 
  2  # -*- coding: utf-8 -*- 
  3  # 
  4  # Copyright © 2010 Red Hat, Inc. 
  5  # 
  6  # This software is licensed to you under the GNU General Public License, 
  7  # version 2 (GPLv2). There is NO WARRANTY for this software, express or 
  8  # implied, including the implied warranties of MERCHANTABILITY or FITNESS 
  9  # FOR A PARTICULAR PURPOSE. You should have received a copy of GPLv2 
 10  # along with this software; if not, see 
 11  # http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt. 
 12  # 
 13  # Red Hat trademarks are not licensed under GPLv2. No permission is 
 14  # granted to use or replicate Red Hat trademarks that are incorporated 
 15  # in this software or its documentation. 
 16   
 17  import os 
 18  from ConfigParser import SafeConfigParser 
 19   
 20  # global configuration -------------------------------------------------------- 
 21   
 22  config = None # ConfigParser.SafeConfigParser instance 
 23   
 24  # to guarantee that a section and/or setting exists, add a default value here 
 25  _default_values = { 
 26      'logs': { 
 27          'level': 'info', 
 28          'max_size': '1048576', 
 29          'backups': '4', 
 30          'pulp_file': '/var/log/pulp/pulp.log', 
 31          'grinder_file': '/var/log/pulp/grinder.log', 
 32      }, 
 33      'auditing': { 
 34          'events_file': '/var/log/pulp/events.log', 
 35          'lifetime': '90', 
 36          'backups': '4', 
 37      }, 
 38      'security': { 
 39          'cacert': '/etc/pki/pulp/ca.crt', 
 40          'cakey': '/etc/pki/pulp/ca.key', 
 41      }, 
 42  } 
 43   
 44  # to add a default configuration file, list the full path here 
 45  _config_files = ['/etc/pulp/pulp.conf'] 
 46   
 47  # configuration api ----------------------------------------------------------- 
 48   
49 -def check_config_files():
50 """ 51 Check for read permissions on the configuration files. Raise a runtime error 52 if the file doesn't exist or the read permissions are lacking. 53 """ 54 for file in _config_files: 55 if not os.access(file, os.F_OK): 56 raise RuntimeError('Cannot find configuration file: %s' % file) 57 if not os.access(file, os.R_OK): 58 raise RuntimeError('Cannot read configuration file: %s' % file) 59 return 'Yeah!'
60 61
62 -def load_configuration():
63 """ 64 Check the configuration files and load the global 'config' object from them. 65 """ 66 global config 67 check_config_files() 68 config = SafeConfigParser() 69 # add the defaults first 70 for section, settings in _default_values.items(): 71 config.add_section(section) 72 for option, value in settings.items(): 73 config.set(section, option, value) 74 # read the config files 75 return config.read(_config_files)
76 77
78 -def add_config_file(file_path):
79 """ 80 Convenience function to add a new file to the list of configuration files, 81 then re-load the global config and re-configure logging. 82 83 @type file_path: str 84 @param file_path: full path to the new file to add 85 """ 86 global _config_files 87 if file_path in _config_files: 88 raise RuntimeError('File, %s, already in configuration files' % file_path) 89 _config_files.append(file_path) 90 load_configuration()
91 92
93 -def remove_config_file(file_path):
94 """ 95 Convenience function to remove a file from the list of configuration files, 96 then re-load the global config and re-configure logging. 97 98 @type file_path: str 99 @param file_path: full path to the file to remove 100 """ 101 global _config_files 102 if file_path not in _config_files: 103 raise RuntimeError('File, %s, not in configuration files' % file_path) 104 _config_files.remove(file_path) 105 load_configuration()
106 107 # initialize on import -------------------------------------------------------- 108 109 load_configuration() 110