Compile time vs. run time in Chef recipes

前端 未结 3 1452
生来不讨喜
生来不讨喜 2020-11-28 11:00

I have the following (simplified) recipe called java, to install Java of course.

File recipes/default.rb

include_recipe \"install_java\"
3条回答
  •  暗喜
    暗喜 (楼主)
    2020-11-28 11:28

    The best thing to do is write a custom resource using unified_mode, instead of a recipe:

    resources/install_java.rb:

    unified_mode true
    provides :install_java
    
    action :install do
      # Install RPM from yum repo via yum_install library function
      yum_install("jdk1.7.0_51")
    
      # List the directories in /usr/java
      jdk_dir = `ls -ld /usr/java/jdk1.* | sort | tail -1`
      if jdk_dir.empty?
        raise "Missing JDK installation"
      end
    end
    

    recipes/default.rb:

    install_java  "install my java -- you could make this name_property the version"
    

    Unified mode eliminates the compile/converge two-phase parsing so that this works, but the code must be moved to a custom resource (which confers additional usability benefits and is a best practice anyway -- now the resource can grow properties and can be reused to install different versions of java, etc).

提交回复
热议问题