Dennis Dmitriev | 3ec2e53 | 2018-06-08 04:33:34 +0300 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 2 | |
| 3 | """ |
| 4 | A wrapper to ``pepper``, a CLI interface to a remote salt-api instance. |
| 5 | |
| 6 | Return a single parameter from the salt model for specified |
| 7 | target and pillar. |
| 8 | |
| 9 | Fails if the result contains more than one parameter. |
| 10 | |
| 11 | Use the pepper CLI parameters to set salt-api access parameters |
| 12 | or set the environment variables: |
| 13 | |
| 14 | export SALTAPI_URL=http://${SALT_MASTER_IP}:6969/; |
| 15 | export SALTAPI_USER='salt'; |
| 16 | export SALTAPI_PASS='pass'; |
| 17 | export SALTAPI_EAUTH='pam'; |
| 18 | """ |
| 19 | from __future__ import print_function |
| 20 | |
| 21 | import sys |
| 22 | import json |
| 23 | |
| 24 | from pepper import cli |
| 25 | |
| 26 | |
| 27 | runner = cli.PepperCli() |
| 28 | runner.parser.description = __doc__ |
| 29 | |
| 30 | |
| 31 | if len(sys.argv) <= 1: |
| 32 | sys.argv.append('--help') |
| 33 | |
| 34 | results = [] |
| 35 | for res in runner.run(): |
| 36 | results.append(res) |
| 37 | |
| 38 | if not results: |
| 39 | print("Empty response", file=sys.stderr) |
| 40 | sys.exit(1) |
| 41 | |
| 42 | if len(results) > 1: |
| 43 | print("Too many results", file=sys.stderr) |
| 44 | sys.exit(1) |
| 45 | |
| 46 | if results[0][0] != 0: |
| 47 | print("Error code returned", file=sys.stderr) |
| 48 | sys.exit(results[0][0]) |
| 49 | |
| 50 | data = json.loads(results[0][1]) |
| 51 | nodes = data['return'][0].keys() |
| 52 | |
| 53 | if not nodes: |
| 54 | print("Wrong target: no minions selected", file=sys.stderr) |
| 55 | sys.exit(1) |
| 56 | |
| 57 | if len(nodes) > 1: |
| 58 | print("Wrong target: too many minions selected: {0}" |
| 59 | .format(nodes), file=sys.stderr) |
| 60 | sys.exit(1) |
| 61 | |
| 62 | print(data['return'][0][nodes[0]]) |