erb-hiera 0.4.4 → 0.5.0.pre.rc0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA256:
3
- metadata.gz: 4642172c2c4bab8f39e492e461256322096897a32272fe87047cfeaf2341816f
4
- data.tar.gz: 7e2cebbe242334a48b41232e94cc47b3484310d973320897f677454c72966c6d
2
+ SHA1:
3
+ metadata.gz: 2ca8894f2efe1edb0039fbacff8c3295e8105e89
4
+ data.tar.gz: a0003dd9922d0c13660882553231e09810592b6d
5
5
  SHA512:
6
- metadata.gz: 85ba2730995a1fe190f572ca7113afbfd6230d573fc3e9a255fa96cc2a985ddb962db5667ce87a48eb2d06602e83f06f8ea59aa3ed648d7a562ce34160907a41
7
- data.tar.gz: d5b80222740cf2a94b0955b09c6824ea199722b8c41be59e4a71709063ad00efb4439988d476b2b830a2b47cb0367ed37c5d6ffafdff78bd42b323248b065b7c
6
+ metadata.gz: 7475d8b195b11559609c2f9e665459516bc7b458dc2de6015ea22d4fc4c475669fc94efc8c83c64c19a2ef1d2be4c843afd5c5242e72372faf66854c7744349c
7
+ data.tar.gz: ff648a3b56a1437e65654ca456ad8982f974d65fefb0831545eff259f7354ed7054c2ab4dcae056e4d26fec556edc52ea87e5177a0b4a45dc74830507818e256
data/README.md CHANGED
@@ -15,18 +15,33 @@ gem install erb-hiera
15
15
  ```
16
16
  $ ./bin/erb-hiera --help
17
17
  Options:
18
- --config=<s> specify config file
19
- --hiera-config=<s> specify hiera config file
20
- --dry-run don't write out files
21
- --verbose print compiled template(s)
22
- --debug print backtrace on error
18
+ -m, --mapping-config=<s> specify mapping config file
19
+ -c, --hiera-config=<s> specify hiera config file
20
+
21
+ -i, --input=<s> override input config options
22
+ -o, --output=<s> override output config options
23
+
24
+ -s, --scope=<s> override the lookup scope
25
+ -v, --variables=<s> override facts
26
+
27
+ --dry-run don't write out files
28
+
29
+ --verbose print compiled templates
30
+ --debug enable hiera logging and print backtrace on error
31
+
23
32
  ```
24
33
 
25
34
  ## Example
26
35
 
27
36
  ```
28
37
  cd example
29
- erb-hiera --config config.yaml --hiera-config hiera.yaml --verbose
38
+ erb-hiera --mapping-config mapping.yaml --hiera-config hiera.yaml --verbose
39
+
40
+ # render a specific template using injected erb scope (outputs only to stdout)
41
+ erb-hiera --mapping-config mapping.yaml --hiera-config hiera.yaml --scope '{ "environment": "prod" }' -o -
42
+
43
+ # use normal lookup path but override a fact at the top level
44
+ erb-hiera --mapping-config mapping.yaml --hiera-config hiera.yaml --variables '{ "environment::description": "override description" }' -o -
30
45
  ```
31
46
 
32
47
  ## References
@@ -0,0 +1 @@
1
+ environment::description: "this is the default environment"
@@ -0,0 +1 @@
1
+ environment::description: "this is the prod environment"
@@ -0,0 +1,2 @@
1
+ common variable: some common variable
2
+ environment variable: this is the prod environment
data/lib/erb-hiera/cli.rb CHANGED
@@ -4,11 +4,19 @@ module ErbHiera
4
4
  module CLI
5
5
  def self.parse
6
6
  option_parser = Trollop::Parser.new do
7
- opt :config, "specify config file", :type => :string
8
- opt :hiera_config, "specify hiera config file", :type => :string
9
- opt :verbose, "print compiled templates"
10
- opt :debug, "print backtrace on error"
11
- opt :dry_run, "don't write out files"
7
+ opt :mapping_config, "specify mapping config file", :type => :string, :short => :m
8
+ opt :hiera_config, "specify hiera config file\n ", :type => :string, :short => :c
9
+
10
+ opt :input, "override input config options", :type => :string, :short => :i
11
+ opt :output, "override output config options\n ", :type => :string, :short => :o
12
+
13
+ opt :scope, "override the lookup scope", :type => :string, :short => :s
14
+ opt :variables, "override facts\n ", :type => :string, :short => :v
15
+
16
+ opt :dry_run, "don't write out files\n "
17
+
18
+ opt :verbose, "print compiled templates"
19
+ opt :debug, "enable hiera logging and print backtrace on error\n "
12
20
  end
13
21
 
14
22
  options = Trollop.with_standard_exception_handling(option_parser) do
@@ -18,9 +26,9 @@ module ErbHiera
18
26
  option_parser.parse ARGV
19
27
  end
20
28
 
21
- # validate cli args
22
- raise ArgumentError, "config file not specified" unless options[:config_given]
23
- raise ArgumentError, "config file not readable" unless File.readable?(options[:config])
29
+ if options[:config_given]
30
+ raise ArgumentError, "config file not readable" unless File.readable?(options[:config])
31
+ end
24
32
 
25
33
  raise ArgumentError, "hiera config file not specified" unless options[:hiera_config_given]
26
34
  raise ArgumentError, "hiera config file not readable" unless File.readable?(options[:hiera_config])
@@ -2,38 +2,26 @@ require "hiera"
2
2
 
3
3
  module ErbHiera
4
4
  module Hiera
5
- def self.hiera(key)
6
- hiera = ::Hiera.new(:config => ErbHiera.options[:hiera_config])
7
- ::Hiera.logger = "noop"
8
- value = hiera.lookup(key, nil, ErbHiera.scope, nil, :priority)
5
+ def self.erb_hiera
6
+ @hiera ||= begin
7
+ hiera = ::Hiera.new(:config => ErbHiera.options[:hiera_config])
9
8
 
10
- unless value
11
- puts "\nerror: cannot find value for key: #{key}"
12
- exit 1
13
- end
9
+ # injecting the Hash backend ensures that the --variables flag will always work as expected
10
+ hiera.config[:backends].insert(0, "hash") unless hiera.config[:backends][0] == "hash"
14
11
 
15
- puts "# #{key}: #{value}" if ErbHiera.options[:verbose]
16
- value
17
- end
12
+ ::Hiera.logger = ErbHiera.options[:debug_given] ? "console" : "noop"
18
13
 
19
- def self.hiera_array(key)
20
- hiera = ::Hiera.new(:config => ErbHiera.options[:hiera_config])
21
- ::Hiera.logger = "noop"
22
- value = hiera.lookup(key, nil, ErbHiera.scope, nil, :array)
14
+ if ErbHiera.options[:debug_given]
15
+ puts "# hiera config"
16
+ puts hiera.config.to_yaml
17
+ end
23
18
 
24
- unless value
25
- puts "\nerror: cannot find value for key: #{key}"
26
- exit 1
19
+ hiera
27
20
  end
28
-
29
- puts "# #{key}: #{value}" if ErbHiera.options[:verbose]
30
- value
31
21
  end
32
22
 
33
- def self.hiera_hash(key)
34
- hiera = ::Hiera.new(:config => ErbHiera.options[:hiera_config])
35
- ::Hiera.logger = "noop"
36
- value = hiera.lookup(key, nil, ErbHiera.scope, nil, :hash)
23
+ def self.hiera(key, type: :priority)
24
+ value = erb_hiera.lookup(key, nil, ErbHiera.scope, nil, type)
37
25
 
38
26
  unless value
39
27
  puts "\nerror: cannot find value for key: #{key}"
@@ -44,6 +32,17 @@ module ErbHiera
44
32
  value
45
33
  end
46
34
 
35
+ def self.hiera_array(key)
36
+ hiera(key, type: :array)
37
+ end
38
+
39
+ def self.hiera_hash(key)
40
+ hiera(key, type: :hash)
41
+ end
42
+
43
+ private
44
+
45
+ # bind calls from template to context of this module
47
46
  def self.get_binding
48
47
  binding
49
48
  end
@@ -1,5 +1,5 @@
1
1
  #!/usr/bin/env ruby
2
2
 
3
3
  module ErbHiera
4
- VERSION = "0.4.4"
4
+ VERSION = "0.5.0-rc0"
5
5
  end
data/lib/erb-hiera.rb CHANGED
@@ -2,6 +2,7 @@
2
2
 
3
3
  require "erb"
4
4
  require "yaml"
5
+ require "json"
5
6
  require "fileutils"
6
7
 
7
8
  require "erb-hiera/version"
@@ -10,6 +11,8 @@ require "erb-hiera/directory"
10
11
  require "erb-hiera/hiera"
11
12
  require "erb-hiera/manifest"
12
13
 
14
+ require "hiera/backend/hash_backend"
15
+
13
16
  module ErbHiera
14
17
  class << self
15
18
  attr_accessor :options, :scope
@@ -17,17 +20,20 @@ module ErbHiera
17
20
 
18
21
  def self.run
19
22
  @options = CLI.parse
20
-
21
23
  mappings.each do |mapping|
22
- ErbHiera.scope = mapping["scope"]
23
- input = mapping["dir"]["input"]
24
- output = mapping["dir"]["output"]
24
+ ErbHiera.scope = scope_from_cli || mapping["scope"]
25
+ input = options[:input] || mapping["dir"]["input"]
26
+ output = options[:output] || mapping["dir"]["output"]
25
27
 
26
28
  [:input, :output].each do |location|
27
- raise StandardError, "error: undefined #{dir.to_s.split('_')[0]}put" unless binding.local_variable_get(location)
29
+ unless binding.local_variable_get(location)
30
+ raise StandardError, "error: undefined #{dir.to_s.split('_')[0]}put"
31
+ end
28
32
  end
29
33
 
30
- # if input is a file then out_file is a file too
34
+ output = STDOUT if output == "-"
35
+
36
+ # if input is a file then out_file should be a file too
31
37
  if input =~ /.erb$/
32
38
  generate(output, input)
33
39
  next
@@ -41,22 +47,29 @@ module ErbHiera
41
47
  end
42
48
  rescue => error
43
49
  handle_error(error)
44
- exit 1
45
50
  end
46
51
 
47
52
  private
48
53
 
54
+ def self.scope_from_cli
55
+ JSON.load(options[:scope]) if options[:scope]
56
+ end
57
+
49
58
  def self.generate(out_file, manifest)
50
59
  Manifest.info(manifest, out_file) if options[:verbose]
51
-
52
60
  erb = ERB.new(File.read(manifest), nil, "-").result(Hiera.get_binding)
53
61
 
54
62
  puts erb if options[:verbose]
55
63
 
56
- unless options[:dry_run]
57
- FileUtils.mkdir_p File.dirname(out_file) unless Dir.exists?(File.dirname(out_file))
58
- File.write(out_file, erb)
64
+ return if options[:dry_run]
65
+
66
+ if out_file == STDOUT
67
+ puts erb
68
+ return
59
69
  end
70
+
71
+ FileUtils.mkdir_p File.dirname(out_file) unless Dir.exists?(File.dirname(out_file))
72
+ File.write(out_file, erb)
60
73
  end
61
74
 
62
75
  def self.handle_error(error)
@@ -67,10 +80,11 @@ module ErbHiera
67
80
 
68
81
  puts
69
82
  puts error
83
+ exit 1
70
84
  end
71
85
 
72
86
  def self.mappings
73
- YAML.load_file(options[:config])
87
+ YAML.load_file(ErbHiera.options[:mapping_config])
74
88
  end
75
89
 
76
90
  def self.manifests(dir)
@@ -0,0 +1,52 @@
1
+ class Hiera
2
+ module Backend
3
+ class Hash_backend
4
+ def initialize(cache=nil)
5
+ Hiera.debug("Hiera Hash backend starting")
6
+ end
7
+
8
+ def lookup(key, scope, order_override, resolution_type, context)
9
+ answer = nil
10
+ found = false
11
+
12
+ Hiera.debug("Looking up #{key} in Hash backend")
13
+
14
+ throw :no_such_key unless ErbHiera.options[:variables_given]
15
+
16
+ data = JSON.load(ErbHiera.options[:variables])
17
+
18
+ throw :no_such_key unless data.include?(key)
19
+ found = true
20
+
21
+ # Extra logging that we found the key. This can be outputted
22
+ # multiple times if the resolution type is array or hash but that
23
+ # should be expected as the logging will then tell the user ALL the
24
+ # places where the key is found.
25
+ Hiera.debug("Found #{key} in Hash backend")
26
+
27
+ # for array resolution we just append to the array whatever
28
+ # we find, we then goes onto the next file and keep adding to
29
+ # the array
30
+ #
31
+ # for priority searches we break after the first found data item
32
+ new_answer = Backend.parse_answer(data[key], scope, {}, context)
33
+ case resolution_type.is_a?(Hash) ? :hash : resolution_type
34
+ when :array
35
+ raise Exception, "Hiera type mismatch for key '#{key}': expected Array and got #{new_answer.class}" unless new_answer.kind_of? Array or new_answer.kind_of? String
36
+ answer ||= []
37
+ answer << new_answer
38
+ when :hash
39
+ raise Exception, "Hiera type mismatch for key '#{key}': expected Hash and got #{new_answer.class}" unless new_answer.kind_of? Hash
40
+ answer ||= {}
41
+ answer = Backend.merge_answer(new_answer, answer, resolution_type)
42
+ else
43
+ answer = new_answer
44
+ return answer
45
+ end
46
+
47
+ throw :no_such_key unless found
48
+ return answer
49
+ end
50
+ end
51
+ end
52
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: erb-hiera
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.4
4
+ version: 0.5.0.pre.rc0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Rob Wilson
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-05-09 00:00:00.000000000 Z
11
+ date: 2018-05-11 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: hiera
@@ -103,12 +103,15 @@ extra_rdoc_files: []
103
103
  files:
104
104
  - README.md
105
105
  - bin/erb-hiera
106
- - example/config.yaml
107
106
  - example/hiera.yaml
108
107
  - example/hiera/common.yaml
108
+ - example/hiera/environment/default.yaml
109
109
  - example/hiera/environment/dev.yaml
110
+ - example/hiera/environment/prod.yaml
110
111
  - example/hiera/environment/stage.yaml
112
+ - example/mapping.yaml
111
113
  - example/output/dev/template.txt
114
+ - example/output/prod/template.txt
112
115
  - example/output/stage/template.txt
113
116
  - example/templates/template.txt
114
117
  - lib/erb-hiera.rb
@@ -117,6 +120,7 @@ files:
117
120
  - lib/erb-hiera/hiera.rb
118
121
  - lib/erb-hiera/manifest.rb
119
122
  - lib/erb-hiera/version.rb
123
+ - lib/hiera/backend/hash_backend.rb
120
124
  homepage: https://github.com/roobert/erb-hiera
121
125
  licenses:
122
126
  - MIT
@@ -132,12 +136,12 @@ required_ruby_version: !ruby/object:Gem::Requirement
132
136
  version: '0'
133
137
  required_rubygems_version: !ruby/object:Gem::Requirement
134
138
  requirements:
135
- - - ">="
139
+ - - ">"
136
140
  - !ruby/object:Gem::Version
137
- version: '0'
141
+ version: 1.3.1
138
142
  requirements: []
139
143
  rubyforge_project:
140
- rubygems_version: 2.7.6
144
+ rubygems_version: 2.6.14
141
145
  signing_key:
142
146
  specification_version: 4
143
147
  summary: ERB Hiera Document Generator
File without changes