hiera-ic-yaml 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 8d5d4c5cb99fa3c85ab258b871bdc9605e2285e4
4
+ data.tar.gz: f2c2bf38bb3d9144679c94e3855429ff5b218482
5
+ SHA512:
6
+ metadata.gz: b40b446b4f5197c66d791cd2ddbd7206f97ae81826594026ed431e500a063d6f3bcb958a48ab3c47746732b1fef42136a15e32b47bc0e7d424f46322e6d548d3
7
+ data.tar.gz: e49ca2e15c9ff4d4ea6d27728b9bca00ff3567db718a077be7d758c9d97055a7f0d74a2a51e4a89c1b909707a2f734d4cb96bef6df287f98742a9bb874ae4293
@@ -0,0 +1,140 @@
1
+ class Hiera
2
+ module Backend
3
+
4
+ class Ic_yaml_backend
5
+
6
+ def initialize(cache=nil)
7
+ require 'yaml'
8
+ Hiera.debug("Hiera IC YAML backend starting")
9
+
10
+ @cache = cache || Filecache.new
11
+ end
12
+
13
+ def ic_yaml_config()
14
+ if Config[:ic_yaml]
15
+ return Config[:ic_yaml]
16
+ end
17
+
18
+ if Config['ic_yaml']
19
+ return Config['ic_yaml']
20
+ end
21
+
22
+ return {}
23
+ end
24
+
25
+ def data_dir(scope)
26
+ return Backend.datadir(:ic_yaml, scope)
27
+ end
28
+
29
+ def file_path(path, scope)
30
+ datadir = data_dir(scope)
31
+ file = File.join(datadir, path)
32
+
33
+ return file
34
+ end
35
+
36
+ def imports_key()
37
+ config = ic_yaml_config()
38
+
39
+ if config[:imports_key]
40
+ return config[:imports_key]
41
+ end
42
+
43
+ if config['imports_key']
44
+ return config['imports_key']
45
+ end
46
+
47
+ return '__imports__'
48
+ end
49
+
50
+ def merge_yaml(overriding, other)
51
+ Backend.merge_answer(overriding, other)
52
+ end
53
+
54
+ def load_yaml_file(path, scope)
55
+ Hiera.debug("Hiera IC YAML backend load import : #{path}")
56
+
57
+ file = file_path(path, scope)
58
+
59
+ if ! File.exist?(file)
60
+ Hiera.warn("Hiera IC YAML Cannot find datafile #{path}, skipping")
61
+ return {}
62
+ end
63
+
64
+ return load_yaml_data(File.open(file), scope)
65
+ end
66
+
67
+ def load_yaml_data(data, scope)
68
+ config = YAML.load(data) || {}
69
+ imports_key = imports_key()
70
+
71
+ if !config.kind_of?(Hash)
72
+ return config
73
+ end
74
+
75
+ if config.has_key?(imports_key)
76
+ imports = {}
77
+
78
+ config[imports_key].each { |f|
79
+ imports = merge_yaml(load_yaml_file(f, scope), imports)
80
+ }
81
+
82
+ config = merge_yaml(config, imports)
83
+ config.delete(imports_key)
84
+ end
85
+
86
+ return config
87
+ end
88
+
89
+ def lookup(key, scope, order_override, resolution_type)
90
+ answer = nil
91
+
92
+ Hiera.debug("Hiera IC YAML Looking up #{key} in YAML backend")
93
+
94
+ Backend.datasourcefiles(:ic_yaml, scope, "yaml", order_override) do |source, yamlfile|
95
+
96
+ data = @cache.read_file(yamlfile, Hash) do |data|
97
+ load_yaml_data(data, scope)
98
+ end
99
+
100
+ next if data.empty?
101
+ next unless data.include?(key)
102
+
103
+ # Extra logging that we found the key. This can be outputted
104
+ # multiple times if the resolution type is array or hash but that
105
+ # should be expected as the logging will then tell the user ALL the
106
+ # places where the key is found.
107
+ Hiera.debug("Found #{key} in #{source}")
108
+
109
+ # for array resolution we just append to the array whatever
110
+ # we find, we then goes onto the next file and keep adding to
111
+ # the array
112
+ #
113
+ # for priority searches we break after the first found data item
114
+ new_answer = Backend.parse_answer(data[key], scope)
115
+ case resolution_type
116
+ when :array
117
+ raise Exception, "Hiera type mismatch: expected Array and got #{new_answer.class}" unless new_answer.kind_of? Array or new_answer.kind_of? String
118
+ answer ||= []
119
+ answer << new_answer
120
+ when :hash
121
+ raise Exception, "Hiera type mismatch: expected Hash and got #{new_answer.class}" unless new_answer.kind_of? Hash
122
+ answer ||= {}
123
+ answer = Backend.merge_answer(new_answer,answer)
124
+ else
125
+ answer = new_answer
126
+ break
127
+ end
128
+ end
129
+
130
+ return answer
131
+ end
132
+
133
+ private
134
+
135
+ def file_exists?(path)
136
+ File.exist? path
137
+ end
138
+ end
139
+ end
140
+ end
metadata ADDED
@@ -0,0 +1,43 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: hiera-ic-yaml
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Fabio B. Silva
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-12-01 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: Hiera yaml backend that support imports
14
+ email: fabio.bat.silva@gmail.com
15
+ executables: []
16
+ extensions: []
17
+ extra_rdoc_files: []
18
+ files:
19
+ - lib/hiera/backend/ic_yaml_backend.rb
20
+ homepage: http://github.com/instaclick/hiera-ic-yaml
21
+ licenses: []
22
+ metadata: {}
23
+ post_install_message:
24
+ rdoc_options: []
25
+ require_paths:
26
+ - lib
27
+ required_ruby_version: !ruby/object:Gem::Requirement
28
+ requirements:
29
+ - - '>='
30
+ - !ruby/object:Gem::Version
31
+ version: '0'
32
+ required_rubygems_version: !ruby/object:Gem::Requirement
33
+ requirements:
34
+ - - '>='
35
+ - !ruby/object:Gem::Version
36
+ version: '0'
37
+ requirements: []
38
+ rubyforge_project:
39
+ rubygems_version: 2.0.14
40
+ signing_key:
41
+ specification_version: 4
42
+ summary: ic yaml backend
43
+ test_files: []