Skip to content

Commit 3e7d159

Browse files
authored
Merge pull request #45 from github/kpaulisse-environment-hiera
Make hiera configuration work for alternate environments too
2 parents 15049e3 + 154ac3d commit 3e7d159

9 files changed

Lines changed: 125 additions & 8 deletions

File tree

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

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ module CatalogUtil
1313
# - Register a handler to remove the temporary directory upon exit
1414
# - Install needed configuration files within the directory (e.g. puppetdb.conf)
1515
# - Install the facts into the directory
16-
# - Install 'environments/production' which is a symlink to the checkout of the puppet code
16+
# - Install 'environments/(environment)' which is a symlink to the checkout of the puppet code
1717
class BuildDir
1818
# Allow the path to the temporary directory to be read
1919
attr_reader :tempdir, :enc, :fact_file
@@ -220,10 +220,10 @@ def install_hiera_config(logger, options)
220220
end
221221
file_src = if hiera_config.start_with? '/'
222222
hiera_config
223-
elsif hiera_config =~ %r{^environments/production/}
223+
elsif hiera_config =~ %r{^environments/#{Regexp.escape(environment)}/}
224224
File.join(@tempdir, hiera_config)
225225
else
226-
File.join(@tempdir, 'environments', 'production', hiera_config)
226+
File.join(@tempdir, 'environments', environment, hiera_config)
227227
end
228228
raise Errno::ENOENT, "hiera.yaml (#{file_src}) wasn't found" unless File.file?(file_src)
229229

@@ -236,10 +236,10 @@ def install_hiera_config(logger, options)
236236
rexp1 = Regexp.new('^' + options[:hiera_path_strip])
237237
obj[key.to_sym][:datadir].sub!(rexp1, @tempdir)
238238
elsif options[:hiera_path].is_a?(String)
239-
obj[key.to_sym][:datadir] = File.join(@tempdir, 'environments', 'production', options[:hiera_path])
239+
obj[key.to_sym][:datadir] = File.join(@tempdir, 'environments', environment, options[:hiera_path])
240240
end
241241
rexp2 = Regexp.new('%{(::)?environment}')
242-
obj[key.to_sym][:datadir].sub!(rexp2, 'production')
242+
obj[key.to_sym][:datadir].sub!(rexp2, environment)
243243

244244
# Make sure the dirctory exists. If not, log a warning. This is *probably* a setup error, but we don't
245245
# want it to be fatal in case (for example) someone is doing an octocatalog-diff to verify moving this
@@ -327,6 +327,10 @@ def install_ssl_client_password(logger, password)
327327
File.open(password_outfile, 'w') { |f| f.write(password) }
328328
logger.debug "Installed SSL client key password in #{password_outfile}"
329329
end
330+
331+
def environment
332+
@options[:preserve_environments] ? @options.fetch(:environment, 'production') : 'production'
333+
end
330334
end
331335
end
332336
end
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
---
2+
:backends:
3+
- yaml
4+
:yaml:
5+
:datadir: /var/lib/puppet/environments/%{::environment}/hieradata
6+
:hierarchy:
7+
- common
8+
:merge_behavior: deeper
9+
:logger: console
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
---
2+
bar::param: 'Value from one/hieradata/common.yaml'

spec/octocatalog-diff/fixtures/repos/preserve-environments/environments/one/modules/bar/manifests/init.pp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
class bar {
1+
class bar (
2+
$param = '',
3+
) {
24
include sitetest
35

46
file { '/tmp/bar':
@@ -9,4 +11,8 @@
911
file { '/tmp/bar-static.txt':
1012
source => 'puppet:///modules/bar/bar-static.txt',
1113
}
14+
15+
file { '/tmp/bar-param.txt':
16+
content => "one ${param}",
17+
}
1218
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
---
2+
:backends:
3+
- yaml
4+
:yaml:
5+
:datadir: /var/lib/puppet/environments/%{::environment}/hieradata
6+
:hierarchy:
7+
- common
8+
:merge_behavior: deeper
9+
:logger: console
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
---
2+
bar::param: 'Value from two/hieradata/common.yaml'

spec/octocatalog-diff/fixtures/repos/preserve-environments/environments/two/modules/bar/manifests/init.pp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
class bar {
1+
class bar (
2+
$param = '',
3+
) {
24
include sitetest
35

46
file { '/tmp/bar':
@@ -9,4 +11,8 @@
911
file { '/tmp/bar-static.txt':
1012
source => 'puppet:///modules/bar/bar-static.txt',
1113
}
14+
15+
file { '/tmp/bar-param.txt':
16+
content => "two ${param}",
17+
}
1218
}

spec/octocatalog-diff/integration/preserve_environments_spec.rb

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,14 +137,17 @@
137137
'--preserve-environments',
138138
'--from-environment', 'one',
139139
'--to-environment', 'two',
140-
'--create-symlinks', 'modules,site'
140+
'--create-symlinks', 'modules,site',
141+
'--hiera-config', 'hiera.yaml',
142+
'--hiera-path-strip', '/var/lib/puppet'
141143
]
142144
)
143145
end
144146

145147
it 'should exit without error' do
146148
expect(@result.exitcode).to eq(2), OctocatalogDiff::Integration.format_exception(@result)
147149
expect(@result.exception).to be_nil, OctocatalogDiff::Integration.format_exception(@result)
150+
expect(@result.diffs.any?).to eq(true), OctocatalogDiff::Integration.format_exception(@result)
148151
end
149152

150153
it 'should display proper diffs' do
@@ -178,6 +181,13 @@
178181
)
179182
).to eq(true)
180183
end
184+
185+
it 'should handle hieradata properly' do
186+
h = @result.diffs.select { |x| x[1] == "File\f/tmp/bar-param.txt\fparameters\fcontent" }
187+
expect(h.size).to eq(1), h.inspect
188+
expect(h.first[2]).to eq('one Value from one/hieradata/common.yaml')
189+
expect(h.first[3]).to eq('two Value from two/hieradata/common.yaml')
190+
end
181191
end
182192

183193
context 'to modules' do

spec/octocatalog-diff/tests/catalog-util/builddir_spec.rb

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -283,6 +283,7 @@
283283
puppetdb_server_url_timeout: 120
284284
}
285285
end
286+
286287
context 'with relative path' do
287288
it 'should install the hiera configuration file' do
288289
options = default_options.merge(hiera_config: 'config/hiera.yaml')
@@ -309,6 +310,74 @@
309310
end
310311
end
311312

313+
context 'with relevant path and an alternate environment' do
314+
let(:alternate_opts) do
315+
{
316+
basedir: OctocatalogDiff::Spec.fixture_path('repos/preserve-environments'),
317+
hiera_config: 'hiera.yaml',
318+
preserve_environments: true,
319+
environment: 'one'
320+
}
321+
end
322+
323+
it 'should install the hiera configuration file' do
324+
options = default_options.merge(alternate_opts)
325+
logger, _logger_str = OctocatalogDiff::Spec.setup_logger
326+
testobj = OctocatalogDiff::CatalogUtil::BuildDir.new(options, logger)
327+
hiera_yaml = File.join(testobj.tempdir, 'hiera.yaml')
328+
expect(File.file?(hiera_yaml)).to eq(true)
329+
hiera_cfg = YAML.load_file(hiera_yaml)
330+
expect(hiera_cfg[:backends]).to eq(['yaml'])
331+
expect(hiera_cfg[:yaml]).to eq(datadir: '/var/lib/puppet/environments/one/hieradata')
332+
end
333+
334+
it 'should handle hiera_path_strip with respect to the environment' do
335+
options = default_options.merge(alternate_opts.merge(hiera_path_strip: '/var/lib/puppet'))
336+
logger, logger_str = OctocatalogDiff::Spec.setup_logger
337+
testobj = OctocatalogDiff::CatalogUtil::BuildDir.new(options, logger)
338+
hiera_yaml = File.join(testobj.tempdir, 'hiera.yaml')
339+
expect(File.file?(hiera_yaml)).to eq(true)
340+
hiera_cfg = YAML.load_file(hiera_yaml)
341+
expect(hiera_cfg[:backends]).to eq(['yaml'])
342+
expect(hiera_cfg[:yaml]).to eq(datadir: File.join(testobj.tempdir, 'environments', 'one', 'hieradata'))
343+
expect(logger_str.string).not_to match(/Hiera datadir for yaml doesn't seem to exist/)
344+
end
345+
end
346+
347+
context 'with relevant path including an alternate environment' do
348+
let(:alternate_opts) do
349+
{
350+
basedir: OctocatalogDiff::Spec.fixture_path('repos/preserve-environments'),
351+
hiera_config: 'environments/one/hiera.yaml',
352+
preserve_environments: true,
353+
environment: 'one'
354+
}
355+
end
356+
357+
it 'should install the hiera configuration file' do
358+
options = default_options.merge(alternate_opts)
359+
logger, _logger_str = OctocatalogDiff::Spec.setup_logger
360+
testobj = OctocatalogDiff::CatalogUtil::BuildDir.new(options, logger)
361+
hiera_yaml = File.join(testobj.tempdir, 'hiera.yaml')
362+
expect(File.file?(hiera_yaml)).to eq(true)
363+
hiera_cfg = YAML.load_file(hiera_yaml)
364+
expect(hiera_cfg[:backends]).to eq(['yaml'])
365+
expect(hiera_cfg[:yaml]).to eq(datadir: '/var/lib/puppet/environments/one/hieradata')
366+
end
367+
368+
it 'should handle hiera_path_strip with respect to the environment' do
369+
options = default_options.merge(alternate_opts.merge(hiera_path_strip: '/var/lib/puppet'))
370+
logger, logger_str = OctocatalogDiff::Spec.setup_logger
371+
testobj = OctocatalogDiff::CatalogUtil::BuildDir.new(options, logger)
372+
hiera_yaml = File.join(testobj.tempdir, 'hiera.yaml')
373+
expect(File.file?(hiera_yaml)).to eq(true)
374+
hiera_cfg = YAML.load_file(hiera_yaml)
375+
expect(hiera_cfg[:backends]).to eq(['yaml'])
376+
expect(hiera_cfg[:yaml]).to eq(datadir: File.join(testobj.tempdir, 'environments', 'one', 'hieradata'))
377+
expect(logger_str.string).not_to match(/Hiera datadir for yaml doesn't seem to exist/)
378+
end
379+
end
380+
312381
context 'with absolute path' do
313382
it 'should install the hiera configuration file' do
314383
options = default_options.merge(hiera_config: OctocatalogDiff::Spec.fixture_path('repos/default/config/hiera.yaml'))

0 commit comments

Comments
 (0)