From 9221dd4eb92d5f37ede407676817009652807659 Mon Sep 17 00:00:00 2001 From: Oleksandr Iuzikov Date: Mon, 9 Feb 2015 15:49:02 +0200 Subject: [PATCH 1/3] Added "ruby" cookbook for maintaining binary ruby distribution --- Cheffile | 1 + Cheffile.lock | 6 +++ Gemfile | 1 + Gemfile.lock | 8 ++++ roles/rails_passenger.rb | 3 +- .../cookbooks/rails/recipes/ruby_binaries.rb | 25 +++++------ vendor/cookbooks/ruby/attributes/binary.rb | 5 +++ vendor/cookbooks/ruby/metadata.rb | 7 +++ vendor/cookbooks/ruby/providers/binary.rb | 44 +++++++++++++++++++ vendor/cookbooks/ruby/resources/binary.rb | 5 +++ .../debian/ubuntu_1404_binary_spec.rb | 9 ++++ vendor/cookbooks/ruby/spec/spec_helper.rb | 1 + 12 files changed, 100 insertions(+), 15 deletions(-) create mode 100644 vendor/cookbooks/ruby/attributes/binary.rb create mode 100644 vendor/cookbooks/ruby/metadata.rb create mode 100644 vendor/cookbooks/ruby/providers/binary.rb create mode 100644 vendor/cookbooks/ruby/resources/binary.rb create mode 100644 vendor/cookbooks/ruby/spec/ruby_binary/debian/ubuntu_1404_binary_spec.rb create mode 100644 vendor/cookbooks/ruby/spec/spec_helper.rb diff --git a/Cheffile b/Cheffile index 56b4d5c7..208e57df 100644 --- a/Cheffile +++ b/Cheffile @@ -16,3 +16,4 @@ cookbook "rails", path: "vendor/cookbooks/rails" cookbook "ssh_deploy_keys", path: "vendor/cookbooks/ssh_deploy_keys" cookbook "backups", path: "vendor/cookbooks/backups" cookbook "sysadmins", path: "vendor/cookbooks/sysadmins" +cookbook "ruby", path: "vendor/cookbooks/ruby" diff --git a/Cheffile.lock b/Cheffile.lock index 58750d84..ae646b6e 100644 --- a/Cheffile.lock +++ b/Cheffile.lock @@ -84,6 +84,11 @@ PATH nginx (>= 0.0.0) sudo (> 1.2.0) +PATH + remote: vendor/cookbooks/ruby + specs: + ruby (0.1.0) + PATH remote: vendor/cookbooks/ssh_deploy_keys specs: @@ -104,6 +109,7 @@ DEPENDENCIES postgresql (~> 3.4.1) rails (>= 0) rbenv (~> 1.7.1) + ruby (>= 0) ruby_build (~> 0.8.0) ssh_deploy_keys (>= 0) sudo (~> 2.7.0) diff --git a/Gemfile b/Gemfile index 88c531bb..82159f64 100644 --- a/Gemfile +++ b/Gemfile @@ -10,3 +10,4 @@ gem "kitchen-vagrant" gem "kitchen-digitalocean" gem "serverspec" gem "rspec" +gem "chefspec" diff --git a/Gemfile.lock b/Gemfile.lock index 39ee0fc5..4eebd252 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -35,6 +35,10 @@ GEM json mixlib-log (~> 1.3) rack + chefspec (4.2.0) + chef (>= 11.14) + fauxhai (~> 2.0) + rspec (~> 3.0) coderay (1.1.0) coercible (1.0.0) descendants_tracker (~> 0.0.1) @@ -49,6 +53,9 @@ GEM erubis (2.7.0) faraday (0.9.0) multipart-post (>= 1.2, < 3) + fauxhai (2.2.0) + net-ssh + ohai ffi (1.9.5) ffi-yajl (1.1.0) ffi (~> 1.5) @@ -167,6 +174,7 @@ PLATFORMS DEPENDENCIES chef (~> 11.16.2) + chefspec kitchen-digitalocean kitchen-vagrant knife-solo (~> 0.4.1) diff --git a/roles/rails_passenger.rb b/roles/rails_passenger.rb index e99563ad..e3ada81e 100644 --- a/roles/rails_passenger.rb +++ b/roles/rails_passenger.rb @@ -10,7 +10,8 @@ "recipe[rails::env_vars]", "recipe[git]", "recipe[ssh_deploy_keys]", - "recipe[postfix]" + "recipe[postfix]", + "recipe[ruby]" ) default_attributes( diff --git a/vendor/cookbooks/rails/recipes/ruby_binaries.rb b/vendor/cookbooks/rails/recipes/ruby_binaries.rb index 22f73fc1..816730f2 100644 --- a/vendor/cookbooks/rails/recipes/ruby_binaries.rb +++ b/vendor/cookbooks/rails/recipes/ruby_binaries.rb @@ -1,23 +1,20 @@ applications_root = node[:rails][:applications_root] available_binaries = node[:rbenv][:available_binaries] -kernel_architecture = node[:kernel][:machine] if node[:active_applications] node[:active_applications].each do |app, app_info| if app_info[:ruby_version] if available_binaries.include? app_info[:ruby_version] - ruby_version = app_info[:ruby_version] - ruby_binary = "ruby-#{ruby_version}.tar.bz2" - execute "Install ruby #{app_info[:ruby_version]} binaries" do - ruby = app_info[:ruby_version] - user node[:rbenv][:user] - group node[:rbenv][:group] - cwd "#{node[:rbenv][:root_path]}/versions" - command <<-EOM - wget #{node[:rbenv][:binaries_url]}/#{node["platform_version"]}/#{kernel_architecture}/#{ruby_binary}; - tar jxf #{ruby_binary} - rm #{ruby_binary} - EOM - not_if { File.directory?(File.join('opt', 'rbenv', 'versions', ruby_version)) } + version, rbenv_versions_path = app_info[:ruby_version], "/opt/rbenv/versions" + + ruby_binary version do + path rbenv_versions_path + not_if { File.directory? "#{rbenv_versions_path}/#{version}" } + end + + ruby_build_ruby version do + prefix_path "#{rbenv_versions_path}/#{version}" + subscribes :install, "ruby_binary[#{version}]", :immediately + not_if { node[:ruby][version][:installed] } end end end diff --git a/vendor/cookbooks/ruby/attributes/binary.rb b/vendor/cookbooks/ruby/attributes/binary.rb new file mode 100644 index 00000000..2ba6ac95 --- /dev/null +++ b/vendor/cookbooks/ruby/attributes/binary.rb @@ -0,0 +1,5 @@ +case node["platform_family"] +when 'debian' + default["ruby"]["download_url"] = "http://binaries.intercityup.com/ruby/ubuntu" +end + diff --git a/vendor/cookbooks/ruby/metadata.rb b/vendor/cookbooks/ruby/metadata.rb new file mode 100644 index 00000000..813fe246 --- /dev/null +++ b/vendor/cookbooks/ruby/metadata.rb @@ -0,0 +1,7 @@ +name "rails" +maintainer "Michiel Sikkes" +maintainer_email "michiel@firmhouse.com" +license "MIT" +description "Installs/Configures ruby" +long_description "Installs/Configures ruby" +version "0.1.0" diff --git a/vendor/cookbooks/ruby/providers/binary.rb b/vendor/cookbooks/ruby/providers/binary.rb new file mode 100644 index 00000000..d7534422 --- /dev/null +++ b/vendor/cookbooks/ruby/providers/binary.rb @@ -0,0 +1,44 @@ +def whyrun_supported? + true +end + +use_inline_resources + +# +# Name parameter: ruby version, for ex. "2.2.0" +# Path parameter: where new ruby should be installed +# +action :install do + version = @new_resource.version + path = @new_resource.path + download_url = "#{node[:ruby][:download_url]}/#{node[:platform_version]}/#{node[:kernel][:machine]}/ruby-#{version}.tar.bz2" + downloaded_ruby = "#{Chef::Config[:file_cache_path]}/#{::File.basename download_url}" + + Chef::Log.info "Installing ruby binaries..." + Chef::Log.debug "version: #{version}" + Chef::Log.debug "installation path: #{path}" + Chef::Log.debug "download from: #{download_url}" + Chef::Log.debug "save ruby to file: #{downloaded_ruby}" + + begin + remote_file downloaded_ruby do + source download_url + "abc" + action :nothing + end.run_action :create + + bash "extract ruby binary" do + cwd path + code <<-EOH + mkdir -p #{path} + tar jxf #{downloaded_ruby} -C #{path} + EOH + only_if { ::File.exists? downloaded_ruby } + end + + Chef::Log.debug "extracted to #{path}" + node.default[:ruby][version][:installed] = true + rescue Exception => e + Chef::Log.warn "Ruby installation failed, cause: #{e}" + node.default[:ruby][version][:installed] = false + end +end diff --git a/vendor/cookbooks/ruby/resources/binary.rb b/vendor/cookbooks/ruby/resources/binary.rb new file mode 100644 index 00000000..f3f763ba --- /dev/null +++ b/vendor/cookbooks/ruby/resources/binary.rb @@ -0,0 +1,5 @@ +actions :install +default_action :install + +attribute :version, kind_of: String, name_attribute: true +attribute :path, kind_of: String diff --git a/vendor/cookbooks/ruby/spec/ruby_binary/debian/ubuntu_1404_binary_spec.rb b/vendor/cookbooks/ruby/spec/ruby_binary/debian/ubuntu_1404_binary_spec.rb new file mode 100644 index 00000000..a5ae00d9 --- /dev/null +++ b/vendor/cookbooks/ruby/spec/ruby_binary/debian/ubuntu_1404_binary_spec.rb @@ -0,0 +1,9 @@ +require "spec_helper" + +describe "ruby::default" do + let(:chef_run) { ChefSpec::SoloRunner.converge(described_recipe) } + + it "installs ruby" do + expect(chef_run).to install_package("foo") + end +end diff --git a/vendor/cookbooks/ruby/spec/spec_helper.rb b/vendor/cookbooks/ruby/spec/spec_helper.rb new file mode 100644 index 00000000..18142d9a --- /dev/null +++ b/vendor/cookbooks/ruby/spec/spec_helper.rb @@ -0,0 +1 @@ +require "chefspec" From f1853265475fd44c602bde662ec23871723776f8 Mon Sep 17 00:00:00 2001 From: Oleksandr Iuzikov Date: Mon, 9 Feb 2015 17:19:21 +0200 Subject: [PATCH 2/3] Fixed HoundCI --- vendor/cookbooks/rails/recipes/ruby_binaries.rb | 9 ++++++--- vendor/cookbooks/ruby/attributes/binary.rb | 5 ++--- vendor/cookbooks/ruby/metadata.rb | 10 +++++----- vendor/cookbooks/ruby/providers/binary.rb | 11 ++++++----- vendor/cookbooks/ruby/recipes/default.rb | 0 vendor/cookbooks/ruby/resources/binary.rb | 2 +- 6 files changed, 20 insertions(+), 17 deletions(-) create mode 100644 vendor/cookbooks/ruby/recipes/default.rb diff --git a/vendor/cookbooks/rails/recipes/ruby_binaries.rb b/vendor/cookbooks/rails/recipes/ruby_binaries.rb index 816730f2..74f45092 100644 --- a/vendor/cookbooks/rails/recipes/ruby_binaries.rb +++ b/vendor/cookbooks/rails/recipes/ruby_binaries.rb @@ -5,16 +5,19 @@ if app_info[:ruby_version] if available_binaries.include? app_info[:ruby_version] version, rbenv_versions_path = app_info[:ruby_version], "/opt/rbenv/versions" - + ruby_binary version do - path rbenv_versions_path + path rbenv_versions_path not_if { File.directory? "#{rbenv_versions_path}/#{version}" } end ruby_build_ruby version do prefix_path "#{rbenv_versions_path}/#{version}" subscribes :install, "ruby_binary[#{version}]", :immediately - not_if { node[:ruby][version][:installed] } + not_if do + File.directory?("#{rbenv_versions_path}/#{version}") || + node[:ruby][version][:installed] + end end end end diff --git a/vendor/cookbooks/ruby/attributes/binary.rb b/vendor/cookbooks/ruby/attributes/binary.rb index 2ba6ac95..3d2cc9ba 100644 --- a/vendor/cookbooks/ruby/attributes/binary.rb +++ b/vendor/cookbooks/ruby/attributes/binary.rb @@ -1,5 +1,4 @@ case node["platform_family"] -when 'debian' +when "debian" default["ruby"]["download_url"] = "http://binaries.intercityup.com/ruby/ubuntu" -end - +end \ No newline at end of file diff --git a/vendor/cookbooks/ruby/metadata.rb b/vendor/cookbooks/ruby/metadata.rb index 813fe246..f1d0fff5 100644 --- a/vendor/cookbooks/ruby/metadata.rb +++ b/vendor/cookbooks/ruby/metadata.rb @@ -1,7 +1,7 @@ -name "rails" -maintainer "Michiel Sikkes" +name "ruby" +maintainer "Michiel Sikkes" maintainer_email "michiel@firmhouse.com" -license "MIT" -description "Installs/Configures ruby" +license "MIT" +description "Installs/Configures ruby" long_description "Installs/Configures ruby" -version "0.1.0" +version "0.1.0" diff --git a/vendor/cookbooks/ruby/providers/binary.rb b/vendor/cookbooks/ruby/providers/binary.rb index d7534422..e6c12a90 100644 --- a/vendor/cookbooks/ruby/providers/binary.rb +++ b/vendor/cookbooks/ruby/providers/binary.rb @@ -14,15 +14,17 @@ def whyrun_supported? download_url = "#{node[:ruby][:download_url]}/#{node[:platform_version]}/#{node[:kernel][:machine]}/ruby-#{version}.tar.bz2" downloaded_ruby = "#{Chef::Config[:file_cache_path]}/#{::File.basename download_url}" - Chef::Log.info "Installing ruby binaries..." + Chef::Log.info "Installing ruby binary #{version} ..." Chef::Log.debug "version: #{version}" Chef::Log.debug "installation path: #{path}" Chef::Log.debug "download from: #{download_url}" Chef::Log.debug "save ruby to file: #{downloaded_ruby}" + node.default[:ruby][version][:installed] = false + begin remote_file downloaded_ruby do - source download_url + "abc" + source download_url action :nothing end.run_action :create @@ -37,8 +39,7 @@ def whyrun_supported? Chef::Log.debug "extracted to #{path}" node.default[:ruby][version][:installed] = true - rescue Exception => e - Chef::Log.warn "Ruby installation failed, cause: #{e}" - node.default[:ruby][version][:installed] = false + rescue Net::HTTPServerException => e + Chef::Log.warn "Binary ruby download failed, error: #{e}" end end diff --git a/vendor/cookbooks/ruby/recipes/default.rb b/vendor/cookbooks/ruby/recipes/default.rb new file mode 100644 index 00000000..e69de29b diff --git a/vendor/cookbooks/ruby/resources/binary.rb b/vendor/cookbooks/ruby/resources/binary.rb index f3f763ba..444a550d 100644 --- a/vendor/cookbooks/ruby/resources/binary.rb +++ b/vendor/cookbooks/ruby/resources/binary.rb @@ -2,4 +2,4 @@ default_action :install attribute :version, kind_of: String, name_attribute: true -attribute :path, kind_of: String +attribute :path, kind_of: String \ No newline at end of file From 5eb922a7ac14eda3387600a9c7b14d8bb89e0ede Mon Sep 17 00:00:00 2001 From: Oleksandr Iuzikov Date: Mon, 9 Feb 2015 17:24:15 +0200 Subject: [PATCH 3/3] HoundCI --- vendor/cookbooks/ruby/attributes/binary.rb | 2 +- vendor/cookbooks/ruby/providers/binary.rb | 2 +- vendor/cookbooks/ruby/resources/binary.rb | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/vendor/cookbooks/ruby/attributes/binary.rb b/vendor/cookbooks/ruby/attributes/binary.rb index 3d2cc9ba..a241bdf4 100644 --- a/vendor/cookbooks/ruby/attributes/binary.rb +++ b/vendor/cookbooks/ruby/attributes/binary.rb @@ -1,4 +1,4 @@ case node["platform_family"] when "debian" default["ruby"]["download_url"] = "http://binaries.intercityup.com/ruby/ubuntu" -end \ No newline at end of file +end diff --git a/vendor/cookbooks/ruby/providers/binary.rb b/vendor/cookbooks/ruby/providers/binary.rb index e6c12a90..aa09f5ee 100644 --- a/vendor/cookbooks/ruby/providers/binary.rb +++ b/vendor/cookbooks/ruby/providers/binary.rb @@ -29,7 +29,7 @@ def whyrun_supported? end.run_action :create bash "extract ruby binary" do - cwd path + cwd path code <<-EOH mkdir -p #{path} tar jxf #{downloaded_ruby} -C #{path} diff --git a/vendor/cookbooks/ruby/resources/binary.rb b/vendor/cookbooks/ruby/resources/binary.rb index 444a550d..f3f763ba 100644 --- a/vendor/cookbooks/ruby/resources/binary.rb +++ b/vendor/cookbooks/ruby/resources/binary.rb @@ -2,4 +2,4 @@ default_action :install attribute :version, kind_of: String, name_attribute: true -attribute :path, kind_of: String \ No newline at end of file +attribute :path, kind_of: String