blob: 19425dce1546036019fee3a6f9c0c7d1d7344850 [file] [log] [blame]
Dennis Dmitriev3ec2e532018-06-08 04:33:34 +03001#!/usr/bin/env python
2
3"""
4A wrapper to ``pepper``, a CLI interface to a remote salt-api instance.
5
6Return a single parameter from the salt model for specified
7target and pillar.
8
9Fails if the result contains more than one parameter.
10
11Use the pepper CLI parameters to set salt-api access parameters
12or 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"""
19from __future__ import print_function
20
21import sys
22import json
Dennis Dmitrievae6c72c2019-02-15 07:24:48 +020023import yaml
Dennis Dmitriev3ec2e532018-06-08 04:33:34 +030024
25from pepper import cli
26
27
Dennis Dmitriev3ec2e532018-06-08 04:33:34 +030028if len(sys.argv) <= 1:
29 sys.argv.append('--help')
30
Dennis Dmitrievae6c72c2019-02-15 07:24:48 +020031# Use async request with checking for failed minions
32sys.argv.append("--fail-if-incomplete")
Dennis Dmitriev3ec2e532018-06-08 04:33:34 +030033
Dennis Dmitrievae6c72c2019-02-15 07:24:48 +020034tries = 3
35for _ 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
55if not results or len(results) < 2:
Dennis Dmitriev3ec2e532018-06-08 04:33:34 +030056 print("Empty response", file=sys.stderr)
57 sys.exit(1)
58
Dennis Dmitrievae6c72c2019-02-15 07:24:48 +020059if len(results) > 2:
Dennis Dmitriev3ec2e532018-06-08 04:33:34 +030060 print("Too many results", file=sys.stderr)
61 sys.exit(1)
62
Dennis Dmitrievae6c72c2019-02-15 07:24:48 +020063if results[-1][0] != 0:
Dennis Dmitriev3ec2e532018-06-08 04:33:34 +030064 print("Error code returned", file=sys.stderr)
65 sys.exit(results[0][0])
66
Dennis Dmitrievae6c72c2019-02-15 07:24:48 +020067data = yaml.load(json.loads(results[0][1]))
68nodes = data.keys()
Dennis Dmitriev3ec2e532018-06-08 04:33:34 +030069
70if not nodes:
71 print("Wrong target: no minions selected", file=sys.stderr)
72 sys.exit(1)
73
74if len(nodes) > 1:
75 print("Wrong target: too many minions selected: {0}"
76 .format(nodes), file=sys.stderr)
77 sys.exit(1)
78
Dennis Dmitrievae6c72c2019-02-15 07:24:48 +020079print(data[nodes[0]])