service.running to only start a service if a previous state stopped it

696 views
Skip to first unread message

Tanoti

unread,
Mar 6, 2014, 9:56:51 AM3/6/14
to salt-...@googlegroups.com
I have a deployment state enforcing a sequence of:
  1. stop service
  2. deploy rpm package
  3. do some config stuff not covered by the rpm
  4. start service
How can I write a state such that the service is only started if it was running when the state file was executed and has been stopped by the first part of the state?

John

Joseph Hall

unread,
Mar 6, 2014, 11:04:40 AM3/6/14
to salt-...@googlegroups.com
I think what you'll want is more along the lines of prereq, which was
added for specifically this use case:

http://docs.saltstack.com/ref/states/requisites.html#prereq

Basic idea: prereq checks to see if an event is going to happen, and
perform an action beforehand (i.e., if a package will be upgraded,
stop the corresponding service first). Then you add a service watch to
the package upgrade, and you should be good to go.
> --
> You received this message because you are subscribed to the Google Groups
> "Salt-users" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to salt-users+...@googlegroups.com.
> For more options, visit https://groups.google.com/groups/opt_out.



--
"In order to create, you have to have the willingness, the desire to
be challenged, to be learning." -- Ferran Adria (speaking at Harvard,
2011)

Tanoti

unread,
Mar 7, 2014, 4:15:49 AM3/7/14
to salt-...@googlegroups.com
You suggestion is interesting and I might include that as it make logical sense but I don't think it addresses my problem in that I need to bring up the service after the deployment but only if it was running before we started. The service must not be started if it has been manually shut down. I think the problem is that the service.dead state returns True for both a service being shut down and for not having to be shut down as it was already down or not installed.

Ethan Erchinger

unread,
Mar 7, 2014, 1:02:55 PM3/7/14
to salt-...@googlegroups.com
Give this a shot.

{% set status = salt['service.status']('service_name') %}

service_name_stop:
  service.dead

rpm_name:
  pkg.latest
    - require:
      - service: service_name_stop

config_stuff:
  file.managed:
    ...
    - require:
      - pkg: rpm_name

{% if status %}
service_name_start:
  service.running
    - require:
      - file: config_stuff
{% endif %}

Ethan

Ethan Erchinger

unread,
Mar 7, 2014, 1:03:42 PM3/7/14
to salt-...@googlegroups.com
There's some typo's in there, but you get the idea.

Tanoti

unread,
Mar 10, 2014, 5:24:42 AM3/10/14
to salt-...@googlegroups.com
Ethan, thank you ever so much for this, it works a treat!

John
Reply all
Reply to author
Forward
0 new messages