diff --git a/.fixtures.yml b/.fixtures.yml index 452ab398..1c0748b2 100644 --- a/.fixtures.yml +++ b/.fixtures.yml @@ -4,5 +4,7 @@ fixtures: 'apt': repo: 'git://github.com/puppetlabs/puppetlabs-apt.git' ref: '1.4.0' + 'inifile': 'git://github.com/puppetlabs/puppetlabs-inifile' + symlinks: 'ceph': "#{source_dir}" diff --git a/.gitignore b/.gitignore index d4a93a06..3bd683ca 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,7 @@ *.swp +*~ +.project +metadata.json spec/fixtures/modules/* pkg Gemfile.lock diff --git a/Modulefile b/Modulefile index 3aaacabe..66046c08 100644 --- a/Modulefile +++ b/Modulefile @@ -7,4 +7,5 @@ summary 'Community Developed Ceph Module' description 'Puppet module to install and configure Ceph' project_page 'https://launchpad.net/puppet-ceph' +dependency 'puppetlabs/inifile', '>=1.0.0 <2.0.0' dependency 'puppetlabs/stdlib', '>= 2.5.0' diff --git a/lib/puppet/provider/ceph_config/ini_setting.rb b/lib/puppet/provider/ceph_config/ini_setting.rb new file mode 100644 index 00000000..71abca94 --- /dev/null +++ b/lib/puppet/provider/ceph_config/ini_setting.rb @@ -0,0 +1,50 @@ +# Copyright (C) Dan Bode +# +# 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. +# +# Author: Dan Bode +# Author: Andrew Woodward + +Puppet::Type.type(:ceph_config).provide( + :ini_setting, + :parent => Puppet::Type.type(:ini_setting).provider(:ruby) +) do + + def section + resource[:name].split('/', 2).first + end + + def setting + resource[:name].split('/', 2).last + end + + def separator + ' = ' + end + + def self.file_path + '/etc/ceph/ceph.conf' + end + + # required to be able to hack the path in unit tests + # also required if a user wants to otherwise overwrite the default file_path + # Note: purge will not work on over-ridden file_path + def file_path + if not resource[:path] + self.class.file_path + else + resource[:path] + end + end + +end diff --git a/lib/puppet/type/ceph_config.rb b/lib/puppet/type/ceph_config.rb new file mode 100644 index 00000000..5a44472d --- /dev/null +++ b/lib/puppet/type/ceph_config.rb @@ -0,0 +1,49 @@ +# Copyright (C) Dan Bode +# +# 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. +# +# Author: Dan Bode +# Author: Mathieu Gagne + + +Puppet::Type.newtype(:ceph_config) do + + ensurable + + newparam(:name, :namevar => true) do + desc 'Section/setting name to manage from ./ceph.conf' + newvalues(/\S+\/\S+/) + end + + # required in order to be able to unit test file contents + # Note: purge will not work on over-ridden file_path + # lifted from ini_file + newparam(:path) do + desc 'A file path to over ride the default file path if necessary' + validate do |value| + unless (Puppet.features.posix? and value =~ /^\//) or (Puppet.features.microsoft_windows? and (value =~ /^.:\// or value =~ /^\/\/[^\/]+\/[^\/]+/)) + raise(Puppet::Error, "File paths must be fully qualified, not '#{value}'") + end + end + defaultto false + end + + newproperty(:value) do + desc 'The value of the setting to be defined.' + munge do |value| + value = value.to_s.strip + value.capitalize! if value =~ /^(true|false)$/i + value + end + end +end diff --git a/spec/unit/provider/ceph_config/ini_setting_spec.rb b/spec/unit/provider/ceph_config/ini_setting_spec.rb new file mode 100644 index 00000000..af248070 --- /dev/null +++ b/spec/unit/provider/ceph_config/ini_setting_spec.rb @@ -0,0 +1,70 @@ +# Copyright (C) 2013 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. +# +# Author: Andrew Woodward + +# This is aparently one of the few ways to do this load +# see https://github.com/stackforge/puppet-nova/blob/master/spec/unit/provider/nova_config/ini_setting_spec.rb +$LOAD_PATH.push( + File.join( + File.dirname(__FILE__), + '..', + '..', + '..', + 'fixtures', + 'modules', + 'inifile', + 'lib') +) + +require 'spec_helper' +require 'puppet' + +provider_class = Puppet::Type.type(:ceph_config).provider(:ini_setting) + +describe provider_class do + include PuppetlabsSpec::Files + + let(:tmpfile) { tmpfilename("ceph_config_test") } + + let(:params) { { + :path => tmpfile, + } } + + def validate(expected, tmpfile = tmpfile) + File.read(tmpfile).should == expected + end + + it 'should create keys = value and ensure space around equals' do + resource = Puppet::Type::Ceph_config.new(params.merge( + :name => 'global/ceph_is_foo', :value => 'bar')) + provider = provider_class.new(resource) + provider.exists?.should be_nil + provider.create + validate(<<-EOS + +[global] +ceph_is_foo = bar + EOS + ) + end + + it 'should default to file_path if param path is not passed' do + resource = Puppet::Type::Ceph_config.new( + :name => 'global/ceph_is_foo', :value => 'bar') + provider = provider_class.new(resource) + provider.file_path.should == '/etc/ceph/ceph.conf' + end + +end \ No newline at end of file diff --git a/spec/unit/type/ceph_config_spec.rb b/spec/unit/type/ceph_config_spec.rb new file mode 100644 index 00000000..01e4af85 --- /dev/null +++ b/spec/unit/type/ceph_config_spec.rb @@ -0,0 +1,41 @@ +# Copyright (C) 2013 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. +# +# Author: Andrew Woodward + +require 'puppet' +require 'puppet/type/ceph_config' + +describe 'Puppet::Type.type(:ceph_config)' do + + before :each do + @ceph_config = Puppet::Type.type(:ceph_config).new( + :name => 'global/ceph_is_foo', :value => 'bar') + end + + it 'should work bascily' do + @ceph_config[:value] = 'max' + @ceph_config[:value].should == 'max' + end + + it 'should convert true to True' do + @ceph_config[:value] = 'tRuE' + @ceph_config[:value].should == 'True' + end + + it 'should convert false to False' do + @ceph_config[:value] = 'fAlSe' + @ceph_config[:value].should == 'False' + end +end \ No newline at end of file