Skip to content

Instantly share code, notes, and snippets.

@mcallaway
Created March 3, 2015 18:11
Show Gist options
  • Save mcallaway/72db68a130fe49b02418 to your computer and use it in GitHub Desktop.
Save mcallaway/72db68a130fe49b02418 to your computer and use it in GitHub Desktop.
packagelock provider
Puppet::Type.type(:packagelock).provide(:yum) do
desc "YUM package locking support; uses the versionlock plugin.
This plugin should be installed and enabled for this to work.
RHEL5 and derivatives have a very sparse version of the plugin
without the 'versionlock' subcommand, requiring us to edit
the versionlock.list file directly."
LOCKCONF = '/etc/yum/pluginconf.d/versionlock.conf'
LOCKLIST = '/etc/yum/pluginconf.d/versionlock.list'
has_feature :versionable
commands :rpm => 'rpm'
commands :yum => 'yum'
confine :osfamily => :redhat
confine :exists => LOCKCONF
defaultfor :osfamily => :redhat
# Obtain the list of current instances of this type.
# Returns a list of objects like:
# { :name => pkg, :ensure => :present }
# This creates @property_hash for later use.
def self.instances
if File.file?(LOCKLIST)
locks = File.read(LOCKLIST)
else
locks = ''
end
# Skips comments and black lines
locklist = locks.split("\n").select { |l| l =~ /^[0-9]+:.*\.\*$/ }
instances = []
locklist.collect do |lock|
lock.gsub!(/\.\*$/,'')
(epoch,therest) = lock.split(/:/)
(therest,toss,release) = therest.rpartition(/-/)
(pkg,toss,version) = therest.rpartition(/-/)
instances << new(:name => pkg, :ensure => "#{version}-#{release}", :provider => name, :version => version, :release => release)
end
instances
end
def self.prefetch(resources)
locks = instances
resources.keys.each do |name|
if provider = locks.find { |lock| lock.name == name }
resources[name].provider = provider
end
end
end
def exists?
@property_hash[:ensure]
end
def create
version = @property_hash[:ensure]
case version
when true, false, Symbol
# pass
else
lockval = "#{resource[:name]}-#{resource[:ensure]}"
end
if (Facter[:operatingsystemrelease].value =~ /^5.*/)
lock = rpm('-q', '--qf', '%|EPOCH?{%{EPOCH}}:{0}|:%{NAME}-%{VERSION}-%{RELEASE}.*\n', resource[:name])
File.open(LOCKLIST, 'a') { |list| list.puts lock }
else
yum('versionlock', '-q', 'add', lockval)
end
end
def destroy
version = rpm('-q', '--qf', '%{VERSION}-%{RELEASE}', resource[:name])
if (Facter[:operatingsystemrelease].value =~ /^5.*/)
lines = File.readlines(LOCKLIST)
File.open(LOCKLIST, 'w') do |file|
lines.each { |line| file.puts(line) unless line =~ /^[0-9]+:#{resource[:name]}-#{version}\.\*$/ }
end
else
yum('versionlock', '-q', 'delete', "*:#{resource[:name]}-#{version}.*")
end
@property_hash.clear
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment