Monday, 30 April 2012

A "Hello, World" from File by Chef

First there was "Hello, World", Chef-style. Then again with attributes. Now to complete this short trilogy, we add some File interaction into the mix, deploying a text file in a step-by-step style...


Once again, start by configuring an exercise for the new recipe. Create config/from-hello_world-run-cookbook_file.json with
{"run_list": [ "recipe[hello_world::cookbook_file]" ]}
Then add this recipe to cookbooks/hello_world/recipes/cookbook_file.rb
#
# For development, use a working directory within the workspace
#
home = File.expand_path(File.join(File.dirname(__FILE__), '..', "tmp"))

#
# Directory is a specialised resource.
#
# See http://wiki.opscode.com/display/chef/Resources#Resources-Directory 
#
# Ensure that the working directory exists 
directory home do
  action :create
end

#
# Deploy the text file
# 
cookbook_file "#{home}/hello.txt" do
 source "hello_world.txt"
end
As before, use for development:
$ sudo chef-solo --config config/chef-solo-config.rb --json-attributes config/from-hello_world-run-cookbook_file.json
[...] INFO: *** Chef 0.10.8 ***
[...] INFO: Setting the run_list to ["recipe[hello_world::cookbook_file]"] from JSON
[...] INFO: Run List is [recipe[hello_world::cookbook_file]]
[...] INFO: Run List expands to [hello_world::cookbook_file]
[...] INFO: Starting Chef Run for jorum2.fishdelish.local
[...] INFO: Running start handlers
[...] INFO: Start handlers complete.
[...] INFO: Processing log[See home /home/rob/chef-workspace/hello/cookbooks/hello_world/tmp for results] action write (hello_world::cookbook_file line 4)
[...] INFO: See home /home/rob/chef-workspace/hello/cookbooks/hello_world/tmp for results
[...] INFO: Processing directory[/home/rob/chef-workspace/hello/cookbooks/hello_world/tmp] action create (hello_world::cookbook_file line 12)
[...] INFO: Processing cookbook_file[/home/rob/chef-workspace/hello/cookbooks/hello_world/tmp/hello.txt] action create (hello_world::cookbook_file line 17)
[...] ERROR: cookbook_file[/home/rob/chef-workspace/hello/cookbooks/hello_world/tmp/hello.txt] (hello_world::cookbook_file line 17) has had an error
[...] ERROR: cookbook_file[/home/rob/chef-workspace/hello/cookbooks/hello_world/tmp/hello.txt] (/home/rob/chef-workspace/hello/cookbooks/hello_world/recipes/cookbook_file.rb:17:in `from_file') had an error:
cookbook_file[/home/rob/chef-workspace/hello/cookbooks/hello_world/tmp/hello.txt] (hello_world::cookbook_file line 17) had an error: Chef::Exceptions::FileNotFound: Cookbook 'hello_world' (0.0.0) does not contain a file at any of these locations:
  files/ubuntu-11.10/hello_world.txt
  files/ubuntu/hello_world.txt
  files/default/hello_world.txt
/usr/lib/ruby/vendor_ruby/chef/cookbook_version.rb:647:in `preferred_manifest_record'
/usr/lib/ruby/vendor_ruby/chef/cookbook_version.rb:655:in `preferred_filename_on_disk_location'
/usr/lib/ruby/vendor_ruby/chef/provider/cookbook_file.rb:64:in `file_cache_location'
/usr/lib/ruby/vendor_ruby/chef/provider/cookbook_file.rb:36:in `action_create'
/usr/lib/ruby/vendor_ruby/chef/resource.rb:440:in `send'
/usr/lib/ruby/vendor_ruby/chef/resource.rb:440:in `run_action'
/usr/lib/ruby/vendor_ruby/chef/runner.rb:45:in `run_action'
/usr/lib/ruby/vendor_ruby/chef/runner.rb:81:in `converge'
/usr/lib/ruby/vendor_ruby/chef/runner.rb:81:in `each'
/usr/lib/ruby/vendor_ruby/chef/runner.rb:81:in `converge'
/usr/lib/ruby/vendor_ruby/chef/resource_collection.rb:94:in `execute_each_resource'
/usr/lib/ruby/vendor_ruby/chef/resource_collection/stepable_iterator.rb:116:in `call'
/usr/lib/ruby/vendor_ruby/chef/resource_collection/stepable_iterator.rb:116:in `call_iterator_block'
/usr/lib/ruby/vendor_ruby/chef/resource_collection/stepable_iterator.rb:85:in `step'
/usr/lib/ruby/vendor_ruby/chef/resource_collection/stepable_iterator.rb:104:in `iterate'
/usr/lib/ruby/vendor_ruby/chef/resource_collection/stepable_iterator.rb:55:in `each_with_index'
/usr/lib/ruby/vendor_ruby/chef/resource_collection.rb:92:in `execute_each_resource'
/usr/lib/ruby/vendor_ruby/chef/runner.rb:76:in `converge'
/usr/lib/ruby/vendor_ruby/chef/client.rb:312:in `converge'
/usr/lib/ruby/vendor_ruby/chef/client.rb:160:in `run'
/usr/lib/ruby/vendor_ruby/chef/application/solo.rb:192:in `run_application'
/usr/lib/ruby/vendor_ruby/chef/application/solo.rb:183:in `loop'
/usr/lib/ruby/vendor_ruby/chef/application/solo.rb:183:in `run_application'
/usr/lib/ruby/vendor_ruby/chef/application.rb:67:in `run'
/usr/bin/chef-solo:24
[...] ERROR: Running exception handlers
[...] ERROR: Exception handlers complete
[...] FATAL: Stacktrace dumped to /var/chef/cache/chef-stacktrace.out
[...] FATAL: Chef::Exceptions::FileNotFound: cookbook_file[/home/rob/chef-workspace/hello/cookbooks/hello_world/tmp/hello.txt] (hello_world::cookbook_file line 17) had an error: Chef::Exceptions::FileNotFound: Cookbook 'hello_world' (0.0.0) does not contain a file at any of these locations:
  files/ubuntu-11.10/hello_world.txt
  files/ubuntu/hello_world.txt
  files/default/hello_world.txt
Chef helpfully prints the places checked for the missing file, illustrating how to override a resource for a particular platform. More on this later, but begin by creating the platform independent default cookbooks/hello_world/files/default/hello_world.txt with
Hello, World! (Platform independent default)
(perhaps after creating the cookbooks/hello_world/files/default directory).
Now run
$ sudo chef-solo --config config/chef-solo-config.rb --json-attributes config/from-hello_world-run-cookbook_file.json 
[...] INFO: *** Chef 0.10.8 ***
[...] INFO: Setting the run_list to ["recipe[hello_world::cookbook_file]"] from JSON
[...] INFO: Run List is [recipe[hello_world::cookbook_file]]
[...] INFO: Run List expands to [hello_world::cookbook_file]
[...] INFO: Starting Chef Run for jorum2.fishdelish.local
[...] INFO: Running start handlers
[...] INFO: Start handlers complete.
[...] INFO: Processing log[See home /home/rob/chef-workspace/hello/tmp for results] action write (hello_world::cookbook_file line 4)
[...] INFO: See home /home/rob/chef-workspace/hello/tmp for results
[...] INFO: Processing directory[/home/rob/chef-workspace/hello/tmp] action create (hello_world::cookbook_file line 12)
[...] INFO: directory[/home/rob/chef-workspace/hello/tmp] created directory /home/rob/chef-workspace/hello/tmp
[...] INFO: Processing cookbook_file[/home/rob/chef-workspace/hello/tmp/hello.txt] action create (hello_world::cookbook_file line 17)
[...] INFO: cookbook_file[/home/rob/chef-workspace/hello/tmp/hello.txt] created file /home/rob/chef-workspace/hello/tmp/hello.txt
[...] INFO: Chef Run complete in 0.002842 seconds
[...] INFO: Running report handlers
[...] INFO: Report handlers complete
and take a look:
$ sudo cat tmp/hello.txt 
Hello, World! (Platform independent default)
Chef has deployed our platform independent default.
To override this by a file specific to your platform, take a look at the search path printed above. My platform is ubuntu (and — more specifically — ubuntu-11.10) so I'll create cookbooks/hello_world/files/ubuntu/hello_world.txt but you need to replace ubuntu with your platform. Wherever the file needs to be, fill it with:
Hello, World! (Platform specific)
Now when we run
$ sudo chef-solo --config config/chef-solo-config.rb --json-attributes config/from-hello_world-run-cookbook_file.json 
[...] INFO: *** Chef 0.10.8 ***
[...] INFO: Setting the run_list to ["recipe[hello_world::cookbook_file]"] from JSON
[...] INFO: Run List is [recipe[hello_world::cookbook_file]]
[...] INFO: Run List expands to [hello_world::cookbook_file]
[...] INFO: Starting Chef Run for jorum2.fishdelish.local
[...] INFO: Running start handlers
[...] INFO: Start handlers complete.
[...] INFO: Processing log[See home /home/rob/chef-workspace/hello/tmp for results] action write (hello_world::cookbook_file line 4)
[...] INFO: See home /home/rob/chef-workspace/hello/tmp for results
[...] INFO: Processing directory[/home/rob/chef-workspace/hello/tmp] action create (hello_world::cookbook_file line 12)
[...] INFO: Processing cookbook_file[/home/rob/chef-workspace/hello/tmp/hello.txt] action create (hello_world::cookbook_file line 17)
[...] INFO: cookbook_file[/home/rob/chef-workspace/hello/tmp/hello.txt] backed up to /var/chef/backup/home/rob/chef-workspace/hello/tmp/hello.txt.chef-20120327162705
[...] INFO: cookbook_file[/home/rob/chef-workspace/hello/tmp/hello.txt] created file /home/rob/chef-workspace/hello/tmp/hello.txt
[...] INFO: Chef Run complete in 0.007998 seconds
[...] INFO: Running report handlers
[...] INFO: Report handlers complete
Chef deploys the platform specific version
$ sudo cat tmp/hello.txt 
Hello, World! (Platform specific)
overriding the default.

3 comments:

  1. Blog seems to be very informative and interesting. It is great pleasure to get a chance to comment on such a nice blog. Thanks for share and keep it up.
    Personal Chef Service New York

    ReplyDelete


  2. For a long time, I did not get pleasure from as well filed new information, thanks!
    Richard Brown best virtual data rooms

    ReplyDelete
  3. Information on the interest can be transferred directly from the computers of other users within the same network.
    https://downloadshareitapp.com

    ReplyDelete