Skip to content

Commit

Permalink
Merge pull request #90 from h0tw1r3/fix-89
Browse files Browse the repository at this point in the history
FIx and improve hiera.yaml selection
  • Loading branch information
zilchms authored Feb 9, 2024
2 parents b4b8109 + c06ab71 commit 6ba47b2
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 3 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ Environment variables

### HIERA_YAML_PATH

The location of the `hiera.yaml` file. Defaults to `/etc/puppetlabs/code/hiera.yaml`
The location of the `hiera.yaml` file. Defaults to `./hiera.yaml` or `/etc/puppetlabs/puppet/hiera.yaml` whichever is found first.

### PUPPETDB_URL

Expand Down
13 changes: 11 additions & 2 deletions lib/puppet-lint/plugins/check_ghostbuster_hiera_files.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,23 @@ def load_data(path, content)

PuppetLint.new_check(:ghostbuster_hiera_files) do
def hiera
@hiera ||= YAML.load_file(ENV['HIERA_YAML_PATH'] || '/etc/puppetlabs/code/hiera.yaml')
@hiera ||= if (hiera_file = ENV.fetch('HIERA_YAML_PATH', false))
YAML.load_file(hiera_file)
else
hiera = ['hiera.yaml', '/etc/puppetlabs/puppet/hiera.yaml'].filter_map do |hf|
YAML.load_file(hf) if hf && File.exist?(hf)
end
hiera[0]
end
end

def default_datadir
hiera.dig('defaults', 'datadir') || 'hieradata'
hiera.dig('defaults', 'datadir') || 'data'
end

def path_in_datadirs?(path)
return false unless hiera

@data_dirs ||= hiera['hierarchy'].map { |h| (h['datadir'] || default_datadir).chomp('/') }.uniq
path.match?(%(./#{Regexp.union(@data_dirs)}/))
end
Expand Down
19 changes: 19 additions & 0 deletions spec/puppet-lint/plugins/ghostbuster_hiera_files_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -99,5 +99,24 @@
expect(problems).to contain_warning("Hiera File #{path} seems unused")
end
end

context 'when no hiera.yaml exists' do
let(:path) { './data/nodes/foo.example.com.yaml' }

it {
allow(File).to receive(:exist?).and_return(false)
ENV.delete('HIERA_YAML_PATH')
expect(problems.size).to eq(0)
}
end

context 'when HIERA_YAML_PATH is set but does not exist' do
let(:path) { './data/domain/example.com.yaml' }

it {
ENV['HIERA_YAML_PATH'] = './spec/fixtures/j.yaml'
expect { problems }.to raise_error(Errno::ENOENT)
}
end
end
end

0 comments on commit 6ba47b2

Please sign in to comment.