Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Access chef resources inside ruby block

I've been trying to find the answer to this in the chef docs and through Google, but I've not been able to come up with anything. I'm not a ruby guy (yet), so the answer to this might stem from my approaching the problem with "just enough ruby for Chef". Here's what I want to do: in my deploy resource, in the before_migrate attribute, I want to execute a resource in my current recipe. What I am doing currently is to just stuff the resource into the block itself, but I know there must be a better way to do it.

before_migrate do

    template "#{app_root}/#{applet_name}/local_settings.py" do
        source "local_settings.py.erb"
        owner app_config['user']
        group app_config['group']
        variables(
            :database_name => app_config['postgresql']['database_name'],
            :user => app_config['postgresql']['user'],
            :password => app_config['postgresql']['password']
        )   
        action :create
    end 
end 

What I'm aiming for is something like

before_migrate do
    "template #{app_root}/#{applet_name}/local_settings.py".execute
end

So I can re-use that template code. Thanks!

like image 759
sashimiblade Avatar asked Feb 18 '13 22:02

sashimiblade


2 Answers

You could specify the resource outside of the "deploy" resource with an action of nothing and then, in the *before_migrate* do something like:

    before_migrate do

        ruby_block "notify_template" do
            block do
              true
            end
            action :create
            notifies :create, "template[#{app_root}/#{applet_name}/local_settings.py]", :immediately
        end

     end

That way, you can notify it when you need it.

like image 135
Giannis Nohj Avatar answered Nov 09 '22 16:11

Giannis Nohj


Thanks to the great guys in the #chef IRC channel, I solved my problem. The notification resource needs to be accessed directly, using

Chef::Resource::Notification.new("template[#{app_root}/#{applet_name}/local_settings.py", :create)

Which will notify the template resource to run the :create action.

like image 21
sashimiblade Avatar answered Nov 09 '22 16:11

sashimiblade