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

Initial refactoring of yumrepo. #2086

Merged
merged 7 commits into from Feb 14, 2014
@@ -0,0 +1,152 @@
require 'puppet/util/inifile'
Puppet::Type.type(:yumrepo).provide(:inifile) do

This comment has been minimized.

@apenney

apenney Jan 22, 2014

Contributor

Renamed to inifile from ruby here.

@apenney

apenney Jan 22, 2014

Contributor

Renamed to inifile from ruby here.

desc 'Manage yum repos'
PROPERTIES = Puppet::Type.type(:yumrepo).validproperties
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

This comment has been minimized.

@apenney

apenney Jan 22, 2014

Contributor

Added the stripping here to handle 'enabled = 1' cases.

@apenney

apenney Jan 22, 2014

Contributor

Added the stripping here to handle 'enabled = 1' cases.

# like enabled = 1 with spaces.
attributes_hash[key] = value.strip
end
end
instances << new(attributes_hash)
end
return instances
end
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
# Search for a reposdir in the yum configuration file and append it to the

This comment has been minimized.

@apenney

apenney Jan 28, 2014

Contributor

I rewrote reposdir and added find_conf_value for now to make this a little cleaner. Are you ok with this @adrienthebo ?

@apenney

apenney Jan 28, 2014

Contributor

I rewrote reposdir and added find_conf_value for now to make this a little cleaner. Are you ok with this @adrienthebo ?

This comment has been minimized.

@adrienthebo

adrienthebo Jan 30, 2014

Contributor

+1, looks good

@adrienthebo

adrienthebo Jan 30, 2014

Contributor

+1, looks good

# list of repodirs to use.
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
return dirs
end
# Find configuration values in .conf files and return them
# if found.
def self.find_conf_value(value, conf='/etc/yum.conf')
if File.exists?(conf)

This comment has been minimized.

@kylog

kylog Feb 14, 2014

After seeing @peterhuene's fix up yesterday at a76d681, I took a look at this pull. There are some file api accesses via the native ruby api (e.g. here) and some via the Puppet::FileSystem abstraction (e.g. line 50). For consistency's sake, we should probably scrub this pull to use that abstraction consistently. @apenney @adrienthebo thoughts?

@kylog

kylog Feb 14, 2014

After seeing @peterhuene's fix up yesterday at a76d681, I took a look at this pull. There are some file api accesses via the native ruby api (e.g. here) and some via the Puppet::FileSystem abstraction (e.g. line 50). For consistency's sake, we should probably scrub this pull to use that abstraction consistently. @apenney @adrienthebo thoughts?

contents = File.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.
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 ::File.file?(file)
end
end
end
return @virtual
end
def self.valid_property?(key)
PROPERTIES.include?(key)
end
# Return the named section out of the virtual_inifile.
def self.section(name)
result = self.virtual_inifile[name]
# Create a new section if not found.
unless result
reposdir.each do |dir|
if File.directory?(dir)
path = ::File.join(dir, "#{name}.repo")
Puppet.info("create new repo #{name} in file #{path}")
result = self.virtual_inifile.add_section(name, path)
end
end
end
result
end
# Store all modifications back to disk
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]
::File.chmod(target_mode, file)
end
end
end
def create
@property_hash[:ensure] = :present
# 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)
section(@resource[:name])[property.to_s] = value
@property_hash[property] = value
end
end
end
def destroy
# Flag file for deletion on flush.
section(@resource[:name]).destroy=(true)
@property_hash.clear
end
def flush
self.class.store
end
def section(name)
self.class.section(name)
end
# Create all of our setters.
mk_resource_methods
PROPERTIES.each do |property|
# Exclude ensure, as we don't need to create an ensure=
next if property == :ensure
# Builds the property= method.
define_method("#{property.to_s}=") do |value|
section(@property_hash[:name])[property.to_s] = value
@property_hash[property] = value
end
end
def exists?
@property_hash[:ensure] == :present
end
end
Oops, something went wrong.
ProTip! Use n and p to navigate between commits in a pull request.