【发布时间】:2018-07-31 01:17:28
【问题描述】:
将系统从厨师 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
【问题讨论】:
-
您面临什么问题?请更具体。
method_missing未被弃用;它是核心 ruby 语言的基本部分。也许此方法的某些特定用法已在此 gem 中标记为“已弃用”?? (但我不知道是什么,因为你没有把问题解释清楚。) -
你说的是this吗? “资源声明的堆栈跟踪通常不再包含
method_missing(而是包含资源的名称)。” -- 它没有说“method_missing已弃用”。上面的代码与堆栈跟踪有什么关系?您是否面临堆栈跟踪中依赖method_missing的问题? -
这个 que 更特定于 chef 而不是 ruby 找不到 php 的资源。尝试过的 application_php 资源可用,但仍然显示未找到 ref:- github.com/chef/chef-rfc/blob/master/…
-
"No resource found for php" 与您上面的问题有什么关系?触发此错误的代码在哪里?您是否定义了
Chef::Resource,例如该示例中显示的内容,并且依赖于隐式定义的 Chef DSL?如果您有错误,请向我们展示完整的错误以及触发它的代码。也就是说,请提供minimal reproducible example的问题;我还是不知道你在问什么。 -
您没有提供minimal reproducible example。您提供了一个没有上下文的方法。我如何重现您的问题?你说这个错误与“No resource found for php”有关,但是这个
php的定义在哪里呢?为什么首先调用method_missing?
标签: ruby chef-infra upgrade chef-recipe cookbook