Chef 13中method_missing的替代方法

时间:2018-07-30 08:11:35

标签: ruby chef upgrade chef-recipe cookbook

将系统从厨师12升级到13,面临method_missing问题 从某些链接中发现不建议使用method_missing,是否还有其他选择呢?

def method_missing(name, *args, &block)
  # Build the set of names to check for a valid resource
  lookup_path = ["application_#{name}"]
  run_context.cookbook_collection.each do |cookbook_name, cookbook_ver|
    if cookbook_name.start_with?("application_")
      lookup_path << "#{cookbook_name}_#{name}"
    end
  end
  lookup_path << name
  resource = nil
  lookup_path = ["application_#{name}"]
  # Try to find our resource
  lookup_path.each do |resource_name|
    begin
      Chef::Log.debug "Trying to load application resource #{resource_name} for #{name}"
      resource = super(resource_name.to_sym, *args, &block)
      break
    rescue NameError => e
      # Works on any MRI ruby
      if e.name == resource_name.to_sym || e.inspect =~ /\b#{resource_name}\b/
        next
      else
        raise e
      end
    end
  end
  raise NameError, "No resource found for #{name}. Tried #{lookup_path.join(', ')}" unless resource
  # Enforce action :nothing in case people forget
  resource.action :nothing
  # Make this a weakref to prevent a cycle between the application resource and the sub resources
  resource.application WeakRef.new(self)
  resource.type name
  @sub_resources << resource
  resource
end

1 个答案:

答案 0 :(得分:0)

啊,我在评论中看到了真正的问题。从未将application_php Cookbook升级为可与其他较新的应用程序Cookbook套件一起使用。您将必须编写自己的版本。

对于任何其他通过Google查找此问题的人:method_missing完全是鲱鱼。用户感到困惑,因为在Chef中我们删除了基于method_missing的节点属性语法。这与我将其用于子资源管理无关。