Skip to content

All data mixed on STDOUT together should generate valid JSON output #25154

Closed

Description

@uvsmtid

Based on this stackoverflow question.

Problem

Config in /etc/salt/master:

show_jid: True

Dummy state in /srv/salt/dummy.sls:

dummy_state:
    cmd.run:
        - name: "echo dummy"

Command:

salt --out json '*' state.sls dummy

Output (for 1 minion):

jid: 20150705154900243813
{
    "elephant": {
        "cmd_|-dummy_state_|-echo dummy_|-run": {
            "comment": "Command \"echo dummy\" run", 
            "name": "echo dummy", 
            "start_time": "15:49:04.196608", 
            "result": true, 
            "duration": 7.117, 
            "__run_num__": 0, 
            "changes": {
                "pid": 6501, 
                "retcode": 0, 
                "stderr": "", 
                "stdout": "dummy"
            }
        }
    }
}

This output is not valid JSON - it contains jid.

The problem is that instead of using JSON output directly, one has to create a special parser to weed out all interleaved JSON and non-JSON text before feeding it to the parser. Or, in short, the output is unusable.

Solution

The solution should stem from requirement to have valid requested format on STDOUT no matter what.

Ideally, I can think of an aggregator which collects outputs from everywhere (1 code honouring show_jid, 2 state modules, 3 anything else) and formats it properly on STDOUT based on user request (JSON, YAML, etc.). Otherwise, when each module or feature generates data on its own, it is hard to think about how they can insert their part of text to create valid format as a whole.

Test

load_json.py:

import sys
import json
data = json.load(sys.stdin)
print str(data)

For example, capturing the output above and feeding it into standard JSON parser in Python does not work:

salt --out json '*' state.sls dummy > output.json
Traceback (most recent call last):
  File "load_json.py", line 4, in <module>
    data = json.load(sys.stdin)
  File "/usr/lib64/python2.7/json/__init__.py", line 290, in load
    **kw)
  File "/usr/lib64/python2.7/json/__init__.py", line 338, in loads
    return _default_decoder.decode(s)
  File "/usr/lib64/python2.7/json/decoder.py", line 366, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "/usr/lib64/python2.7/json/decoder.py", line 384, in raw_decode
    raise ValueError("No JSON object could be decoded")
ValueError: No JSON object could be decoded

Activity

  1. uvsmtid commented on Jul 5, 2015

    @uvsmtid
    ContributorAuthor

    #25153 is a similar issue specific to results generated by multiple minions.
    #14725 is a similar issue specific to salt-cloud.

  2. changed the title [-]All data mixed on STDOUT should generate valid JSON output[/-] [+]All data mixed on STDOUT together should generate valid JSON output[/+] on Jul 5, 2015
  3. added
    bug
    severity-medium
    Core
    P1
    on Jul 6, 2015
  4. added this to the Approved milestone on Jul 6, 2015
  5. jfindlay commented on Jul 6, 2015

    @jfindlay
    Contributor

    @uvsmtid, thanks for the report. Along with #25153, you are welcome to look at the code and help fix these issues or provide other input, thanks.

  6. uvsmtid commented on Jul 16, 2015

    @uvsmtid
    ContributorAuthor

    The issue can be considered fixed in 2015.5.3 because --static option cleans the output producing only result of requested command (there is no jid:). In other words, JSON format is not broken:

    salt --out json --static 'projector' state.sls dummy 2> /dev/null
    {
        "projector": {
            "cmd_|-dummy_state_|-echo dummy_|-run": {
                "comment": "Command \"echo dummy\" run", 
                "name": "echo dummy", 
                "start_time": "02:02:05.361428", 
                "result": true, 
                "duration": 4.486, 
                "__run_num__": 0, 
                "changes": {
                    "pid": 15385, 
                    "retcode": 0, 
                    "stderr": "", 
                    "stdout": "dummy"
                }
            }
        }
    }
    

    See also comment here.

  7. jfindlay commented on Jul 16, 2015

    @jfindlay
    Contributor

    @uvsmtid, thanks for finding that. I did not know that that option existed. Do you think we need better documentation on it? Otherwise I think this issue and #25153 could probably closed.

  8. uvsmtid commented on Jul 18, 2015

    @uvsmtid
    ContributorAuthor

    @jfindlay, the only thing I could suggest is to have --static be on by default at least in case of --out=json. It's natural to expect valid JSON by default. However, such change can wait until the problem gets large at scale (many reports and annoys everyone). The documentation is actually correct and has Note section specifically for --out=json - it's just the case when the note only makes sense when you already hit the problem.

    Maybe changing the first line by the second:

    - Without the static option, you will get a JSON string for each minion.
    + Without the static option, you will get a separate JSON string per minion which makes JSON output invalid as a whole.
    
  9. jfindlay commented on Jul 18, 2015

    @jfindlay
    Contributor

    @uvsmtid, you are welcome to submit a pull request. :-)

  10. rallytime commented on Sep 9, 2015

    @rallytime
    Contributor

    @uvsmtid Thanks again for this great explanation as well as for the documentation fix. I am going to go ahead and close this per your comments in #25153.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    CoreP1bugseverity-medium

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions