小编Xia*_*ing的帖子

如何在厨师ruby_block中要求我的库

我正在开发一本食谱来部署一个简单的ROR应用程序.我写了一个app_helper.rb并把它放到我的cookbook的libraries目录中,这里是内容:

module AppHelper

    def self.find_gem
      if File.exists?("/usr/local/rvm/bin/rvm")
        return `/usr/local/rvm/bin/rvm default exec which gem`.chomp
      else
        return "/usr/bin/gem"
      end
    end
  end
Run Code Online (Sandbox Code Playgroud)

在recipes/default.rb中,我将上面的模块混合到Chef :: Recipe类中

class Chef::Recipe
  include AppHelper
end
Run Code Online (Sandbox Code Playgroud)

如您所知,可以从配方的任何位置调用find_gem函数.

当我试图在我的ruby_block中使用find_gem函数时,如下所示:

ruby_block "find gem" do
   block do
    gem_bin = Chef::Recipe::find_gem
    # or gem_bin = find_gem
  end
end
Run Code Online (Sandbox Code Playgroud)

我得到了一个N​​oMethodError:未定义的方法'find_gem'.

还尝试将模块混合到Chef :: Resource :: RubyBlock中,它既不起作用也不起作用.

class Chef::Resource::RubyBlock
  include AppHelper
end

ruby_block "find gem" do
   block do
    gem_bin = Chef::Resource::RubyBlock::find_gem
    # or gem_bin = find_gem
  end
end
Run Code Online (Sandbox Code Playgroud)

有没有办法从ruby_block调用模块中的函数?或者是否有一个厨师变量来定位库中的文件,以便我能够在ruby_block中需要该模块.

谢谢!

cookbook chef-infra

15
推荐指数
1
解决办法
2万
查看次数

curl 和 nslookup 之间的名称查找区别是什么

当我试图弄清楚 DNS 查询何时会超时时,我迷失了方向。尝试了多种场景(在Linux上):

  1. /etc/resolv.conf 中未配置名称服务器

    ###################### curl #######################
    WRITE_OUT="%{http_code}\t%{time_namelookup}\t%{time_connect}\t\t%{time_starttransfer}\t\t%{time_total}\n"
    
    time curl -k -w "$WRITE_OUT" https://www.google.com/
    000     0.000   0.000           0.000           0.000
    
    curl: (6) Could not resolve host: www.goole.com; Unknown error
    
    real    0m0.009s
    user    0m0.000s
    sys     0m0.006s
    
    ##################### nslookup ####################
    time nslookup www.google.com
    ;; connection timed out; trying next origin
    ;; connection timed out; no servers could be reached
    
    real    0m24.012s
    user    0m0.004s
    sys     0m0.009s
    
    Run Code Online (Sandbox Code Playgroud)

    正如我们所看到的,curl 立即返回(9ms),而 nslookup 需要更长的时间(24s)。这让我非常困惑,curl 的行为更有意义,因为主机上没有指定名称服务器。

  2. 在/etc/resolv.conf中添加无法访问的主机IP,无法ping来模拟名称服务器关闭的情况

    ###################### curl #######################
    time curl -k -w "$WRITE_OUT" https://www.google.com/
    000     0.000 …
    Run Code Online (Sandbox Code Playgroud)

dns nslookup curl dig

6
推荐指数
2
解决办法
3万
查看次数

标签 统计

chef-infra ×1

cookbook ×1

curl ×1

dig ×1

dns ×1

nslookup ×1