Skip to content

Adding feature to postgres returner to save minion which are expected to return for a job #67930

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions salt/returners/postgres.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,9 @@

import logging
import sys
import ast
import subprocess
import json
from contextlib import contextmanager

import salt.exceptions
Expand Down Expand Up @@ -269,6 +272,25 @@ def event_return(events):
cur.execute(sql, (tag, salt.utils.json.dumps(data), __opts__["id"]))


def all_minions(tgt,tgt_type):
"""
Retrive all the minions which are expected to return for a job based of the target or target type passed.
"""
if tgt_type == 'glob':
salt_command = ['salt',tgt,'--preview-target','--out=text']
elif tgt_type == 'compound' or tgt_type == 'list' or tgt_type == 'grain':
if tgt_type == "compound":
tgt_type = '-C'
elif tgt_type == "list":
return tgt
elif tgt_type == "grain":
tgt_type = '-G'
salt_command=['salt',tgt_type,tgt,'--preview-target','--out=text']
result = subprocess.run(salt_command, capture_output=True, text=True, check=True)
minions= ast.literal_eval(result.stdout)
return minions


def save_load(jid, load, minions=None): # pylint: disable=unused-argument
"""
Save the load to the specified jid id
Expand All @@ -280,6 +302,10 @@ def save_load(jid, load, minions=None): # pylint: disable=unused-argument
VALUES (%s, %s)"""

json_data = salt.utils.json.dumps(salt.utils.data.decode(load))
load_dict = json.loads(json_data)
if load_dict.get('tgt') and load_dict.get('tgt_type') is not None:
load_dict['Minions'] = all_minions(load_dict['tgt'], load_dict['tgt_type']) #add a key 'Minions' containing list of minions which are expected to return
json_data = json.dumps(load_dict)
try:
cur.execute(sql, (jid, json_data))
except psycopg2.IntegrityError:
Expand Down