Skip to content

Commit 80e0dd1

Browse files
committed
Merge branch 'kpaulisse-hiera-5' into kpaulisse-mega-merge
2 parents 6bd73dd + 018c7d7 commit 80e0dd1

31 files changed

+950
-269
lines changed

doc/configuration-hiera.md

Lines changed: 29 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,28 @@
11
# Configuring octocatalog-diff to use Hiera
22

3-
If you are using Hiera with Puppet, then you must already have a [`hiera.yaml`](https://docs.puppet.com/puppet/latest/reference/config_file_hiera.html) file to configure it. These instructions will guide you through pointing octocatalog-diff at that configuration file.
3+
## Hiera 5
4+
5+
Hiera 5 is included with Puppet 4.9 and higher.
6+
7+
If there is a `hiera.yaml` file in the base directory of the environment that is in hiera 5 format, and you are running Puppet 4.9 or higher, then that file will be recognized by Puppet (and therefore, by octocatalog-diff). There is no special configuration for octocatalog-diff needed to make this work. Similarly, there is no command line option or setting to changed this behavior, because there is no corresponding option to change Puppet's behavior.
8+
9+
If you are running Puppet 4.8 or lower, then the `hiera.yaml` file in the base directory of the environment will be ignored (unless you use `--hiera-config` to specify it as your global configuration file).
10+
11+
If you have no global hiera configuration and you wish to rely on a `hiera.yaml` file in the base directory of your environment, make sure that you are *not* using any of the following command line options or [configuration settings](/doc/configuration.md):
12+
13+
- `--hiera-path` or `settings[:hiera_path]`
14+
- `--hiera-path-strip` or `settings[:hiera_path_strip]`
15+
- `--hiera-config` or `settings[:hiera_config]`
16+
17+
There is more information about Hiera 5 in Puppet's documentation:
18+
19+
- [Enable the environment layer for existing Hiera data](https://puppet.com/docs/puppet/5.3/hiera_migrate_environments.html)
20+
21+
## Hiera global configuration
22+
23+
If you are using Hiera 5 with a global configuration, or you are using Hiera 3 or before, then you must already have a [`hiera.yaml`](https://docs.puppet.com/puppet/latest/reference/config_file_hiera.html) file to configure it. These instructions will guide you through pointing octocatalog-diff at that configuration file.
24+
25+
octocatalog-diff will automatically determine the version of your Hiera configuration file and treat it accordingly. (Hiera 5 configuration files are identified as such by a `version: 5` line in the file itself.)
426

527
Before you start, please understand how octocatalog-diff compiles a catalog:
628

@@ -10,9 +32,9 @@ Before you start, please understand how octocatalog-diff compiles a catalog:
1032
- It compiles the catalog, based on the temporary directory, for environment=production
1133
- It removes the temporary directory
1234

13-
## Configuring the location of hiera.yaml
35+
### Configuring the location of global hiera.yaml
1436

15-
The command line option `--hiera-config PATH` allows you to set the path to hiera.yaml.
37+
The command line option `--hiera-config PATH` allows you to set the path to the global hiera.yaml.
1638

1739
You may specify this as either an absolute or a relative path.
1840

@@ -24,12 +46,9 @@ You may specify this as either an absolute or a relative path.
2446
bin/octocatalog-diff --hiera-config hiera.yaml ...
2547
```
2648
27-
The path is relative to a checkout of your Puppet repository. With the setting above, it will use the file named `hiera.yaml` that is at the top level
28-
of your Puppet checkout.
49+
The path is relative to a checkout of your Puppet repository. With the setting above, it will use the file named `hiera.yaml` that is at the top level of your Puppet checkout.
2950
30-
Perhaps your hiera.yaml file is in a subdirectory of your Puppet checkout. In that case, just use the relative directory path. Be sure not to add a leading `/` though,
31-
because you don't want octocatalog-diff to treat it as an absolute path. In the following example, suppose you have a top level directory called `config` and your
32-
`hiera.yaml` file is contained within it. You could then use:
51+
Perhaps your hiera.yaml file is in a subdirectory of your Puppet checkout. In that case, just use the relative directory path. Be sure not to add a leading `/` though, because you don't want octocatalog-diff to treat it as an absolute path. In the following example, suppose you have a top level directory called `config` and your `hiera.yaml` file is contained within it. You could then use:
3352
3453
```
3554
bin/octocatalog-diff --hiera-config config/hiera.yaml ...
@@ -65,7 +84,7 @@ You may specify this as either an absolute or a relative path.
6584
6685
We strongly recommend that you version-control your hiera.yaml file within your Puppet repository, and use the relative path option described above.
6786
68-
## Configuring the directory in your repository in which hiera data files are found
87+
### Configuring the directory in your repository in which hiera data files are found
6988
7089
The command line option `--hiera-path PATH` allows you to set the directory path, relative to the checkout of your Puppet repository, of your Hiera YAML/JSON data files.
7190
@@ -88,7 +107,7 @@ If you are specifying the Hiera data path in the [configuration file](/doc/confi
88107
89108
octocatalog-diff will fail if you specify a path that is not a directory.
90109
91-
## Configuring the prefix path to strip
110+
### Configuring the prefix path to strip
92111
93112
This is a different, and potentially more complex, alternative to `--hiera-path` / `settings[:hiera_path]` described in the prior section. Unless you have a very good reason, you should prefer to use the instructions above.
94113

doc/optionsref.md

Lines changed: 92 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,18 @@ Usage: octocatalog-diff [command line options]
4343
--master-cache-branch BRANCH Branch to cache
4444
--safe-to-delete-cached-master-dir PATH
4545
OK to delete cached master directory at this path
46-
--hiera-config PATH Relative path to hiera YAML file
46+
--hiera-config STRING Full or relative path to global Hiera configuration file globally
47+
--to-hiera-config STRING Full or relative path to global Hiera configuration file for the to branch
48+
--from-hiera-config STRING Full or relative path to global Hiera configuration file for the from branch
4749
--no-hiera-config Disable hiera config file installation
48-
--hiera-path PATH Path to hiera data directory, relative to top directory of repository
50+
--hiera-path STRING Path to hiera data directory, relative to top directory of repository globally
51+
--to-hiera-path STRING Path to hiera data directory, relative to top directory of repository for the to branch
52+
--from-hiera-path STRING Path to hiera data directory, relative to top directory of repository for the from branch
4953
--no-hiera-path Do not use any default hiera path settings
50-
--hiera-path-strip PATH Path prefix to strip when munging hiera.yaml
54+
--hiera-path-strip STRING Path prefix to strip when munging hiera.yaml globally
55+
--to-hiera-path-strip STRING Path prefix to strip when munging hiera.yaml for the to branch
56+
--from-hiera-path-strip STRING
57+
Path prefix to strip when munging hiera.yaml for the from branch
5158
--no-hiera-path-strip Do not use any default hiera path strip settings
5259
--ignore-attr "attr1,attr2,..."
5360
Attributes to ignore
@@ -81,10 +88,10 @@ Usage: octocatalog-diff [command line options]
8188
--from-puppet-binary STRING Full path to puppet binary for the from branch
8289
--facts-terminus STRING Facts terminus: one of yaml, facter
8390
--puppetdb-ssl-ca FILENAME CA certificate that signed the PuppetDB certificate
84-
--puppetdb-ssl-client-password PASSWORD
85-
Password for SSL client key to connect to PuppetDB
8691
--puppetdb-ssl-client-cert FILENAME
8792
SSL client certificate to connect to PuppetDB
93+
--puppetdb-ssl-client-password PASSWORD
94+
Password for SSL client key to connect to PuppetDB
8895
--puppetdb-ssl-client-key FILENAME
8996
SSL client key to connect to PuppetDB
9097
--puppetdb-ssl-client-password-file FILENAME
@@ -643,6 +650,43 @@ by permitting a data type specification as well. (<a href="../lib/octocatalog-di
643650
</td>
644651
</tr>
645652

653+
<tr>
654+
<td valign=top>
655+
<pre><code>--from-hiera-config STRING</code></pre>
656+
</td>
657+
<td valign=top>
658+
Full or relative path to global Hiera configuration file for the from branch
659+
</td>
660+
<td valign=top>
661+
Specify a relative path to the Hiera yaml file (<a href="../lib/octocatalog-diff/cli/options/hiera_config.rb">hiera_config.rb</a>)
662+
</td>
663+
</tr>
664+
665+
<tr>
666+
<td valign=top>
667+
<pre><code>--from-hiera-path STRING</code></pre>
668+
</td>
669+
<td valign=top>
670+
Path to hiera data directory, relative to top directory of repository for the from branch
671+
</td>
672+
<td valign=top>
673+
Specify the path to the Hiera data directory (relative to the top level Puppet checkout). For Puppet Enterprise and the
674+
Puppet control repo template, the value of this should be 'hieradata', which is the default. (<a href="../lib/octocatalog-diff/cli/options/hiera_path.rb">hiera_path.rb</a>)
675+
</td>
676+
</tr>
677+
678+
<tr>
679+
<td valign=top>
680+
<pre><code>--from-hiera-path-strip STRING</code></pre>
681+
</td>
682+
<td valign=top>
683+
Path prefix to strip when munging hiera.yaml for the from branch
684+
</td>
685+
<td valign=top>
686+
Specify the path to strip off the datadir to munge hiera.yaml file (<a href="../lib/octocatalog-diff/cli/options/hiera_path_strip.rb">hiera_path_strip.rb</a>)
687+
</td>
688+
</tr>
689+
646690
<tr>
647691
<td valign=top>
648692
<pre><code>--from-puppet-binary STRING</code></pre>
@@ -773,10 +817,10 @@ This timeout is specified in seconds. (<a href="../lib/octocatalog-diff/cli/opti
773817

774818
<tr>
775819
<td valign=top>
776-
<pre><code>--hiera-config PATH</code></pre>
820+
<pre><code>--hiera-config STRING</code></pre>
777821
</td>
778822
<td valign=top>
779-
Relative path to hiera YAML file
823+
Full or relative path to global Hiera configuration file globally
780824
</td>
781825
<td valign=top>
782826
Specify a relative path to the Hiera yaml file (<a href="../lib/octocatalog-diff/cli/options/hiera_config.rb">hiera_config.rb</a>)
@@ -785,10 +829,10 @@ This timeout is specified in seconds. (<a href="../lib/octocatalog-diff/cli/opti
785829

786830
<tr>
787831
<td valign=top>
788-
<pre><code>--hiera-path PATH</code></pre>
832+
<pre><code>--hiera-path STRING</code></pre>
789833
</td>
790834
<td valign=top>
791-
Path to hiera data directory, relative to top directory of repository
835+
Path to hiera data directory, relative to top directory of repository globally
792836
</td>
793837
<td valign=top>
794838
Specify the path to the Hiera data directory (relative to the top level Puppet checkout). For Puppet Enterprise and the
@@ -798,10 +842,10 @@ Puppet control repo template, the value of this should be 'hieradata', which is
798842

799843
<tr>
800844
<td valign=top>
801-
<pre><code>--hiera-path-strip PATH</code></pre>
845+
<pre><code>--hiera-path-strip STRING</code></pre>
802846
</td>
803847
<td valign=top>
804-
Path prefix to strip when munging hiera.yaml
848+
Path prefix to strip when munging hiera.yaml globally
805849
</td>
806850
<td valign=top>
807851
Specify the path to strip off the datadir to munge hiera.yaml file (<a href="../lib/octocatalog-diff/cli/options/hiera_path_strip.rb">hiera_path_strip.rb</a>)
@@ -1541,6 +1585,43 @@ by permitting a data type specification as well. (<a href="../lib/octocatalog-di
15411585
</td>
15421586
</tr>
15431587

1588+
<tr>
1589+
<td valign=top>
1590+
<pre><code>--to-hiera-config STRING</code></pre>
1591+
</td>
1592+
<td valign=top>
1593+
Full or relative path to global Hiera configuration file for the to branch
1594+
</td>
1595+
<td valign=top>
1596+
Specify a relative path to the Hiera yaml file (<a href="../lib/octocatalog-diff/cli/options/hiera_config.rb">hiera_config.rb</a>)
1597+
</td>
1598+
</tr>
1599+
1600+
<tr>
1601+
<td valign=top>
1602+
<pre><code>--to-hiera-path STRING</code></pre>
1603+
</td>
1604+
<td valign=top>
1605+
Path to hiera data directory, relative to top directory of repository for the to branch
1606+
</td>
1607+
<td valign=top>
1608+
Specify the path to the Hiera data directory (relative to the top level Puppet checkout). For Puppet Enterprise and the
1609+
Puppet control repo template, the value of this should be 'hieradata', which is the default. (<a href="../lib/octocatalog-diff/cli/options/hiera_path.rb">hiera_path.rb</a>)
1610+
</td>
1611+
</tr>
1612+
1613+
<tr>
1614+
<td valign=top>
1615+
<pre><code>--to-hiera-path-strip STRING</code></pre>
1616+
</td>
1617+
<td valign=top>
1618+
Path prefix to strip when munging hiera.yaml for the to branch
1619+
</td>
1620+
<td valign=top>
1621+
Specify the path to strip off the datadir to munge hiera.yaml file (<a href="../lib/octocatalog-diff/cli/options/hiera_path_strip.rb">hiera_path_strip.rb</a>)
1622+
</td>
1623+
</tr>
1624+
15441625
<tr>
15451626
<td valign=top>
15461627
<pre><code>--to-puppet-binary STRING</code></pre>

lib/octocatalog-diff/catalog-util/builddir.rb

Lines changed: 76 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -236,25 +236,11 @@ def install_hiera_config(logger, options)
236236

237237
# Munge datadir in hiera config file
238238
obj = YAML.load_file(file_src)
239-
([obj[:backends]].flatten || %w(yaml json)).each do |key|
240-
next unless obj.key?(key.to_sym)
241-
if options[:hiera_path_strip].is_a?(String)
242-
next if obj[key.to_sym][:datadir].nil?
243-
rexp1 = Regexp.new('^' + options[:hiera_path_strip])
244-
obj[key.to_sym][:datadir].sub!(rexp1, @tempdir)
245-
elsif options[:hiera_path].is_a?(String)
246-
obj[key.to_sym][:datadir] = File.join(@tempdir, 'environments', environment, options[:hiera_path])
247-
end
248-
rexp2 = Regexp.new('%{(::)?environment}')
249-
obj[key.to_sym][:datadir].sub!(rexp2, environment)
250-
251-
# Make sure the dirctory exists. If not, log a warning. This is *probably* a setup error, but we don't
252-
# want it to be fatal in case (for example) someone is doing an octocatalog-diff to verify moving this
253-
# directory around or even setting up Hiera for the very first time.
254-
unless File.directory?(obj[key.to_sym][:datadir])
255-
message = "WARNING: Hiera datadir for #{key} doesn't seem to exist at #{obj[key.to_sym][:datadir]}"
256-
logger.warn message
257-
end
239+
version = obj['version'] || obj[:version] || 3
240+
if version.to_i == 5
241+
update_hiera_config_v5(logger, options, obj)
242+
else
243+
update_hiera_config_v3(logger, options, obj)
258244
end
259245

260246
# Write properly formatted hiera config file into temporary directory
@@ -291,6 +277,77 @@ def install_ssl(logger, options)
291277

292278
private
293279

280+
# Jump-off for hiera v3 (or earlier)
281+
# @param logger [Logger] Logger object
282+
# @param options [Hash] Options hash
283+
# @param obj [Hash] Parsed hiera.yaml file
284+
def update_hiera_config_v3(logger, options, obj)
285+
([obj[:backends]].flatten || %w(yaml json)).each do |key|
286+
next unless obj.key?(key.to_sym)
287+
obj[key.to_sym][:datadir] = hiera_munge(options, obj[key.to_sym][:datadir])
288+
289+
# Make sure the directory exists. If not, log a warning. This is *probably* a setup error, but we don't
290+
# want it to be fatal in case (for example) someone is doing an octocatalog-diff to verify moving this
291+
# directory around or even setting up Hiera for the very first time.
292+
unless File.directory?(obj[key.to_sym][:datadir])
293+
message = "WARNING: Hiera datadir for #{key} doesn't seem to exist at #{obj[key.to_sym][:datadir]}"
294+
logger.warn message
295+
end
296+
end
297+
end
298+
299+
# Jump-off for hiera v5
300+
# @param logger [Logger] Logger object
301+
# @param options [Hash] Options hash
302+
# @param obj [Hash] Parsed hiera.yaml file
303+
def update_hiera_config_v5(_logger, options, obj)
304+
defaults_key = obj.key?(:defaults) ? :defaults : 'defaults'
305+
hierarchy_key = obj.key?(:hierarchy) ? :hierarchy : 'hierarchy'
306+
307+
# Fix defaults:datadir
308+
if obj[defaults_key].is_a?(Hash)
309+
[:datadir, 'datadir'].each do |key|
310+
next unless obj[defaults_key].key?(key)
311+
obj[defaults_key][key] = hiera_munge(options, obj[defaults_key][key])
312+
end
313+
end
314+
315+
# Fix hierarchy:datadir
316+
if obj[hierarchy_key].is_a?(Array)
317+
obj[hierarchy_key].each do |level|
318+
[:datadir, 'datadir'].each do |key|
319+
next unless level.key?(key)
320+
if options[:hiera_path_strip].is_a?(String)
321+
level[key] = hiera_munge(options, level[key])
322+
elsif options[:hiera_path].is_a?(String)
323+
message = [
324+
"Hierarchy item #{level.inspect} has a datadir.",
325+
'--hiera-path is not supported in this situation.',
326+
'Please use --hiera-path-strip.'
327+
].join(' ')
328+
raise ArgumentError, message
329+
end
330+
end
331+
end
332+
end
333+
end
334+
335+
# Hiera munge - shared method to apply :hiera_path_strip and :hiera_path
336+
def hiera_munge(options, current_value)
337+
return if current_value.nil?
338+
339+
if options[:hiera_path_strip].is_a?(String)
340+
rexp1 = Regexp.new('^' + options[:hiera_path_strip])
341+
current_value.sub!(rexp1, @tempdir)
342+
elsif options[:hiera_path].is_a?(String)
343+
current_value = File.join(@tempdir, 'environments', environment, options[:hiera_path])
344+
end
345+
rexp2 = Regexp.new('%{(::)?environment}')
346+
current_value.sub!(rexp2, environment)
347+
348+
current_value
349+
end
350+
294351
# Install SSL certificate authority certificate
295352
# @param logger [Logger] Logger object
296353
# @param options [Hash] Options hash

lib/octocatalog-diff/cli.rb

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
require_relative 'cli/printer'
88
require_relative 'errors'
99
require_relative 'util/catalogs'
10+
require_relative 'util/util'
1011
require_relative 'version'
1112

1213
require 'logger'
@@ -53,7 +54,7 @@ class Cli
5354
# @return [Integer] Exit code: 0=no diffs, 1=something went wrong, 2=worked but there are diffs
5455
def self.cli(argv = ARGV, logger = Logger.new(STDERR), opts = {})
5556
# Save a copy of argv to print out later in debugging
56-
argv_save = argv.dup
57+
argv_save = OctocatalogDiff::Util::Util.deep_dup(argv)
5758

5859
# Are there additional ARGV to munge, e.g. that have been supplied in the options from a
5960
# configuration file?
@@ -70,8 +71,14 @@ def self.cli(argv = ARGV, logger = Logger.new(STDERR), opts = {})
7071
# Note: do NOT use 'options[k] ||= v' here because if the value of options[k] is boolean(false)
7172
# it will then be overridden. Whereas the intent is to define values only for those keys that don't exist.
7273
opts.each { |k, v| options[k] = v unless options.key?(k) }
73-
veto_options = %w(enc header hiera_config include_tags)
74+
veto_options = %w(enc header include_tags)
7475
veto_options.each { |x| options.delete(x.to_sym) if options["no_#{x}".to_sym] }
76+
if options[:no_hiera_config]
77+
vetoes = %w[hiera_config to_hiera_config from_hiera_config]
78+
vetoes.each do |key|
79+
options.delete(key.to_sym)
80+
end
81+
end
7582
options[:ignore].concat opts.fetch(:additional_ignores, [])
7683

7784
# Incorporate default options where needed.
@@ -125,7 +132,7 @@ def self.cli(argv = ARGV, logger = Logger.new(STDERR), opts = {})
125132
# @param argv [Array] Command line arguments (MUST be specified)
126133
# @return [Hash] Options
127134
def self.parse_opts(argv)
128-
options = { ignore: DEFAULT_IGNORES.dup }
135+
options = { ignore: OctocatalogDiff::Util::Util.deep_dup(DEFAULT_IGNORES) }
129136
Options.parse_options(argv, options)
130137
end
131138

0 commit comments

Comments
 (0)