厨师new_resource。 与

问题描述 投票:0回答:1

我继承了一本食谱,利用另一本自定义资源的食谱。

我试图弄清楚在这段代码中使用new_resource

在下面的代码中,path new_resource.fullpath导致“未定义的方法”。

当我将该行更改为简单的path fullpath以引用局部变量时,它工作得很好。我无法弄清楚以前的意图是什么。

我确信这很简单。寻找ELI5。谢谢!

完整代码块。

resource_name :mb_worker_role
default_action :create

property :service_name, String, name_property: true
property :root_path, String, required: true
property :executable, String, required: true
property :wcf_port, [Integer, String], default: 808
property :health_port, [Integer, String], default: 8000
property :use_dummy, [TrueClass, FalseClass], default: true

action :create do
  # Create firewall allow for WCF port
  windows_firewall_rule "#{new_resource.service_name} - WCF" do
    localport new_resource.wcf_port.to_s
    protocol 'TCP'
    firewall_action :allow
  end

  # Create firewall allow for health port
  windows_firewall_rule "#{new_resource.service_name} - Health" do
    localport new_resource.health_port.to_s
    protocol 'TCP'
    firewall_action :allow
  end

  # Full path to service executable at root_path\executable
  fullpath = "#{new_resource.root_path}\\#{new_resource.executable}"

  # Create directory for worker role application root
  directory new_resource.root_path do
    recursive true
    action :create
  end

  # Set NetTCPPortSharing to start on demand
  windows_service 'NetTCPPortSharing' do
    action :configure_startup
    startup_type :manual
  end

  # Stage the dummy worker role executable if requested
  # Only used to verify this resource when testing
  if property_is_set?(:use_dummy)
    cookbook_file 'Stage dummy worker role executable' do
      # path new_resource.fullpath
      path fullpath
      cookbook 'mb_worker_role'
      source 'WorkerRole.Default.exe'
      only_if { new_resource.use_dummy }
      action :create
    end
  end

  # Create windows service if it does not exist
  powershell_script "Installing Windows service #{new_resource.service_name}" do
    code <<-EOH
      # Create the Windows service
      sc.exe create "#{new_resource.service_name}" binPath="#{fullpath}"
    EOH
    not_if "(Get-Service -Name #{new_resource.service_name}).Name -eq '#{new_resource.service_name}'"
  end

  # Set service to automatic and make sure it's running
  windows_service new_resource.service_name do
    action [:configure_startup, :enable, :start]
    startup_type :automatic
  end
end
ruby chef
1个回答
0
投票

path fullpath是一个神奇的别名系统,我们在这一点上积极劝阻。尝试这是一件好事,但语法有很多问题导致意外行为。使用new_resource.fullpath,更少的脚枪。

© www.soinside.com 2019 - 2024. All rights reserved.