Example #1
0
 def test_merge_config_with_options(self):
   options = { "a": "b" }
   config = ConfigParser.ConfigParser()
   self.assertEqual({ "a": "b" },
                    merge_config_with_options("section", config, options))
   config.add_section("section")
   self.assertEqual({ "a": "b" },
                    merge_config_with_options("section", config, options))
   config.set("section", "a", "z")
   config.set("section", "c", "d")
   self.assertEqual({ "a": "z", "c": "d" },
                    merge_config_with_options("section", config, {}))
   self.assertEqual({ "a": "b", "c": "d" },
                    merge_config_with_options("section", config, options))
Example #2
0
def parse_options_and_config(command, option_list=[], extra_arguments=(),
                             unbounded_args=False):
  """
  Parse the arguments to command using the given option list, and combine with
  any configuration parameters.

  If unbounded_args is true then there must be at least as many extra arguments
  as specified by extra_arguments (the first argument is always CLUSTER).
  Otherwise there must be exactly the same number of arguments as
  extra_arguments.
  """
  expected_arguments = ["CLUSTER",]
  expected_arguments.extend(extra_arguments)
  (options_dict, args) = parse_options(command, option_list, expected_arguments,
                                       unbounded_args)

  config_dir = get_config_dir(options_dict)
  logging.debug("Config dir %s from %s", config_dir, options_dict)
  config_files = [os.path.join(config_dir, CONFIG_FILENAME)]
  if 'config_dir' not in options_dict:
    # if config_dir not set, then also search in current directory
    config_files.insert(0, CONFIG_FILENAME)

  config = ConfigParser.ConfigParser()
  read_files = config.read(config_files)
  logging.debug("Read %d configuration files: %s", len(read_files),
                ", ".join(read_files))
  cluster_name = args[0]
  opt = merge_config_with_options(cluster_name, config, options_dict)
  logging.debug("Options: %s", str(opt))
  service_name = get_service_name(opt)
  cloud_provider = get_cloud_provider(opt)
  cluster = get_cluster(cloud_provider)(cluster_name, config_dir)
  service = get_service(service_name, cloud_provider)(cluster)
  return (opt, args, service)
Example #3
0
  def test_merge_config_with_options_list(self):
    config = ConfigParser.ConfigParser()
    config.readfp(StringIO.StringIO("""[section]
env1=a=b
 c=d
env2=e=f
 g=h"""))
    self.assertEqual({ "env1": ["a=b", "c=d"], "env2": ["e=f", "g=h"] },
                     merge_config_with_options("section", config, {}))
Example #4
0
 def test_merge_config_with_options(self):
     options = {"a": "b"}
     config = ConfigParser.ConfigParser()
     self.assertEqual({"a": "b"},
                      merge_config_with_options("section", config, options))
     config.add_section("section")
     self.assertEqual({"a": "b"},
                      merge_config_with_options("section", config, options))
     config.set("section", "a", "z")
     config.set("section", "c", "d")
     self.assertEqual({
         "a": "z",
         "c": "d"
     }, merge_config_with_options("section", config, {}))
     self.assertEqual({
         "a": "b",
         "c": "d"
     }, merge_config_with_options("section", config, options))
Example #5
0
    def test_merge_config_with_options_list(self):
        config = ConfigParser.ConfigParser()
        config.readfp(
            StringIO.StringIO("""[section]
env1=a=b
 c=d
env2=e=f
 g=h"""))
        self.assertEqual({
            "env1": ["a=b", "c=d"],
            "env2": ["e=f", "g=h"]
        }, merge_config_with_options("section", config, {}))
Example #6
0
def parse_options_and_config(command,
                             option_list=[],
                             extra_arguments=(),
                             unbounded_args=False):
    """
  Parse the arguments to command using the given option list, and combine with
  any configuration parameters.

  If unbounded_args is true then there must be at least as many extra arguments
  as specified by extra_arguments (the first argument is always CLUSTER).
  Otherwise there must be exactly the same number of arguments as
  extra_arguments.
  """
    expected_arguments = [
        "CLUSTER",
    ]
    expected_arguments.extend(extra_arguments)
    (options_dict, args) = parse_options(command, option_list,
                                         expected_arguments, unbounded_args)

    config_dir = get_config_dir(options_dict)
    config_files = [os.path.join(config_dir, CONFIG_FILENAME)]
    if 'config_dir' not in options_dict:
        # if config_dir not set, then also search in current directory
        config_files.insert(0, CONFIG_FILENAME)

    config = ConfigParser.RawConfigParser()
    read_files = config.read(config_files)
    logging.debug("Read %d configuration files: %s", len(read_files),
                  ", ".join(read_files))
    cluster_name = args[0]
    opt = merge_config_with_options(cluster_name, config, options_dict)
    logging.debug("Options: %s", str(opt))
    service_name = get_service_name(opt)
    cloud_provider = get_cloud_provider(opt)
    cluster = get_cluster(cloud_provider)(cluster_name, config_dir)
    service = get_service(service_name, cloud_provider)(cluster)
    return (opt, args, service)