コード例 #1
0
 def test_collect_config(self):
     conflict_configs = [('ec2', {
         'local-ipv4': '192.0.2.99',
         'instance-id': 'feeddead'
     }), ('cfn', {
         'foo': {
             'bar': 'foo-bar'
         },
         'local-ipv4': '198.51.100.50'
     })]
     config_files = []
     tdir = self.useFixture(fixtures.TempDir())
     for name, config in conflict_configs:
         path = os.path.join(tdir.path, '%s.json' % name)
         with open(path, 'w') as out:
             out.write(json.dumps(config))
         config_files.append(path)
     config = collect_config.collect_config(config_files)
     self.assertEqual(
         {
             'local-ipv4': '198.51.100.50',
             'instance-id': 'feeddead',
             'foo': {
                 'bar': 'foo-bar'
             }
         }, config)
コード例 #2
0
def install_config(
        config_path, template_root, output_path, validate, subhash=None,
        fallback_metadata=None):
    config = strip_hash(
        collect_config.collect_config(config_path, fallback_metadata), subhash)
    tree = build_tree(template_paths(template_root), config)
    if not validate:
        for path, contents in tree.items():
            write_file(os.path.join(
                output_path, strip_prefix('/', path)), contents)
コード例 #3
0
def install_config(
        config_path, template_root, output_path, validate, subhash=None,
        fallback_metadata=None):
    config = strip_hash(
        collect_config.collect_config(config_path, fallback_metadata), subhash)
    tree = build_tree(template_paths(template_root), config)
    if not validate:
        for path, obj in tree.items():
            write_file(os.path.join(
                output_path, strip_prefix('/', path)), obj)
コード例 #4
0
    def test_collect_config_fallback(self):
        tdir = self.useFixture(fixtures.TempDir())
        with open(os.path.join(tdir.path, 'does_exist.json'), 'w') as t:
            t.write(json.dumps({'a': 1}))
        noexist_path = os.path.join(tdir.path, 'does_not_exist.json')

        config = collect_config.collect_config([], [noexist_path, t.name])
        self.assertEqual({'a': 1}, config)

        with open(os.path.join(tdir.path, 'does_exist_new.json'), 'w') as t2:
            t2.write(json.dumps({'a': 2}))

        config = collect_config.collect_config([t2.name], [t.name])
        self.assertEqual({'a': 2}, config)

        config = collect_config.collect_config([], [t.name, noexist_path])
        self.assertEqual({'a': 1}, config)
        self.assertEqual({}, collect_config.collect_config([], [noexist_path]))
        self.assertEqual({}, collect_config.collect_config([]))
コード例 #5
0
    def test_collect_config_fallback(self):
        tdir = self.useFixture(fixtures.TempDir())
        with open(os.path.join(tdir.path, 'does_exist.json'), 'w') as t:
            t.write(json.dumps({'a': 1}))
        noexist_path = os.path.join(tdir.path, 'does_not_exist.json')

        config = collect_config.collect_config([], [noexist_path, t.name])
        self.assertEqual({'a': 1}, config)

        with open(os.path.join(tdir.path, 'does_exist_new.json'), 'w') as t2:
            t2.write(json.dumps({'a': 2}))

        config = collect_config.collect_config([t2.name], [t.name])
        self.assertEqual({'a': 2}, config)

        config = collect_config.collect_config([], [t.name, noexist_path])
        self.assertEqual({'a': 1}, config)
        self.assertEqual({},
                         collect_config.collect_config([], [noexist_path]))
        self.assertEqual({},
                         collect_config.collect_config([]))
コード例 #6
0
def print_key(
        config_path, key, type_name, default=None, fallback_metadata=None):
    config = collect_config.collect_config(config_path, fallback_metadata)
    keys = key.split('.')
    for key in keys:
        try:
            config = config[key]
        except (KeyError, TypeError):
            if default is not None:
                print(str(default))
                return
            else:
                raise exc.ConfigException(
                    'key %s does not exist in %s' % (key, config_path))
    value_types.ensure_type(str(config), type_name)
    print(str(config))
コード例 #7
0
def print_key(
        config_path, key, type_name, default=None, fallback_metadata=None):
    config = collect_config.collect_config(config_path, fallback_metadata)
    config = _extract_key(config_path, key, fallback_metadata)
    if config is None:
        if default is not None:
            print(str(default))
            return
        else:
            raise exc.ConfigException(
                'key %s does not exist in %s' % (key, config_path))
    value_types.ensure_type(str(config), type_name)
    if isinstance(config, (dict, list, bool)):
        print(json.dumps(config))
    else:
        print(str(config))
コード例 #8
0
def _extract_key(config_path, key, fallback_metadata=None):
    config = collect_config.collect_config(config_path, fallback_metadata)
    keys = key.split('.')
    for key in keys:
        try:
            config = config[key]
            if config is None:
                raise TypeError()
        except (KeyError, TypeError):
            try:
                if type(config) == list:
                    config = config[int(key)]
                    continue
            except (IndexError, ValueError):
                pass
            return None
    return config
コード例 #9
0
 def test_collect_config(self):
     conflict_configs = [('ec2', {'local-ipv4': '192.0.2.99',
                                  'instance-id': 'feeddead'}),
                         ('cfn', {'foo': {'bar': 'foo-bar'},
                                  'local-ipv4': '198.51.100.50'})]
     config_files = []
     tdir = self.useFixture(fixtures.TempDir())
     for name, config in conflict_configs:
         path = os.path.join(tdir.path, '%s.json' % name)
         with open(path, 'w') as out:
             out.write(json.dumps(config))
         config_files.append(path)
     config = collect_config.collect_config(config_files)
     self.assertEqual(
         {'local-ipv4': '198.51.100.50',
          'instance-id': 'feeddead',
          'foo': {'bar': 'foo-bar'}}, config)