mirror of
https://github.com/saltstack/salt.git
synced 2025-04-17 10:10:20 +00:00
Add service module for ssh proxy example
This commit is contained in:
parent
559a517ad6
commit
76d8d859f1
2 changed files with 177 additions and 2 deletions
136
salt/modules/ssh_service.py
Normal file
136
salt/modules/ssh_service.py
Normal file
|
@ -0,0 +1,136 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
'''
|
||||
Provide the service module for the proxy-minion SSH sample
|
||||
'''
|
||||
# Import python libs
|
||||
from __future__ import absolute_import
|
||||
import logging
|
||||
|
||||
__proxyenabled__ = ['ssh_sample']
|
||||
|
||||
log = logging.getLogger(__name__)
|
||||
|
||||
__func_alias__ = {
|
||||
'list_': 'list'
|
||||
}
|
||||
|
||||
|
||||
# Define the module's virtual name
|
||||
__virtualname__ = 'service'
|
||||
|
||||
|
||||
def __virtual__():
|
||||
'''
|
||||
Only work on systems that are a proxy minion
|
||||
'''
|
||||
if __grains__['os'] == 'proxy':
|
||||
return __virtualname__
|
||||
return False
|
||||
|
||||
|
||||
def get_all():
|
||||
'''
|
||||
Return a list of all available services
|
||||
|
||||
.. versionadded:: 2015.8.0
|
||||
|
||||
CLI Example:
|
||||
|
||||
.. code-block:: bash
|
||||
|
||||
salt '*' service.get_all
|
||||
'''
|
||||
proxy_fn = 'ssh_sample.service_list'
|
||||
return __proxy__[proxy_fn]()
|
||||
|
||||
|
||||
def list_():
|
||||
'''
|
||||
Return a list of all available services.
|
||||
|
||||
.. versionadded: 2015.8.1
|
||||
|
||||
CLI Example:
|
||||
|
||||
.. code-block:: bash
|
||||
|
||||
salt '*' service.list
|
||||
'''
|
||||
return get_all()
|
||||
|
||||
|
||||
def start(name, sig=None):
|
||||
'''
|
||||
Start the specified service on the rest_sample
|
||||
|
||||
CLI Example:
|
||||
|
||||
.. code-block:: bash
|
||||
|
||||
salt '*' service.start <service name>
|
||||
'''
|
||||
|
||||
proxy_fn = 'ssh_sample.service_start'
|
||||
return __proxy__[proxy_fn](name)
|
||||
|
||||
|
||||
def stop(name, sig=None):
|
||||
'''
|
||||
Stop the specified service on the rest_sample
|
||||
|
||||
CLI Example:
|
||||
|
||||
.. code-block:: bash
|
||||
|
||||
salt '*' service.stop <service name>
|
||||
'''
|
||||
proxy_fn = 'ssh_sample.service_stop'
|
||||
return __proxy__[proxy_fn](name)
|
||||
|
||||
|
||||
def restart(name, sig=None):
|
||||
'''
|
||||
Restart the specified service with rest_sample
|
||||
CLI Example:
|
||||
|
||||
.. code-block:: bash
|
||||
|
||||
salt '*' service.restart <service name>
|
||||
'''
|
||||
|
||||
proxy_fn = 'ssh_sample.service_restart'
|
||||
return __proxy__[proxy_fn](name)
|
||||
|
||||
|
||||
def status(name, sig=None):
|
||||
'''
|
||||
Return the status for a service via rest_sample, returns a bool
|
||||
whether the service is running.
|
||||
|
||||
CLI Example:
|
||||
|
||||
.. code-block:: bash
|
||||
|
||||
salt '*' service.status <service name>
|
||||
'''
|
||||
|
||||
proxy_fn = 'ssh_sample.service_status'
|
||||
resp = __proxy__[proxy_fn](name)
|
||||
if resp['comment'] == 'stopped':
|
||||
return False
|
||||
if resp['comment'] == 'running':
|
||||
return True
|
||||
|
||||
|
||||
def running(name, sig=None):
|
||||
'''
|
||||
Return whether this service is running.
|
||||
'''
|
||||
return status(name).get(name, False)
|
||||
|
||||
|
||||
def enabled(name, sig=None):
|
||||
'''
|
||||
Only the 'redbull' service is 'enabled' in the test
|
||||
'''
|
||||
return name == 'redbull'
|
|
@ -96,7 +96,7 @@ def package_list():
|
|||
|
||||
def package_install(name, **kwargs):
|
||||
'''
|
||||
Install a "package" on the REST server
|
||||
Install a "package" on the ssh server
|
||||
'''
|
||||
cmd = 'pkg_install ' + name
|
||||
if 'version' in kwargs:
|
||||
|
@ -113,7 +113,7 @@ def package_install(name, **kwargs):
|
|||
|
||||
def package_remove(name):
|
||||
'''
|
||||
Remove a "package" on the REST server
|
||||
Remove a "package" on the ssh server
|
||||
'''
|
||||
cmd = 'pkg_remove ' + name
|
||||
|
||||
|
@ -122,3 +122,42 @@ def package_remove(name):
|
|||
|
||||
# "scrape" the output and return the right fields as a dict
|
||||
return parse(out)
|
||||
|
||||
|
||||
def service_start(name):
|
||||
'''
|
||||
Start a "service" on the ssh server
|
||||
'''
|
||||
cmd = 'start ' + name
|
||||
|
||||
# Send the command to execute
|
||||
out, err = DETAILS['server'].sendline(cmd)
|
||||
|
||||
# "scrape" the output and return the right fields as a dict
|
||||
return parse(out)
|
||||
|
||||
|
||||
def service_stop(name):
|
||||
'''
|
||||
Stop a "service" on the ssh server
|
||||
'''
|
||||
cmd = 'stop ' + name
|
||||
|
||||
# Send the command to execute
|
||||
out, err = DETAILS['server'].sendline(cmd)
|
||||
|
||||
# "scrape" the output and return the right fields as a dict
|
||||
return parse(out)
|
||||
|
||||
|
||||
def service_restart(name):
|
||||
'''
|
||||
Restart a "service" on the ssh server
|
||||
'''
|
||||
cmd = 'restart ' + name
|
||||
|
||||
# Send the command to execute
|
||||
out, err = DETAILS['server'].sendline(cmd)
|
||||
|
||||
# "scrape" the output and return the right fields as a dict
|
||||
return parse(out)
|
||||
|
|
Loading…
Add table
Reference in a new issue