Skip to content

Commit

Permalink
Add rvm_shell resource.
Browse files Browse the repository at this point in the history
  • Loading branch information
fnichol committed Feb 20, 2011
1 parent 8d40c77 commit d7da781
Show file tree
Hide file tree
Showing 3 changed files with 120 additions and 0 deletions.
9 changes: 9 additions & 0 deletions libraries/helpers.rb
Expand Up @@ -54,6 +54,15 @@ def ruby_installed?(rubie)
! installed_rubies.select { |r| r.start_with?(rubie) }.empty?
end

##
# Inverse of #ruby_installed?
#
# @param [String, #to_s] the fully qualified RVM ruby string
# @return [Boolean] is this ruby not installed?
def ruby_not_installed?(rubie)
!ruby_installed?(rubie)
end

##
# Determines whether or not the given ruby is a known ruby string
#
Expand Down
70 changes: 70 additions & 0 deletions providers/shell.rb
@@ -0,0 +1,70 @@
#
# Cookbook Name:: rvm
# Provider:: shell
#
# 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.
#

action :run do
ruby_string = normalize_ruby_string(new_resource.ruby_string)
rubie = select_ruby(ruby_string)
gemset = select_gemset(ruby_string)

if ruby_not_installed?(rubie)
Chef::Log.warn("rvm_ruby[#{rubie}] not installed, so skipping")
elsif gemset && !gemset_exists?(:ruby => rubie, :gemset => gemset)
Chef::Log.warn("rvm_gemset[#{ruby_string}] not created, so skipping")
else
script_wrapper :run
end
end


##
# Wraps the script resource for RVM-dependent code.
#
# @param [Symbol] action to be performed with gem_package provider
# @param [optional, String, #to_s] the fully qualifed rvm string
def script_wrapper(exec_action, ruby_string=new_resource.ruby_string)
script_code = <<-CODE
if [ -s "${HOME}/.rvm/scripts/rvm" ]; then
source "${HOME}/.rvm/scripts/rvm"
elif [ -s "#{::File.dirname(node[:rvm][:root_path])}/lib/rvm" ]; then
source "#{::File.dirname(node[:rvm][:root_path])}/lib/rvm"
fi
rvm use #{ruby_string}
#{new_resource.code}
CODE

s = script new_resource.name do
interpreter "bash"
code script_code
creates new_resource.creates if new_resource.creates
cwd new_resource.cwd if new_resource.cwd
environment new_resource.environment if new_resource.environment
group new_resource.group if new_resource.group
path new_resource.path if new_resource.path
returns new_resource.returns if new_resource.returns
timeout new_resource.timeout if new_resource.timeout
user new_resource.user if new_resource.user
umask new_resource.umask if new_resource.umask
action :nothing
end
s.run_action(exec_action)
end
41 changes: 41 additions & 0 deletions resources/shell.rb
@@ -0,0 +1,41 @@
#
# Cookbook Name:: rvm
# Resource:: shell
#
# 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.
#

actions :run

attribute :name, :kind_of => String, :name_attribute => true
attribute :ruby_string, :kind_of => String, :default => "default"
attribute :code, :kind_of => String
attribute :creates, :kind_of => String
attribute :cwd, :kind_of => String
attribute :environment, :kind_of => Hash
attribute :group, :kind_of => String
attribute :path, :kind_of => Array
attribute :returns, :kind_of => Array, :default => [ 0 ]
attribute :timeout, :kind_of => Integer
attribute :user, :kind_of => String
attribute :umask, :kind_of => String

def initialize(name, run_context=nil)
super
@action = :run
@command = name
end

0 comments on commit d7da781

Please sign in to comment.