Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix and improve hiera.yaml selection #90

Merged
merged 4 commits into from
Feb 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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