Add Puppet provider for managing Hiera sources

This change introduces a new Puppet provider that can add and remove
sources in the Hiera configuration. It replaces the previous mechanism
that used the file_line resource because it was too fragile and
couldn't cope with differences in YAML formatting.

Eventually this will be natively supported by the Fuel plugin framework
but it's not there yet.

Change-Id: Ie7563a9edf540bb81bdc30bc0200b7a25e25962f
Closes-Bug: #1527253
This commit is contained in:
Simon Pasquier 2015-12-22 09:31:57 +01:00 committed by Swann Croiset
parent c7e7697365
commit ef2d5d8bb4
3 changed files with 88 additions and 4 deletions

View File

@ -0,0 +1,61 @@
# Copyright 2015 Mirantis, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
require 'yaml'
HIERA_CONFIG = '/etc/hiera.yaml'
Puppet::Type.type(:hiera_custom_source).provide(:ruby) do
desc "Support for Hiera source configuration"
defaultfor :kernel => 'Linux'
def sources
if @hiera.has_key?(:hierarchy)
return @hiera[:hierarchy]
elsif @hiera.has_key?('hierarchy')
return @hiera['hierarchy']
else
raise Puppet::Error, "No 'hierarchy' key in the Hiera configuration"
end
end
# Load the Hiera configuration
def load_hiera
@hiera = YAML.load_file(HIERA_CONFIG)
end
# Save the current Hiera configuration
def save_hiera
File.open(HIERA_CONFIG, 'w') do |file|
file.puts @hiera.to_yaml
end
end
def create
self.load_hiera
self.sources.insert(0, resource[:name])
self.save_hiera
end
def destroy
self.load_hiera
self.sources.select!{|x| x != resource[:name] }
self.save_hiera
end
def exists?
self.load_hiera
return self.sources.any?{|x| x == resource[:name] }
end
end

View File

@ -0,0 +1,25 @@
# Copyright 2015 Mirantis, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
Puppet::Type.newtype(:hiera_custom_source) do
desc 'Manage Hiera sources'
ensurable
newparam(:name) do
desc 'The path to the Hiera source'
isnamevar
end
end

View File

@ -46,9 +46,7 @@ define lma_collector::hiera_data (
require => File[$hiera_directory],
}
file_line { "${name}_hiera_override":
path => '/etc/hiera.yaml',
line => " - override/${name}",
after => ' - override/module/%{calling_module}',
hiera_custom_source { "override/${name}":
ensure => present
}
}