Merge pull request #2031 from scott-w/develop

Add Supervisor Support
This commit is contained in:
Thomas S Hatch 2012-09-23 22:56:45 -07:00
commit cf1e87e185
5 changed files with 247 additions and 0 deletions

View File

@ -168,6 +168,8 @@ def install(pkgs=None,
fd_, treq = tempfile.mkstemp()
os.close(fd_)
shutil.copyfile(req, treq)
else:
treq = requirements
cmd = '{cmd} --requirement "{requirements}" '.format(
cmd=cmd, requirements=treq or requirements)

View File

@ -0,0 +1,67 @@
'''
Provide the service module for supervisord
'''
from salt import exceptions, utils
def __virtual__():
'''
Check for supervisor.
'''
try:
utils.check_or_die('supervisorctl')
except exceptions.CommandNotFoundError:
return False
return 'supervisord'
def _ctl_cmd(cmd, name):
return 'supervisorctl {cmd} {name}'.format(
cmd=cmd, name=(name or ''))
def _get_return(ret):
if ret['retcode'] == 0:
return ret['stdout']
else:
return ''
def start(name='all', user=None):
'''
Start the named service
CLI Example::
salt '*' supervisord.start <service>
'''
ret = __salt__['cmd.run_all'](_ctl_cmd('start', name), runas=user)
return _get_return(ret)
def restart(name='all', user=None):
'''
Restart the named service.
CLI Example::
salt '*' supervisord.restart <service>
'''
ret = __salt__['cmd.run_all'](_ctl_cmd('restart', name), runas=user)
return _get_return(ret)
def stop(name='all', user=None):
'''
Stop the named service.
CLI Example::
salt '*' supervisord.stop <service>
'''
ret = __salt__['cmd.run_all'](_ctl_cmd('stop', name), runas=user)
return _get_return(ret)
def status(name=None, user=None):
ret = __salt__['cmd.run_all'](_ctl_cmd('status', name), runas=user)
return _get_return(ret)

View File

@ -0,0 +1,76 @@
'''
Interaction with the Supervisor daemon.
=======================================
.. code-block:: yaml
wsgi_server:
supervisord:
- running
- restart: False
- require:
- pkg: supervisor
'''
import logging
log = logging.getLogger(__name__)
def __virtual__():
'''
Check for supervisorctl script
'''
if __salt__['cmd.has_exec']('supervisorctl'):
return 'supervisord'
return False
def _check_error(result, success_message):
ret = {}
if 'ERROR' in result:
ret['comment'] = result
ret['result'] = False
else:
ret['comment'] = success_message
return ret
def running(name,
restart=False,
runas=None,
):
'''
Ensure the named service is running.
name
Service name as defined in the supervisor configuration file
restart
Whether to force a restart e.g. when updating a service
runas
Name of the user to run the supervisorctl command
'''
ret = {'name': name, 'result': True, 'comment': '', 'changes': ''}
if __opts__['test']:
ret['result'] = None
_msg = 'restarted' if restart else 'started'
ret['comment'] = (
'Service {0} is set to be {1}'.format(
name, _msg))
elif restart:
comment = 'Restarting service: {0}'.format(name)
log.debug(comment)
result = __salt__['supervisord.restart'](name, user=runas)
ret.update(_check_error(result, comment))
else:
comment = 'Starting service: {0}'.format(name)
log.debug(comment)
result = __salt__['supervisord.start'](name, user=runas)
ret.update(_check_error(result, comment))
log.debug(unicode(result))
return ret

View File

@ -0,0 +1,72 @@
import integration
class SupervisordModuleTest(integration.ModuleCase):
'''
Validates the supervisorctl functions.
To run these tests, you will need to allow the current user to read/write
to supervisor.sock.
Note that these tests don't actually do anything, since supervisor
will most likely not be configured on the test machine.
'''
def setUp(self):
super(SupervisordModuleTest, self).setUp()
ret = self.run_function('cmd.has_exec', ['supervisorctl'])
if not ret:
self.skipTest('supervisor not installed')
def test_start_all(self):
'''
Passing nothing into supervisord.start will start all services.
'''
ret = self.run_function('supervisord.start', [])
self.assertEqual(ret, '')
def test_start_one(self):
'''
Start a specific service.
'''
ret = self.run_function('supervisord.start', ['null_service'])
self.assertTrue('ERROR' in ret)
def test_restart_all(self):
'''
Restart all services
'''
ret = self.run_function('supervisord.restart', [])
self.assertEqual(ret, '')
def test_restart_one(self):
'''
Restart a specific service.
'''
ret = self.run_function('supervisord.restart', ['null_service'])
self.assertTrue('ERROR' in ret)
def test_stop_all(self):
'''
stop all services
'''
ret = self.run_function('supervisord.stop', [])
self.assertEqual(ret, '')
def test_stop_one(self):
'''
stop a specific service.
'''
ret = self.run_function('supervisord.stop', ['null_service'])
self.assertTrue('ERROR' in ret)
def test_status_all(self):
'''
status all services
'''
ret = self.run_function('supervisord.status', [])
self.assertEqual(ret, '')
def test_status_one(self):
'''
status a specific service.
'''
ret = self.run_function('supervisord.status', ['null_service'])
self.assertTrue(ret)

View File

@ -0,0 +1,30 @@
'''
Tests for the supervisord state
'''
import integration
class SupervisordTest(integration.ModuleCase):
'''
Validate the supervisord states.
'''
def test_start(self):
'''
supervisord.running restart = False
'''
ret = self.run_state('supervisord.running', name='null_service')
self.assertTrue(ret)
# Unlikely we have a service called test on the test machine
self.assertFalse(ret.items()[0][1]['result'])
def test_restart(self):
'''
supervisord.running restart = True
'''
ret = self.run_state(
'supervisord.running', name='null_service', restart=True)
self.assertTrue(ret)
# Unlikely we have a service called test on the test machine
self.assertFalse(ret.items()[0][1]['result'])