From cea048a465a94515e02af488db61b749a42899a4 Mon Sep 17 00:00:00 2001 From: John Bellone Date: Mon, 27 Jan 2014 16:21:20 -0500 Subject: [PATCH 1/2] Add LWRP for chruby_exec and chruby_gem. This commit adds the LWRP for running chruby-exec and gem commands in the context of a supplied Ruby version. Additional changes: - Default to the latest, stable version of chruby; - Default to the latest, stable version of Ruby 1.9.3; - Install default gems using LWRP for all rubies; - Attribute for specifying installation prefix. --- LICENSE | 1 + attributes/default.rb | 6 ++++-- providers/exec.rb | 24 ++++++++++++++++++++++++ providers/gem.rb | 30 ++++++++++++++++++++++++++++++ recipes/system.rb | 10 +++++++++- resources/exec.rb | 27 +++++++++++++++++++++++++++ resources/gem.rb | 24 ++++++++++++++++++++++++ 7 files changed, 119 insertions(+), 3 deletions(-) create mode 100644 providers/exec.rb create mode 100644 providers/gem.rb create mode 100644 resources/exec.rb create mode 100644 resources/gem.rb diff --git a/LICENSE b/LICENSE index 02cfb01..95bd3bd 100644 --- a/LICENSE +++ b/LICENSE @@ -1,4 +1,5 @@ Copyright (C) 2013 Atalanta Systems Ltd +Copyright (C) 2014 Bloomberg Finance L.P. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. diff --git a/attributes/default.rb b/attributes/default.rb index c1b96d3..bbdaf1f 100644 --- a/attributes/default.rb +++ b/attributes/default.rb @@ -1,10 +1,12 @@ -default['chruby']['version'] = '0.3.4' +default['chruby']['version'] = '0.3.8' default['chruby']['gpg_check'] = false default['chruby']['use_rvm_rubies'] = false default['chruby']['use_rbenv_rubies'] = false default['chruby']['auto_switch'] = true -default['chruby']['rubies'] = {'1.9.3-p392' => true} +default['chruby']['rubies'] = {'1.9.3-p484' => true} default['chruby']['default'] = 'embedded' +default['chruby']['default_gems'] = { bundler: '~> 1.5', rake: '~> 10.0' } default['chruby']['user_rubies'] = {} default['chruby']['sh_dir'] = "/etc/profile.d" default['chruby']['sh_name'] = 'chruby.sh' +default['chruby']['install_prefix'] = '/opt/rubies' diff --git a/providers/exec.rb b/providers/exec.rb new file mode 100644 index 0000000..8474218 --- /dev/null +++ b/providers/exec.rb @@ -0,0 +1,24 @@ +# Copyright 2014, Bloomberg Finance L.P. +# +# 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 + + execute "chruby_exec-#{new_resource.ruby}[#{new_resource.command}]" do + command %(/usr/local/bin/chruby-exec #{new_resource.ruby} -- #{new_resource.command}) + action :nothing + subscribes [:install, :reinstall], "ruby-build[#{new_resource.ruby}]", :delayed + end + + new_resource.updated_by_last_action(true) +end diff --git a/providers/gem.rb b/providers/gem.rb new file mode 100644 index 0000000..e12da5a --- /dev/null +++ b/providers/gem.rb @@ -0,0 +1,30 @@ +# Copyright 2014, Bloomberg Finance L.P. +# +# 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. + +def load_current_resource + @options = new_resource.options + @options = @options.each { |k,v| "--#{k}" if v }.join(' ') if @options.is_a?(Hash) +end + +%w(install remove).each do |type| + action type.to_sym do + gem_package "chruby #{new_resource.ruby}: #{new_resource.package_name}" do + package_name new_resource.package_name + gem_binary ::File.join(node['chruby']['install_prefix'], new_resource.ruby, 'bin/gem') + version new_resource.version if new_resource.version + options @options if @options + action type.to_sym + end + end +end diff --git a/recipes/system.rb b/recipes/system.rb index 1603160..c2b1fc1 100644 --- a/recipes/system.rb +++ b/recipes/system.rb @@ -17,8 +17,16 @@ node['chruby']['rubies'].each do |ruby, flag| if flag ruby_build_ruby ruby do - prefix_path "/opt/rubies/#{ruby}" + prefix_path ::File.join(node['chruby']['install_prefix'], ruby) end + + node['chruby']['default_gems'].each do |gem, version| + chruby_gem gem do + version version + ruby ruby + end + end + end end diff --git a/resources/exec.rb b/resources/exec.rb new file mode 100644 index 0000000..4011801 --- /dev/null +++ b/resources/exec.rb @@ -0,0 +1,27 @@ +# Copyright 2014, Bloomberg Finance L.P. +# +# 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 +default_action :run + +attribute :command, :kind_of => String, :name_attribute => true +attribute :ruby, :kind_of => String + +def initialize(*args) + super +end + +def to_s + "#{super} (#{command} with #{ruby})" +end diff --git a/resources/gem.rb b/resources/gem.rb new file mode 100644 index 0000000..3eb12da --- /dev/null +++ b/resources/gem.rb @@ -0,0 +1,24 @@ +# Copyright 2014, Bloomberg Finance L.P. +# +# 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 :install, :remove + +attribute :package_name, :kind_of => String, :name_attribute => true +attribute :version, :kind_of => String +attribute :ruby, :kind_of => String, :default => node['chruby']['default'] +attribute :options, :kind_of => [Hash, String] + +def initialize(*args) + super +end From cde490ac253c054b1b083ca2c9ff1f8873390586 Mon Sep 17 00:00:00 2001 From: John Bellone Date: Tue, 3 Mar 2015 11:07:02 -0500 Subject: [PATCH 2/2] Refactor cookbook and cleanup resources/recipes. --- .ruby_version | 1 - LICENSE | 2 +- attributes/default.rb | 22 ++++++++++----------- libraries/provider_chruby_exec.rb | 19 ++++++++++++++++++ libraries/resource_chruby_exec.rb | 14 +++++++++++++ metadata.rb | 24 +++++++++++----------- providers/exec.rb | 24 ---------------------- providers/gem.rb | 30 ---------------------------- recipes/default.rb | 33 ++++++++++++++++--------------- recipes/system.rb | 33 ------------------------------- resources/exec.rb | 27 ------------------------- resources/gem.rb | 24 ---------------------- templates/default/chruby.sh.erb | 24 +++++++++------------- 13 files changed, 83 insertions(+), 194 deletions(-) delete mode 100644 .ruby_version create mode 100644 libraries/provider_chruby_exec.rb create mode 100644 libraries/resource_chruby_exec.rb delete mode 100644 providers/exec.rb delete mode 100644 providers/gem.rb delete mode 100644 recipes/system.rb delete mode 100644 resources/exec.rb delete mode 100644 resources/gem.rb mode change 100644 => 100755 templates/default/chruby.sh.erb diff --git a/.ruby_version b/.ruby_version deleted file mode 100644 index 650d0c2..0000000 --- a/.ruby_version +++ /dev/null @@ -1 +0,0 @@ -1.9.3-p432 diff --git a/LICENSE b/LICENSE index 95bd3bd..9335a08 100644 --- a/LICENSE +++ b/LICENSE @@ -1,5 +1,5 @@ Copyright (C) 2013 Atalanta Systems Ltd -Copyright (C) 2014 Bloomberg Finance L.P. +Copyright (C) 2014, 2015 Bloomberg Finance L.P. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. diff --git a/attributes/default.rb b/attributes/default.rb index bbdaf1f..6bf4c89 100644 --- a/attributes/default.rb +++ b/attributes/default.rb @@ -1,12 +1,10 @@ -default['chruby']['version'] = '0.3.8' -default['chruby']['gpg_check'] = false -default['chruby']['use_rvm_rubies'] = false -default['chruby']['use_rbenv_rubies'] = false -default['chruby']['auto_switch'] = true -default['chruby']['rubies'] = {'1.9.3-p484' => true} -default['chruby']['default'] = 'embedded' -default['chruby']['default_gems'] = { bundler: '~> 1.5', rake: '~> 10.0' } -default['chruby']['user_rubies'] = {} -default['chruby']['sh_dir'] = "/etc/profile.d" -default['chruby']['sh_name'] = 'chruby.sh' -default['chruby']['install_prefix'] = '/opt/rubies' +default['chruby']['source_url'] = 'https://github.com/postmodern/chruby/archive/v%{version}.zip' +default['chruby']['source_checksum'] = nil +default['chruby']['version'] = '0.3.9' +default['chruby']['prefix_path'] = '/usr/local/chruby' +default['chruby']['install_path'] = '/usr/local/src/chruby' + +default['chruby']['sh_profile'] = false +default['chruby']['sh_rubies'] = [] +default['chruby']['sh_default_ruby'] = nil +default['chruby']['sh_auto_switch'] = false diff --git a/libraries/provider_chruby_exec.rb b/libraries/provider_chruby_exec.rb new file mode 100644 index 0000000..e62b917 --- /dev/null +++ b/libraries/provider_chruby_exec.rb @@ -0,0 +1,19 @@ +# +# Cookbook Name:: chruby +# License:: Apache 2.0 +# +# Copyright 2014, 2015 Bloomberg Finance L.P. +# +class Chef::Provider::ChrubyExec < Chef::Provider::LWRPBase + use_inline_resources if defined?(use_inline_resources) + + def whyrun_supported? + true + end + + action :run do + execute "chruby exec-#{new_resource.ruby}[#{new_resource.command}]" do + command %Q(chruby-exec "#{new_resource.ruby}" -- #{new_resource.command}) + end + end +end diff --git a/libraries/resource_chruby_exec.rb b/libraries/resource_chruby_exec.rb new file mode 100644 index 0000000..aad4ce6 --- /dev/null +++ b/libraries/resource_chruby_exec.rb @@ -0,0 +1,14 @@ +# +# Cookbook Name:: chruby +# License:: Apache 2.0 +# +# Copyright 2014, 2015 Bloomberg Finance L.P. +# +class Chef::Resource::ChrubyExec < Chef::Resource::LWRPBase + self.resource_name = :chruby_exec + actions :run + default_action :run + + attribute :command, kind_of: String, name_attribute: true + attribute :ruby, kind_of: String +end diff --git a/metadata.rb b/metadata.rb index dbc83b2..47197c8 100644 --- a/metadata.rb +++ b/metadata.rb @@ -1,11 +1,13 @@ -name "chruby" -maintainer "Atalanta Systems Ltd" -maintainer_email "support@atalanta-systems.com" -license "Apache 2.0" -description "Installs/Configures chruby" -long_description IO.read(File.join(File.dirname(__FILE__), 'README.md')) -version "0.2.1" -depends "ark" -depends "ruby_build" -supports "centos" -supports "ubuntu" +name 'chruby' +maintainer 'Bloomberg Finance L.P.' +maintainer_email 'webops@bloomberg.net' +license 'Apache 2.0' +description 'Installs/configures chruby' +long_description 'Installs/configures chruby' +version '1.0.0' + +depends 'libarchive' + +supports 'centos' +supports 'redhat' +supports 'ubuntu' diff --git a/providers/exec.rb b/providers/exec.rb deleted file mode 100644 index 8474218..0000000 --- a/providers/exec.rb +++ /dev/null @@ -1,24 +0,0 @@ -# Copyright 2014, Bloomberg Finance L.P. -# -# 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 - - execute "chruby_exec-#{new_resource.ruby}[#{new_resource.command}]" do - command %(/usr/local/bin/chruby-exec #{new_resource.ruby} -- #{new_resource.command}) - action :nothing - subscribes [:install, :reinstall], "ruby-build[#{new_resource.ruby}]", :delayed - end - - new_resource.updated_by_last_action(true) -end diff --git a/providers/gem.rb b/providers/gem.rb deleted file mode 100644 index e12da5a..0000000 --- a/providers/gem.rb +++ /dev/null @@ -1,30 +0,0 @@ -# Copyright 2014, Bloomberg Finance L.P. -# -# 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. - -def load_current_resource - @options = new_resource.options - @options = @options.each { |k,v| "--#{k}" if v }.join(' ') if @options.is_a?(Hash) -end - -%w(install remove).each do |type| - action type.to_sym do - gem_package "chruby #{new_resource.ruby}: #{new_resource.package_name}" do - package_name new_resource.package_name - gem_binary ::File.join(node['chruby']['install_prefix'], new_resource.ruby, 'bin/gem') - version new_resource.version if new_resource.version - options @options if @options - action type.to_sym - end - end -end diff --git a/recipes/default.rb b/recipes/default.rb index 24ab8d7..da8de78 100644 --- a/recipes/default.rb +++ b/recipes/default.rb @@ -3,6 +3,7 @@ # Recipe:: default # # Copyright (C) 2013 Atalanta Systems Ltd +# Copyright (C) 2014, 2015 Bloomberg Finance L.P. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. @@ -15,29 +16,29 @@ # 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_recipe "ark" +include_recipe 'libarchive::default' -ark "chruby" do - url "https://github.com/postmodern/chruby/archive/v#{node['chruby']['version']}.tar.gz" - action :install_with_make +archive = remote_file node['chruby']['source_url'] % { version: node['chruby']['version'] } do + checksum node['chruby']['source_checksum'] end -# Workaround for Github issue 5 https://github.com/Atalanta/chef-chruby/issues/5 - -link "/usr/local/chruby" do - to "/usr/local/chruby-1" +directory node['chruby']['install_path'] do + recursive true end -sh_owner = node['chruby']['sh_owner'] +libarchive_file File.basename(archive.path) do + path archive.path + extract_to File.join(node['chruby']['install_path'], node['chruby']['version']) + action :extract +end -directory node['chruby']['sh_dir'] do - recursive true - owner sh_owner if sh_owner +execute "make install --prefix #{node['chruby']['prefix_path']}" do + cwd File.join(node['chruby']['install_path'], node['chruby']['version']) end -template File.join(node['chruby']['sh_dir'], node['chruby']['sh_name']) do - source "chruby.sh.erb" - mode "0644" - owner sh_owner if sh_owner +template '/etc/profile.d/chruby.sh' do + source 'chruby.sh.erb' + only_if { node['chruby']['sh_profile'] } end diff --git a/recipes/system.rb b/recipes/system.rb deleted file mode 100644 index c2b1fc1..0000000 --- a/recipes/system.rb +++ /dev/null @@ -1,33 +0,0 @@ -# Copyright 2013, Atalanta Systems Ltd -# -# 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_recipe "ruby_build" - -node['chruby']['rubies'].each do |ruby, flag| - if flag - ruby_build_ruby ruby do - prefix_path ::File.join(node['chruby']['install_prefix'], ruby) - end - - node['chruby']['default_gems'].each do |gem, version| - chruby_gem gem do - version version - ruby ruby - end - end - - end -end - -include_recipe "chruby" diff --git a/resources/exec.rb b/resources/exec.rb deleted file mode 100644 index 4011801..0000000 --- a/resources/exec.rb +++ /dev/null @@ -1,27 +0,0 @@ -# Copyright 2014, Bloomberg Finance L.P. -# -# 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 -default_action :run - -attribute :command, :kind_of => String, :name_attribute => true -attribute :ruby, :kind_of => String - -def initialize(*args) - super -end - -def to_s - "#{super} (#{command} with #{ruby})" -end diff --git a/resources/gem.rb b/resources/gem.rb deleted file mode 100644 index 3eb12da..0000000 --- a/resources/gem.rb +++ /dev/null @@ -1,24 +0,0 @@ -# Copyright 2014, Bloomberg Finance L.P. -# -# 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 :install, :remove - -attribute :package_name, :kind_of => String, :name_attribute => true -attribute :version, :kind_of => String -attribute :ruby, :kind_of => String, :default => node['chruby']['default'] -attribute :options, :kind_of => [Hash, String] - -def initialize(*args) - super -end diff --git a/templates/default/chruby.sh.erb b/templates/default/chruby.sh.erb old mode 100644 new mode 100755 index b71cd55..6f96f35 --- a/templates/default/chruby.sh.erb +++ b/templates/default/chruby.sh.erb @@ -1,22 +1,16 @@ -source /usr/local/chruby/share/chruby/chruby.sh +#!/bin/sh +[ -z "$BASH_VERSION" ] && [ -z "$ZSH_VERSION" ] && exit 0 -<% if ::File.exists?("/opt/chef/embedded") -%> -RUBIES+=(/opt/chef/embedded) -<% end -%> +source '<%= node['chruby']['prefix_path'] %>/share/chruby/chruby.sh' -<% if node['chruby']['use_rvm'] && ! Dir["~/.rubies/*"].empty? -%> -RUBIES+=($HOME/.rvm/rubies/*) +<% if node['chruby']['sh_rubies'] -%> +export RUBIES=(<%= node['chruby']['sh_rubies'].join(' ') %>) <% end -%> -<% if node['chruby']['use_rbenv'] && ! Dir["~/.rbenv/versions/*"].empty? -%> -RUBIES+=($HOME/.rbenv/rubies/*) +<% if node['chruby']['sh_auto_switch'] -%> +source '<%= node['chruby']['prefix_path'] %>/share/chruby/auto.sh' <% end -%> -<% if ::File.exists?("/opt/chef/embedded") and node['chruby']['default'] == "embedded" -%> -chruby embedded -<% elsif node['chruby']['default'] -%> -chruby <%= node['chruby']['default'] %> +<% if node['chruby']['sh_default_ruby'] -%> +chruby '<%= node['chruby']['sh_default_ruby'] %>' <% end -%> - -<%= node['chruby']['auto_switch'] && "source /usr/local/chruby/share/chruby/auto.sh" %> -