diff --git a/lib/puppet/provider/yumrepo/inifile.rb b/lib/puppet/provider/yumrepo/inifile.rb index b87fd2eaf..748096c5a 100644 --- a/lib/puppet/provider/yumrepo/inifile.rb +++ b/lib/puppet/provider/yumrepo/inifile.rb @@ -1,210 +1,227 @@ require 'puppet/util/inifile' Puppet::Type.type(:yumrepo).provide(:inifile) do desc 'Manage yum repos' PROPERTIES = Puppet::Type.type(:yumrepo).validproperties # @return [Array] Return all the providers built up from # discovered content on the local node. def self.instances instances = [] # Iterate over each section of our virtual file. virtual_inifile.each_section do |section| attributes_hash = {:name => section.name, :ensure => :present, :provider => :yumrepo} # We need to build up a attributes hash section.entries.each do |key, value| key = key.to_sym if valid_property?(key) # We strip the values here to handle cases where distros set values # like enabled = 1 with spaces. attributes_hash[key] = value + elsif key == :name + attributes_hash[:descr] = value end end instances << new(attributes_hash) end return instances end # @param resources [Array] Resources to prefetch. # @return [Array] Resources with providers set. def self.prefetch(resources) repos = instances resources.keys.each do |name| if provider = repos.find { |repo| repo.name == name } resources[name].provider = provider end end end # Return a list of existing directories that could contain repo files. Fail if none found. # @param conf [String] Configuration file to look for directories in. # @param dirs [Array] Default locations for yum repos. # @return [Array] Directories that were found to exist on the node. def self.reposdir(conf='/etc/yum.conf', dirs=['/etc/yum.repos.d', '/etc/yum/repos.d']) reposdir = find_conf_value('reposdir', conf) dirs << reposdir if reposdir # We can't use the below due to Ruby 1.8.7 # dirs.select! { |dir| Puppet::FileSystem.exist?(dir) } dirs.delete_if { |dir| ! Puppet::FileSystem.exist?(dir) } if dirs.empty? fail('No yum directories were found on the local filesystem') else return dirs end end # Helper method to look up specific values in ini style files. # @todo Migrate this into Puppet::Util::IniConfig. # @param value [String] Value to look for in the configuration file. # @param conf [String] Configuration file to check for value. # @return [String] The value of a looked up key from the configuration file. def self.find_conf_value(value, conf='/etc/yum.conf') if Puppet::FileSystem.exist?(conf) contents = Puppet::FileSystem.read(conf) match = /^#{value}\s*=\s*(.*)/.match(contents) end return match.captures[0] if match end # Build a virtual inifile by reading in numerous .repo # files into a single virtual file to ease manipulation. # @return [Puppet::Util::IniConfig::File] The virtual inifile representing # multiple real files. def self.virtual_inifile unless @virtual @virtual = Puppet::Util::IniConfig::File.new reposdir.each do |dir| Dir.glob("#{dir}/*.repo").each do |file| @virtual.read(file) if Puppet::FileSystem.file?(file) end end end return @virtual end # @param key [String] The property to look up. # @return [Boolean] Returns true if the property is defined in the type. def self.valid_property?(key) PROPERTIES.include?(key) end # We need to return a valid section from the larger virtual inifile here, # which we do by first looking it up and then creating a new section for # the appropriate name if none was found. # @param name [String] Section name to lookup in the virtual inifile. # @return [Puppet::Util::IniConfig] The IniConfig section def self.section(name) result = self.virtual_inifile[name] # Create a new section if not found. unless result # Previously we did an .each on reposdir with the effect that we # constantly created and overwrote result until the last entry of # the array. This was done because the ordering is # [defaults, custom] for reposdir and we want to use the custom if # we have it and the defaults if not. path = ::File.join(reposdir.last, "#{name}.repo") Puppet.info("create new repo #{name} in file #{path}") result = self.virtual_inifile.add_section(name, path) end result end # Here we store all modifications to disk, forcing the output file to 0644 if it differs. # @return [void] def self.store inifile = self.virtual_inifile inifile.store target_mode = 0644 inifile.each_file do |file| current_mode = Puppet::FileSystem.stat(file).mode & 0777 unless current_mode == target_mode Puppet.info "changing mode of #{file} from %03o to %03o" % [current_mode, target_mode] Puppet::FileSystem.chmod(target_mode, file) end end end # @return [void] def create @property_hash[:ensure] = :present new_section = current_section # We fetch a list of properties from the type, then iterate # over them, avoiding ensure. We're relying on .should to # check if the property has been set and should be modified, # and if so we set it in the virtual inifile. PROPERTIES.each do |property| next if property == :ensure if value = @resource.should(property) self.send("#{property}=", value) end end end # @return [Boolean] Returns true if ensure => present. def exists? @property_hash[:ensure] == :present end # We don't actually destroy the file here, merely mark it for # destruction in the section. # @return [void] def destroy # Flag file for deletion on flush. current_section.destroy=(true) @property_hash.clear end # @return [void] def flush self.class.store end # Generate setters and getters for our INI properties. PROPERTIES.each do |property| # The ensure property uses #create, #exists, and #destroy we can't generate # meaningful setters and getters for this next if property == :ensure define_method(property) do get_property(property) end define_method("#{property}=") do |value| set_property(property, value) end end + # Map the yumrepo 'descr' type property to the 'name' INI property. + def descr + if ! @property_hash.has_key?(:descr) + @property_hash[:descr] = current_section['name'] + end + value = @property_hash[:descr] + value.nil? ? :absent : value + end + + def descr=(value) + value = (value == :absent ? nil : value) + current_section['name'] = value + @property_hash[:descr] = value + end + private def get_property(property) if ! @property_hash.has_key?(property) @property_hash[property] = current_section[property.to_s] end value = @property_hash[property] value.nil? ? :absent : value end def set_property(property, value) value = (value == :absent ? nil : value) current_section[property.to_s] = value @property_hash[property] = value end # @return [void] def section(name) self.class.section(name) end def current_section self.class.section(self.name) end end diff --git a/spec/unit/provider/yumrepo/inifile_spec.rb b/spec/unit/provider/yumrepo/inifile_spec.rb index 4fa4a0d5d..ac755ebb5 100644 --- a/spec/unit/provider/yumrepo/inifile_spec.rb +++ b/spec/unit/provider/yumrepo/inifile_spec.rb @@ -1,154 +1,153 @@ require 'spec_helper' describe Puppet::Type.type(:yumrepo).provider(:inifile) do let(:virtual_inifile) { stub('virtual inifile') } before :each do described_class.stubs(:virtual_inifile).returns(virtual_inifile) end describe 'self.instances' do let(:updates_section) do sect = Puppet::Util::IniConfig::Section.new('updates', '/some/imaginary/file') sect.entries << ['name', 'Some long description of the repo'] sect.entries << ['enabled', '1'] sect end it 'finds any existing sections' do virtual_inifile.expects(:each_section).yields(updates_section) virtual_inifile.stubs(:[]).with('updates').returns(updates_section) providers = described_class.instances providers.should have(1).items providers[0].name.should == 'updates' - #providers[0].descr.should == 'Some long description of the repo' + providers[0].descr.should == 'Some long description of the repo' providers[0].enabled.should == '1' end end describe "setting and getting properties" do let(:type_instance) do Puppet::Type.type(:yumrepo).new( :name => 'puppetlabs-products', :ensure => :present, :baseurl => 'http://yum.puppetlabs.com/el/6/products/$basearch', :descr => 'Puppet Labs Products El 6 - $basearch', :enabled => '1', :gpgcheck => '1', :gpgkey => 'file:///etc/pki/rpm-gpg/RPM-GPG-KEY-puppetlabs' ) end let(:provider) do described_class.new(type_instance) end let(:section) do stub('inifile puppetlabs section', :name => 'puppetlabs-products') end before do type_instance.provider = provider described_class.stubs(:section).with('puppetlabs-products').returns(section) end describe "methods used by ensurable" do it "#create sets the yumrepo properties on the according section" do section.expects(:[]=).with('baseurl', 'http://yum.puppetlabs.com/el/6/products/$basearch') - #section.expects(:[]=).with('name', 'Puppet Labs Products El 6 - $basearch') - section.stubs(:[]=).with('descr', 'Puppet Labs Products El 6 - $basearch') + section.expects(:[]=).with('name', 'Puppet Labs Products El 6 - $basearch') section.expects(:[]=).with('enabled', '1') section.expects(:[]=).with('gpgcheck', '1') section.expects(:[]=).with('gpgkey', 'file:///etc/pki/rpm-gpg/RPM-GPG-KEY-puppetlabs') provider.create end it "#exists? checks if the repo has been marked as present" do described_class.stubs(:section).returns(stub(:[]= => nil)) provider.create expect(provider).to be_exist end it "#destroy deletes the associated ini file section" do described_class.expects(:section).returns(section) section.expects(:destroy=).with(true) provider.destroy end end describe "getting properties" do - it "maps the 'descr' property to the 'name' INI property", :pending => "proper mapping of descr to name" do + it "maps the 'descr' property to the 'name' INI property" do section.expects(:[]).with('name').returns 'Some rather long description of the repository' expect(provider.descr).to eq 'Some rather long description of the repository' end it "gets the property from the INI section" do section.expects(:[]).with('enabled').returns '1' expect(provider.enabled).to eq '1' end it "sets the property as :absent if the INI property is nil" do section.expects(:[]).with('exclude').returns nil expect(provider.exclude).to eq :absent end end describe "setting properties" do - it "maps the 'descr' property to the 'name' INI property", :pending => "proper mapping of descr to name" do + it "maps the 'descr' property to the 'name' INI property" do section.expects(:[]=).with('name', 'Some rather long description of the repository') provider.descr = 'Some rather long description of the repository' end it "sets the property on the INI section" do section.expects(:[]=).with('enabled', '0') provider.enabled = '0' end it "sets the section field to nil when the specified value is absent" do section.expects(:[]=).with('exclude', nil) provider.exclude = :absent end end end describe 'reposdir' do let(:defaults) { ['/etc/yum.repos.d', '/etc/yum/repos.d'] } before do Puppet::FileSystem.stubs(:exist?).with('/etc/yum.repos.d').returns(true) Puppet::FileSystem.stubs(:exist?).with('/etc/yum/repos.d').returns(true) end it "returns the default directories if yum.conf doesn't contain a `reposdir` entry" do described_class.stubs(:find_conf_value).with('reposdir', '/etc/yum.conf') described_class.reposdir('/etc/yum.conf').should == defaults end it "includes the directory specified by the yum.conf 'reposdir' entry when the directory is present" do Puppet::FileSystem.expects(:exist?).with("/etc/yum/extra.repos.d").returns(true) described_class.expects(:find_conf_value).with('reposdir', '/etc/yum.conf').returns "/etc/yum/extra.repos.d" described_class.reposdir('/etc/yum.conf').should include("/etc/yum/extra.repos.d") end it "doesn't the directory specified by the yum.conf 'reposdir' entry when the directory is absent" do Puppet::FileSystem.expects(:exist?).with("/etc/yum/extra.repos.d").returns(false) described_class.expects(:find_conf_value).with('reposdir', '/etc/yum.conf').returns "/etc/yum/extra.repos.d" described_class.reposdir('/etc/yum.conf').should_not include("/etc/yum/extra.repos.d") end it "raises an entry if none of the specified repo directories exist" do Puppet::FileSystem.unstub(:exist?) Puppet::FileSystem.stubs(:exist?).returns false described_class.stubs(:find_conf_value).with('reposdir', '/etc/yum.conf') expect { described_class.reposdir('/etc/yum.conf') }.to raise_error('No yum directories were found on the local filesystem') end end end