service module: use sysrc on FreeBSD (#4042)

* service module: use sysrc on FreeBSD

sysrc(8) is the designated userland program to edit rc files on FreeBSD.

It first appeared in FreeBSD 9.2, hence is available on all supported
versions of FreeBSD.

Side effect: fixes #2664

* Incorporate changes suggested by bcoca.

- Use `get_bin_path` to find sysrc binary.
- Only use sysrc when available (support for legacy versions of FreeBSD)
This commit is contained in:
Christian Schwarz 2016-07-05 15:43:05 +02:00 committed by Matt Clay
parent 186db40864
commit e25cb4df77

View file

@ -944,10 +944,11 @@ class FreeBsdService(Service):
def get_service_tools(self): def get_service_tools(self):
self.svc_cmd = self.module.get_bin_path('service', True) self.svc_cmd = self.module.get_bin_path('service', True)
if not self.svc_cmd: if not self.svc_cmd:
self.module.fail_json(msg='unable to find service binary') self.module.fail_json(msg='unable to find service binary')
self.sysrc_cmd = self.module.get_bin_path('sysrc')
def get_service_status(self): def get_service_status(self):
rc, stdout, stderr = self.execute_command("%s %s %s %s" % (self.svc_cmd, self.name, 'onestatus', self.arguments)) rc, stdout, stderr = self.execute_command("%s %s %s %s" % (self.svc_cmd, self.name, 'onestatus', self.arguments))
if self.name == "pf": if self.name == "pf":
@ -992,11 +993,33 @@ class FreeBsdService(Service):
if self.rcconf_key is None: if self.rcconf_key is None:
self.module.fail_json(msg="unable to determine rcvar", stdout=stdout, stderr=stderr) self.module.fail_json(msg="unable to determine rcvar", stdout=stdout, stderr=stderr)
if self.sysrc_cmd: # FreeBSD >= 9.2
rc, current_rcconf_value, stderr = self.execute_command("%s -n %s" % (self.sysrc_cmd, self.rcconf_key))
if rc != 0:
self.module.fail_json(msg="unable to get current rcvar value", stdout=stdout, stderr=stderr)
if current_rcconf_value.strip().upper() != self.rcconf_value:
self.changed = True
if self.module.check_mode:
self.module.exit_json(changed=True, msg="changing service enablement")
rc, stdout, stderr = self.execute_command("%s %s=\"%s\"" % (self.sysrc_cmd, self.rcconf_key, self.rcconf_value ) )
if rc != 0:
self.module.fail_json(msg="unable to set rcvar using sysrc", stdout=stdout, stderr=stderr)
else:
self.changed = False
else: # Legacy (FreeBSD < 9.2)
try: try:
return self.service_enable_rcconf() return self.service_enable_rcconf()
except Exception: except Exception:
self.module.fail_json(msg='unable to set rcvar') self.module.fail_json(msg='unable to set rcvar')
def service_control(self): def service_control(self):
if self.action == "start": if self.action == "start":