From b85f6ab3398007eb225d5c6126cf372181de5a5f Mon Sep 17 00:00:00 2001 From: Aditya Kulkarni Date: Thu, 15 Oct 2015 11:22:32 -0400 Subject: [PATCH] Add example for salt-proxy over SSH --- salt/modules/ssh_package.py | 31 ++++++++++++++++ salt/proxy/ssh_sample.py | 74 +++++++++++++++++++++++++++++++++++++ 2 files changed, 105 insertions(+) create mode 100644 salt/modules/ssh_package.py create mode 100644 salt/proxy/ssh_sample.py diff --git a/salt/modules/ssh_package.py b/salt/modules/ssh_package.py new file mode 100644 index 0000000000..44e888736b --- /dev/null +++ b/salt/modules/ssh_package.py @@ -0,0 +1,31 @@ +# -*- coding: utf-8 -*- +''' +Service support for the REST example +''' +from __future__ import absolute_import + +# Import python libs +import logging + +# Import Salt's libs +import salt.utils + + +log = logging.getLogger(__name__) + +__proxyenabled__ = ['ssh_sample'] +# Define the module's virtual name +__virtualname__ = 'pkg' + + +def __virtual__(): + ''' + Only work on proxy + ''' + if salt.utils.is_proxy(): + return __virtualname__ + return False + + +def list_pkgs(versions_as_list=False, **kwargs): + return __proxy__['ssh_sample.package_list']() diff --git a/salt/proxy/ssh_sample.py b/salt/proxy/ssh_sample.py new file mode 100644 index 0000000000..eccb9e8b0d --- /dev/null +++ b/salt/proxy/ssh_sample.py @@ -0,0 +1,74 @@ +# -*- coding: utf-8 -*- +''' + This is a simple proxy-minion designed to connect to and communicate with + a server that exposes functionality via SSH. + This can be used as an option when the device does not provide + an api over HTTP and doesn't have the python stack to run a minion. +''' +from __future__ import absolute_import + +# Import python libs +import json +import logging + +# Import Salt's libs +from salt.utils.vt_helper import SSHConnection + +# This must be present or the Salt loader won't load this module +__proxyenabled__ = ['ssh_sample'] + +# Want logging! +log = logging.getLogger(__file__) + + +# This does nothing, it's here just as an example and to provide a log +# entry when the module is loaded. +def __virtual__(): + ''' + Only return if all the modules are available + ''' + log.info('ssh_sample proxy __virtual__() called...') + return True + + +def init(opts): + ''' + Required. + Can be used to initialize the server connection. + ''' + pass + + +def shutdown(opts): + ''' + Required. + Can be used to dispose the server connection. + ''' + pass + + +def package_list(): + ''' + List "packages" by executing a command via ssh + This function is called in response to the salt command + + ..code-block::bash + salt target_minion pkg.list_pkgs + + ''' + # This method shows the full sequence from + # initializing a connection, executing a command, + # parsing the output and closing the connection. + # In production these steps can (and probably should) + # be in separate methods. + + # Create the server connection + server = SSHConnection(host='salt', + username='salt', + password='password') + + # Send the command to execute + out, err = server.sendline('pkg_list') + + # "scrape" the output and return the right fields as a dict + return json.loads(out[9:-7])