Valter Silva
Valter Silva

Reputation: 16656

How to install ruby and bundler via Chef?

I'm new to Chef and I'm trying, for hours now, figure out how to install ruby and install the gem bundler, I'm new to ruby too.

I would like to clone my repository and execute like this:

$ bundle install
$ bundle exec rackup

Simple via Shell scripts, but it seems, for now, very difficult with Chef. Although, I created this recipe:

metadata.rb

depends 'apt'
depends 'git'
# depends 'rbenv' -> # very old version
depends 'ruby_rbenv' # -> new version
depends 'application'
depends 'application_ruby'
depends 'build-essential'

metadata.rb

include_recipe 'git'
include_recipe 'ruby_rbenv::system'
include_recipe 'build-essential'

rbenv_global 'system'

group 'sinatra_app'

user 'sinatra_app' do
  group 'sinatra_app'
  system true
  shell '/bin/bash'
end

# DON'T WORK    
# rbenv_gem "bundler" do
  # action :install
# end

# DON'T WORK
# rbenv_gem "bundler" do
  # ruby_version "2.4.0"
# end

directory '/srv/sinatra-app' do
  owner 'sinatra_app'
  group 'sinatra_app'
  mode '0755'
  recursive true
end

application '/srv/sinatra-app' do
  # Application resource properties.
  owner 'sinatra_app'
  group 'sinatra_app'

  # Subresources, like normal recipe code.
  package 'ruby'
  git "/srv/sinatra-app" do
    repository "git://github.com/tnh/sinatra-app.git"
    reference "master"
    action :sync
  end

  # DON'T WORK
  bundle_install do
    deployment true
  end

end

But I'm not sure if ruby is being installed properly. And also the bundle_install it seems not be working..

And this is my error output:

node1-ubuntu     ================================================================================
node1-ubuntu     Error executing action `install` on resource 'application_bundle_install[/srv/sinatra-app]'
node1-ubuntu     ================================================================================
node1-ubuntu     
node1-ubuntu     Mixlib::ShellOut::ShellCommandFailed
node1-ubuntu     ------------------------------------
node1-ubuntu     Expected process to exit with [0], but received '1'
node1-ubuntu     ---- Begin output of ["/usr/bin/ruby", "/usr/local/bin/bundle", "install", "--deployment"] ----
node1-ubuntu     STDOUT: 
node1-ubuntu     STDERR: /usr/bin/ruby: No such file or directory -- /usr/local/bin/bundle (LoadError)
node1-ubuntu     ---- End output of ["/usr/bin/ruby", "/usr/local/bin/bundle", "install", "--deployment"] ----
node1-ubuntu     Ran ["/usr/bin/ruby", "/usr/local/bin/bundle", "install", "--deployment"] returned 1
node1-ubuntu     
node1-ubuntu     Cookbook Trace:
node1-ubuntu     ---------------
node1-ubuntu     /var/chef/cache/cookbooks/poise-languages/files/halite_gem/poise_languages/command/mixin.rb:207:in `tap'
node1-ubuntu     /var/chef/cache/cookbooks/poise-languages/files/halite_gem/poise_languages/command/mixin.rb:207:in `language_command_shell_out!'
node1-ubuntu     /var/chef/cache/cookbooks/poise-languages/files/halite_gem/poise_languages/command/mixin.rb:222:in `block in language_command_mixin'
node1-ubuntu     /var/chef/cache/cookbooks/poise-ruby/files/halite_gem/poise_ruby/resources/bundle_install.rb:160:in `run_bundler'
node1-ubuntu     /var/chef/cache/cookbooks/poise-ruby/files/halite_gem/poise_ruby/resources/bundle_install.rb:119:in `action_install'
node1-ubuntu     
node1-ubuntu     Resource Declaration:
node1-ubuntu     ---------------------
node1-ubuntu     # In /var/chef/cache/cookbooks/sinatra-app/recipes/default.rb
node1-ubuntu     
node1-ubuntu      68:   bundle_install do
node1-ubuntu      69:     deployment true
node1-ubuntu      70:   end
node1-ubuntu      71:   
node1-ubuntu     
node1-ubuntu     Compiled Resource:
node1-ubuntu     ------------------
node1-ubuntu     # Declared in /var/chef/cache/cookbooks/sinatra-app/recipes/default.rb:68:in `block in from_file'
node1-ubuntu     
node1-ubuntu     application_bundle_install("/srv/sinatra-app") do
node1-ubuntu       action [:install]
node1-ubuntu       retries 0
node1-ubuntu       retry_delay 2
node1-ubuntu       default_guard_interpreter :default
node1-ubuntu       declared_type :application_bundle_install
node1-ubuntu       cookbook_name "sinatra-app"
node1-ubuntu       recipe_name "default"
node1-ubuntu       parent application[/srv/sinatra-app]
node1-ubuntu       deployment true
node1-ubuntu       parent_ruby nil
node1-ubuntu       gem_binary "/usr/bin/gem"
node1-ubuntu       timeout 900
node1-ubuntu       path "/srv/sinatra-app"
node1-ubuntu     end
node1-ubuntu     
node1-ubuntu     Platform:
node1-ubuntu     ---------
node1-ubuntu     x86_64-linux

Any help would be very much appreciated!

Upvotes: 1

Views: 1697

Answers (1)

coderanger
coderanger

Reputation: 54191

You're already using application_ruby which depends on poise-ruby so use that. Check out the docs at https://github.com/poise/poise-ruby but short version just add ruby_runtime 'myapp' to the application block.

You can also check out application_examples for a more detailed example. That's for rails but you can probably figure out how to adapt it for sinatra.

Upvotes: 1

Related Questions