blob: 6179e708b7545b50142e265b86b57c7d41e7f0f4 [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
23
24from pepper import cli
25
26
27runner = cli.PepperCli()
28runner.parser.description = __doc__
29
30
31if len(sys.argv) <= 1:
32 sys.argv.append('--help')
33
34results = []
35for res in runner.run():
36 results.append(res)
37
38if not results:
39 print("Empty response", file=sys.stderr)
40 sys.exit(1)
41
42if len(results) > 1:
43 print("Too many results", file=sys.stderr)
44 sys.exit(1)
45
46if results[0][0] != 0:
47 print("Error code returned", file=sys.stderr)
48 sys.exit(results[0][0])
49
50data = json.loads(results[0][1])
51nodes = data['return'][0].keys()
52
53if not nodes:
54 print("Wrong target: no minions selected", file=sys.stderr)
55 sys.exit(1)
56
57if len(nodes) > 1:
58 print("Wrong target: too many minions selected: {0}"
59 .format(nodes), file=sys.stderr)
60 sys.exit(1)
61
62print(data['return'][0][nodes[0]])