Managing windows services via chef

Hello again all -

First - thanks all for all the really helpful replies to any/all earlier
posts. I’ve been able to move pretty quickly because of the help.

Now I have a tougher question.

I’m trying to work with a windows service for an item that we build and
install.

This means stopping it if it exists, installing it if it doesn’t and then
starting it if it isn’t already running.

I’ve tried to make the only_if/not_if be a "sc query ", but I’m
seeing mixed results in different windows servers.

On Windows 7, if the service doesn’t exist, I get a non-zero (1060) return
value, on Windows server 2003 (R2), I get 0 regardless if the service exists
or doesn’t exist.

I’ve been thinking about just parsing the output of the “sc query” command
versus just relying on the return value, but this is starting to feel
kludgy.

Any suggestions?

I'm trying this approach, but it seems to wait for user input (and then
doesn't see the whole command):

ruby_block "first-search" do
    block do
        puts "the service WZCSV was found"
    end
    action :create
    only_if "sc query WZCSVC \| find /C /I \"running\""
end

Is there something wrong with how I've phrased the "only_if portion of this
ruby block?

It halts there and when I hit enter, it's as if something was not sent to
the "sc" command:

[Mon, 24 Oct 2011 10:58:18 -0400] DEBUG: Executing sc query WZCSVC | find /C
/I "running"
[Mon, 24 Oct 2011 11:00:18 -0400] DEBUG: ---- Begin output of sc query
WZCSVC | find /C /I "running" ----
[Mon, 24 Oct 2011 11:00:18 -0400] DEBUG: STDOUT:
ERROR: Invalid Option
....
[Mon, 24 Oct 2011 11:00:18 -0400] DEBUG: STDERR:
[Mon, 24 Oct 2011 11:00:18 -0400] DEBUG: ---- End output of sc query WZCSVC
| find /C /I "running" ----
[Mon, 24 Oct 2011 11:00:18 -0400] DEBUG: Ran sc query WZCSVC | find /C /I
"running" returned 0
the service WZCSV was found
[Mon, 24 Oct 2011 11:00:18 -0400] INFO: ruby_block[first-search] called

But that all looks healthy - any suggestions?

On Thu, Oct 20, 2011 at 2:48 PM, Maven User maven.2.user@gmail.com wrote:

Hello again all -

First - thanks all for all the really helpful replies to any/all earlier
posts. I've been able to move pretty quickly because of the help.

Now I have a tougher question.

I'm trying to work with a windows service for an item that we build and
install.

This means stopping it if it exists, installing it if it doesn't and then
starting it if it isn't already running.

I've tried to make the only_if/not_if be a "sc query ", but
I'm seeing mixed results in different windows servers.

On Windows 7, if the service doesn't exist, I get a non-zero (1060) return
value, on Windows server 2003 (R2), I get 0 regardless if the service exists
or doesn't exist.

I've been thinking about just parsing the output of the "sc query" command
versus just relying on the return value, but this is starting to feel
kludgy.

Any suggestions?

Well, I got this to work (I think) - more testing now...

only_if 0==sc query WZCSVC \| find /C /I \"running\"

On Mon, Oct 24, 2011 at 11:25 AM, Maven User maven.2.user@gmail.com wrote:

I'm trying this approach, but it seems to wait for user input (and then
doesn't see the whole command):

ruby_block "first-search" do
    block do
        puts "the service WZCSV was found"
    end
    action :create
    only_if "sc query WZCSVC \| find /C /I \"running\""
end

Is there something wrong with how I've phrased the "only_if portion of this
ruby block?

It halts there and when I hit enter, it's as if something was not sent to
the "sc" command:

[Mon, 24 Oct 2011 10:58:18 -0400] DEBUG: Executing sc query WZCSVC | find
/C /I "running"
[Mon, 24 Oct 2011 11:00:18 -0400] DEBUG: ---- Begin output of sc query
WZCSVC | find /C /I "running" ----
[Mon, 24 Oct 2011 11:00:18 -0400] DEBUG: STDOUT:
ERROR: Invalid Option
....
[Mon, 24 Oct 2011 11:00:18 -0400] DEBUG: STDERR:
[Mon, 24 Oct 2011 11:00:18 -0400] DEBUG: ---- End output of sc query WZCSVC
| find /C /I "running" ----
[Mon, 24 Oct 2011 11:00:18 -0400] DEBUG: Ran sc query WZCSVC | find /C /I
"running" returned 0
the service WZCSV was found
[Mon, 24 Oct 2011 11:00:18 -0400] INFO: ruby_block[first-search] called

But that all looks healthy - any suggestions?

On Thu, Oct 20, 2011 at 2:48 PM, Maven User maven.2.user@gmail.comwrote:

Hello again all -

First - thanks all for all the really helpful replies to any/all earlier
posts. I've been able to move pretty quickly because of the help.

Now I have a tougher question.

I'm trying to work with a windows service for an item that we build and
install.

This means stopping it if it exists, installing it if it doesn't and then
starting it if it isn't already running.

I've tried to make the only_if/not_if be a "sc query ", but
I'm seeing mixed results in different windows servers.

On Windows 7, if the service doesn't exist, I get a non-zero (1060) return
value, on Windows server 2003 (R2), I get 0 regardless if the service exists
or doesn't exist.

I've been thinking about just parsing the output of the "sc query" command
versus just relying on the return value, but this is starting to feel
kludgy.

Any suggestions?

Take a look at the 'Win32::Service.exists?' method in the 'win32-service' gem [0]. You could easily use this in a 'not_if' block.

As an FYI Chef 0.10.6 will ship with a heavily refactored Windows service provider:
http://tickets.opscode.com/browse/CHEF-2541

It leverages win32-service heavily and talks directly to the WinAPI vs shelling out to sc.exe.

--
Seth Chisamore
Software Design Engineer, Opscode, Inc.
IRC, Skype, Twitter, Github: schisamo

[0] GitHub - chef/win32-service: A Ruby library that allows users to inspect, control or create services on MS Windows

On Monday, October 24, 2011 at 11:47 AM, Maven User wrote:

Well, I got this to work (I think) - more testing now...

only_if 0==sc query WZCSVC \| find /C /I \"running\"

On Mon, Oct 24, 2011 at 11:25 AM, Maven User <maven.2.user@gmail.com (mailto:maven.2.user@gmail.com)> wrote:

I'm trying this approach, but it seems to wait for user input (and then doesn't see the whole command):

ruby_block "first-search" do
block do
puts "the service WZCSV was found"
end
action :create
only_if "sc query WZCSVC | find /C /I "running""
end

Is there something wrong with how I've phrased the "only_if portion of this ruby block?

It halts there and when I hit enter, it's as if something was not sent to the "sc" command:

[Mon, 24 Oct 2011 10:58:18 -0400] DEBUG: Executing sc query WZCSVC | find /C /I "running"
[Mon, 24 Oct 2011 11:00:18 -0400] DEBUG: ---- Begin output of sc query WZCSVC | find /C /I "running" ----
[Mon, 24 Oct 2011 11:00:18 -0400] DEBUG: STDOUT:
ERROR: Invalid Option
....
[Mon, 24 Oct 2011 11:00:18 -0400] DEBUG: STDERR:
[Mon, 24 Oct 2011 11:00:18 -0400] DEBUG: ---- End output of sc query WZCSVC | find /C /I "running" ----
[Mon, 24 Oct 2011 11:00:18 -0400] DEBUG: Ran sc query WZCSVC | find /C /I "running" returned 0
the service WZCSV was found
[Mon, 24 Oct 2011 11:00:18 -0400] INFO: ruby_block[first-search] called

But that all looks healthy - any suggestions?

On Thu, Oct 20, 2011 at 2:48 PM, Maven User <maven.2.user@gmail.com (mailto:maven.2.user@gmail.com)> wrote:

Hello again all -

First - thanks all for all the really helpful replies to any/all earlier posts. I've been able to move pretty quickly because of the help.

Now I have a tougher question.

I'm trying to work with a windows service for an item that we build and install.

This means stopping it if it exists, installing it if it doesn't and then starting it if it isn't already running.

I've tried to make the only_if/not_if be a "sc query ", but I'm seeing mixed results in different windows servers.

On Windows 7, if the service doesn't exist, I get a non-zero (1060) return value, on Windows server 2003 (R2), I get 0 regardless if the service exists or doesn't exist.

I've been thinking about just parsing the output of the "sc query" command versus just relying on the return value, but this is starting to feel kludgy.

Any suggestions?