Using git resource in a Chef LWRP

290 views Asked by At

I'm attempting to use an LWRP (with Chef 10) to create my own deployment resource, but it's throwing an error and I haven't been able to find any examples in the docs.

My resource file looks like this:

actions :install

attribute :package_name, :kind_of => String, :name_attribute => true
attribute :app_name, :kind_of => String
attribute :cmd_prefix, :kind_of => String, :default => ''
attribute :deploy_tag, :kind_of => String
attribute :wsgi_stat_port, :kind_of => Fixnum

When I try to use this resource, I get the following error:

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

Cookbook Trace:
---------------
/var/cache/chef/cookbooks/dpn_python_common/providers/app.rb:59:in `class_from_file'
/var/cache/chef/cookbooks/dpn_python_common/providers/app.rb:57:in `class_from_file'

where the block of the provider file referenced in the trace is the following:

git DEPLOY_PATH do
    repository repo_url
    reference @new_resource.deploy_tag
    user 'root'
    group 'root'
end

I have tried printing out @new_resource.deploy_tag and it contains the value I'm expecting, so I'm left thinking that the git resource just isn't working properly from within the LWRP, but I haven't been able to find any documentation that points to how I should be calling this thing. Any suggestions?


Actual answer:

I apparently need to create a local variable before using the resource block, like so:

deploy_tag = @new_resource.deploy_tag
git DEPLOY_PATH do
    repository repo_url
    reference deploy_tag
    user 'root'
    group 'root'
end
1

There are 1 answers

2
coderanger On BEST ANSWER

The block on a resource is already evaluated in the context the resource object. Basically just make that deploy_tag.