diff options
author | Adam Jacob <adam@hjksolutions.com> | 2008-10-08 00:05:46 -0700 |
---|---|---|
committer | Adam Jacob <adam@hjksolutions.com> | 2008-10-08 00:05:46 -0700 |
commit | 2ee34bb3b7f15e2607f5d3c02581b6bc0e9198f2 (patch) | |
tree | 09bf6616e235ee1839cf3e751acd21b546d76ca5 /lib/chef/provider/package/rubygems.rb | |
parent | 7dce6f22ef7caf8babc2ef9a76da0f548ed66039 (diff) | |
download | chef-2ee34bb3b7f15e2607f5d3c02581b6bc0e9198f2.tar.gz |
Adding rubygems provider, along with convenience resources for the package types
Diffstat (limited to 'lib/chef/provider/package/rubygems.rb')
-rw-r--r-- | lib/chef/provider/package/rubygems.rb | 116 |
1 files changed, 116 insertions, 0 deletions
diff --git a/lib/chef/provider/package/rubygems.rb b/lib/chef/provider/package/rubygems.rb new file mode 100644 index 0000000000..69459ffef4 --- /dev/null +++ b/lib/chef/provider/package/rubygems.rb @@ -0,0 +1,116 @@ +# +# Author:: Adam Jacob (<adam@hjksolutions.com>) +# Copyright:: Copyright (c) 2008 HJK Solutions, LLC +# 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 File.join(File.dirname(__FILE__), "..", "package") +require File.join(File.dirname(__FILE__), "..", "..", "mixin", "command") + +class Chef + class Provider + class Package + class Rubygems < Chef::Provider::Package + + def gem_list_parse(line) + installed_versions = Array.new + if line.match("^#{@new_resource.package_name} \\((.+?)\\)$") + installed_versions = $1.split(/, /) + installed_versions + else + nil + end + end + + def load_current_resource + @current_resource = Chef::Resource::Package.new(@new_resource.name) + @current_resource.package_name(@new_resource.package_name) + @current_resource.version(nil) + + # First, we need to look up whether we have the local gem installed or not + status = popen4("gem list --local #{@new_resource.package_name}") do |pid, stdin, stdout, stderr| + stdin.close + stdout.each do |line| + installed_versions = gem_list_parse(line) + next unless installed_versions + # If the version we are asking for is installed, make that our current + # version. Otherwise, go ahead and use the highest one, which + # happens to come first in the array. + if installed_versions.detect { |v| v == @new_resource.version } + Chef::Log.debug("#{@new_resource.package_name} at version #{@new_resource.version}") + @current_resource.version(@new_resource.version) + else + iv = installed_versions.first + Chef::Log.debug("#{@new_resource.package_name} at version #{iv}") + @current_resource.version(iv) + end + end + end + + unless status.exitstatus == 0 + raise Chef::Exception::Package, "gem list --local failed - #{status.inspect}!" + end + + status = popen4("gem list --remote #{@new_resource.package_name}") do |pid, stdin, stdout, stderr| + stdin.close + stdout.each do |line| + installed_versions = gem_list_parse(line) + next unless installed_versions + Chef::Log.debug("I have #{installed_versions.inspect}") + + if installed_versions.length >= 1 + Chef::Log.debug("Setting candidate version") + @candidate_version = installed_versions.first + end + end + end + + unless status.exitstatus == 0 + raise Chef::Exception::Package, "gem list --remote failed - #{status.inspect}!" + end + + @current_resource + end + + def install_package(name, version) + run_command( + :command => "gem install #{name} -q --no-rdoc --no-ri -v #{version}" + ) + end + + def upgrade_package(name, version) + install_package(name, version) + end + + def remove_package(name, version) + if version + run_command( + :command => "gem uninstall #{name} -q -v #{version}" + ) + else + run_command( + :command => "gem uninstall #{name} -q -a" + ) + end + end + + def purge_package(name, version) + remove_package(name, version) + end + + end + end + end +end |