From 8a85dd2774f8ee7ae2f18579103d3d71338ef582 Mon Sep 17 00:00:00 2001 From: Russell Jackson Date: Wed, 5 Mar 2014 19:43:20 -0800 Subject: [PATCH] package provider: add support for `source` parameter Allow using the `source` parameter as an URI to determine from where to install the package. If `source` is not set, then use `pkg install` to install the package using the default repository logic. A URN of the form: urn:freebsd:repo: will force `pkg install` to install from the given repository using the '-r' flag. URLs are passed as-is to `pkg add` to install directly. --- lib/puppet/provider/package/pkgng.rb | 30 ++++++++++++++++++++++++---- 1 file changed, 26 insertions(+), 4 deletions(-) diff --git a/lib/puppet/provider/package/pkgng.rb b/lib/puppet/provider/package/pkgng.rb index 00584be..d3340f4 100644 --- a/lib/puppet/provider/package/pkgng.rb +++ b/lib/puppet/provider/package/pkgng.rb @@ -76,12 +76,34 @@ def self.prefetch(resources) end end + def repo_tag_from_urn(urn) + # extract repo tag from URN: urn:freebsd:repo: + schema = ['urn', 'freebsd', 'repo', nil] + validation = schema.zip(urn.split(':')) + result = validation.map do |should, actual| + if should.nil? + value = actual + else + raise ArgumentError source.inspect unless should == actual + value = nil + end + value + end + result.compact.first + end + def install - if File.exists?('/usr/local/etc/pkg.conf') - pkg(['install', '-qy', resource[:name]]) - else - raise Puppet::Error.new("/usr/local/etc/pkg.conf does not exist") + source = resource[:source] + source = URI(source) unless source.nil? + if not source # install using default repo logic + args = ['install', '-qy', resource[:name]] + elsif source.scheme == 'urn' # install from repo named in URN + tag = repo_tag_from_urn(source.to_s) + args = ['install', '-qy', '-r', tag, resource[:name]] + else # add package located at URL + args = ['add', '-q', source.to_s] end + pkg(args) end def uninstall