Permalink
Browse files

(feat) Add feature upgradeable

This commit adds the ability to specify ensure => latest to a package
 resource, instructing pkgng to upgrade to the latest available version
 of the package.
  • Loading branch information...
1 parent a935e82 commit b6daed9ffcffbcad621983b51903171c06e34c32 @xaque208 committed Nov 11, 2013
Showing with 131 additions and 27 deletions.
  1. +1 −0 .gitignore
  2. +43 −8 lib/puppet/provider/package/pkgng.rb
  3. +12 −14 spec/fixtures/pkg.info
  4. +2 −0 spec/fixtures/pkg.version
  5. +73 −5 spec/unit/puppet/provider/pkgng_spec.rb
View
@@ -1,2 +1,3 @@
pkg
Gemfile.lock
+.bundle
@@ -9,9 +9,24 @@
defaultfor :operatingsystem => :freebsd if $pkgng_enabled
has_feature :versionable
+ has_feature :upgradeable
def self.get_info
- pkg(['info','-ao'])
+ @pkg_info = @pkg_info || pkg(['info','-ao'])
+ @pkg_info
+ end
+
+ def self.get_version_list
+ @version_list = @version_list || pkg(['version', '-voRL='])
+ @version_list
+ end
+
+ def self.get_latest_version(origin)
+ if latest_version = self.get_version_list.lines.find { |l| l =~ /^#{origin}/ }
+ latest_version = latest_version.split(' ').last.split(')').first
+ return latest_version
+ end
+ nil
end
def self.instances
@@ -24,16 +39,24 @@ def self.instances
end
info.lines.each do |line|
+ unless line =~ /\w+-\d.*\s*\w\/\w.*/
+ debug "skipping line: #{line}"
+ next
+ end
+
package, origin = line.split
- pkg_info = package.split('-')
- version = pkg_info.pop
- name = pkg_info.join('-')
+ pkg_info = package.split('-')
+ version = pkg_info.pop
+ name = pkg_info.join('-')
+ latest_version = get_latest_version(origin) || version
+
pkg = {
- :ensure => :present,
+ :ensure => version,
:name => name,
:provider => self.name,
+ :origin => origin,
:version => version,
- :origin => origin
+ :latest => latest_version
}
packages << new(pkg)
end
@@ -76,7 +99,7 @@ def query
end
def version
- debug @property_hash[:version]
+ debug @property_hash[:version].inspect
@property_hash[:version]
end
@@ -85,8 +108,20 @@ def version=
end
def origin
- debug @property_hash[:origin]
+ debug @property_hash[:origin].inspect
@property_hash[:origin]
end
+ # Upgrade to the latest version
+ def update
+ debug 'pkgng: update called'
+ install
+ end
+
+ # Returnthe latest version of the package
+ def latest
+ debug "returning the latest #{@property_hash[:name].inspect} version #{@property_hash[:latest].inspect}"
+ @property_hash[:latest]
+ end
+
end
View
@@ -1,14 +1,12 @@
-GeoIP-1.4.8_3 Find the country that any IP address or hostname originates from
-ca_root_nss-3.14.3 The root certificate bundle from the Mozilla Project
-curl-7.24.0_2 Non-interactive tool to get files from FTP, GOPHER, HTTP(S) servers
-nginx-1.4.1,1 Robust and small WWW server
-nmap-6.25_1 Port scanning utility for large networks
-openldap-sasl-client-2.4.35 Open source LDAP client implementation with SASL2 support
-pkg-1.0.12 New generation package manager
-postfix-2.10.0,1 Secure alternative to widely-used Sendmail
-ruby-1.9.3.392,1 An object-oriented interpreted scripting language
-sudo-1.8.6.p8 Allow others to run commands as root
-tmux-1.8 A Terminal Multiplexer
-vim-lite-7.3.669_2 Vi "workalike", with many additional features (Lite package)
-zfs-stats-1.2.2 Display human-readable ZFS statistics
-zsh-5.0.2 The Z shell
+====== BEGIN pkg.conf ======
+"PACKAGESITE": "http://pkgbeta.freebsd.org/freebsd:9:amd64/latest/"
+
+====== END pkg.conf ======
+pkg: PACKAGESITE in pkg.conf is deprecated. Please create a repository configuration file
+ca_root_nss-3.15.3.1 security/ca_root_nss
+curl-7.33.0 ftp/curl
+gnupg-2.0.22 security/gnupg
+mcollective-2.2.4 sysutils/mcollective
+nmap-6.40 security/nmap
+pkg-1.2.4_1 ports-mgmt/pkg
+zsh-5.0.2_1 shells/zsh
@@ -0,0 +1,2 @@
+ftp/curl < needs updating (index has 7.33.0_2)
+shells/zsh < needs updating (index has 5.0.4)
@@ -9,28 +9,66 @@
let(:pkgng) { 'pkgng' }
let(:resource) do
+ # When bash is not present
Puppet::Type.type(:package).new(:name => name, :provider => pkgng)
end
+ let(:installed_resource) do
+ # When zsh is present
+ Puppet::Type.type(:package).new(:name => 'zsh', :provider => pkgng)
+ end
+
+ let(:latest_resource) do
+ # When curl is installed but not the latest
+ Puppet::Type.type(:package).new(:name => 'ftp/curl', :provider => pkgng, :ensure => latest)
+ end
+
let (:provider) { resource.provider }
+ def run_in_catalog(*resources)
+ catalog = Puppet::Resource::Catalog.new
+ catalog.host_config = false
+ resources.each do |resource|
+ #resource.expects(:err).never
+ catalog.add_resource(resource)
+ end
+ catalog.apply
+ end
+
before do
provider_class.stub(:command).with(:pkg) {'/usr/local/sbin/pkg'}
provider.stub(:command).with(:pkg) {'/usr/local/sbin/pkg'}
+
+ info = File.read('spec/fixtures/pkg.info')
+ provider_class.stub(:get_info) { info }
+
+ version_list = File.read('spec/fixtures/pkg.version')
+ provider_class.stub(:get_version_list) { version_list }
end
context "::instances" do
it "should return the empty set if no packages are listed" do
provider_class.stub(:get_info) { '' }
+ provider_class.stub(:get_version_list) { '' }
provider_class.instances.should be_empty
end
it "should return all packages when invoked" do
- fixture = File.read('spec/fixtures/pkg.info')
- provider_class.stub(:get_info) { fixture }
provider_class.instances.map(&:name).sort.should ==
- %w{GeoIP ca_root_nss curl nginx nmap openldap-sasl-client
- pkg postfix ruby sudo tmux vim-lite zfs-stats zsh}.sort
+ %w{ca_root_nss curl nmap pkg gnupg mcollective zsh}.sort
+ end
+
+ it "should set latest to current version when no upgrade available" do
+ nmap = provider_class.instances.find {|i| i.properties[:origin] == 'security/nmap' }
+
+ nmap.properties[:version].should == nmap.properties[:latest]
+ end
+
+ describe "version" do
+ it "should retrieve the correct version of the current package" do
+ zsh = provider_class.instances.find {|i| i.properties[:origin] == 'shells/zsh' }
+ zsh.properties[:version].should == '5.0.2_1'
+ end
end
end
@@ -60,5 +98,35 @@
end
end
-end
+ describe "latest" do
+ it "should retrieve the correct version of the latest package" do
+ provider.latest.should_not nil
+ end
+
+ it "should set latest to newer package version when available" do
+ instances = provider_class.instances
+ curl = instances.find {|i| i.properties[:origin] == 'ftp/curl' }
+ curl.properties[:latest].should == "7.33.0_2"
+ end
+
+ it "should call update to upgrade the version" do
+ resource = Puppet::Type.type(:package).new(
+ :name => 'ftp/curl',
+ :provider => pkgng,
+ :ensure => :latest
+ )
+
+
+ resource.provider.should_receive(:update)
+ resource.property(:ensure).sync
+ end
+ end
+
+ describe "get_latest_version" do
+ it "should rereturn nil when the current package is the latest" do
+ nmap_latest_version = provider_class.get_latest_version('security/nmap')
+ nmap_latest_version.should be_nil
+ end
+ end
+end

0 comments on commit b6daed9

Please sign in to comment.