diff 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
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/kcl/kcl.py	Mon Feb 06 01:10:22 2017 +0000
@@ -0,0 +1,60 @@
+#!/usr/bin/env python
+# -*- coding: utf-8 -*-
+
+"""
+fake salt
+"""
+
+# imports
+import argparse
+import json
+import ssh
+import sys
+
+class KCl(object):
+    """fake salt's interface to real salt"""
+
+    def __init__(self, salt_master):
+        self.salt_master = salt_master
+        self.ssh = ssh.Ssh(self.salt_master)
+
+    def __call__(self, target, module, *args):
+        command = self.command(target, module, *args)
+        code, output = self.ssh.sudo(command)
+
+        return code, json.loads(output)
+
+    def command(self, target, module, *args):
+        retval = "salt --output=json --static '{target}' {module}".format(target=target, module=module)
+        for arg in args:
+            retval += " '{arg}'".format(arg=arg)
+        return retval
+
+    def ping(self, target='*'):
+        return self(target, 'test.ping')
+
+    def run(self, command, target='*'):
+        return self(target, 'cmd.run', command)
+
+
+def main(args=sys.argv[1:]):
+    """CLI"""
+
+    # parse command line
+    parser = argparse.ArgumentParser(description=__doc__)
+    parser.add_argument('salt_master')
+    options = parser.parse_args(args)
+
+    # instantiate KCl
+    kcl = KCl(options.salt_master)
+
+    # ping, just for fun
+    code, pong = kcl.ping()
+
+    # output
+    print (json.dumps(pong, indent=2, sort_keys=True))
+
+    sys.exit(code)
+
+if __name__ == '__main__':
+    main()