Ask Your Question

Stop service, do something, start service: Error 400 on SERVER

asked 2015-06-11 10:09:43 -0500

sinned gravatar image

I am managing a windows machine where I have installed a service using puppet.

When the files of the service are updated, I want the service to be stopped. After the update, the service should be started.

Background is, the "service" here is actually a bunch of services, written by different departments and I don't know what they are doing. So during maintenance, I can shut those services down, one by one, and update their files through puppet. After all is done, I can start the services again. This is to ensure they work with valid data. I want to avoid cases where a part of their files are changed during the process, making their filebase inconsistent. On a request, they could get into a undefined state or something.

So, puppet doesn't allow stopping and starting a service, because they would have the same name:

service { 'stop the thing':
  ensure => stopped,
  name => $servicename,
} -> 
file { 'update the files':
  ensure => directory,
  path => 'C:\foo\bar',
  recurse => true,
  purge => true,
  force => true,
  source => 'puppet:///modules/shinyservice',
  source_permissions => ignore,
} -> 
service { 'start the thing':
  ensure => running,
  name => $servicename,

The error I get looks like this:

Error: Could not retrieve catalog from remote server: Error 400 on SERVER: Puppet::Parser::AST::Resource failed with error ArgumentError: Cannot alias Service[xxx: start xxx] to ["xxx"] at /etc/puppet/modules/shinyservice/manifests/init.pp:48; resource ["Service", "xxx"] already declared at /etc/puppet/modules/shinyservice/manifests/init.pp:24 at /etc/puppet/modules/shinyservice/manifests/init.pp:48 on node

The line number points to the closing bracket of the second service ressource.

I could of course put a command into a powershell script and run that to stop/start the service, but is there really no way to do this with puppet?

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted

answered 2015-06-11 19:53:08 -0500

cbarbour gravatar image

You are trying to write a procedure. Puppet doesn't work that way; Puppet is a declarative language. You declare the desired state of the system, rather than the procedure to reach that state. What you're doing here is giving Puppet conflicting directives; you are asking it to make 'the thing' simultaneously both running and stopped, and asking 'the thing' to be simultaneously managed before and after the files. This causes Puppet to throw an error.

The normal solution to this problem is to create a notify relationship from the files to the service. If the files are updated, Puppet will automatically restart the service. This has the bonus advantage that service will not be stopped unless the files are actually updated.

edit flag offensive delete link more


How to stop the service before the files are changed? I don't want the service to run during the update. Is there really no way to achieve this with puppet?

sinned gravatar imagesinned ( 2015-06-12 02:25:06 -0500 )edit

You can use an exec resource to stop the service before copying the files, but I strongly advise not using that approach, because it violates a lot of Puppet's design principles. If you use that approach, you will probably render your code non-idempotent.

cbarbour gravatar imagecbarbour ( 2015-06-14 18:35:34 -0500 )edit

@cbarbour: What else should I do? Just let the service keep running? I could script everything, maybe build an MSI. But then I would loose the powerful puppet directory synchronization feature. Also, it's more work.

sinned gravatar imagesinned ( 2015-06-15 01:32:08 -0500 )edit

If your application allows it, the best approach is to leave the service running. Creating a notify relationship between your file and service resource will cause the service to restart only if the files are updated. For this kind of problem, an orchistration / deployment tool is good too.

cbarbour gravatar imagecbarbour ( 2015-06-15 02:11:18 -0500 )edit

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


Asked: 2015-06-11 10:09:43 -0500

Seen: 1,341 times

Last updated: Jun 11 '15