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.
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'
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? :-)
Interested in Cloud or Chef Trainings? Have a look at our Commandemy Trainings page. Need help migrating to the cloud? Check out Infralovers.
comments powered by Disqus