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 http://www.projectserengeti.org/

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 http://www.projectserengeti.org/

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 http://www.projectserengeti.org/

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 http://www.projectserengeti.org/

Seems like you'd need an ohai plugin that queries the RPM database and pops
that into a node attribute. Then it's as simple as declaring the resources
with "only_if { node[:packages].include?('mysql-server') } " , or whatever
the package name is.

Surely someone's written an OSS Ohai plugin that does this already...

On Fri, 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 http://www.projectserengeti.org/

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 http://www.projectserengeti.org/

The default Ohai shipped with Chef doesn't contains the package plugin. It
will be a solution. But the more natual way in Chef to do the checking is
support code like this :

do-work if package('mypkg').installed?
do-work if service('mysvr').running?

I found that in code of package and service resources, it has logic to
chech wheter the package is intalled or whether the service is running, but
the method is not exposed as a method of the package/service resource, or
it's exposed but I don't know how to call it.

Thanks
Jesse Hu, Project Serengeti http://www.projectserengeti.org/

2013/10/12 steve . leftathome@gmail.com

Seems like you'd need an ohai plugin that queries the RPM database and
pops that into a node attribute. Then it's as simple as declaring the
resources with "only_if { node[:packages].include?('mysql-server') } " , or
whatever the package name is.

Surely someone's written an OSS Ohai plugin that does this already...

On Fri, 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 http://www.projectserengeti.org/

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 http://www.projectserengeti.org/

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

Thanks Noah. Actually I want to do something before the service is going
to be started (or the package is going to be installed), if the service
is already started, I will do nothing. So your code is not what I want.

Thanks
Jesse Hu | Project Serengeti http://projectserengeti.org/


Noah Kantrowitz wrote On Fri, 11 Oct 2013 22:48:41 -0700 :

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

Just change the order of the resources.

--Noah

Jesse Hu huhui14@gmail.com wrote:

Thanks Noah. Actually I want to do something before the service is
going
to be started (or the package is going to be installed), if the service

is already started, I will do nothing. So your code is not what I want.

Thanks
Jesse Hu | Project Serengeti http://projectserengeti.org/


Noah Kantrowitz wrote On Fri, 11 Oct 2013 22:48:41 -0700 :

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