Just Lucky Really
Just Lucky Really

Reputation: 1401

Chef - Notify an execute before resource

I have a simple execute[apt-update] resource in an apt recipe:

execute "apt-update" do
  command "apt-get update"
  action :nothing
end

And I want to be able to notify it, only if a package needs to be installed. I was hoping for something like:

include_recipe "apt"

package "openssh-server" do
  action :install
  notifies :run, 'execute[apt-update]', :before
end

The :before isn't currently a valid timing, and probably wont be any time soon

It's something that is possible with Puppet, and was wondering if I'm missing something simple.

Upvotes: 3

Views: 3131

Answers (2)

timurb
timurb

Reputation: 5544

The functionality in question was added in Chef 12.6: https://docs.chef.io/release_notes.html#what-s-new-in-12-6

So you can write code exactly like you pasted above now.

Upvotes: 2

StephenKing
StephenKing

Reputation: 37580

Keeping the APT Cache Up-to-Date

While this is not an answer to your question, I can hopefully provide you an answer to your actual problem (which is a pretty common one btw.):

The apt cookbook that you should include in your run list before you use the package resource will automatically pull the repos.

In order to avoid pulling repos during every chef run, it by default pulls them only once per day, while this minimum delay can be configured via node['apt']['periodic_update_min_delay'].

Therefore, at least for the use cases I had during the last years with Chef, I think I never needed to notify a resource doing apt-get update. Even when adding an additional apt repository, this can easily be done as follows:

apt_repository 'security-ubuntu-multiverse' do
  uri        'http://security.ubuntu.com/ubuntu'
  distribution 'trusty-security'
  components ['multiverse']
  deb_src 'true'
 end

The clever thing with the apt_repository resource is that it automatically triggers an apt-get update run.

The Smell of Triggering a Resource To Run Before

(Added with permission from original Author, contributed by @Tensibai)

On why a :before is a false 'good idea' 99% of the time:

When you think about it on an isolated package install, it sounds the way to go, ask the package to notify an execute to run apt-get update before.

Now when you extend this to N packages (in 1 or more recipes), you will wish to ensure apt-get update has been run before each package if it has to be installed.

Here's come the dragons, how many time would we call apt-get update ? with a straight :before, :immediately it will be called N times, once before each package, that's silly but could be your wish.

You may object it should be done only once, and as :delayed it should be queued up, but when ? At compile time we don't know if the package will need to be installed or not, this suppose adding a new 'compile' phase to test those resources and build the 'pre-notification' queue.

That's part of what Puppet does, and where it comes a problem when you wish complete control over execution order.

More details on this here

The New :immediately_before Resource

Now I said why it should be used sparingly, here the good news:
A new :immediately_before is on its way into chef in a near future per rfc-154 and this PR

Upvotes: 4

Related Questions