Compile time vs. run time in Chef recipes -


i have following (simplified) recipe called java, install java of course.

file recipes/default.rb

include_recipe "install_java" 

file recipes/install_java.rb

# install rpm yum repo via yum_install library function yum_install("jdk1.7.0_51")  # list directories in /usr/java jdk_dir = `ls -ld /usr/java/jdk1.* | sort | tail -1` if jdk_dir.empty?   raise "missing jdk installation" end 

when run recipe "chef-client -o recipe[java]"

synchronizing cookbooks:   - java compiling cookbooks... ls: /usr/java/jdk1.*: no such file or directory 

=========================================================================== recipe compile error in /var/chef/cache/cookbooks/java/recipes/default.rb ===========================================================================

runtimeerror ------------ missing jdk installation 

it seems yum_install() function not being called. however, if modify install_java.rb recipe have

# install rpm yum repo via yum_install library function yum_install("jdk1.7.0_51") 

it works.

why this?

ok, chef runs take 2 passes.

"compile time"

i call collection phase.
@ point, actual ruby code in recipe run. means statements jdk_dir = ls -ld /usr/java/jdk1.* | sort | tail -1 going executed @ point. however, ruby code creates chef resource yum_install("jdk1.7.0_51") creates resources. resources, created recipe code, added chef resource_collection, resource actions not run yet.

"converge time"

i call resolution phase. @ point - after recipes have run (creating resources, not running actions) - ready run resource actions. chef starts first resource in resource_collection , runs specified action on resource. works through collection, calling notifications needed, until resources' actions have been run. run complete.

your specific case

so, in case, trying access directory in collection phase don't create directory until resolution phase. if want run ruby code during resolution phase can in ruby_block resource. example:

ruby_block 'verify java there'   block     if jdk_dir.empty?       raise "missing jdk installation, reinstall"     end   end end 

if ruby_block resource placed after yum_install (which should yum_package) resource, placed after install resource in collection_phase, , executed during resolution phase (i.e., run time) of chef run.


Comments

Popular posts from this blog

php - Submit Form Data without Reloading page -

linux - Rails running on virtual machine in Windows -