厨师配方中的编译时间与运行时间 [英] Compile time vs. run time in Chef recipes

查看:64
本文介绍了厨师配方中的编译时间与运行时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下名为Java的(简化的)食谱,用于安装Java。



文件秘诀/default.rb

  include_recipe install_java 

文件配方/install_java.rb

 #通过yum_install库函数从yum repo安装RPM 
yum_install( jdk1.7.0_51 )

#列出/ usr / java中的目录
jdk_dir =`ls -ld /usr/java/jdk1.*排序尾部-1`
如果jdk_dir.empty?
提高缺少JDK安装
结束

厨师客户端-o食谱[java]

 同步菜谱:
-java
编译菜谱...
ls:/usr/java/jdk1.*:没有这样的文件或目录

================================================ ===========================
/ var / chef / cache / cookbooks / java / recipes / default中的
配方编译错误。 rb ================================================= ================

  RuntimeError 
------------
缺少JDK安装

似乎没有调用yum_install()函数。但是,如果我将install_java.rb配方修改为

 #通过yum_install库函数$ b $从yum repo安装RPM b yum_install( jdk1.7.0_51)

它可以工作。



这是为什么?

解决方案

好,所以Chef运行需要两遍。


编译时间


我想将其称为收集阶段

配方中的实际红宝石代码将运行。这意味着任何语句,例如
jdk_dir = ls -ld / usr / java / jdk1。排序tail -1
将会在此时执行。但是,创建Chef资源 yum_install( jdk1.7.0_51)的ruby代码仅创建资源。然后,将由您的配方代码创建的那些资源添加到Chef resource_collection 中,但是这些资源操作尚未运行。


收敛时间


我称此为解决阶段
此时,在所有配方都已运行(创建资源,但未运行操作)之后,我们现在可以实际运行资源操作了。 Chef从 resource_collection 中的第一个资源开始,然后对该资源运行指定的操作。它遍历集合,并根据需要调用通知,直到所有资源的操作都已运行为止。这样您的运行就完成了。


您的特定情况


因此,在您的情况下,您尝试访问集合中的目录阶段,但是直到解决阶段,您才创建目录。如果要在解析阶段中运行ruby代码,则可以在 ruby​​_block 资源中运行。例如:

  ruby​​_block‘验证Java是否存在,如果jdk_dir.empty是
块会执行
吗?
引发缺少JDK安装,重新安装;
结束
结束
结束

如果此 ruby​​_block 资源放在您的 yum_install (可能应该是 yum_package )资源之后,然后它将在 collection_phase 中放置在安装资源之后,然后在Chef运行的解析阶段(即运行时间)中执行。 / p>

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

File recipes/default.rb

include_recipe "install_java"

File recipes/install_java.rb

# 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

When I run the recipe by "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 like the yum_install() function is NOT being called. However, if I modify the install_java.rb recipe to just have

# Install RPM from yum repo via yum_install library function
yum_install("jdk1.7.0_51")

it works.

Why is this?

解决方案

Ok, so Chef runs take two passes.

"Compile time"

I like to call this the collection phase.
At this point, the actual ruby code in your recipe is run. That means any statements like jdk_dir = ls -ld /usr/java/jdk1.* | sort | tail -1 are going to be executed at that point. However, the ruby code that creates a Chef resource yum_install("jdk1.7.0_51") only creates the resources. Those resources, created by your recipe code, are then added to the Chef resource_collection, but the resource actions are NOT run yet.

"Converge Time"

I call this the resolution phase. At this point - after ALL recipes have run (creating resources, but not running actions) - we are now ready to actually run the resource actions. Chef starts with the first resource in the resource_collection and runs the specified action on that resource. It works through the collection, calling notifications as needed, until all resources' actions have been run. Then your run is complete.

Your specific case

So, in your case, you are trying to access the directory in the collection phase but you don't create the directory until the resolution phase. If you want to run ruby code during the resolution phase you can do so in a ruby_block resource. For example:

ruby_block 'verify java is there' do
  block do
    if jdk_dir.empty?
      raise "Missing JDK installation, reinstall"
    end
  end
end

If this ruby_block resource is placed after your yum_install (which should probably be yum_package) resource, then it will get placed after the install resource in the collection_phase, and then executed during the resolution phase (i.e., run time) of the Chef run.

这篇关于厨师配方中的编译时间与运行时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆