Skip to content

Instantly share code, notes, and snippets.

@ari
Forked from whiteinge/complex_salt_orchestrate.sls
Created August 21, 2017 00:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ari/4f0efff2dddcd210156beea7e250bc67 to your computer and use it in GitHub Desktop.
Save ari/4f0efff2dddcd210156beea7e250bc67 to your computer and use it in GitHub Desktop.
An example of a complex, multi-host Salt Orchestrate state that performs status checks as it goes
{% set backends = [ 'colo.dash', 'colo.flash' ] %}
{% set applications = [ 'willow_api', 'willow_services' ] %}
####
# Loop through the applications, and for each loop through the backends
####
{% for application in applications %}
{% for backend in backends %}
{% set pillar_data = salt['saltutil.runner']('pillar.show_pillar', minion=backend)['applications'] %}
{% set ip = pillar_data.ip %}
{% set application_data = pillar_data[application] %}
{% if application_data.port is not number %}
{% do salt.test.exception('The port ' + str(application_data.port) + ' is not valid.' ) %}
{% endif %}
haproxy_stop_{{ application }}_{{ backend }}:
salt.function:
- tgt: smash*
- name: haproxy.disable_server
- arg:
- {{ backend }}
- {{ application }}
# Deploy the application
upgrade_{{ application }}_{{ backend }}:
salt.state:
- tgt: {{ backend }}
- highstate: True
# Using prereq here prevents stopping haproxy if the highstate run
# above will not produce any changes! I.e., if we're already running the
# right version then the Orchestrate loop for this node does nothing.
- prereq_in:
- haproxy_stop_{{ application }}_{{ backend }}
# Wait for the new application to become available by repeatedly trying to
# connect to the host and port that it will be running on.
application_test_{{ application }}_{{ backend }}:
http.wait_for_successful_query:
- name: http://{{ ip }}:{{ application_data.port }}{{ application_data.check_path }}
- request_interval: 5
- status: 200
- match: {{ application_data.version }}
- require:
- upgrade_{{ application }}_{{ backend }}
haproxy_start_{{ application }}_{{ backend }}:
salt.function:
- tgt: smash*
- name: haproxy.enable_server
- arg:
- {{ backend }}
- {{ application }}
- require:
- application_test_{{ application }}_{{ backend }}
failure_abort_{{ application }}_{{ backend }}:
module.run:
- name: test.exception
- message: 'Error deploying {{ application }} to {{ backend }}. Before trying again check the loadbalancer might be in MAINT mode'
{# failhard here will abort the Orchestrate run early. #}
- failhard: True
{% endfor %}
{% endfor %}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment