diff options
Diffstat (limited to 'lib/chef/provider/package')
-rw-r--r-- | lib/chef/provider/package/freebsd.rb | 149 | ||||
-rw-r--r-- | lib/chef/provider/package/freebsd/base.rb | 92 | ||||
-rw-r--r-- | lib/chef/provider/package/freebsd/pkg.rb | 113 | ||||
-rw-r--r-- | lib/chef/provider/package/freebsd/pkgng.rb | 80 | ||||
-rw-r--r-- | lib/chef/provider/package/freebsd/port.rb | 70 | ||||
-rw-r--r-- | lib/chef/provider/package/freebsd_next_gen.rb | 131 |
6 files changed, 355 insertions, 280 deletions
diff --git a/lib/chef/provider/package/freebsd.rb b/lib/chef/provider/package/freebsd.rb deleted file mode 100644 index f9cb5eb422..0000000000 --- a/lib/chef/provider/package/freebsd.rb +++ /dev/null @@ -1,149 +0,0 @@ -# -# Authors:: Bryan McLellan (btm@loftninjas.org) -# Matthew Landauer (matthew@openaustralia.org) -# Copyright:: Copyright (c) 2009 Bryan McLellan, Matthew Landauer -# License:: Apache License, Version 2.0 -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# - -require 'chef/provider/package' -require 'chef/mixin/shell_out' -require 'chef/resource/package' -require 'chef/mixin/get_source_from_package' - -class Chef - class Provider - class Package - class Freebsd < Chef::Provider::Package - include Chef::Mixin::ShellOut - - include Chef::Mixin::GetSourceFromPackage - - def initialize(*args) - super - @current_resource = Chef::Resource::Package.new(@new_resource.name) - end - - def current_installed_version - pkg_info = shell_out!("pkg_info -E \"#{package_name}*\"", :env => nil, :returns => [0,1]) - pkg_info.stdout[/^#{Regexp.escape(package_name)}-(.+)/, 1] - end - - def port_path - case @new_resource.package_name - # When the package name starts with a '/' treat it as the full path to the ports directory - when /^\// - @new_resource.package_name - # Otherwise if the package name contains a '/' not at the start (like 'www/wordpress') treat as a relative - # path from /usr/ports - when /\// - "/usr/ports/#{@new_resource.package_name}" - # Otherwise look up the path to the ports directory using 'whereis' - else - whereis = shell_out!("whereis -s #{@new_resource.package_name}", :env => nil) - unless path = whereis.stdout[/^#{Regexp.escape(@new_resource.package_name)}:\s+(.+)$/, 1] - raise Chef::Exceptions::Package, "Could not find port with the name #{@new_resource.package_name}" - end - path - end - end - - def ports_makefile_variable_value(variable) - make_v = shell_out!("make -V #{variable}", :cwd => port_path, :env => nil, :returns => [0,1]) - make_v.stdout.strip.split($\).first # $\ is the line separator, i.e., newline - end - - def ports_candidate_version - ports_makefile_variable_value("PORTVERSION") - end - - def file_candidate_version_path - Dir["#{@new_resource.source}/#{@current_resource.package_name}*"][-1].to_s - end - - def file_candidate_version - file_candidate_version_path.split(/-/).last.split(/.tbz/).first - end - - def load_current_resource - @current_resource.package_name(@new_resource.package_name) - - @current_resource.version(current_installed_version) - Chef::Log.debug("#{@new_resource} current version is #{@current_resource.version}") if @current_resource.version - - case @new_resource.source - when /^http/, /^ftp/ - @candidate_version = "0.0.0" - when /^\// - @candidate_version = file_candidate_version - else - @candidate_version = ports_candidate_version - end - - Chef::Log.debug("#{@new_resource} ports candidate version is #{@candidate_version}") if @candidate_version - - @current_resource - end - - def latest_link_name - ports_makefile_variable_value("LATEST_LINK") - end - - # The name of the package (without the version number) as understood by pkg_add and pkg_info - def package_name - if ::File.exist?("/usr/ports/Makefile") - if ports_makefile_variable_value("PKGNAME") =~ /^(.+)-[^-]+$/ - $1 - else - raise Chef::Exceptions::Package, "Unexpected form for PKGNAME variable in #{port_path}/Makefile" - end - else - @new_resource.package_name - end - end - - def install_package(name, version) - unless @current_resource.version - case @new_resource.source - when /^ports$/ - shell_out!("make -DBATCH install", :timeout => 1200, :env => nil, :cwd => port_path).status - when /^http/, /^ftp/ - if @new_resource.source =~ /\/$/ - shell_out!("pkg_add -r #{package_name}", :env => { "PACKAGESITE" => @new_resource.source, 'LC_ALL' => nil }).status - else - shell_out!("pkg_add -r #{package_name}", :env => { "PACKAGEROOT" => @new_resource.source, 'LC_ALL' => nil }).status - end - Chef::Log.debug("#{@new_resource} installed from: #{@new_resource.source}") - when /^\// - shell_out!("pkg_add #{file_candidate_version_path}", :env => { "PKG_PATH" => @new_resource.source , 'LC_ALL'=>nil}).status - Chef::Log.debug("#{@new_resource} installed from: #{@new_resource.source}") - else - shell_out!("pkg_add -r #{latest_link_name}", :env => nil).status - end - end - end - - def remove_package(name, version) - # a version is mandatory - if version - shell_out!("pkg_delete #{package_name}-#{version}", :env => nil).status - else - shell_out!("pkg_delete #{package_name}-#{@current_resource.version}", :env => nil).status - end - end - - end - end - end -end diff --git a/lib/chef/provider/package/freebsd/base.rb b/lib/chef/provider/package/freebsd/base.rb new file mode 100644 index 0000000000..24f79484f0 --- /dev/null +++ b/lib/chef/provider/package/freebsd/base.rb @@ -0,0 +1,92 @@ +# +# Authors:: Bryan McLellan (btm@loftninjas.org) +# Matthew Landauer (matthew@openaustralia.org) +# Richard Manyanza (liseki@nyikacraftsmen.com) +# Copyright:: Copyright (c) 2009 Bryan McLellan, Matthew Landauer +# Copyright:: Copyright (c) 2014 Richard Manyanza +# License:: Apache License, Version 2.0 +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +require 'chef/resource/package' +require 'chef/provider/package' +require 'chef/mixin/shell_out' +require 'chef/mixin/get_source_from_package' + +class Chef + class Provider + class Package + module Freebsd + + module PortsHelper + def supports_ports? + ::File.exist?("/usr/ports/Makefile") + end + + def port_dir(port) + case port + + # When the package name starts with a '/' treat it as the full path to the ports directory. + when /^\// + port + + # Otherwise if the package name contains a '/' not at the start (like 'www/wordpress') treat + # as a relative path from /usr/ports. + when /\// + "/usr/ports/#{port}" + + # Otherwise look up the path to the ports directory using 'whereis' + else + whereis = shell_out!("whereis -s #{port}", :env => nil) + unless _path = whereis.stdout[/^#{Regexp.escape(port)}:\s+(.+)$/, 1] + raise Chef::Exceptions::Package, "Could not find port with the name #{port}" + end + _path + end + end + + def makefile_variable_value(variable, dir = nil) + options = dir ? { :cwd => dir } : {} + make_v = shell_out!("make -V #{variable}", options.merge!(:env => nil, :returns => [0,1])) + make_v.exitstatus.zero? ? make_v.stdout.strip.split($\).first : nil # $\ is the line separator, i.e. newline. + end + end + + + class Base < Chef::Provider::Package + include Chef::Mixin::ShellOut + include Chef::Mixin::GetSourceFromPackage + + def initialize(*args) + super + @current_resource = Chef::Resource::Package.new(@new_resource.name) + end + + def load_current_resource + @current_resource.package_name(@new_resource.package_name) + + @current_resource.version(current_installed_version) + Chef::Log.debug("#{@new_resource} current version is #{@current_resource.version}") if @current_resource.version + + @candidate_version = candidate_version + Chef::Log.debug("#{@new_resource} candidate version is #{@candidate_version}") if @candidate_version + + @current_resource + end + end + + end + end + end +end diff --git a/lib/chef/provider/package/freebsd/pkg.rb b/lib/chef/provider/package/freebsd/pkg.rb new file mode 100644 index 0000000000..c757d26fe5 --- /dev/null +++ b/lib/chef/provider/package/freebsd/pkg.rb @@ -0,0 +1,113 @@ +# +# Authors:: Bryan McLellan (btm@loftninjas.org) +# Matthew Landauer (matthew@openaustralia.org) +# Richard Manyanza (liseki@nyikacraftsmen.com) +# Copyright:: Copyright (c) 2009 Bryan McLellan, Matthew Landauer +# Copyright:: Copyright (c) 2014 Richard Manyanza +# License:: Apache License, Version 2.0 +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +require 'chef/provider/package/freebsd/base' + +class Chef + class Provider + class Package + module Freebsd + class Pkg < Base + include PortsHelper + + def install_package(name, version) + unless @current_resource.version + case @new_resource.source + when /^http/, /^ftp/ + if @new_resource.source =~ /\/$/ + shell_out!("pkg_add -r #{package_name}", :env => { "PACKAGESITE" => @new_resource.source, 'LC_ALL' => nil }).status + else + shell_out!("pkg_add -r #{package_name}", :env => { "PACKAGEROOT" => @new_resource.source, 'LC_ALL' => nil }).status + end + Chef::Log.debug("#{@new_resource} installed from: #{@new_resource.source}") + + when /^\// + shell_out!("pkg_add #{file_candidate_version_path}", :env => { "PKG_PATH" => @new_resource.source , 'LC_ALL'=>nil}).status + Chef::Log.debug("#{@new_resource} installed from: #{@new_resource.source}") + + else + shell_out!("pkg_add -r #{latest_link_name}", :env => nil).status + end + end + end + + def remove_package(name, version) + shell_out!("pkg_delete #{package_name}-#{version || @current_resource.version}", :env => nil).status + end + + # The name of the package (without the version number) as understood by pkg_add and pkg_info. + def package_name + if supports_ports? + if makefile_variable_value("PKGNAME", port_path) =~ /^(.+)-[^-]+$/ + $1 + else + raise Chef::Exceptions::Package, "Unexpected form for PKGNAME variable in #{port_path}/Makefile" + end + else + @new_resource.package_name + end + end + + def latest_link_name + makefile_variable_value("LATEST_LINK", port_path) + end + + def current_installed_version + pkg_info = shell_out!("pkg_info -E \"#{package_name}*\"", :env => nil, :returns => [0,1]) + pkg_info.stdout[/^#{Regexp.escape(package_name)}-(.+)/, 1] + end + + def candidate_version + case @new_resource.source + when /^http/, /^ftp/ + repo_candidate_version + when /^\// + file_candidate_version + else + ports_candidate_version + end + end + + def file_candidate_version_path + Dir["#{@new_resource.source}/#{@current_resource.package_name}*"][-1].to_s + end + + def file_candidate_version + file_candidate_version_path.split(/-/).last.split(/.tbz/).first + end + + def repo_candidate_version + "0.0.0" + end + + def ports_candidate_version + makefile_variable_value("PORTVERSION", port_path) + end + + def port_path + port_dir @new_resource.package_name + end + + end + end + end + end +end diff --git a/lib/chef/provider/package/freebsd/pkgng.rb b/lib/chef/provider/package/freebsd/pkgng.rb new file mode 100644 index 0000000000..da531facc1 --- /dev/null +++ b/lib/chef/provider/package/freebsd/pkgng.rb @@ -0,0 +1,80 @@ +# +# Authors:: Richard Manyanza (liseki@nyikacraftsmen.com) +# Copyright:: Copyright (c) 2014 Richard Manyanza +# License:: Apache License, Version 2.0 +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +require 'chef/provider/package/freebsd/base' + +class Chef + class Provider + class Package + module Freebsd + class Pkgng < Base + + def install_package(name, version) + unless @current_resource.version + case @new_resource.source + when /^(http|ftp|\/)/ + shell_out!("pkg add#{expand_options(@new_resource.options)} #{@new_resource.source}", :env => { 'LC_ALL' => nil }).status + Chef::Log.debug("#{@new_resource} installed from: #{@new_resource.source}") + + else + shell_out!("pkg install -y#{expand_options(@new_resource.options)} #{name}", :env => { 'LC_ALL' => nil }).status + end + end + end + + def remove_package(name, version) + options = @new_resource.options && @new_resource.options.sub(repo_regex, '') + options && !options.empty? || options = nil + shell_out!("pkg delete -y#{expand_options(options)} #{name}#{version ? '-' + version : ''}", :env => nil).status + end + + def current_installed_version + pkg_info = shell_out!("pkg info \"#{@new_resource.package_name}\"", :env => nil, :returns => [0,70]) + pkg_info.stdout[/^#{Regexp.escape(@new_resource.package_name)}-(.+)/, 1] + end + + def candidate_version + @new_resource.source ? file_candidate_version : repo_candidate_version + end + + + + private + + def file_candidate_version + @new_resource.source[/#{Regexp.escape(@new_resource.package_name)}-(.+)\.txz/, 1] + end + + def repo_candidate_version + if @new_resource.options && @new_resource.options.match(repo_regex) + options = $1 + end + + pkg_query = shell_out!("pkg rquery#{expand_options(options)} '%v' #{@new_resource.package_name}", :env => nil) + pkg_query.exitstatus.zero? ? pkg_query.stdout.strip.split(/\n/).last : nil + end + + def repo_regex + /(-r\s?\S+)\b/ + end + + end + end + end + end +end diff --git a/lib/chef/provider/package/freebsd/port.rb b/lib/chef/provider/package/freebsd/port.rb new file mode 100644 index 0000000000..6f4471a6f7 --- /dev/null +++ b/lib/chef/provider/package/freebsd/port.rb @@ -0,0 +1,70 @@ +# +# Authors:: Richard Manyanza (liseki@nyikacraftsmen.com) +# Copyright:: Copyright (c) 2014 Richard Manyanza +# License:: Apache License, Version 2.0 +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +require 'chef/provider/package/freebsd/base' + +class Chef + class Provider + class Package + module Freebsd + class Port < Base + include PortsHelper + + def install_package(name, version) + shell_out!("make -DBATCH install clean", :timeout => 1800, :env => nil, :cwd => port_dir).status + end + + def remove_package(name, version) + shell_out!("make deinstall", :timeout => 300, :env => nil, :cwd => port_dir).status + end + + def current_installed_version + pkg_info = if supports_pkgng? + shell_out!("pkg info \"#{@new_resource.package_name}\"", :env => nil, :returns => [0,70]) + else + shell_out!("pkg_info -E \"#{@new_resource.package_name}*\"", :env => nil, :returns => [0,1]) + end + pkg_info.stdout[/^#{Regexp.escape(@new_resource.package_name)}-(.+)/, 1] + end + + def candidate_version + if supports_ports? + makefile_variable_value("PORTVERSION", port_dir) + else + raise Chef::Exceptions::Package, "Ports collection could not be found" + end + end + + def port_dir + super(@new_resource.package_name) + end + + + + private + + def supports_pkgng? + with_pkgng = makefile_variable_value('WITH_PKGNG') + with_pkgng && with_pkgng =~ /yes/i + end + + end + end + end + end +end diff --git a/lib/chef/provider/package/freebsd_next_gen.rb b/lib/chef/provider/package/freebsd_next_gen.rb deleted file mode 100644 index d7dcd757bc..0000000000 --- a/lib/chef/provider/package/freebsd_next_gen.rb +++ /dev/null @@ -1,131 +0,0 @@ -# -# Authors:: Richard Manyanza (liseki@nyikacraftsmen.com) -# Copyright:: Copyright (c) 2013 Richard Manyanza -# License:: Apache License, Version 2.0 -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# - -require 'chef/provider/package' -require 'chef/mixin/shell_out' -require 'chef/resource/package' -require 'chef/mixin/get_source_from_package' - -class Chef - class Provider - class Package - class FreebsdNextGen < Chef::Provider::Package - include Chef::Mixin::ShellOut - - include Chef::Mixin::GetSourceFromPackage - - def initialize(*args) - super - @current_resource = Chef::Resource::Package.new(@new_resource.name) - end - - def load_current_resource - @current_resource.package_name(@new_resource.package_name) - - @current_resource.version(current_installed_version) - Chef::Log.debug("#{@new_resource} current version is #{@current_resource.version}") if @current_resource.version - - @candidate_version = case @new_resource.source - when /^ports$/i - ports_candidate_version - else - package_candidate_version - end - - Chef::Log.debug("#{@new_resource} ports candidate version is #{@candidate_version}") if @candidate_version - - @current_resource - end - - def install_package(name, version) - unless @current_resource.version - case @new_resource.source - when /^ports$/i - shell_out!("make -DBATCH install", :timeout => 1800, :env => nil, :cwd => port_path).status - when /^(http|ftp|\/)/ - shell_out!("pkg add#{expand_options(@new_resource.options)} #{@new_resource.source}", :env => { 'LC_ALL' => nil }).status - Chef::Log.debug("#{@new_resource} installed from: #{@new_resource.source}") - else - shell_out!("pkg install#{expand_options(@new_resource.options || '-y')} #{name}", :env => { 'LC_ALL' => nil }).status - end - end - end - - def remove_package(name, version) - shell_out!("pkg delete#{expand_options(@new_resource.options || '-y')} #{name}#{version ? '-' + version : ''}", :env => nil).status - end - - - - - private - - def current_installed_version - pkg_info = shell_out!("pkg info \"#{@new_resource.package_name}\"", :env => nil, :returns => [0,70]) - pkg_info.stdout[/^#{Regexp.escape(@new_resource.package_name)}-(.+)/, 1] - end - - def package_candidate_version - if @new_resource.source - @new_resource.source[/#{Regexp.escape(@new_resource.package_name)}-(.+)\.[[:alpha:]]{3}/, 1] - else - repo_candidate_version - end - end - - def repo_candidate_version - if @new_resource.options && @new_resource.options.match(/(r .+)\b/) - options = "-#{$1}" - end - - pkg_query = shell_out!("pkg rquery#{expand_options(options)} '%v' #{@new_resource.package_name}", :env => nil, :returns => [0,69]) - pkg_query.stdout.strip.split(/\n/).first - end - - def ports_candidate_version - ports_makefile_variable_value("PORTVERSION") - end - - def port_path - case @new_resource.package_name - # When the package name starts with a '/' treat it as the full path to the ports directory - when /^\// - @new_resource.package_name - # Otherwise if the package name contains a '/' not at the start (like 'www/wordpress') treat as a relative - # path from /usr/ports - when /\// - "/usr/ports/#{@new_resource.package_name}" - # Otherwise look up the path to the ports directory using 'whereis' - else - whereis = shell_out!("whereis -s #{@new_resource.package_name}", :env => nil) - unless path = whereis.stdout[/^#{Regexp.escape(@new_resource.package_name)}:\s+(.+)$/, 1] - raise Chef::Exceptions::Package, "Could not find port with the name #{@new_resource.package_name}" - end - path - end - end - - def ports_makefile_variable_value(variable) - make_v = shell_out!("make -V #{variable}", :cwd => port_path, :env => nil, :returns => [0,1]) - make_v.stdout.strip.split(/\n/).first - end - - end - end - end -end |