Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
@property
def is_running(self):
return self.run_test("/etc/rc.d/%s check", self.name).rc == 0
@property
def is_enabled(self):
if self.name in self.check_output('rcctl ls on').splitlines():
return True
if self.name in self.check_output('rcctl ls off').splitlines():
return False
raise RuntimeError(
"Unable to determine state of {0}. Does this service exist?"
.format(self.name))
class NetBSDService(Service):
@property
def is_running(self):
return self.run_test("/etc/rc.d/%s onestatus", self.name).rc == 0
@property
def is_enabled(self):
raise NotImplementedError
@property
def is_running(self):
return self.run_test("service %s onestatus", self.name).rc == 0
@property
def is_enabled(self):
# Return list of enabled services like
# /etc/rc.d/sshd
# /etc/rc.d/sendmail
for path in self.check_output("service -e").splitlines():
if path and path.rsplit("/", 1)[1] == self.name:
return True
return False
class OpenBSDService(Service):
@property
def is_running(self):
return self.run_test("/etc/rc.d/%s check", self.name).rc == 0
@property
def is_enabled(self):
if self.name in self.check_output('rcctl ls on').splitlines():
return True
if self.name in self.check_output('rcctl ls off').splitlines():
return False
raise RuntimeError(
"Unable to determine state of {0}. Does this service exist?"
.format(self.name))
class OpenRCService(SysvService):
@cached_property
def _service_command(self):
return self.find_command("rc-service")
@property
def is_enabled(self):
return bool(self.check_output(
"find /etc/runlevels/ -name %s",
self.name,
))
class FreeBSDService(Service):
@property
def is_running(self):
return self.run_test("service %s onestatus", self.name).rc == 0
@property
def is_enabled(self):
# Return list of enabled services like
# /etc/rc.d/sshd
# /etc/rc.d/sendmail
for path in self.check_output("service -e").splitlines():
if path and path.rsplit("/", 1)[1] == self.name:
return True
return False
def __init__(self, name):
self.name = name
super(Service, self).__init__()
if host.exists("rc-service"):
return OpenRCService
return SysvService
if host.system_info.type == "freebsd":
return FreeBSDService
if host.system_info.type == "openbsd":
return OpenBSDService
if host.system_info.type == "netbsd":
return NetBSDService
raise NotImplementedError
def __repr__(self):
return "" % (self.name,)
class SysvService(Service):
@cached_property
def _service_command(self):
return self.find_command('service')
@property
def is_running(self):
# based on /lib/lsb/init-functions
# 0: program running
# 1: program is dead and pid file exists
# 3: not running and pid file does not exists
# 4: Unable to determine status
# 8: starting (alpine specific ?)
return self.run_expect(
[0, 1, 3, 8], "%s %s status",
self._service_command, self.name).rc == 0