2

We using Boxen/Puppet to automate the build of our Mac development laptops, one step is to install rabbitmq, declare vhosts and add users.

But it takes little bit more time for rabbitmq to be ready for vhosts declaration and adding new users so we always have to run boxen twice.

Here is my logic

Checking if rabbitmq is ready by running following command rabbitmqadmin list vhost But we need rabbitmq installed and running so I added require => Service['dev.rabbitmq'] If above commands worked then we know rabbit is accepting connections.

Here, let's put everything together.

  exec { "Wait for rabbitmq":
    command => "rabbitmqadmin list vhosts",
    tries   => 2,
    try_sleep => 30,
    require   => Service['dev.rabbitmq']
  }
  dev::rabbitmq::vhost { '/clearvh':
    require => Exec['Wait for rabbitmq']
  }
  dev::rabbitmq::user { 'clear': password => 'password' }
  dev::rabbitmq::permission { [ 'guest', 'clear' ]: vhost => '/clearvh' }
}

That works well, but Exec {"Wait for rabbit"} is getting called every time, even when dev::rabbit::vhost is not. it would be idea if it is only called by dev::rabbitmq::vhost.

Is that possible ?

Thanks

Ask and Learn
  • 8,661
  • 8
  • 37
  • 43
  • 1
    You could try with `refreshonly => true` and `subscribe` the service instead of just requiring it. But I'm not sure whether the retry semantics are applied to `refresh` actions as opposed to the actual sync that you are doing now. If not, you could work around that by executing a wrapper script that does the fail/sleep/retry loop internally. – Felix Frank Aug 29 '14 at 08:52
  • `refreshonly`/`subscribe` would also have the advantage that if Puppet doesn't have to (re)start the service then the `Exec` will not run. But I also do not know whether Puppet will retry on an `Exec` refresh. I'm guessing it will do, but I don't know. Could you instead use the shell provider and exec "sleep 30; rabbitmqadmin list vhosts"? – John Bollinger Aug 29 '14 at 15:55

1 Answers1

0

Your going about the exec the wrong way. You need to have a condition to stop the exec from running.

exec { "Wait for rabbitmq":
  command => "rabbitmqadmin list vhosts",
  tries   => 2,
  try_sleep => 30,
  unless    => 'some command which returns "0" that tells you rabbitmq is already ready for vhosts',
  require   => Service['dev.rabbitmq']

}

Unless you tell your exec not to run for some reason or another, it will always run.

See the Puppet Type Reference for the exec resource and look for unless and onlyif.

And as for the title of this question which I originally overlooked. require means that the the resource passed to require must be applied before the calling resource. So in this case

Service['dev.rabbitmq']

will be applied before

Exec['Wait for rabbitmq'].

require does not ensure that an exec resource will not be run.

ptierno
  • 9,534
  • 2
  • 23
  • 35
  • Let's forgot about what I did, what would you do to check rabbitmq is ready(accepting connections) before declare a vhost ? `onlyif` is not what I wanted because if it failed, my vhost declaration will just be skipped. – Ask and Learn Aug 29 '14 at 04:57
  • you may be able to keep the exec. use `tries` and `try_sleep` parameters to your exec. I'm going to update my answer with it. If it is still not what your looking for we can explore further. – ptierno Aug 31 '14 at 03:09
  • heh. disregard that. didn't realize i had `tries` and `try_sleep` in there already :) – ptierno Aug 31 '14 at 03:10
  • Assuming your puppet master can reach your rabbitmq server. maybe write a custom function to ensure the rabbitmq server is ready? – ptierno Aug 31 '14 at 03:12
  • My `Exec wait for rabbitmq` is to check if rabbitmq is ready before declaring vhost, it works fine I am just trying to find a way so it will only be called if the vhost does not exist but looks like exec is not designed for that. – Ask and Learn Aug 31 '14 at 07:07