/
ruby.rb
176 lines (156 loc) · 5.61 KB
/
ruby.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
#
# Cookbook Name:: rvm
# Provider:: ruby
#
# 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.
#
include Chef::RVM::StringHelpers
include Chef::RVM::RubyHelpers
def load_current_resource
@rubie = normalize_ruby_string(select_ruby(new_resource.ruby_string))
@ruby_string = new_resource.ruby_string
@rvm_env = ::RVM::ChefUserEnvironment.new(
new_resource.user, "default",
:rvm_rubygems_version => new_resource.rubygems_version,
:source_environment => false
)
end
action :install do
next if skip_ruby?
if ruby_installed?(@ruby_string)
Chef::Log.debug("rvm_ruby[#{@rubie}] is already installed, so skipping")
else
install_start = Time.now
install_options = {:rvm_by_path => true}
install_options[:patch] = new_resource.patch if new_resource.patch
install_ruby_dependencies @rubie
Chef::Log.info("Building rvm_ruby[#{@rubie}], this could take awhile...")
if @rvm_env.install(@rubie, install_options)
Chef::Log.info("Installation of rvm_ruby[#{@rubie}] was successful.")
@rvm_env.use @rubie
update_installed_rubies
new_resource.updated_by_last_action(true)
Chef::Log.info("Importing initial gemsets for rvm_ruby[#{@rubie}]")
if @rvm_env.gemset_initial
Chef::Log.debug("Initial gemsets for rvm_ruby[#{@rubie}] are installed")
else
Chef::Log.warn(
"Failed to install initial gemsets for rvm_ruby[#{@rubie}] ")
end
else
Chef::Log.warn("Failed to install rvm_ruby[#{@rubie}]. " +
"Check logs in #{::RVM.path}/log/#{@rubie}")
end
Chef::Log.info("rvm_ruby[#{@rubie}] build time was " +
"#{(Time.now - install_start)/60.0} minutes.")
end
end
action :uninstall do
next if skip_ruby?
if ruby_installed?(@rubie)
Chef::Log.info("Uninstalling rvm_ruby[#{@rubie}]")
if @rvm_env.uninstall(@rubie, :rvm_by_path => true)
update_installed_rubies
Chef::Log.debug("Uninstallation of rvm_ruby[#{@rubie}] was successful.")
new_resource.updated_by_last_action(true)
else
Chef::Log.warn("Failed to uninstall rvm_ruby[#{@rubie}]. " +
"Check logs in #{::RVM.path}/log/#{@rubie}")
end
else
Chef::Log.debug("rvm_ruby[#{@rubie}] was not installed, so skipping")
end
end
action :remove do
next if skip_ruby?
if ruby_installed?(@rubie)
Chef::Log.info("Removing rvm_ruby[#{@rubie}]")
if @rvm_env.remove(@rubie, :rvm_by_path => true)
update_installed_rubies
Chef::Log.debug("Removal of rvm_ruby[#{@rubie}] was successful.")
new_resource.updated_by_last_action(true)
else
Chef::Log.warn("Failed to remove rvm_ruby[#{@rubie}]. " +
"Check logs in #{::RVM.path}/log/#{@rubie}")
end
else
Chef::Log.debug("rvm_ruby[#{@rubie}] was not installed, so skipping")
end
end
private
def skip_ruby?
if @rubie.nil?
Chef::Log.warn("#{self.class.name}: RVM ruby string `#{@rubie}' " +
"is not known. Use `rvm list known` to get a full list.")
true
else
false
end
end
##
# Installs any package dependencies needed by a given ruby
#
# @param [String, #to_s] the fully qualified RVM ruby string
def install_ruby_dependencies(rubie)
pkgs = []
case rubie
when /^ruby-/, /^ree-/, /^rbx-/, /^kiji/
case node['platform']
when "debian","ubuntu"
pkgs = %w{ build-essential openssl libreadline6 libreadline6-dev
zlib1g zlib1g-dev libssl-dev libyaml-dev libsqlite3-dev
sqlite3 libxml2-dev libxslt-dev autoconf libc6-dev
ncurses-dev automake libtool bison ssl-cert }
pkgs += %w{ subversion } if rubie =~ /^ruby-head$/
when "suse"
pkgs = %w{ gcc-c++ patch zlib zlib-devel libffi-devel
sqlite3-devel libxml2-devel libxslt-devel }
if node['platform_version'].to_f >= 11.0
pkgs += %w{ libreadline5 readline-devel libopenssl-devel }
else
pkgs += %w{ readline readline-devel openssl-devel }
end
pkgs += %w{ git subversion autoconf } if rubie =~ /^ruby-head$/
when "centos","redhat","fedora","scientific","amazon"
pkgs = %w{ gcc-c++ patch readline readline-devel zlib zlib-devel
libyaml-devel libffi-devel openssl-devel
make bzip2 autoconf automake libtool bison
libxml2 libxml2-devel libxslt libxslt-devel }
pkgs += %w{ git subversion autoconf } if rubie =~ /^ruby-head$/
end
when /^jruby/
begin
resource_collection.find("ruby_block[update-java-alternatives]").
run_action(:create)
rescue Chef::Exceptions::ResourceNotFound
Chef::Log.debug(
"java cookbook not loaded or not on ubuntu/debian, so skipping")
end
# TODO: need to figure out how to pull in java recipe only when needed. For
# now, users of jruby will have to add the "java" recipe to their run_list.
#include_recipe "java"
case node['platform']
when "debian","ubuntu"
pkgs += %w{ g++ ant }
end
end
pkgs.each do |pkg|
package pkg do
action :nothing
end.run_action(:install)
end
end