Skip to content

Commit

Permalink
New experimental recipe gem_package which patches gem_package resource.
Browse files Browse the repository at this point in the history
  • Loading branch information
fnichol committed May 12, 2011
1 parent 6dc6a76 commit 5615b2d
Show file tree
Hide file tree
Showing 5 changed files with 113 additions and 8 deletions.
18 changes: 18 additions & 0 deletions README.md
Expand Up @@ -32,6 +32,18 @@ default vagrant user to the RVM unix group and installs a `chef-solo`
wrapper script so Chef doesn't need to be re-installed in the default
RVM Ruby.

## gem_package

An experimental recipe that patches the
[gem_package resource](http://wiki.opscode.com/display/chef/Resources#Resources-Package)
to use the `Chef::Provider::Package::RVMRubygems` provider. An attribute
`rvm/gem_package/rvm_string` will determine which RVM ruby is used for
install/remove/upgrade/purge actions. This may help when using a third
party or upstream cookbook that assumes a non-RVM managed system ruby.

**Warning:** Here be dragons! This is either brilliant or the dumbest idea
ever, so feedback is appreciated.

# USAGE

# ATTRIBUTES
Expand Down Expand Up @@ -157,6 +169,12 @@ is an empty list.
If using the `vagrant` recipe, this sets the path to the package-installed
`chef-solo` binary. The default is `/usr/bin/chef-solo`.

## `gem_package/rvm_string`

If using the `gem_package` recipe, this determines which ruby will be used by the
`gem_package` resource in other cookbooks. The default is the value of the
`default_ruby` attribute.

# RESOURCES AND PROVIDERS

## rvm_ruby
Expand Down
23 changes: 23 additions & 0 deletions attributes/gem_package.rb
@@ -0,0 +1,23 @@
#
# Cookbook Name:: rvm
# Attributes:: gem_package
#
# Author:: Fletcher Nichol <fnichol@nichol.ca>
#
# Copyright 2010, 2011, Fletcher Nichol
#
# 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.
#

# rvm ruby that will be used for gem_package resources
default[:rvm][:gem_package][:rvm_string] = node[:rvm][:default_ruby]
34 changes: 34 additions & 0 deletions libraries/gem_package_monkeypatch.rb
@@ -0,0 +1,34 @@
#
# Cookbook Name:: rvm
# Library: gem_package resource monkey patch
#
# Author:: Fletcher Nichol <fnichol@nichol.ca>
#
# Copyright 2011, Fletcher Nichol
#
# 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.
#

##
# Patch Chef::Reousrce::GemPackage resource to use the RVMRubygems provider.
# This has potentially dangerous side effects and should be considered
# experimental. You have been warned.
def patch_gem_package
::Chef::Resource::GemPackage.class_eval do
def initialize(name, run_context=nil)
super
@resource_name = :gem_package
@provider = Chef::Provider::Package::RVMRubygems
end
end
end
23 changes: 15 additions & 8 deletions libraries/rvm_rubygems_package.rb
Expand Up @@ -54,24 +54,31 @@ def gem_platforms

def initialize(new_resource, run_context=nil)
super
@gem_env = RVMGemEnvironment.new(
gem_binary_path, new_resource.ruby_string)
@gem_env = RVMGemEnvironment.new(gem_binary_path, ruby_string)
end

def ruby_string
@ruby_string ||= if new_resource.respond_to?("ruby_string")
new_resource.ruby_string
else
node[:rvm][:gem_package][:rvm_string]
end
end

def install_package(name, version)
ruby_string = normalize_ruby_string(new_resource.ruby_string)
ruby_string_normalized = normalize_ruby_string(ruby_string)

# ensure ruby is installed and gemset exists
e = rvm_environment ruby_string do
e = rvm_environment ruby_string_normalized do
action :nothing
end
e.run_action(:create)

install_via_gem_command(name, version, new_resource.ruby_string)
install_via_gem_command(name, version)
true
end

def install_via_gem_command(name, version, ruby_string)
def install_via_gem_command(name, version)
src = @new_resource.source &&
" --source=#{@new_resource.source} --source=http://rubygems.org"

Expand All @@ -82,10 +89,10 @@ def install_via_gem_command(name, version, ruby_string)
end

def remove_package(name, version)
uninstall_via_gem_command(name, version, new_resource.ruby_string)
uninstall_via_gem_command(name, version)
end

def uninstall_via_gem_command(name, version, ruby_string)
def uninstall_via_gem_command(name, version)
cmd = %{rvm #{ruby_string} #{gem_binary_path}}
cmd << %{ uninstall #{name} -q -x -I}
if version
Expand Down
23 changes: 23 additions & 0 deletions recipes/gem_package.rb
@@ -0,0 +1,23 @@
#
# Cookbook Name:: rvm
# Recipe:: gem_package
#
# Copyright 2011, Fletcher Nichol
#
# 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.
#

patch_gem_package
::Chef::Log.info "gem_package resource has been patched to use provider " <<
"Chef::Provider::Package::RVMRubygems and will install gems to " <<
"the #{node[:rvm][:gem_package][:rvm_string]} RVM Ruby."

0 comments on commit 5615b2d

Please sign in to comment.