def reload(self, force=False): if force: return self.force_reload() try: return utils.execute('service', self.name, 'reload') except ScalixExternalCommandException as exception: logger.critical("failed to reload service", exception)
def __call__(self, *args): cmd = ['service', self.name] if args[0] not in self.commands: raise NotImplementedError( "Service {name} does not support {cmd}".format(name=self.name, cmd=args[0])) cmd.append(*args) try: return utils.execute(cmd) except ScalixExternalCommandException as exception: logger.critical("Service init failed ", exception)
def open_url(url): """open url in default browser @rtype bool @param url string @return True or False """ try: return webbrowser.open(url, new=1) except webbrowser.Error as exception: logger.critical("Couldn't open browser", exception, " url ", url) return False
def run_level(self): """Returns run level on linux @rtype: int @return int if could not determine run level it will return -1 """ try: cmd = ["runlevel", "|", utils.bash_command("gawk"), "'{print $2}'"] return int(utils.execute(cmd)[0]) except ScalixExternalCommandException as exception: logger.critical("Could not get run level", exception) return -1
def determine_ip(): """ get ip address @rtype string return machine ip or localhost ip """ try: ip_list = socket.gethostbyaddr(System.get_fqdn())[2] except socket.error as exception: logger.critical(exception) return '127.0.0.1' if ip_list: return ip_list[0] return '127.0.0.1'
def memory_total(self): """ get total memory in system(linux only) @rtype int @return total memory or -1 if something went wrong """ try: #gawk '/MemTotal/ { print $2 }' /proc/meminfo result = utils.execute("gawk", "'/MemTotal/ { print $2 }'", "/proc/meminfo") return int(result[0]) * 1024 except ScalixExternalCommandException as exception: logger.critical("Could not get total memory", exception) return -1
def memory_free(self): """ get free memory in system(linux only) @rtype int @return total memory or -1 if something went wrong """ try: #"gawk '/MemFree/ { print $2 }' /proc/meminfo" cmd = [ "gawk", "'/MemFree|Buffers|Cached/ {mem=mem+int($2)} END {print mem}'", "/proc/meminfo" ] return int(utils.execute(cmd)[0]) * 1024 except ScalixExternalCommandException as exception: logger.critical("Could not get free memory", exception) return -1
def partition_size(self, folder): """ get folder size in system(linux only) @rtype int @param folder - string full path to the folder @return size or -1 if something went wrong some """ try: #"df -lP %s | gawk '{print $4}'" result = utils.execute("df", "-BK", "-lP", folder, "|", utils.bash_command("gawk"), "'{print int($4)}'") try: return int(result[1]) * 1024 except (UnicodeEncodeError, ValueError) as exception: logger.critical("Could not get partition size", exception, result) return -1 except ScalixExternalCommandException as exception: logger.critical("Could not get partition size", exception) return -1
def force_reload(self): try: return utils.execute('service', self.name, 'force-reload') except ScalixExternalCommandException as exception: logger.critical("failed to force-reload service", exception)
def status(self): try: return utils.execute('service', self.name, 'status')[0] except ScalixExternalCommandException as exception: logger.critical("failed to get status of the service", exception)
def stop(self): try: return utils.execute('service', self.name, 'stop') except ScalixExternalCommandException as exception: logger.critical("failed to stop service", exception)
def restart(self): try: return utils.execute('service', self.name, 'restart') except ScalixExternalCommandException as exception: logger.critical("failed to restart service", exception)