Skip to content

Commit

Permalink
RcUpdate: Fix detection of running services
Browse files Browse the repository at this point in the history
Signed-off-by: Sol Jerome <sol.jerome@gmail.com>
  • Loading branch information
solj committed Apr 10, 2013
1 parent 2d861fb commit 2cb245f
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 10 deletions.
24 changes: 16 additions & 8 deletions src/lib/Bcfg2/Client/Tools/RcUpdate.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,15 @@ class RcUpdate(Bcfg2.Client.Tools.SvcTool):
__handles__ = [('Service', 'rc-update')]
__req__ = {'Service': ['name', 'status']}

def get_enabled_svcs(self):
"""
Return a list of all enabled services.
"""
return [line.split()[0]
for line in self.cmd.run(['/bin/rc-status',
'-s']).stdout.splitlines()
if 'started' in line]

def VerifyService(self, entry, _):
"""
Verify Service status for entry.
Expand All @@ -21,9 +30,12 @@ def VerifyService(self, entry, _):
if entry.get('status') == 'ignore':
return True

# get a list of all started services
allsrv = self.get_enabled_svcs()

# check if service is enabled
result = self.cmd.run(["/sbin/rc-update", "show", "default"])
is_enabled = entry.get("name") in result.stdout
result = self.cmd.run(["/sbin/rc-update", "show", "default"]).stdout
is_enabled = entry.get("name") in result

# check if init script exists
try:
Expand All @@ -34,8 +46,7 @@ def VerifyService(self, entry, _):
return False

# check if service is enabled
result = self.cmd.run(self.get_svc_command(entry, "status"))
is_running = "started" in result.stdout
is_running = entry.get('name') in allsrv

if entry.get('status') == 'on' and not (is_enabled and is_running):
entry.set('current_status', 'off')
Expand Down Expand Up @@ -70,10 +81,7 @@ def InstallService(self, entry):

def FindExtra(self):
"""Locate extra rc-update services."""
allsrv = [line.split()[0]
for line in self.cmd.run(['/bin/rc-status',
'-s']).stdout.splitlines()
if 'started' in line]
allsrv = self.get_enabled_svcs()
self.logger.debug('Found active services:')
self.logger.debug(allsrv)
specified = [srv.get('name') for srv in self.getSupportedEntries()]
Expand Down
17 changes: 15 additions & 2 deletions src/lib/Bcfg2/Utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,10 +108,16 @@ class ExecutorResult(object):

def __init__(self, stdout, stderr, retval):
#: The output of the command
self.stdout = stdout
if isinstance(stdout, str):
self.stdout = stdout
else:
self.stdout = stdout.decode('utf-8')

#: The error produced by the command
self.stderr = stderr
if isinstance(stdout, str):
self.stderr = stderr
else:
self.stderr = stderr.decode('utf-8')

#: The return value of the command.
self.retval = retval
Expand Down Expand Up @@ -234,6 +240,13 @@ def run(self, command, inputdata=None, shell=False, timeout=None):
for line in inputdata.splitlines():
self.logger.debug('> %s' % line)
(stdout, stderr) = proc.communicate(input=inputdata)

# py3k fixes
if not isinstance(stdout, str):
stdout = stdout.decode('utf-8')
if not isinstance(stderr, str):
stderr = stderr.decode('utf-8')

for line in stdout.splitlines(): # pylint: disable=E1103
self.logger.debug('< %s' % line)
for line in stderr.splitlines(): # pylint: disable=E1103
Expand Down

0 comments on commit 2cb245f

Please sign in to comment.