Create a gist now

Instantly share code, notes, and snippets.

@MTecknology /git_hook Secret
Created Apr 10, 2018

Embed
What would you like to do?
root@prd-git-01:/var/lib/gitolite3/repositories/salt/states.git# cat hooks/post-receive.h00-salt-global-highstate
#!/bin/sh
sudo /usr/local/sbin/saltevent 'global' 'highstate'
{% if 'action' in data['data'] %}
{% set action = data['data']['action'] %}{% endif %}
{% if 'host' in data['data'] %}
{% set host = data['data']['host'] %}{% endif %}
{% if 'env' in data['data'] %}
{% set env = data['data']['env'] %}{% endif %}
{% set tick = salt['sdb.get']('sdb://localsdb/hst_tick') %}
{% set src = data['id'] %}
##
# git.lustfield.net
##
{% if src == 'git.lustfield.net' and action == 'highstate' %}
update_fileserver:
runner.fileserver.update:
- tgt: salt.lustfield.net
{% if 'env' in data['data'] %}
highstate_run:
local.state.highstate:
- tgt: "G@id:{{ host }}* and G@environment:{{ env }}"
- expr_form: compound
- require:
- runner: update_fileserver
{% elif host == 'global' %}
{% if not tick or (tick > 10 or tick == 0) %}
{##
# This is my magic highstate scheduling!
# When a salt event comes in from the git server that requests
# a highstate be pushed across the environment, check the
# current tick value. If a global highstate was not recently
# executed, then set the tick to 15.
#
# A separate process on the salt master is used to decrease
# this tick value by one every minute and triggers a global
# highstate runner when the value reaches zero.
#
# This results in a global highstate orchestration run being
# scheduled with a five minute delay when certain git repositories
# are pushed to. It also creates a delay before the next global
# higshtate can be triggered, meaning time for this process to
# complete.
#
# Message me if this deserves a separate blog post!
##}
highstate_countdown:
local.sdb.set:
- tgt: salt.lustfield.net
- arg:
- sdb://local/hst_tick
- '15'
{% endif %}
{% else %}
highstate_run:
local.state.highstate:
- tgt: "{{ host }}*"
- require:
- runner: update_fileserver
{% endif %}
#!/usr/bin/env python
'''
Author: Michael Lustfield (MTecknology)
License: CC BY-NC-ND 4.0
https://creativecommons.org/licenses/by-nc-nd/4.0/
Description: This script is meant to be triggered via the salt scheduler. It
reads a counter stored in sdb and drops the value by one. If the
counter reaches a specified value, a global highstate is triggered
via the orchestrate runner. Setting the value and the conditions
in which it's set are handled by separate logic.
'''
import salt.config
import subprocess
import time
from salt.utils.sdb import sdb_get, sdb_set
opts = salt.config.minion_config('/etc/salt/minion')
tick = int(sdb_get('sdb://localsdb/hst_tick', opts))
if tick:
if tick > 0:
sdb_set('sdb://localsdb/hst_tick', tick - 1, opts)
if tick == 10:
p = subprocess.Popen(
['/usr/bin/salt-run', 'state.orch', '_orchestrate.global_highstate'],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE)
stdout, stderr = p.communicate()
if stdout or stderr:
with open('/var/log/salt/global_highstate.log', 'a') as fh:
fh.write('{}\n'.format(time.strftime("%c")))
if stdout:
fh.write('{}\n'.format(stdout))
if stderr:
fh.write('{}\n'.format(stderr))
fh.close()
localsdb:
driver: sqlite3
database: /var/cache/salt/local.db
table: sdb_local
create_table: True
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment