Skip to content

Instantly share code, notes, and snippets.

@bbinet
Last active March 15, 2021 14:13
Show Gist options
  • Save bbinet/d87bf2571652e4dec68df6b280b2af9d to your computer and use it in GitHub Desktop.
Save bbinet/d87bf2571652e4dec68df6b280b2af9d to your computer and use it in GitHub Desktop.
Use salt outputters to format pepper output, see https://github.com/saltstack/pepper/issues/3
#!/home/bruno/.virtualenvs/salt/bin/python
"""
This script formats pepper output using salt outputters.
It can both format sync and async jobs, and can be used in the following ways:
```
$ pepper \* state.sls core | pepper_format.py -o highstate
$ pepper --client=local_async \* state.highstate | pepper_format.py
$ pepper_format.py 20170504160400750792
```
```
$ pepper_format.py -h
usage: pepper_format.py [-h] [-o OUTPUTTER] [-v] [-p] [-n] [jid]
Format pepper output using any salt outputter
positional arguments:
jid Optional job id
optional arguments:
-h, --help show this help message and exit
-o OUTPUTTER, --outputter OUTPUTTER
Outputter to use
-v, --verbose Verbose output
-p, --profiling Activate profiling
-n, --no-color No color in output
```
"""
import subprocess
import sys
import json
import time
import argparse
import salt.config
from salt.output import display_output
def lookup_jid(jid):
return json.loads(subprocess.check_output(
['pepper', '--client=runner', 'jobs.lookup_jid', 'jid=%s' % jid]
))['return'][0]
def parse_output(ret):
outputter = None
if isinstance(ret, dict) and set(ret) == set(('data', 'outputter')):
outputter = ret['outputter']
ret = ret['data']
return ret, outputter
parser = argparse.ArgumentParser(description='Format pepper output using any salt outputter')
parser.add_argument('jid', nargs='?', help='Optional job id', default=None)
parser.add_argument('-o', '--outputter', help='Outputter to use')
parser.add_argument('-v', '--verbose', action='count', help='Verbose output')
parser.add_argument('-p', '--profiling', action='store_true', help='Activate profiling')
parser.add_argument('-n', '--no-color', action='store_true', help='No color in output')
args = parser.parse_args()
__opts__ = salt.config.minion_config('')
__opts__['color'] = not args.no_color
__opts__['state_output_profile'] = args.profiling
__opts__['state_verbose'] = args.verbose > 0
if args.verbose == 1:
__opts__['state_output'] = 'changes'
jid = args.jid
if not jid and not sys.stdin.isatty():
ret = json.load(sys.stdin)['return'][0]
if 'jid' in ret:
jid = ret['jid']
else:
ret, outputter = parse_output(ret)
display_output(ret, args.outputter or outputter, __opts__)
sys.exit(0)
if not jid:
print('Please specify a job id or provide stdin data to parse')
sys.exit(1)
print('Looking for job id: %s' % jid)
while True:
ret, outputter = parse_output(lookup_jid(jid))
if ret:
print
break
sys.stdout.write('.')
sys.stdout.flush()
time.sleep(1)
display_output(ret, args.outputter or outputter, __opts__)
@drts01
Copy link

drts01 commented Apr 23, 2019

Line 63 should be __opts__['state_verbose'] = args.verbose. I got a None type error. If args.verbose, that would still be falsy. I do not think it could ever be negative.

or __opts__['state_verbose'] = args.verbose AND args.verbose > 0

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