Skip to content
This repository has been archived by the owner on Apr 23, 2019. It is now read-only.

Added "ruby" cookbook for maintaining binary ruby distribution #172

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cheffile
Original file line number Diff line number Diff line change
Expand Up @@ -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"
6 changes: 6 additions & 0 deletions Cheffile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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)
Expand Down
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,4 @@ gem "kitchen-vagrant"
gem "kitchen-digitalocean"
gem "serverspec"
gem "rspec"
gem "chefspec"
8 changes: 8 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand Down Expand Up @@ -167,6 +174,7 @@ PLATFORMS

DEPENDENCIES
chef (~> 11.16.2)
chefspec
kitchen-digitalocean
kitchen-vagrant
knife-solo (~> 0.4.1)
Expand Down
3 changes: 2 additions & 1 deletion roles/rails_passenger.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
"recipe[rails::env_vars]",
"recipe[git]",
"recipe[ssh_deploy_keys]",
"recipe[postfix]"
"recipe[postfix]",
"recipe[ruby]"
)

default_attributes(
Expand Down
25 changes: 11 additions & 14 deletions vendor/cookbooks/rails/recipes/ruby_binaries.rb
Original file line number Diff line number Diff line change
@@ -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"

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Trailing whitespace detected.

ruby_binary version do
path rbenv_versions_path

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Trailing whitespace detected.

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
Expand Down
5 changes: 5 additions & 0 deletions vendor/cookbooks/ruby/attributes/binary.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
case node["platform_family"]
when 'debian'

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Prefer double-quoted strings unless you need single quotes to avoid extra backslashes for escaping.

default["ruby"]["download_url"] = "http://binaries.intercityup.com/ruby/ubuntu"
end

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

1 trailing blank lines detected.

7 changes: 7 additions & 0 deletions vendor/cookbooks/ruby/metadata.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
name "rails"

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Put one space between the method name and the first argument.

maintainer "Michiel Sikkes"

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Put one space between the method name and the first argument.

maintainer_email "[email protected]"
license "MIT"

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Put one space between the method name and the first argument.

description "Installs/Configures ruby"

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Put one space between the method name and the first argument.

long_description "Installs/Configures ruby"
version "0.1.0"

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Put one space between the method name and the first argument.

44 changes: 44 additions & 0 deletions vendor/cookbooks/ruby/providers/binary.rb
Original file line number Diff line number Diff line change
@@ -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

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Trailing whitespace detected.

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

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Avoid rescuing the Exception class.

Chef::Log.warn "Ruby installation failed, cause: #{e}"
node.default[:ruby][version][:installed] = false
end
end
5 changes: 5 additions & 0 deletions vendor/cookbooks/ruby/resources/binary.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
actions :install
default_action :install

attribute :version, kind_of: String, name_attribute: true
attribute :path, kind_of: String
Original file line number Diff line number Diff line change
@@ -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")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we need to actually test something here, right? :)

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes we do. I'm adding some meaningful tests right now

end
end
1 change: 1 addition & 0 deletions vendor/cookbooks/ruby/spec/spec_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
require "chefspec"