[chef] Re: Re: detect if a service is running?


Chronological Thread 
  • From: Kenneth Barry < >
  • To: " " < >
  • Subject: [chef] Re: Re: detect if a service is running?
  • Date: Mon, 12 Jan 2015 08:59:05 -0800

Any chance the installer has a switch to remove previous versions of itself, if they are present?

On Mon, Jan 12, 2015 at 8:45 AM, Daniel DeLeo < " target="_blank"> > wrote:


On Friday, January 9, 2015 at 3:31 PM, James Harrison wrote:

> Hi chefs,
>
> I’m pretty new to chef, and feel like I’m missing something obvious. Any advice would be appreciated. I’m trying to cover two scenarios with one recipe, in a Windows environment. What I’m trying to do is:
>
> - Scenario 1: install a package
> - Scenario 2: if the package is already installed, stop a service and then reinstall the package.
>
> The recipe, as currently written, goes through a series of steps involving downloading the installer, always attempting to stop the service, then running the installer if the package is not already installed, then starting the service.
>
> The problem I’m facing is in scenario 1. If I attempt to stop the service, but the service is not installed, then I receive an exception and execution of the recipe halts.
>
> Am I trying to do something that is better covered by two recipes? Or is there some easy way to check if a service is installed?
>
> Thanks
>
> James


A few things here. Firstly, our platform abstraction code is generally pretty tightly coupled to providers, which are also responsible for providing idempotent behavior, why-run support, logging events, and all sorts of things not related to, say, talking to the service manager or package manager. Which means that Chef doesn’t provide a nice abstract way of querying these things. So to meet your requirement to check if a package or service is installed, you’re best off running the command yourself.

As for how you accomplish that, there are a few ways. In the other branch of this thread, it was suggested to use `notifies`, but Chef doesn’t provide a “before notification,” which is what you’d need to stop the service before the package is upgraded (but only if it’s going to be upgraded). So one way or another, you’re going to have to write some code that can tell you if the package is about to be upgraded and will stop the service if that check returns true. You can use `shell_out` (specifics about doing this depend on whether you’re on Chef 11 or 12) to run a command and give you the return code and output. Once you have that, it might be as simple as adding a resource like:

service “stop FOO service before upgrading” do
  # other important stuff
  action :stop
  only_if { about_to_upgrade_package_foo? } # Ruby convention: true/false methods named w/ question mark
end

# resources to install/upgrade package, start service

HTH,

--
Daniel DeLeo






--

Kenneth Barry 

TuneIn | Build and Release Engineer
M: 409-673-0544



Archive powered by MHonArc 2.6.16.

§