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