【问题标题】:Chef library other cookbook library dependency厨师库其他食谱库依赖项
【发布时间】:2015-05-28 11:15:40
【问题描述】:

我正在尝试为 Chef 中的 Public Aws 食谱编写包装食谱。我正在制作一个安装 CodeDeploy 代理的配方,该代理可能会因地区而异。我已经编写了几个库来帮助我做到这一点,但是当我的包装食谱中的库依赖于原始食谱中的库时,我陷入了困境。我不确定是否需要在此处执行 require + include(如果需要,我不知道 require 的路径)。

我的 metadata.rb 文件中有“取决于“aws”。

如何正确设置依赖项以使其正常工作?目前我收到这样的错误

undefined method `instance_availability_zone' for Chef::Resource::RemoteFile

还是我这样做完全错了?

这是有问题的图书馆

module Opscode
  module Aws
    module Ec2
      module Region
        # require '?????' # Do I need this?
        # include Opscode::Aws::Ec2 # Do I need this?
        def instance_region
          # query instance region comes from Opscode::Aws::Ec2
          # this is the dependency. 
          # Using Opscode::Aws::Ec2.instance_availability_zone doesn't
          # work either.
          @@instance_region ||= query_instance_region 
        end
        def query_instance_region
          region = instance_availability_zone
          region = region[0, region.length - 1]
          region
        end
      end
    end
  end
end

这个库在我知道需要路径的情况下工作

require File.join(File.dirname(__FILE__), 'ec2_region')
module Opscode
  module Aws
    module CodeDeploy
      include Opscode::Aws::Ec2::Region
      def codedeploy_region_url
        @@region_url ||= query_codedeploy_region_url
      end
      def query_codedeploy_region_url
        region = instance_region
        codeDeployPath = "https://s3.amazonaws.com/aws-codedeploy-#{region}/latest/codedeploy-agent.noarch.rpm"
        codeDeployPath
      end
    end
  end
end

这是食谱

Chef::Resource::RemoteFile.send(:include, Opscode::Aws::CodeDeploy)

remote_file "#{Chef::Config[:file_cache_path]}/codedeploy-agent.rpm" do
  source codedeploy_region_url
end

【问题讨论】:

    标签: ruby chef-infra


    【解决方案1】:

    您可能需要来自 chef-cache 的已知路径和食谱内库的已知路径。

    应该这样做:

    require File.join(Chef::Config['chef_repo_path'],'cookbooks/aws/libraries/ec2_region')
    

    【讨论】:

    • 谢谢,这似乎确实有效(使用 ::Config 而不是 ::config)。一般来说,有没有更好的方法来做到这一点?当 Chef 有自己的结构和加载库的方式时,回退使用像常规 ruby​​ 这样的文件路径似乎很有趣。我认为 Chef 可能有某种内置方式来查找库依赖项,尤其是对于像这样的子模块。
    猜你喜欢
    • 2016-05-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多