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 |
Dennis Dmitriev | ae6c72c | 2019-02-15 07:24:48 +0200 | [diff] [blame] | 23 | import yaml |
Dennis Dmitriev | 3ec2e53 | 2018-06-08 04:33:34 +0300 | [diff] [blame] | 24 | |
| 25 | from pepper import cli |
| 26 | |
| 27 | |
Dennis Dmitriev | 3ec2e53 | 2018-06-08 04:33:34 +0300 | [diff] [blame] | 28 | if len(sys.argv) <= 1: |
| 29 | sys.argv.append('--help') |
| 30 | |
Dennis Dmitriev | ae6c72c | 2019-02-15 07:24:48 +0200 | [diff] [blame] | 31 | # Use async request with checking for failed minions |
| 32 | sys.argv.append("--fail-if-incomplete") |
Dennis Dmitriev | 3ec2e53 | 2018-06-08 04:33:34 +0300 | [diff] [blame] | 33 | |
Dennis Dmitriev | ae6c72c | 2019-02-15 07:24:48 +0200 | [diff] [blame] | 34 | tries = 3 |
| 35 | for _ in range(tries): |
| 36 | results = [] |
| 37 | |
| 38 | runner = cli.PepperCli() |
| 39 | runner.parser.description = __doc__ |
| 40 | |
| 41 | for res in runner.run(): |
| 42 | results.append(res) |
| 43 | if results and len(results) > 1: |
| 44 | break |
| 45 | |
| 46 | # Expected: list of two touples: |
| 47 | # - first touple should contain the result in string format (pepper issue) |
| 48 | # - second touple should contain failed nodes |
| 49 | # Example: |
| 50 | # [(None, '"{cid01.cookied-cicd-queens-dvr-sl.local: "some data"}"'), |
| 51 | # (0, '"{Failed: []}"')] |
| 52 | # Example when node is not responding: |
| 53 | # [(1, '"{Failed: [u\'cid01.cookied-cicd-queens-dvr-sl.local\']}"')] |
| 54 | |
| 55 | if not results or len(results) < 2: |
Dennis Dmitriev | 3ec2e53 | 2018-06-08 04:33:34 +0300 | [diff] [blame] | 56 | print("Empty response", file=sys.stderr) |
| 57 | sys.exit(1) |
| 58 | |
Dennis Dmitriev | ae6c72c | 2019-02-15 07:24:48 +0200 | [diff] [blame] | 59 | if len(results) > 2: |
Dennis Dmitriev | 3ec2e53 | 2018-06-08 04:33:34 +0300 | [diff] [blame] | 60 | print("Too many results", file=sys.stderr) |
| 61 | sys.exit(1) |
| 62 | |
Dennis Dmitriev | ae6c72c | 2019-02-15 07:24:48 +0200 | [diff] [blame] | 63 | if results[-1][0] != 0: |
Dennis Dmitriev | 3ec2e53 | 2018-06-08 04:33:34 +0300 | [diff] [blame] | 64 | print("Error code returned", file=sys.stderr) |
| 65 | sys.exit(results[0][0]) |
| 66 | |
Dennis Dmitriev | ae6c72c | 2019-02-15 07:24:48 +0200 | [diff] [blame] | 67 | data = yaml.load(json.loads(results[0][1])) |
| 68 | nodes = data.keys() |
Dennis Dmitriev | 3ec2e53 | 2018-06-08 04:33:34 +0300 | [diff] [blame] | 69 | |
| 70 | if not nodes: |
| 71 | print("Wrong target: no minions selected", file=sys.stderr) |
| 72 | sys.exit(1) |
| 73 | |
| 74 | if len(nodes) > 1: |
| 75 | print("Wrong target: too many minions selected: {0}" |
| 76 | .format(nodes), file=sys.stderr) |
| 77 | sys.exit(1) |
| 78 | |
Dennis Dmitriev | ae6c72c | 2019-02-15 07:24:48 +0200 | [diff] [blame] | 79 | print(data[nodes[0]]) |