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..74f45092 100644 --- a/vendor/cookbooks/rails/recipes/ruby_binaries.rb +++ b/vendor/cookbooks/rails/recipes/ruby_binaries.rb @@ -1,23 +1,23 @@ 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 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 new file mode 100644 index 00000000..a241bdf4 --- /dev/null +++ b/vendor/cookbooks/ruby/attributes/binary.rb @@ -0,0 +1,4 @@ +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..f1d0fff5 --- /dev/null +++ b/vendor/cookbooks/ruby/metadata.rb @@ -0,0 +1,7 @@ +name "ruby" +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..aa09f5ee --- /dev/null +++ b/vendor/cookbooks/ruby/providers/binary.rb @@ -0,0 +1,45 @@ +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 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 + 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 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 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"