我正在使用我的提供商iis_site
中的Opscode IIS食谱
include ::Opscode::IIS::Helper
在我的库文件夹中,我有一个名为helper.rb的文件
#
# Cookbook Name:: iis
# Library:: helper
#
# Author:: Julian C. Dunn <jdunn@getchef.com>
#
# Copyright 2013, Chef Software, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
module Opscode
module IIS
module Helper
if RUBY_PLATFORM =~ /mswin|mingw32|windows/
require 'chef/win32/version'
end
require 'rexml/document'
include REXML
def self.older_than_windows2008r2?
if RUBY_PLATFORM =~ /mswin|mingw32|windows/
win_version = Chef::ReservedNames::Win32::Version.new
win_version.windows_server_2008? ||
win_version.windows_vista? ||
win_version.windows_server_2003_r2? ||
win_version.windows_home_server? ||
win_version.windows_server_2003? ||
win_version.windows_xp? ||
win_version.windows_2000?
end
end
def self.is_new_value?(document, xpath, value_to_check)
return XPath.first(document, xpath).to_s == value_to_check ? false : true
end
def self.is_new_or_empty_value?(document, xpath, value_to_check)
return is_new_value(document, xpath, value_to_check) || value_to_check == '' ? false : true
end
def self.appcmd(node)
return "#{node['iis']['home']}/appcmd.exe"
end
end
end
end
在iis_site.rb
文件中,我收到以下错误:
致命错误:NoMethodError:iis_site [默认网站](iis :: remove_default_site第21行)出现错误:NoMethodError:没有名为
appcmd' for
的资源或方法:Chef :: Provider :: IisSite&#34;&# 34;&#39;
如果我从:
更改提供者行cmd = "#{appcmd(node)} add site /name:\"#{@new_resource.site_name}\""
到
cmd = "#{Opscode::IIS::Helper.appcmd(node)} add site /name:\"#{@new_resource.site_name}\""
问题已经解决,但是这增加了很多不需要的重复。
答案 0 :(得分:1)
完成以下原因的自我回答:
在方法定义中使用self意味着它只是一个实例方法。即:只能通过类的对象访问它。
在您的情况下,您必须有一个班级::Opscode::IIS::Helper
的对象来呼叫appcmd
您使用方法的完整定义(This part:
“#{Opscode :: IIS :: Helper.appcmd(node)}`)动态创建临时对象以调用方法
除此之外,您将模块发送到提供程序命名空间,因此您无需在提供商的顶部要求库。
答案 1 :(得分:0)
self.
以下是固定代码。
#
# Cookbook Name:: iis
# Library:: helper
#
# Author:: Julian C. Dunn <jdunn@getchef.com>
#
# Copyright 2013, Chef Software, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
module Opscode
module IIS
module Helper
if RUBY_PLATFORM =~ /mswin|mingw32|windows/
require 'chef/win32/version'
end
require 'rexml/document'
include REXML
def self.older_than_windows2008r2?
if RUBY_PLATFORM =~ /mswin|mingw32|windows/
win_version = Chef::ReservedNames::Win32::Version.new
win_version.windows_server_2008? ||
win_version.windows_vista? ||
win_version.windows_server_2003_r2? ||
win_version.windows_home_server? ||
win_version.windows_server_2003? ||
win_version.windows_xp? ||
win_version.windows_2000?
end
end
def is_new_value?(document, xpath, value_to_check)
return XPath.first(document, xpath).to_s == value_to_check ? false : true
end
def is_new_or_empty_value?(document, xpath, value_to_check)
return is_new_value(document, xpath, value_to_check) || value_to_check == '' ? false : true
end
def appcmd(node)
@appcmd ||= begin
"#{node['iis']['home']}\\appcmd.exe"
end
end
end
end
end