Mercurial > hg > KCl
comparison kcl/kcl.py @ 0:0f44ee073173 default tip
fake salt, initial commit
author | Jeff Hammel <k0scist@gmail.com> |
---|---|
date | Mon, 06 Feb 2017 01:10:22 +0000 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:0f44ee073173 |
---|---|
1 #!/usr/bin/env python | |
2 # -*- coding: utf-8 -*- | |
3 | |
4 """ | |
5 fake salt | |
6 """ | |
7 | |
8 # imports | |
9 import argparse | |
10 import json | |
11 import ssh | |
12 import sys | |
13 | |
14 class KCl(object): | |
15 """fake salt's interface to real salt""" | |
16 | |
17 def __init__(self, salt_master): | |
18 self.salt_master = salt_master | |
19 self.ssh = ssh.Ssh(self.salt_master) | |
20 | |
21 def __call__(self, target, module, *args): | |
22 command = self.command(target, module, *args) | |
23 code, output = self.ssh.sudo(command) | |
24 | |
25 return code, json.loads(output) | |
26 | |
27 def command(self, target, module, *args): | |
28 retval = "salt --output=json --static '{target}' {module}".format(target=target, module=module) | |
29 for arg in args: | |
30 retval += " '{arg}'".format(arg=arg) | |
31 return retval | |
32 | |
33 def ping(self, target='*'): | |
34 return self(target, 'test.ping') | |
35 | |
36 def run(self, command, target='*'): | |
37 return self(target, 'cmd.run', command) | |
38 | |
39 | |
40 def main(args=sys.argv[1:]): | |
41 """CLI""" | |
42 | |
43 # parse command line | |
44 parser = argparse.ArgumentParser(description=__doc__) | |
45 parser.add_argument('salt_master') | |
46 options = parser.parse_args(args) | |
47 | |
48 # instantiate KCl | |
49 kcl = KCl(options.salt_master) | |
50 | |
51 # ping, just for fun | |
52 code, pong = kcl.ping() | |
53 | |
54 # output | |
55 print (json.dumps(pong, indent=2, sort_keys=True)) | |
56 | |
57 sys.exit(code) | |
58 | |
59 if __name__ == '__main__': | |
60 main() |