rearranged systemd check, removed redundant systemctl check

fixed unused cmd and state var assignements
This commit is contained in:
Brian Coca 2015-10-23 18:59:05 -04:00
parent dae3718e79
commit 4072bc1da0

View file

@ -395,7 +395,7 @@ class LinuxService(Service):
location = dict() location = dict()
for binary in binaries: for binary in binaries:
location[binary] = self.module.get_bin_path(binary) location[binary] = self.module.get_bin_path(binary, opt_dirs=paths)
for initdir in initpaths: for initdir in initpaths:
initscript = "%s/%s" % (initdir,self.name) initscript = "%s/%s" % (initdir,self.name)
@ -403,10 +403,31 @@ class LinuxService(Service):
self.svc_initscript = initscript self.svc_initscript = initscript
def check_systemd(): def check_systemd():
return os.path.exists("/run/systemd/system/") or os.path.exists("/dev/.run/systemd/") or os.path.exists("/dev/.systemd/")
# tools must be installed
if location.get('systemctl',False):
# this should show if systemd is the boot init system
# these mirror systemd's own sd_boot test http://www.freedesktop.org/software/systemd/man/sd_booted.html
for canary in ["/run/systemd/system/", "/dev/.run/systemd/", "/dev/.systemd/"]:
if os.path.exists(canary):
return True
# If all else fails, check if init is the systemd command, using comm as cmdline could be symlink
try:
f = open('/proc/1/comm', 'r')
except IOError:
# If comm doesn't exist, old kernel, no systemd
return False
for line in f:
if 'systemd' in line:
return True
return False
# Locate a tool to enable/disable a service # Locate a tool to enable/disable a service
if location.get('systemctl',False) and check_systemd(): if check_systemd():
# service is managed by systemd # service is managed by systemd
self.__systemd_unit = self.name self.__systemd_unit = self.name
self.svc_cmd = location['systemctl'] self.svc_cmd = location['systemctl']
@ -684,7 +705,8 @@ class LinuxService(Service):
(rc, out, err) = self.execute_command("%s --list %s" % (self.enable_cmd, self.name)) (rc, out, err) = self.execute_command("%s --list %s" % (self.enable_cmd, self.name))
if not self.name in out: if not self.name in out:
self.module.fail_json(msg="service %s does not support chkconfig" % self.name) self.module.fail_json(msg="service %s does not support chkconfig" % self.name)
state = out.split()[-1] #TODO: look back on why this is here
#state = out.split()[-1]
# Check if we're already in the correct state # Check if we're already in the correct state
if "3:%s" % action in out and "5:%s" % action in out: if "3:%s" % action in out and "5:%s" % action in out:
@ -946,7 +968,6 @@ class FreeBsdService(Service):
self.rcconf_file = rcfile self.rcconf_file = rcfile
rc, stdout, stderr = self.execute_command("%s %s %s %s" % (self.svc_cmd, self.name, 'rcvar', self.arguments)) rc, stdout, stderr = self.execute_command("%s %s %s %s" % (self.svc_cmd, self.name, 'rcvar', self.arguments))
cmd = "%s %s %s %s" % (self.svc_cmd, self.name, 'rcvar', self.arguments)
try: try:
rcvars = shlex.split(stdout, comments=True) rcvars = shlex.split(stdout, comments=True)
except: except: