Skip to content

Commit

Permalink
Increase code health
Browse files Browse the repository at this point in the history
  • Loading branch information
nir0s committed Nov 23, 2016
1 parent 9bd8352 commit 1f76277
Show file tree
Hide file tree
Showing 5 changed files with 6 additions and 61 deletions.
2 changes: 1 addition & 1 deletion serv/init/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ def uninstall(self):
"""
# raise NotImplementedError('Must be implemented by a subclass')

def status(self, name=''):
def status(self, **kwargs):
"""Retrieve the status of a service `name` or all services
for the current init system.
"""
Expand Down
2 changes: 1 addition & 1 deletion serv/init/systemd.py
Original file line number Diff line number Diff line change
Expand Up @@ -170,5 +170,5 @@ def is_system_exists():
try:
sh.systemctl('--version')
return True
except:
except sh.CommandNotFound:
return False
33 changes: 1 addition & 32 deletions serv/init/sysv.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ def start(self):
service = sh.Command('/etc/init.d/{0}'.format(self.name))
service.start(_bg=True)
except sh.CommandNotFound as ex:
raise ServError('Comnand not found: {0}'.format(ex))
raise ServError('Command not found: {0}'.format(ex))
except:
self.logger.info('Service already started.')

Expand Down Expand Up @@ -85,37 +85,6 @@ def status(self, name=''):
"""WIP!"""
raise NotImplementedError()

super(SysV, self).status(name=name)
try:
sh.service(name, 'status')
except sh.CommandNotFound:
self.logger.warning(
'service command unavailable. Trying to run script directly')
try:
service = sh.Command('/etc/init.d/{0}'.format(self.name))
except sh.CommandNotFound as ex:
raise ServError('Command not found: {0}'.format(ex))
svc_info = self._parse_service_info(service.status())
self.services['services'] = svc_info
return self.services

@staticmethod
def _parse_service_info(svc):
# ssh start/running, process 1214
s = svc.split()
name = s[0]
last_action, status = s[1].split('/')
try:
pid = s[2].split()[1]
except:
pid = ''
return dict(
name=name,
last_action=last_action,
status=status,
pid=pid
)

@staticmethod
def is_system_exists():
return is_system_exists()
Expand Down
28 changes: 2 additions & 26 deletions serv/init/upstart.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,31 +53,7 @@ def uninstall(self):
os.remove(self.svc_file_dest)

def status(self, name=''):
super(Upstart, self).status(name=name)

svc_list = sh.initctl.list()
svcs_info = [self._parse_service_info(svc) for svc in svc_list]
if name:
# return list of one item for specific service
svcs_info = [s for s in svcs_info if s['name'] == name]
self.services['services'] = svcs_info
return self.services

@staticmethod
def _parse_service_info(svc):
s = svc.split()
name = s[0]
last_action, status = s[1].split('/')
try:
pid = s[2].split()[1]
except:
pid = ''
return dict(
name=name,
last_action=last_action,
status=status,
pid=pid
)
raise NotImplementedError()

@staticmethod
def is_system_exists():
Expand All @@ -96,5 +72,5 @@ def is_system_exists():
try:
sh.initctl.version()
return True
except:
except sh.CommandNotFound:
return False
2 changes: 1 addition & 1 deletion serv/serv.py
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ def status(self, name=''):
init = self._get_implementation(name)
if name:
self._assert_service_installed(init, name)
logger.info('Retrieving status...'.format(name))
logger.info('Retrieving status...')
return init.status(name)

def stop(self, name):
Expand Down

0 comments on commit 1f76277

Please sign in to comment.