【问题标题】:Embedded resource cannot access chef property in custom resource?嵌入式资源无法访问自定义资源中的厨师属性?
【发布时间】:2017-02-10 03:41:01
【问题描述】:

我正在尝试编写我的第一个厨师自定义资源。但是,我无法访问 一些 其他嵌入式资源中的属性。

我创建了以下资源

property :template_shortname, String

action :setup do
  node.set['apache']['version'] = '2.4'
  node.set['apache']['package'] = 'apache2'

  include_recipe "apache2::default"

  print "the shortname is " + template_shortname

  web_app "sample_app" do
    server_name "server.com"
    server_aliases []
    docroot "/www-data"
    template "prefix " + template_shortname
  end
end

但是当我执行这个时,我得到:

来自调试日志

The shortname is: server

但是 template_shortname 的参数包含一个空数组。所以它退出:

       TypeError
       ---------
       no implicit conversion of Array into String

       Cookbook Trace:
       ---------------
       /tmp/kitchen/cache/cookbooks/phpapp/resources/setup.rb:21:in `+'
       /tmp/kitchen/cache/cookbooks/phpapp/resources/setup.rb:21:in `block (2 levels) in class_from_file'
       /tmp/kitchen/cache/cookbooks/phpapp/resources/setup.rb:17:in `block in class_from_file'

我正在使用 chef-dk Chef 开发工具包版本:1.1.16 厨师客户端版本:12.17.44 发货版本:master (83358fb62c0f711c70ad5a81030a6cae4017f103) 伯克斯版本:5.2.0 厨房版本:1.14.2

【问题讨论】:

  • template "prefix #{template_shortname}" 使用字符串插值,而不是加法
  • 可能来自docnew_resource.template_shortname。总而言之,设置这样的属性并调用 include_recipe 操作听起来很脆弱,可能很快就会咬你。这些应该在你的 phpapp 食谱属性文件和食谱中,总而言之,我不明白你在这里想要做什么。
  • 对不起,我应该提到 new_resource 也是空的。据我了解,这篇博文blog.backslasher.net/chef-custom-resources.html 不再需要。如果我正确阅读了厨师文档,则仅在名称冲突的情况下才需要。
  • 确实如此,但我认为最好保持一致。总而言之,您的问题来自web_apptemplateproperty 是一个数组,因此+ 运算符充当数组而不是字符串。使用字符串插值,它应该可以工作,我仍然认为属性和 include_recipe 绝对不应该在自定义资源中定义(尝试为两个应用程序安装两次 apache 是没有意义的)。

标签: chef-infra


【解决方案1】:

问题在于web_app 本身不是资源,它是一个定义。所以魔法范围的东西在那里不起作用。总的来说,无论如何都建议使用new_resource.whatever,因为融合模式的瞄准镜魔法非常容易使用。

【讨论】:

    【解决方案2】:

    原来平衡资源正在做类似的事情:

    https://github.com/poise/application_php/blob/master/providers/mod_php_apache2.rb

    这行得通:

      new_resource = @new_resource
    
      web_app "sample_app" do
        server_name "server.com"
        server_aliases []
        docroot "/www-data"
        template "prefix " + new_resource.template_shortname
      end
    

    我仍然很高兴知道为什么会这样......

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-01-28
      • 2017-07-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-12-30
      • 1970-01-01
      • 2013-02-03
      相关资源
      最近更新 更多