Instead of your example you could do it idemopotently and asset the desired state. Something more like
if node['should_mysrv']
package 'mypkg' do
action :install
end
service 'mysvr' do
action [:enable, :start]
end
ruby_block do
code do
do_work
end
end
else
package 'mypkg' do
action :remove
end
service 'mysvr' do
action [:disable, :stop]
end
end
Rather than reacting to state, that is enforcing and updating state.
--Noah
On Oct 11, 2013, at 3:27 AM, Hui Hu huhui14@gmail.com wrote:
Thanks Kevin. Scenario 2 is what I want. How to use Ohai automatic attribute to check whether a pacakge is installed or a service is running ?
What I want is some code like this :
do-work if package('mypkg').installed?
do-work if service('mysvr').running?
Thanks
Jesse Hu, Project Serengeti
2013/10/10 Kevin Keane Subscription subscription@kkeane.com
What exactly are you trying to accomplish? There are two different scenarios I can think of, and you can address them in two different ways.
Scenario 1: you want to install MySQL, and then perform another action. In that case, you would simply use the package resource, which would then notify your execute resource. In that case, your actions will only execute once right after the package gets installed.
Scenario 2: you want to execute your action only on those servers that already have MySQL installed. In this case, the generic solution would be to use an automatic attribute that Chef automatically populates at the beginning of the chef run (actually, Ohai does the populating). In that case, your actions will be executed on every single Chef run.
The first scenario is probably closer to what Chef is intended for. The second scenario means that your action isn't idempotent as Chef expects.
Kevin Keane
The NetTech
760-721-8339
http://www.4nettech.com
Our values: Privacy, Liberty, Justice
See https://www.4nettech.com/corp/the-nettech-values.html
-----Original message-----
From: Hui Hu huhui14@gmail.com
Sent: Thursday 10th October 2013 0:17
To: chef@lists.opscode.com
Subject: [chef] How to check whether a package is installed ?
Hi Chef,
Is there a simple chef way to check whether a package is installed or a service is running ?
For example, I want to write some code like this :
do-work if package('mypkg').installed?
do-work if service('mysvr').running?
I know we can do it by the code below, but it's bind to the specific platform. What I need is a platform independent way.
execute "..." do
only_if "rpm -q mypkg"
only_if "service mysvr status"
end
Thanks
Jesse Hu, Project Serengeti