Skip to content

Instantly share code, notes, and snippets.

@cnk
Created July 10, 2015 00:11
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cnk/e5fa8cafea8c2953cf91 to your computer and use it in GitHub Desktop.
Save cnk/e5fa8cafea8c2953cf91 to your computer and use it in GitHub Desktop.
Example code from Chapter 7 (Library Example 1: Modules and Mixins) of Customizing Ruby
$ chef-client --once --local-mode --config /\
Users/cnk/Code/sandbox/customizing_chef/part3_examples/solo.rb --override-runlist testcookbook::default
Starting Chef Client, version 12.3.0
[2015-07-09T17:08:42-07:00] WARN: Run List override has been provided.
[2015-07-09T17:08:42-07:00] WARN: Original Run List: []
[2015-07-09T17:08:42-07:00] WARN: Overridden Run List: [recipe[testcookbook::default]]
resolving cookbooks for run list: ["testcookbook::default"]
Synchronizing Cookbooks:
- testcookbook
Compiling Cookbooks...
puts from the top of the UsefulMethods file
[2015-07-09T17:08:42-07:00] WARN: Chef::Log warning at the top of the UsefulMethods file
This is a puts from the top of the default recipe; node info: bar
[2015-07-09T17:08:42-07:00] WARN: You can log node info bar from a recipe using 'Chef::Log'
puts from inside a definition in the UsefulMethods file
[2015-07-09T17:08:42-07:00] WARN: Chef::Log warning from inside a definition in the UsefulMethods file
this is a puts inside a coditional in the default recipe - in else
[2015-07-09T17:08:42-07:00] WARN: No stop file found. Continuing...
Converging 3 resources
Recipe: testcookbook::default
* log[this logged node info bar from a 'log' command] action write
* log[something else] action write
* ruby_block[testing a ruby block] action run
This is a puts inside a ruby block in recipes/default.rb
[2015-07-09T17:08:42-07:00] WARN: This is a Chef::Log.warn inside a ruby block in recipes/default.rb
- execute the ruby block testing a ruby block
[2015-07-09T17:08:42-07:00] WARN: Skipping final node save because override_runlist was given
Running handlers:
Running handlers complete
Chef Client finished, 3/3 resources updated in 1.79505 seconds
module UsefulMethods
puts "puts from the top of the UsefulMethods file"
Chef::Log.warn('Chef::Log warning at the top of the UsefulMethods file')
def stop_file_exists?
puts "puts from inside a definition in the UsefulMethods file"
Chef::Log.warn('Chef::Log warning from inside a definition in the UsefulMethods file')
::File.exists?("/tmp/stop_chef")
end
end
puts "This is a puts from the top of the default recipe; node info: #{node['foo']} "
Chef::Log.warn("You can log node info #{node['foo']} from a recipe using 'Chef::Log'")
log "this logged node info #{node['foo']} from a 'log' command"
::Chef::Recipe.send(:include, UsefulMethods)
if stop_file_exists?
puts "this is a puts inside a coditional in the default recipe - in if"
Chef::Log.fatal("Found a stop file. Stopping NOW!")
log "something if"
exit 1
else
puts "this is a puts inside a coditional in the default recipe - in else"
Chef::Log.warn("No stop file found. Continuing...")
log "something else"
end
ruby_block "testing a ruby block" do
block do
puts "\n This is a puts inside a ruby block in recipes/default.rb"
Chef::Log.warn("This is a Chef::Log.warn inside a ruby block in recipes/default.rb")
end
action :run
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment