Skip to content

Commit

Permalink
Convert to a custom resource
Browse files Browse the repository at this point in the history
Resolve foodcritic warnings where we were managing state ourselves.
Remove the incorrect load current resource usage to save variables off
and do default values.

Signed-off-by: Tim Smith <tsmith@chef.io>
  • Loading branch information
tas50 committed May 8, 2018
1 parent ffc04b3 commit adb6044
Show file tree
Hide file tree
Showing 6 changed files with 98 additions and 149 deletions.
14 changes: 7 additions & 7 deletions .kitchen.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,15 @@ verifier:
name: inspec

platforms:
- name: centos-6.8
- name: centos-7.3
- name: debian-7.11
- name: debian-8.6
- name: fedora-25
- name: opensuse-leap-42.2
- name: centos-6
- name: centos-7
- name: debian-8
- name: debian-9
- name: fedora-28
- name: opensuse-leap-42
- name: ubuntu-14.04
- name: ubuntu-16.04
- name: freebsd-10.3
- name: ubuntu-18.04
- name: freebsd-11.0

suites:
Expand Down
11 changes: 0 additions & 11 deletions libraries/matchers.rb

This file was deleted.

2 changes: 1 addition & 1 deletion metadata.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,4 @@

source_url "https://github.com/sous-chefs/#{name}"
issues_url "https://github.com/sous-chefs/#{name}/issues"
chef_version '>= 12.1' if respond_to?(:chef_version)
chef_version '>= 13.0' if respond_to?(:chef_version)
120 changes: 0 additions & 120 deletions providers/ruby.rb

This file was deleted.

96 changes: 88 additions & 8 deletions resources/ruby.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,91 @@
# limitations under the License.
#

actions :install, :reinstall
default_action :install

attribute :definition, kind_of: String, name_attribute: true
attribute :prefix_path, kind_of: String
attribute :user, kind_of: String
attribute :group, kind_of: String
attribute :environment, kind_of: Hash
property :definition, String, name_property: true
property :prefix_path, String, default: lazy { |r| "#{node['ruby_build']['default_ruby_base_path']}/#{r.definition}" }
property :user, String
property :group, String
property :environment, Hash

action :install do
perform_install
end

action :reinstall do
perform_install
end

action_class do
def perform_install
if ruby_installed?
Chef::Log.debug(
"ruby_build_ruby[#{new_resource.definition}] is already installed, so skipping")
else
install_start = Time.now

install_ruby_dependencies

Chef::Log.info(
"Building ruby_build_ruby[#{new_resource.definition}], this could take a while...")

rubie = new_resource.definition # bypass block scoping issue
prefix_path = new_resource.prefix_path # bypass block scoping issue
execute "ruby-build[#{rubie}]" do
command %(/usr/local/bin/ruby-build "#{rubie}" "#{prefix_path}")
user new_resource.user if new_resource.user
group new_resource.group if new_resource.group
environment new_resource.environment if new_resource.environment
action :run
end

Chef::Log.info("ruby_build_ruby[#{new_resource.definition}] build time was " \
"#{(Time.now - install_start) / 60.0} minutes")
end
end

def ruby_installed?
if Array(new_resource.action).include?(:reinstall)
false
else
::File.exist?("#{new_resource.prefix_path}/bin/ruby")
end
end

# def install_ruby_dependencies
# case ::File.basename(new_resource.version)
# when /^jruby-/
# package jruby_package_deps
# when /^rbx-/
# package rbx_package_deps
# else
# package package_deps
# end
# # ensure_java_environment if new_resource.version =~ /^jruby-/
# end

# def ensure_java_environment
# resource_collection.find('ruby_block[update-java-alternatives]').run_action(:create)
# rescue Chef::Exceptions::ResourceNotFound
# Chef::Log.info 'The java cookbook does not appear to in the run_list.'
# end

def install_ruby_dependencies
case ::File.basename(new_resource.definition)
when /^\d\.\d\.\d/, /^ree-/
pkgs = node['ruby_build']['install_pkgs_cruby']
when /^rbx-/
pkgs = node['ruby_build']['install_pkgs_rbx']
when /^jruby-/
pkgs = node['ruby_build']['install_pkgs_jruby']
end

# use multi-package when available since it's much faster
if platform_family?('rhel', 'suse', 'debian', 'fedora', 'amazon')
package pkgs
else
Array(pkgs).each do |pkg|
package pkg
end
end
end
end
4 changes: 2 additions & 2 deletions test/cookbooks/test/recipes/default.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@
# limitations under the License.
#

apt_update 'update'
apt_update

include_recipe 'java'
include_recipe 'ruby_build'

%w(2.4.2 jruby-9.1.13.0).each do |rubie|
%w(2.4.4 jruby-9.1.13.0).each do |rubie|
ruby_build_ruby rubie do
environment('MAKE_OPTS' => "-j #{node['cpu']['total'].to_i + 1}")
end
Expand Down

0 comments on commit adb6044

Please sign in to comment.