Recent releases of chef from opscode provide a new resource: ruby_block

This is really great news as this allows for interesting problem solving.

Consider you want to do a very special HTTP request. E.g. a GET to localhost but with custom “Host” header information.

Install a Ruby Http Client

First we have to install a Ruby http client library. I’ll use curb in this example. It builds on top of libcurl so lets install that first.

curl_devel = package "curl-devel" do
  action :install
end

curl_devel.run_action(:install)

With this code we tell chef to immediately execute this code. Otherwise we would have to face the problem that we just fullfill the chefrun post-condition that we have curl-devel installed. Next we have to install the curb gem.

curb_gem = gem_package "curb" do
  action :install
end

curb_gem.run_action(:install)

as you see this is done the same way. Next we want to use curb in THIS chef-run. So we have tell chef the update of Gem, followed by requiring curb

Gem.clear_paths

require 'curb'

Ruby Code in Action

Now we are ready to take action.

ruby_block "do-http-request-with-cutom-header" do
  block do
    timeout = 600
    host = "localhost:8080"
    real_host = "my.realhost.com"
    Chef::Log.info "call get on #{host}, maximal request time: #{timeout} seconds"
    c = Curl::Easy.new("http://#{host}/maintenance/do_something") do |curl|
      curl.headers['Host'] = real_host
      curl.verbose = true
      curl.timeout = timeout
    end
    c.perform
    if c.response_code == 200
      Chef::Log.info "GET success! response was:#{c.body_str}"
    else
      Chef::Log.error "GET FAILED. request response was HTTP #{c.response_code}, body: #{c.body_str}"
    end
  end
  action :create
end

Great. Isn’t it? :-)

blog comments powered by Disqus

related Articles:

Deployment of Java Web Applications with Chef from Opscode

There is a rework of the data driven deployment of java web applications. This Vagrant project on github ( https://github.com/iteh/vagrant-demos/ ) showcases the application cookbook for chef with a reworked application definition and LWRP for...

Links der Woche vom 2011-05-23 bis 2011-05-29

Diese Woche Links zu den Themen Windows, Ruby, opscode, JavaScript, maps, Leaflet, WordPress, Chef, windows, powershell, opschef, ios, ruby, devops, sysadmin, monitoringsucks

Links der Woche vom 2011-05-16 bis 2011-05-22

Diese Woche Links zu den Themen coffeebeans, Rails, CoffeeScript, Array, Ruby, Chef, Django, zo, devops, sysadmin, git, chef, CSS3, HTML5, WordPress, nipplegate, MongoDB, rails, rails3, asciicast, test, Android, opschef, Gem, aws, slim, Haml, ruby

Links der Woche vom 2011-05-09 bis 2011-05-15

Diese Woche Links zu den Themen wi, opschef, ActiveAdmin, rails, ror, ruby, HTML5, Chef, cnet, MongoDB, Sinatra, Rack, apple, git, aws, chef, opscode, sysadmin, Ksplice, noSQL, RubyTutorials, CSS3, EC2, AWS, MacRuby, iOS, m

Links der Woche vom 2011-05-02 bis 2011-05-08

Diese Woche Links zu den Themen rails, rack, torquebox, jruby, oreilly, opschef, sh, doc, shell, AWS, veewee, devops, mcollective

Links der Woche vom 2011-04-25 bis 2011-05-01

Diese Woche Links zu den Themen op, oschef, opscode, LWRP, Noah, chef, devops, opschef, rails, gem, railscast, Chef

Links der Woche vom 2011-04-11 bis 2011-04-17

Diese Woche Links zu den Themen ipad, iphone, opschef, ruby, OpenCV, detection, image, Weinberg, Ohai, automation, pr, test, gem, cloudfoundry, devops, cfoundry

Data Driven Deployment of a Java Web Application with Chef Solo

Chef Application Cookbook Showcase This Vagrant project on github ( https://github.com/iteh/vagrant-demos/ ) showcases the application cookbook for chef

Links der Woche vom 2011-04-04 bis 2011-04-10

Diese Woche Links zu den Themen Pow, Rack, ActiveRecord, Rails, Algorithms, Ruby, rails, optimize, CSS3, railscasts, chat, macbook, opschef, ruby

Links der Woche vom 2011-03-28 bis 2011-04-03

Diese Woche Links zu den Themen design, utilities, rails, asciicast, gem, ux, Awesome, jQuery, devops, Git, radiantcms, rvm, opschef, osx, MacRuby, chef, opscode, Rails, i18n, java, Vagrant, automation, Typography, Type