Ask Your Question
0

exec to uninstall windows service?

asked 2014-12-03 07:05:35 -0500

tek0011 gravatar image

I am trying to do the following:

stop a windows service replace files start the windows service

The issue I am running in to is trying to stop the service onlyif it exists (lines 4-12). If I try to stop the service and it doesn't exist, then puppet errors out. I can not seem to get it to work with unless or onlyif. I can't use service because of re-declaring issues. I tried a module called puppetlabs-transition, but that only checks if it exists, not if it doesnt exist.

  1 # Installs and manages mcollective service for windows
  2 class windows_mcollective {
  3
  4         # Stops mcollective service. In case log file is needed to be written over.
  5         exec { 'mcollectivedstop':
  6                 command => 'sc stop mcollectived',
  7                 onlyif  => 'sc query mcollectived',
  8                 path    => $::path,
  9         #       unless  => 'C:\\Windows\\System32\\cmd.exe /c sc query \"mcollectived\" | find \"STOPPED\"',
 10                 logoutput => true,
 11                 before  => File['C:\marionette-collective'],
 12         }
 13
 14         # Stop mcollectived service. mcollectived.log gets locked when service is running.
 15         #transition { 'stop mcollectived':
 16         #       resource        => Service['mcollectived'],
 17         #       attributes      => { ensure => stopped },
 18         #       prior_to        => File['C:\marionette-collective'],
 19         #       onlyif          => "C:\\Windows\\System32\\cmd.exe /c sc query \"mcollectived\"",
 20         #}
 21
 22
 23         # Copies over main mcollective files
 24         file { 'C:\marionette-collective':
 25                 ensure  => present,
 26                 source  => 'puppet:///extra_files/mcollective_windows',
 27                 recurse => true,
 28         }
 29
 30         # Populates facts.yaml file
 31         file{ 'C:\marionette-collective\etc\facts.yaml':
 32                 loglevel => debug, # reduce noise in Puppet reports
 33                 content  => inline_template("<%= scope.to_hash.reject { |k,v| k.to_s =~ /(uptime_seconds|timestamp|free)/ }.to_yaml %>"), # exclude rapidly changing facts
 34                 require  =>  File['C:\marionette-collective'],
 35         }
 36
 37         # Runs batch file to create mcollective as a service.
 38         exec { 'create service':
 39                 command     => '"C:\marionette-collective\bin\register_service.bat"',
 40                 path        => "$::path",
 41                 require     => File['C:\marionette-collective'],
 42                 subscribe   => File['C:\marionette-collective'],
 43                 refreshonly => true,
 44                 before      => Service['mcollectived'],
 45
 46                 # Attempted to run only when the service did not exist. Line below does not work, but is in correct direction.
 47                 # Without line below, will call bat file everytime mcollective files change. Results in an ignorable error.
 48 #               onlyif      => 'C:\Windows\System32\net.exe start | C:\Windows\System32\find.exe "The Marionette Collective"',
 49         }
 50
 51         # Ensures that the mcollective service is running and starts at boot.
 52         service { 'mcollectived':
 53                 ensure  => running,
 54                 enable  => true,
 55                 require => Exec['create service'],
 56         }
 57 }

I found two links online of people that were able to get it to work with exec, but the same syntax is not working for me:

define disable_services {
  exec { "stop_${title}":
    command => "C:\\Windows\\System32\\cmd.exe /c sc stop \"${title}\"",
    onlyif  => "C:\\Windows\\System32\\cmd.exe /c sc query \"${title}\"",
    unless  => "C:\\Windows\\System32\\cmd.exe /c sc query \"${title}\" | find \"STOPPED\"",
    before  => Exec["disable_${title}"],
    }

Anyone have any idea how I can get this done? I have some nodes where I need to overwrite an existing file, but the service locks that file, so puppet fails. I have other nodes where the service exists, so ... (more)

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
0

answered 2014-12-03 12:27:36 -0500

tek0011 gravatar image

I was able to resolve this using exec with:

  4         # Stops mcollective service. In case log file is needed to be written over.
  5         exec { 'mcollectivedstop':
  6                 command => 'sc stop mcollectived',
  7                 onlyif  => 'reg query HKLM\SYSTEM\CurrentControlSet\Services\mcollectived',
  8                 path    => $::path,
  9                 logoutput => true,
 10                 before  => File['C:\marionette-collective'],
 11         }

Specifically line 7. I ended up searching for the reg entry, instead of querying the sc service.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower

Stats

Asked: 2014-12-03 07:05:35 -0500

Seen: 717 times

Last updated: Dec 03 '14