首页 > 解决方案 > Chef 13 中 method_missing 的替代方法

问题描述

将系统从厨师 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

标签: rubychef-infraupgradechef-recipecookbook

解决方案


啊,我在评论中看到了真正的问题。application_php 说明书从未升级为与较新的应用说明书套件的其余部分一起使用。您必须编写自己的版本。

对于通过谷歌找到这个问题的其他人:这method_missing件事完全是红鲱鱼。用户很困惑,因为在 Chef 中我们删除了method_missing基于 - 的节点属性语法。这与我将其用于子资源管理无关。


推荐阅读