Skip to content

Commit 33dd541

Browse files
author
Kevin Paulisse
committed
Merge branch 'kpaulisse-hiera-simplification' into kpaulisse-octocatalog-diff-0-5-4
2 parents e3eb6a7 + 8756b99 commit 33dd541

10 files changed

Lines changed: 308 additions & 75 deletions

File tree

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# Configuring octocatalog-diff to use Hiera path stripping
2+
3+
This is a different, and potentially more complex, alternative to `hiera-path` / `settings[:hiera_path]` described in the [Configuring octocatalog-diff to use Hiera](/doc/configuration-hiera.md) document. Unless you have a very good reason, you should prefer to use the instructions in that document instead of using the more complicated option that is described herein.
4+
5+
The command line option `--hiera-path-strip PATH` allows you to manipulate directory paths for the JSON or YAML hiera backends. This setting only has an effect on the copy of hiera.yaml that is copied into the temporary compilation directory. This does not make any changes to the actual source hiera.yaml file on your system or in your checkout.
6+
7+
For example, perhaps your production hiera.yaml file has entries such as the following:
8+
9+
```
10+
---
11+
:backends:
12+
- yaml
13+
:hierarchy:
14+
- "nodes/%{::trusted.certname}"
15+
- common
16+
17+
:yaml:
18+
:datadir: /etc/puppetlabs/code/environments/%{environment}/hieradata
19+
```
20+
21+
However, when you run octocatalog-diff on a machine that is not a Puppet master, the hiera data will not actually be found in `/etc/puppetlabs/code/environments/production/hieradata`, but rather in a directory called `hieradata` relative to the checkout of your Puppet code.
22+
23+
Specifying `--hiera-path-strip PATH` causes octocatalog-diff to munge the datadir for the YAML and JSON configuration. The correct command in this case is now:
24+
25+
```
26+
bin/octocatalog-diff --hiera-config hiera.yaml --hiera-path-strip /etc/puppetlabs/code
27+
```
28+
29+
```
30+
---
31+
:backends:
32+
- yaml
33+
:hierarchy:
34+
- "nodes/%{::trusted.certname}"
35+
- common
36+
37+
:yaml:
38+
:datadir: /var/tmp/puppet-compile-dir-92347829847/environments/%{environment}/hieradata
39+
```

doc/configuration-hiera.md

Lines changed: 37 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ Before you start, please understand how octocatalog-diff compiles a catalog:
1010
- It compiles the catalog, based on the temporary directory, for environment=production
1111
- It removes the temporary directory
1212

13-
## Configuring the path to hiera.yaml
13+
## Configuring the location of hiera.yaml
1414

1515
The command line option `--hiera-config PATH` allows you to set the path to hiera.yaml.
1616

@@ -21,22 +21,26 @@ You may specify this as either an absolute or a relative path.
2121
octocatalog-diff knows to use a relative path when the supplied path for `--hiera-config` does not start with a `/`.
2222

2323
```
24-
bin/octocatalog-diff --hiera-config config/hiera.yaml ...
24+
bin/octocatalog-diff --hiera-config hiera.yaml ...
2525
```
2626
27-
The path is relative to a checkout of your Puppet repository. As per the example in the introduction, say that octocatalog-diff is using a temporary directory of `/var/tmp/puppet-compile-dir-92347829847` when compiling a Puppet catalog. With the setting above, it will copy `config/hiera.yaml` (relative to your Puppet checkout) into the temporary directory.
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.
2829
29-
If you use Puppet to manage your hiera.yaml file on Puppet masters, perhaps it is found in one of the modules in your code. In that case, you may use syntax like:
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:
3033
3134
```
32-
bin/octocatalog-diff --hiera-config modules/puppet/files/hiera.yaml ...
35+
bin/octocatalog-diff --hiera-config config/hiera.yaml ...
3336
```
3437
3538
If you are specifying the hiera.yaml path in the [configuration file](/doc/configuration.md), you will instead set the variable like this:
3639
3740
```
41+
settings[:hiera_config] = 'hiera.yaml'
42+
(or)
3843
settings[:hiera_config] = 'config/hiera.yaml'
39-
settings[:hiera_config] = 'modules/puppet/files/hiera.yaml'
4044
```
4145
4246
octocatalog-diff will fail if you specify a hiera configuration location that cannot be opened.
@@ -57,47 +61,35 @@ You may specify this as either an absolute or a relative path.
5761
settings[:hiera_config] = '/etc/puppetlabs/puppet/hiera.yaml'
5862
```
5963
60-
Please note that octocatalog-diff will copy the file from the specified location into the compile directory. Since this hiera.yaml file is not copied from your Puppet repo, there is no way to compile the "to" and "from" branches using different hiera.yaml files. Furthermore, you are responsible for getting this file into place on any machine that will run octocatalog-diff.
64+
Please note that octocatalog-diff will copy the file from the specified location into the compile directory. Since this hiera.yaml file is not copied from your Puppet repo, there is no way to compile the "to" and "from" branches using different hiera.yaml files. Furthermore, you are responsible for getting this file into place on any machine that will run octocatalog-diff. An absolute path may make octocatalog-diff work correctly on your Puppet master servers, but the structure may differ on other machines where you wish to run the utility.
6165
6266
We strongly recommend that you version-control your hiera.yaml file within your Puppet repository, and use the relative path option described above.
6367
68+
## Configuring the directory in your repository in which hiera data files are found
69+
70+
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.
71+
72+
If you are using the out-of-the-box Puppet Enterprise configuration, or the [Puppet Control Repo template](https://github.com/puppetlabs/control-repo), then the correct setting here is simply 'hieradata'.
73+
74+
You must specify this as a relative path. octocatalog-diff knows to use a relative path when the supplied path for `--hiera-path` does not start with a `/`.
75+
76+
```
77+
bin/octocatalog-diff --hiera-path hieradata ...
78+
```
79+
80+
The path is relative to a checkout of your Puppet repository. With the setting above, it will look for Hiera data in a directory called `hieradata` that is at the top level
81+
of your Puppet checkout.
82+
83+
If you are specifying the Hiera data path in the [configuration file](/doc/configuration.md), you will instead set the variable like this:
84+
85+
```
86+
settings[:hiera_path] = 'hieradata'
87+
```
88+
89+
octocatalog-diff will fail if you specify a path that is not a directory.
90+
6491
## Configuring the prefix path to strip
6592
66-
The command line option `--hiera-path-strip PATH` allows you to manipulate directory paths for the JSON or YAML hiera backends. This setting only has an effect on the copy of hiera.yaml that is copied into the temporary compilation directory. This does not make any changes to the actual source hiera.yaml file on your system or in your checkout.
67-
68-
For example, perhaps your production hiera.yaml file has entries such as the following:
69-
70-
```
71-
:backends:
72-
- yaml
73-
:yaml:
74-
:datadir: /var/lib/puppet/environments/%{::environment}/hieradata
75-
:hierarchy:
76-
- servers/%{::fqdn}
77-
- platform/%{::virtual}
78-
- datacenter/%{::datacenter}
79-
- os/%{::operatingsystem}
80-
- common
81-
```
82-
83-
However, when you run octocatalog-diff, the hiera data will not actually be found in `/var/lib/puppet/environments/production/hieradata`, but rather in a directory called `environments/production/hieradata` relative to the checkout of your Puppet code.
84-
85-
Specifying `--hiera-path-strip PATH` causes octocatalog-diff will rewrite the datadir for the YAML and JSON configuration. In the example above, the correct setting is `--hiera-path-strip /var/lib/puppet`, which will result in the following configuration in the hiera.yaml file:
86-
87-
```
88-
bin/octocatalog-diff --hiera-config environments/production/config/hiera.yaml --hiera-path-strip /var/lib/puppet
89-
```
90-
91-
```
92-
# This is the temporary hiera.yaml file used for octocatalog-diff catalog compilation
93-
:backends:
94-
- yaml
95-
:yaml:
96-
:datadir: /var/tmp/puppet-compile-dir-92347829847/environments/%{::environment}/hieradata
97-
:hierarchy:
98-
- servers/%{::fqdn}
99-
- platform/%{::virtual}
100-
- datacenter/%{::datacenter}
101-
- os/%{::operatingsystem}
102-
- common
103-
```
93+
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.
94+
95+
If you need to use the more complex path strip alternative, see: [Configuring octocatalog-diff to use Hiera path stripping](/doc/advanced-hiera-path-stripping.md).

examples/octocatalog-diff.cfg.rb

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -34,21 +34,37 @@ def self.config
3434
##############################################################################################
3535

3636
# settings[:hiera_config] = '/etc/puppetlabs/puppet/hiera.yaml' # Absolute path
37-
# settings[:hiera_config] = 'environments/production/config/hiera.yaml' # Relative path
37+
settings[:hiera_config] = 'hiera.yaml' # Relative path, assumes hiera.yaml at top of repo
3838

3939
##############################################################################################
40+
# hiera_path
4041
# hiera_path_strip
41-
# Portion of the `:datadir:` to strip (used for JSON and YAML data sources). For
42-
# example, perhaps your hiera.yaml file contains this code:
43-
# :yaml:
44-
# :datadir: /var/lib/puppet/environments/%{::environment}/hieradata
45-
# In this case, you desire to strip `/var/lib/puppet` from the beginning of the path,
46-
# in order that octocatalog-diff can find your hiera datafiles in the compilation
47-
# location, which is {temporary directory}/environments/production/hieradata.
48-
# More: https://github.com/github/octocatalog-diff/blob/master/doc/configuration-hiera.md
42+
# These control the setup of the 'datadir' when you are using the JSON or YAML data source.
43+
# There are two ways to configure this setting - do one or the other but not both.
44+
#
45+
# 1. (EASIEST METHOD)
46+
# You can specify the path to the hieradata relative to the checkout of your Puppet repo.
47+
# This may be the most straightforward to configure. For example, if your Hiera data YAML
48+
# and JSON files are found under a `hieradata` directory in the top level of your Puppet
49+
# repo, simply set `settings[:hiera_path] = 'hieradata'` and you're done!
50+
#
51+
# 2. (MORE COMPLEX METHOD)
52+
# You can specify a string that will be stripped off the existing defined data directory
53+
# in the hiera.yaml file. For example, perhaps your hiera.yaml file contains this code:
54+
# :yaml:
55+
# :datadir: /etc/puppetlabs/code/environments/%{environment}/hieradata
56+
# In this case, you desire to strip `/etc/puppetlabs/code` from the beginning of the path,
57+
# in order that octocatalog-diff can find your hiera datafiles in the compilation
58+
# location, which is {temporary directory}/environments/production/hieradata.
59+
#
60+
# More: https://github.com/github/octocatalog-diff/blob/master/doc/configuration-hiera.md
4961
##############################################################################################
5062

51-
# settings[:hiera_path_strip] = '/var/lib/puppet'
63+
# This should work out-of-the-box with a default Puppet Enterprise or Puppet Control Repo setup.
64+
settings[:hiera_path] = 'hieradata'
65+
66+
# If you want to use the 'strip' method described above, this may work.
67+
# settings[:hiera_path_strip] = '/etc/puppetlabs/code'
5268

5369
##############################################################################################
5470
# puppetdb_url

lib/octocatalog-diff/catalog-diff/cli.rb

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,8 @@ class Cli
3939
compare_file_text: true,
4040
display_datatype_changes: true,
4141
parallel: true,
42-
suppress_absent_file_details: true
42+
suppress_absent_file_details: true,
43+
hiera_path: 'hieradata'
4344
}.freeze
4445

4546
# This method is the one to call externally. It is possible to specify alternate
@@ -75,6 +76,8 @@ def self.cli(argv = ARGV, logger = Logger.new(STDERR), opts = {})
7576
# Note: do NOT use 'options[k] ||= v' here because if the value of options[k] is boolean(false)
7677
# it will then be overridden. Whereas the intent is to define values only for those keys that don't exist.
7778
DEFAULT_OPTIONS.each { |k, v| options[k] = v unless options.key?(k) }
79+
veto_with_none_options = %w(hiera_path hiera_path_strip)
80+
veto_with_none_options.each { |x| options.delete(x.to_sym) if options[x.to_sym] == :none }
7881

7982
# Fact overrides come in here - 'options' is modified
8083
setup_fact_overrides(options)
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# Specify the path to the Hiera data directory (relative to the top level Puppet checkout). For Puppet Enterprise and the
2+
# Puppet control repo template, the value of this should be 'hieradata', which is the default.
3+
# @param parser [OptionParser object] The OptionParser argument
4+
# @param options [Hash] Options hash being constructed; this is modified in this method.
5+
OctocatalogDiff::CatalogDiff::Cli::Options::Option.newoption(:hiera_path) do
6+
has_weight 181
7+
8+
def parse(parser, options)
9+
parser.on('--hiera-path PATH', 'Path to hiera data directory, relative to top directory of repository') do |path_in|
10+
if options.key?(:hiera_path_strip) && options[:hiera_path_strip] != :none
11+
raise ArgumentError, '--hiera-path and --hiera-path-strip are mutually exclusive'
12+
end
13+
14+
if options[:hiera_path] == :none
15+
raise ArgumentError, '--hiera-path and --no-hiera-path are mutually exclusive'
16+
end
17+
18+
options[:hiera_path] = path_in
19+
20+
if options[:hiera_path].start_with?('/')
21+
raise ArgumentError, '--hiera-path PATH must be a relative path not an absolute path'
22+
end
23+
24+
options[:hiera_path].sub!(%r{/+$}, '')
25+
raise ArgumentError, '--hiera-path must not be empty' if options[:hiera_path].empty?
26+
end
27+
28+
parser.on('--no-hiera-path', 'Do not use any default hiera path settings') do
29+
if options[:hiera_path].is_a?(String)
30+
raise ArgumentError, '--hiera-path and --no-hiera-path are mutually exclusive'
31+
end
32+
33+
options[:hiera_path] = :none
34+
end
35+
end
36+
end

lib/octocatalog-diff/catalog-diff/cli/options/hiera_path_strip.rb

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,26 @@
22
# @param parser [OptionParser object] The OptionParser argument
33
# @param options [Hash] Options hash being constructed; this is modified in this method.
44
OctocatalogDiff::CatalogDiff::Cli::Options::Option.newoption(:hiera_path_strip) do
5-
has_weight 181
5+
has_weight 182
66

77
def parse(parser, options)
88
parser.on('--hiera-path-strip PATH', 'Path prefix to strip when munging hiera.yaml') do |path_in|
9+
if options.key?(:hiera_path) && options[:hiera_path] != :none
10+
raise ArgumentError, '--hiera-path and --hiera-path-strip are mutually exclusive'
11+
end
12+
13+
if options[:hiera_path_strip] == :none
14+
raise ArgumentError, '--hiera-path and --no-hiera-path are mutually exclusive'
15+
end
16+
917
options[:hiera_path_strip] = path_in
1018
end
19+
20+
parser.on('--no-hiera-path-strip', 'Do not use any default hiera path strip settings') do
21+
if options[:hiera_path_strip].is_a?(String)
22+
raise ArgumentError, '--hiera-path and --no-hiera-path are mutually exclusive'
23+
end
24+
options[:hiera_path_strip] = :none
25+
end
1126
end
1227
end

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

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ class BuildDir
2727
# :enc [String] ENC script file (can be relative or absolute path)
2828
# :pe_enc_url [String] ENC URL (for Puppet Enterprise node classification service)
2929
# :hiera_config [String] hiera configuration file (relative to base directory)
30+
# :hiera_path [String] relative path to hiera data files (mutually exclusive with :hiera_path_strip)
3031
# :hiera_path_strip [String] string to strip off the beginning of :datadir
3132
# :puppetdb_ssl_ca [String] Path to SSL CA certificate
3233
# :puppetdb_ssl_client_key [String] String representation of SSL client key
@@ -52,9 +53,8 @@ def initialize(options = {}, logger = nil)
5253
install_puppetdb_conf(logger, options[:puppetdb_url], options[:puppetdb_server_url_timeout])
5354
install_routes_yaml(logger)
5455
end
55-
unless options[:hiera_config].nil?
56-
install_hiera_config(logger, options[:hiera_config], options[:hiera_path_strip])
57-
end
56+
install_hiera_config(logger, options) unless options[:hiera_config].nil?
57+
5858
@fact_file = install_fact_file(logger, options) if @facts_terminus == 'yaml'
5959
@enc = install_enc(logger) unless options[:enc].nil? && options[:pe_enc_url].nil?
6060
install_ssl(logger, options) if options[:puppetdb_ssl_ca] || options[:puppetdb_ssl_client_cert]
@@ -179,10 +179,10 @@ def install_enc(logger)
179179
end
180180

181181
# Install hiera config file
182-
# @param hiera_config [String] Path to file, relative to checkout
183-
# @param hiera_path_strip [String] Prefix to strip off when munging file
184-
def install_hiera_config(logger, hiera_config, hiera_path_strip)
182+
# @param options [Hash] Options hash
183+
def install_hiera_config(logger, options)
185184
# Validate hiera config file
185+
hiera_config = options[:hiera_config]
186186
unless hiera_config.is_a?(String)
187187
raise ArgumentError, "Called install_hiera_config with a #{hiera_config.class} argument"
188188
end
@@ -199,13 +199,23 @@ def install_hiera_config(logger, hiera_config, hiera_path_strip)
199199
obj = YAML.load_file(file_src)
200200
%w(yaml json).each do |key|
201201
next unless obj.key?(key.to_sym)
202-
next if obj[key.to_sym][:datadir].nil?
203-
unless hiera_path_strip.nil?
204-
rexp1 = Regexp.new('^' + hiera_path_strip)
202+
if options[:hiera_path_strip].is_a?(String)
203+
next if obj[key.to_sym][:datadir].nil?
204+
rexp1 = Regexp.new('^' + options[:hiera_path_strip])
205205
obj[key.to_sym][:datadir].sub!(rexp1, @tempdir)
206+
elsif options[:hiera_path]
207+
obj[key.to_sym][:datadir] = File.join(@tempdir, 'environments', 'production', options[:hiera_path])
206208
end
207209
rexp2 = Regexp.new('%{(::)?environment}')
208210
obj[key.to_sym][:datadir].sub!(rexp2, 'production')
211+
212+
# Make sure the dirctory exists. If not, log a warning. This is *probably* a setup error, but we don't
213+
# want it to be fatal in case (for example) someone is doing an octocatalog-diff to verify moving this
214+
# directory around or even setting up Hiera for the very first time.
215+
unless File.directory?(obj[key.to_sym][:datadir])
216+
message = "WARNING: Hiera datadir for #{key} doesn't seem to exist at #{obj[key.to_sym][:datadir]}"
217+
logger.warn message
218+
end
209219
end
210220

211221
# Write properly formatted hiera config file into temporary directory

0 commit comments

Comments
 (0)