Search code examples
rubychef-infraprovisioningcookbook

Chef - Skip resources from status of previous run


For server rebuild, I want to skip some section of my cookbook according to the results of a previous run.

For instance, we have a resource to start Weblogic servers.

weblogic_server "server_name" do 
    action :start
end

These startups take a lot of time during the build. I want to skip this if it was run succesfully in the last build, to avoid having to wait too much for the rebuild. Something like this:

weblogic_server "server_name" do 
    action :start
    not_if { it_was_run_successfully_during_the_previous_run }
end

I know the best way to do it would be have script checking on weblogic servers status, but that's up to another team and I need a temporary solution.

I thought about a logfile in JSON format referencing the different steps of the build.

e.g:

{
    "provisioning"          : true,
    "start_weblogic_servers : true,
    "configuring_ohs"       : false
}

In this case I would have a template resource for this logfile and then update the values during the run. Then in every run I would check this file first and skip the right section according to the values I find.

Is there a better way to go ?


Solution

  • Nabeel Amjad solution worked for me. Follow these steps:

    Create a file resource with action :nothing

    file '/tmp/logfile' do
        action: nothing
    end
    

    Set your resource to notify the file resource after running

    weblogic_server 'server_name' do 
        action   :start
        notifies :create, 'file[/tmp/logfile]', :immediately
    end
    

    Add a guard not_if that will skip future execution of this resource if the file exists on the server

    weblogic_server 'server_name' do 
        action   :start
        notifies :create, 'file[/tmp/logfile]', :immediately
        not_if   { ::File.exist?('/tmp/logfile') }
    end