为什么LWRP自定义定义为nil:NilClass提供了未定义的方法 [英] Why does the LWRP custom definition gives me undefined method for nil:NilClass

查看:74
本文介绍了为什么LWRP自定义定义为nil:NilClass提供了未定义的方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对此LWRP的自定义定义有疑问.

I have problem with my custom definition of this LWRP.

资源:user_postgresql.

actions :create, :alter
default_action :create

attribute :username, :kind_of => String
attribute :password, :kind_of => String
attribute :database_name,  :kind_of => String

提供者:user_postgresql.

def whyrun_supported?
  true
end

action :create do
  converge_by "Create [#{new_resource}]" do

    USER_NAME     = new_resource.username
    USER_PASSWORD = new_resource.password unless (new_resource.password.nil? ||
                                                        new_resource.password.empty?)
    USER_PASSWORD = USER_NAME
    DATABASE_NAME = new_resource.database_name unless (new_resource.database_name.nil? ||
                                                              new_resource.database_name.empty?)
    DATABASE_NAME = USER_NAME

    execute "create user [#{USER_NAME}]" do
      user "postgres"
      exists = <<-EOH
      PGPASSWORD='postgres' psql -U postgres -c "select * from pg_user where usename='#{USER_NAME}'" | grep -c #{USER_NAME}
      EOH
      command "PGPASSWORD='postgres' createuser -U postgres -sw #{USER_NAME}"
      not_if exists, :user => "postgres"
    end

    execute "set-password" do
      user "postgres"
      command "PGPASSWORD='postgres' psql -U postgres -c \"alter role #{USER_NAME} with password '#{USER_PASSWORD}'\""
    end

    execute "create-database" do
      user "postgres"
      exists = <<-EOH
      PGPASSWORD='postgres' psql -U postgres -c "select * from pg_database WHERE datname='#{DATABASE_NAME}'" | grep -c #{DATABASE_NAME}
      EOH
      command "PGPASSWORD='postgres' createdb -U postgres -O #{USER_NAME} -T template0 #{DATABASE_NAME}"
      not_if exists, :user => "postgres"
    end
  end
  new_resource.updated_by_last_action(true)    
end

更改操作.

action :alter do

  converge_by "Alter user password" do
    execute "alter-password" do
      user "postgres"
      command "psql -U postgres -c \"alter role #{current_resource.username} with password '#{current_resource.password}';\""
    end
  end
  current_resource.updated_by_last_action(true)
end

还有我的 load_current_resource:

def load_current_resource
  begin
    current_resource = Chef::Resource::AppCookbookUserPostgresql.new(new_resource.name)
    current_resource.name(new_resource.name)
    current_resource.username(new_resource.username)
    current_resource.password(new_resource.password)
    current_resource.database_name(new_resource.database_name)
    current_resource
  rescue
    Chef::Log.debug("Cannot find #{new_resource} in the swarm")
  end
end

这是错误:

Error executing action `alter` on resource 'app_cookbook_user_postgresql[change password to user postgres]'       
================================================================================       


NoMethodError       
-------------       
undefined method `username' for nil:NilClass       


Cookbook Trace:       
---------------       
/tmp/kitchen/cookbooks/app_cookbook/providers/user_postgresql.rb:53:in `block (2 levels) in class_from_file'       
/tmp/kitchen/cookbooks/app_cookbook/providers/user_postgresql.rb:52:in `block in class_from_file'       


Resource Declaration:       
---------------------       
# In /tmp/kitchen/cookbooks/app_cookbook/recipes/postgresql.rb       

 31: app_cookbook_user_postgresql 'change password to user postgres' do       
 32:   username 'postgres'       
 33:   action :alter       
 34: end       
 35:        



Compiled Resource:       
------------------       
# Declared in /tmp/kitchen/cookbooks/app_cookbook/recipes/postgresql.rb:31:in `from_file'       

app_cookbook_user_postgresql("change password to user postgres") do       
  action [:alter]       
  retries 0       
  retry_delay 2       
  cookbook_name :app_cookbook       
  recipe_name "postgresql"       
  username "postgres"       
end

为什么在action :alter定义中current_resource为零?

Why does the current_resource is nil in the action :alter definition ???

推荐答案

啊,太近了!您的load_current_resource方法不太正确.您需要设置 instance_variable ,但只设置了一个本地变量:

Ahh so close! Your load_current_resource method isn't quite right. You need to set the instance_variable, but you've only set a local variable:

def load_current_resource
  @current_resource = Chef::Resource::AppCookbookUserPostgresql.new(new_resource.name)

  begin
    @current_resource.name(new_resource.name)
    @current_resource.username(new_resource.username)
    @current_resource.password(new_resource.password)
    @current_resource.database_name(new_resource.database_name)
    @current_resource
  rescue
    Chef::Log.debug("Cannot find #{new_resource} in the swarm")
  end
end

我还将初始创建内容移到了begin块之外.即使出现故障,您仍然想创建一个空资源.话虽如此,该救援计划完全没有必要.应该是:

I've also moved the initial creation outside of the begin block. You still want to create an empty resource, even if something fails. That being said, that rescue block is entirely unnecessary. It should just be:

def load_current_resource
  @current_resource = Chef::Resource::AppCookbookUserPostgresql.new(new_resource.name)
  @current_resource.username(new_resource.username)
  @current_resource.password(new_resource.password)
  @current_resource.database_name(new_resource.database_name)
  @current_resource
end

这篇关于为什么LWRP自定义定义为nil:NilClass提供了未定义的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆