# Copyright (C) 2011 RightScale, Inc, All Rights Reserved Worldwide. # # THIS PROGRAM IS CONFIDENTIAL AND PROPRIETARY TO RIGHTSCALE # AND CONSTITUTES A VALUABLE TRADE SECRET. Any unauthorized use, # reproduction, modification, or disclosure of this program is # strictly prohibited. Any use of this program by an authorized # licensee is strictly subject to the terms and conditions, # including confidentiality obligations, set forth in the applicable # License Agreement between RightScale.com, Inc. and # the licensee module RightConf class RubyConfigurator # RVM version used to install rubies RVM_VERSION = '1.2.6' include Configurator register :ruby description "Installs ruby interpreter and rubygems.\n" + 'Installs and uses rvm on supported (i.e. non-Windows) platforms' settings :ruby_version => 'Ruby version using rvm notation (see "rvm list known")', :rubygems_version => 'Rubygems version, e.g. "1.3.7"', :gemset => 'Gemset to be used for platforms supporting rvm' validate_has_settings :ruby_version, :rubygems_version # Switch to ruby version defined in settings # Use rvm and install it if needed # # === Return # true:: Always return true def run_linux check_rvm(RVM_VERSION) report_check("Checking whether #{ruby_version} is the active ruby") out = Command.execute('rvm', 'list').output if out =~ /^=> #{ruby_version.gsub('.', '\\.')}/ report_success else report_failure report_check("Switching to #{ruby_version}") out = Command.execute('rvm', 'use', ruby_version).output case out when /is not installed\./ report_failure report_fatal "Failed to install #{ruby}: #{@out}" if @out @out = install_ruby(ruby_version) run return true when /^Using / report_success check_rvmrc post_note 'Configuration required switching the active ruby, please "cd" into the project directory again to activate it' else report_fatal("Failed to use #{ruby_version}:\n#{out}") end end if gemset report_check("Switching to gemset #{gemset}") res = Command.execute('rvm', 'gemset', 'use', gemset, :abort_on_failure => "Failed to switch to gemset '#{gemset}'") report_success end Command.set_prefix("rvm #{ruby_version}@#{gemset} exec --") true end alias :run_darwin :run_linux # Switch to ruby version defined in settings # TBD # # === Return # true:: Always return true def run_windows end protected # Check whether the right version of RVM is installed and install it if not # # === Parameters # version(String):: Version of RVM to be checked, e.g. '1.2.6' # # === Return # true:: Always return true def check_rvm(version) report_check("Checking that rvm #{version} is installed") out = Command.execute('rvm', '--version').output if out =~ /rvm #{RVM_VERSION.gsub('.', '\\.')}/ report_success else report_failure report_check("Installing rvm #{version}") rvm_src = File.join(ENV['HOME'] || '/root', '.rvm/src') FileUtils.mkdir_p(rvm_src) Dir.chdir(rvm_src) do res = Command.execute('curl', '-O', '-f', "http://rvm.beginrescueend.com/releases/rvm-#{version}.tar.gz", :abort_on_failure => "Failed to download rvm #{version}") Command.execute('tar', 'zxf', "rvm-#{version}.tar.gz") end Dir.chdir(File.join(rvm_src, "rvm-#{version}")) do res = Command.execute('./install', :abort_on_failure => "Failed to install rvm #{version}") end report_success end true end # Install given ruby version using rvm # # === Parameters # ruby(String):: Ruby version compatible with rvm # # === Return # out(String):: Installation output def install_ruby(ruby) report_check("Installing #{ruby} (this will take a while, please be patient)") res = Command.execute('rvm', 'install', ruby) report_result(res.success?) res.output end # Check .rvmrc and its content # # === Return # true:: Always return true def check_rvmrc report_check('Setting up .rvmrc') begin File.open('.rvmrc', 'w') do |f| f.puts "rvm #{ruby_version}@#{gemset}" f.puts "type -P rconf &>/dev/null && { rconf; }" f.puts "type -P rconf &>/dev/null || { echo 'rconf not installed, skipping (see .rvmrc)'; }" end Command.execute('rvm', 'trust', 'rvmrc') report_success rescue Exception => e report_failure report_error(e.message) end true end end end