Skip to content

Instantly share code, notes, and snippets.

@whiteinge
Last active October 3, 2021 21:49
Show Gist options
  • Save whiteinge/b3c2ee46241e0512ef5b to your computer and use it in GitHub Desktop.
Save whiteinge/b3c2ee46241e0512ef5b to your computer and use it in GitHub Desktop.
POC for writing status updates to a sqlite db. Updates are written using an Orchestrate file. The Orchestrate file can be called via the Reactor.
{# Place this file in /srv/reactor/process_status_updates.sls
Call via sending a custom event on a minion as:
salt-call event.send myco/external_task/status_update task=foo status='Update number one!'
Requires reactor config such as:
reactor:
- 'myco/external_task/status_update':
- /srv/reactor/process_status_updates.sls
#}
process_status_updates:
runner.state.orchestrate:
- mods: orch.write_to_db
- pillar:
mid: {{ data.id }}
task: {{ data.data.task }}
status: {{ data.data.status }}
{# Place this file in /srv/salt/orch/write_to_db.sls
Call from the CLI as:
salt-run state.orchestrate orch.write_to_db pillar='{task: foo, status: "Update number one!"}'
#}
{# Create the db if it doesn't yet exist. #}
{% set db_file = '/tmp/job_events.sqlite3' %}
{% if not salt['file.file_exists'](db_file) %}
create_db:
module.run:
- name: sqlite3.modify
- db: {{ db_file }}
- sql: |
CREATE TABLE log_entries(
id INTEGER PRIMARY KEY AUTOINCREMENT,
task TEXT,
status_update TEXT
);
{% endif %}
{# Pull the db entry from Pillar data passed in via the CLI. Don't write to the
DB if the update is empty. #}
{% set task = salt['pillar.get']('task', '') %}
{% set status = salt['pillar.get']('status', '') %}
{% if task != '' and status != '' %}
write_event_updates_to_db:
module.run:
- name: sqlite3.modify
- db: {{ db_file }}
- sql: |
INSERT INTO log_entries VALUES(
null,
'{{ task }}',
'{{ status }}'
);
log_success_to_minion:
salt.function:
- tgt: {{ salt['pillar.get']('mid') }}
- name: cmd.run
- arg:
- |
echo "Success" > /some/log_file/on/the/minion.log
- onchanges:
- module: write_event_updates_to_db
notify_on_failure:
cmd.run:
- name: echo 'sqlite falure!' >> sqlite.log
- onfail:
- module: write_event_updates_to_db
{% endif %}
@marbx
Copy link

marbx commented Apr 11, 2015

if you are on a Windows minion, you must double quotes for the string:

salt-call event.send myco/external_task/status_update task=foo status="Update number one!"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment