예제 #1
0
def load_config_file( paths ):
   """
   Load the config file from the first path given on the command-line.
   """
   if paths is None:
      return None 
   
   path = paths[0]
   log.info( "Loading config from %s" % path )
   config_txt = storage.read_file( path )
   
   if config_txt is None:
      raise Exception("Failed to read config from %s" % path)
   
   return config_txt
예제 #2
0
def get_extra_config( argv, section_name, section_options, parse_all=False ):
    """
    Load extra configuration from argv 
    and from the config file, under the header
    'section_name'.

    The returned dict will have the extra section information.

    Return None if there were no options found for
    this section.
    """
    
    global CONFIG_DESCRIPTION, CONFIG_OPTIONS

    import storage
    import client

    parser = build_parser( argv[0], CONFIG_DESCRIPTION, CONFIG_OPTIONS )
    opts, _ = parser.parse_known_args( argv[1:] )
   
    # load everything into a dictionary and return it
    config_str = None
    config_file_path = None
   
    if hasattr( opts, "config" ) and opts.config != None:
        config_file_path = opts.config[0]
    else:
        config_file_path = default_config_path()

    if not os.path.exists(config_file_path):
        log.error("No config file at '%s'" % config_file_path)
        return None 

    if not os.path.isfile(config_file_path):
        log.error("Not a file: '%s'" % config_file_path)
        return None 

    config_str = storage.read_file( config_file_path )

    # get the extra options 
    extra_config = load_config( config_file_path, config_str, opts, section_name, section_options, parse_all=parse_all )
    if extra_config is None:
        log.error("No configuration for '%s'" % section_name )
        return None 

    return extra_config
예제 #3
0
def get_config_from_argv(argv):
    """
    Load up our configuration dict, using
    either the default config file (if none is specified in argv),
    or loading the config and any overrides from
    command-line options.

    Return a dict with the config information on success.
    Return None on error.
    """

    global CONFIG_OPTIONS, HELPER_OPTIONS, CONFIG_DESCRIPTION, log

    import syndicate.util.storage as storage
    import syndicate.util.client as client

    parser = build_parser(argv[0], CONFIG_DESCRIPTION, CONFIG_OPTIONS)
    opts, _ = parser.parse_known_args(argv[1:])
    if opts.debug:
        log = get_logger("syndicate.util.config", debug=True)
        log.setLevel(logging.DEBUG)

    # load everything into a dictionary and return it
    config_str = None
    config_file_path = None

    if hasattr(opts, "config") and opts.config != None:
        config_file_path = opts.config[0]
    else:
        config_file_path = default_config_path()

    config_str = storage.read_file(config_file_path)

    # generate the actual config
    config = {}
    fill_defaults(config)

    # get syndicate options...
    syndicate_config = load_config(config_file_path, config_str, opts,
                                   "syndicate", CONFIG_OPTIONS)
    if syndicate_config is None:
        log.error(
            "Failed to parse configuration section 'syndicate' from '%s'" %
            opts.config_file_path)
        return None

    config.update(syndicate_config)

    # helpers..
    helper_config = load_config(config_file_path, config_str, opts, "helpers",
                                HELPER_OPTIONS)
    if helper_config is None:
        log.error("Failed to parse configuration section 'helpers' from '%s'" %
                  opts.config_file_path)
        return None

    config['helpers'].update(helper_config)

    # generate syndicate_host and syndicate_port from URL, if needed
    if config.get('MS_url', None) is not None:
        # use https if no :// is given
        url = config['MS_url']

        host, port, no_tls = client.parse_url(url)

        config['syndicate_host'] = host
        config['syndicate_port'] = port
        config['no_tls'] = no_tls

    # trust public key?
    if opts.trust_public_key:
        config['trust_public_key'] = True
    else:
        config['trust_public_key'] = False

    # obtain syndicate public key, if on file
    syndicate_pubkey = storage.load_public_key(config, "syndicate",
                                               syndicate_object_name(config))
    if syndicate_pubkey is None:
        config['syndicate_public_key'] = None
        config['syndicate_public_key_pem'] = None

    else:
        config['syndicate_public_key'] = syndicate_pubkey
        config['syndicate_public_key_pem'] = syndicate_pubkey.exportKey()

    config['params'] = getattr(opts, "params", [])
    return config
예제 #4
0
def load_public_key( key_path ):
   try:
      key_text = storage.read_file( key_path )
   except Exception, e:
      log.error("Failed to read public key '%s'" % key_path )
      return None
예제 #5
0
def get_config_from_argv( argv ):
    """
    Load up our configuration dict, using
    either the default config file (if none is specified in argv),
    or loading the config and any overrides from
    command-line options.

    Return a dict with the config information on success.
    Return None on error.
    """

    global CONFIG_OPTIONS, HELPER_OPTIONS, CONFIG_DESCRIPTION, log

    import syndicate.util.storage as storage
    import syndicate.util.client as client

    parser = build_parser( argv[0], CONFIG_DESCRIPTION, CONFIG_OPTIONS )
    opts, _ = parser.parse_known_args( argv[1:] )
    if opts.debug:
        log = get_logger("syndicate.util.config", debug=True)
        log.setLevel(logging.DEBUG)
   
    # load everything into a dictionary and return it
    config_str = None
    config_file_path = None
   
    if hasattr( opts, "config" ) and opts.config != None:
        config_file_path = opts.config[0]
    else:
        config_file_path = default_config_path()
   
    config_str = storage.read_file( config_file_path )
   
    # generate the actual config
    config = {}
    fill_defaults( config )

    # get syndicate options...
    syndicate_config = load_config( config_file_path, config_str, opts, "syndicate", CONFIG_OPTIONS )
    if syndicate_config is None:
        log.error("Failed to parse configuration section 'syndicate' from '%s'" % opts.config_file_path)
        return None

    config.update( syndicate_config )

    # helpers..
    helper_config = load_config( config_file_path, config_str, opts, "helpers", HELPER_OPTIONS )
    if helper_config is None:
        log.error("Failed to parse configuration section 'helpers' from '%s'" % opts.config_file_path )
        return None

    config['helpers'].update( helper_config )
 
    # generate syndicate_host and syndicate_port from URL, if needed
    if config.get('MS_url', None) is not None:
        # use https if no :// is given 
        url = config['MS_url']
      
        host, port, no_tls = client.parse_url( url )
      
        config['syndicate_host'] = host 
        config['syndicate_port'] = port
        config['no_tls'] = no_tls
      
    # trust public key?
    if opts.trust_public_key:
        config['trust_public_key'] = True 
    else:
        config['trust_public_key'] = False

    # obtain syndicate public key, if on file
    syndicate_pubkey = storage.load_public_key( config, "syndicate", syndicate_object_name( config ) )
    if syndicate_pubkey is None:
        config['syndicate_public_key'] = None 
        config['syndicate_public_key_pem'] = None 

    else:
        config['syndicate_public_key'] = syndicate_pubkey
        config['syndicate_public_key_pem'] = syndicate_pubkey.exportKey()

    config['params'] = getattr( opts, "params", [] ) 
    return config
예제 #6
0
def read_file( file_path ):
   return util_storage.read_file( file_path )
예제 #7
0
def load_private_key(key_path):
    try:
        key_text = storage.read_file(key_path)
    except Exception, e:
        log.error("Failed to read private key '%s'" % key_path)
        return None