def unregister( self, jid ): """ Unregister the user with JID I{jid}. Note that unlike the L{register} command, it makes sense to use this command accross many transports. @param jid: The JID to unregister @type jid: string """ env.log( "Unregistering user %s" %(jid) ) for instance in self.instances: self._single_action( instance, 'unregister', [ jid ] )
def _single_action( self, instance, action, args=[] ): if not instance.enabled(): return env.log( instance.get_jid() + '...', newline=False ) if not instance.running(): env.log( "Not running" ) return # exceptions should be handled by the caller! try: retVal = getattr( instance, action )( *args ) env.log( 'ok.' ) return retVal except RuntimeError, e: env.log( e.message )
def set_vip_status( self, jid, state ): """ Set the VIP status of the user L{jid}. The I{status} should be "0" to disable VIP status and "1" to enable it. @param jid: The JID that should have its status set @type jid: str @param state: The state you want to set. See above for more information. @type state: str """ status_string = "Setting VIP-status for '%s' to " if state == "0": status_string += "False:" elif state == "1": status_string += "True:" else: env.error( "Error: status (third argument) must be either 0 or 1" ) return 1 env.log( status_string%(jid) ) for instance in self.instances: self._single_action( instance, 'set_vip_status', [jid,state] )
def _simple_action( self, action, title=None, args=[] ): ret = 0 for instance in self.instances: if not title: title = action.title() jid = instance.get_jid() env.log( "%s %s..."%(title, jid), False ) try: getattr( instance, action )( *args ) env.log( "Ok." ) except socket.error, e: if hasattr( e, 'strerror' ): # python2.5 does not have msg.strerror err = e.strerror else: err = e.message env.log( "Failed (config_interface: %s)"%(err) ) except RuntimeError, e: env.log( "Failed (%s)"%e.args[0] ) ret += e.args[1]
from pyepsolartracer.client import EPsolarTracerClient import re import datetime from env import log logger = log(__name__) class TracerService(object): def __init__(self, serialclient=None, port='/dev/ttyXRUSB0'): self.port = port self.tracer_client = TracerService.create_client(serialclient, port) @staticmethod def create_client(serial, port): logger.info('create_client() port: {0}'.format(port)) tracer_client = EPsolarTracerClient(serialclient=serial, port=port) return tracer_client def device_info(self): logger.info('device_info() port: {0}'.format(self.port)) self.connect() result = self.tracer_client.read_device_info() logger.debug('result: {0}'.format(result.information)) self.disconnect() return result.information
def stop(self, debug=False): """ Stops the instance (sends SIGTERM). If the instance is not running, the method will silently return. The method will also return the pid-file, if it still exists. @raise RuntimeError: If stopping the instance fails. @param debug: Print loads of debug-output if True. @type debug: boolean @see: U{LSB standard for init script actions <http://refspecs.freestandards.org/LSB_3.1.0/LSB-Core-generic/LSB-Core-generic/iniscrptact.html>} """ status = self.status() if status == 3: # stopping while not running is also success! if debug: print('Transport already stopped') return elif status == 1: if debug: print('Transport already stopped, but PID file stil existed') os.remove(self.pid_file) return pid = self.get_pid() try: if debug: env.log('Attempting to kill PID: %s' % (pid)) os.kill(pid, signal.SIGTERM) time.sleep(0.2) for i in range(1, 10): if debug: env.log('Try again...') status = self.status() if status == 3 or status == 1: os.remove(self.pid_file) return 0 os.kill(pid, signal.SIGTERM) time.sleep(1) status = self.status() if status == 3 or status == 1: return 0 else: if debug: env.log('Attempting to kill with SIGABRT') os.kill(pid, signal.SIGABRT) else: env.log('Attempting to kill with SIGKILL') os.kill(pid, signal.SIGKILL) time.sleep(0.2) status = self.status() if status == 3 or status == 1: if debug: env.log('ok') return 0 else: raise RuntimeError("Spectrum did not die", 1) except OSError, e: env.log("pid file: '%s' (%s)" % (self.pid_file, debug)) raise RuntimeError("Failed to kill pid '%s'" % (pid), 1)
else: env.log('Attempting to kill with SIGKILL') os.kill(pid, signal.SIGKILL) time.sleep(0.2) status = self.status() if status == 3 or status == 1: if debug: env.log('ok') return 0 else: raise RuntimeError("Spectrum did not die", 1) except OSError, e: env.log("pid file: '%s' (%s)" % (self.pid_file, debug)) raise RuntimeError("Failed to kill pid '%s'" % (pid), 1) except Exception, e: env.log("%s: %s" % (type(e), e)) raise RuntimeError("Unknown Error occured", 1) def restart(self, no_daemon=False, debug=False): """ Restarts the instance (kills the process and starts it again). This method just calles L{stop} and L{start}. @param no_daemon: Passed to L{start}. @type no_daemon: boolean @param debug: Passed to L{start}. @type debug: boolean @raise RuntimeError: If stopping/starting the instance fails. @see: U{LSB standard for init script actions <http://refspecs.freestandards.org/LSB_3.1.0/LSB-Core-generic/LSB-Core-generic/iniscrptact.html>} """
def stop(self, debug=False): """ Stops the instance (sends SIGTERM). If the instance is not running, the method will silently return. The method will also return the pid-file, if it still exists. @raise RuntimeError: If stopping the instance fails. @param debug: Print loads of debug-output if True. @type debug: boolean @see: U{LSB standard for init script actions <http://refspecs.freestandards.org/LSB_3.1.0/LSB-Core-generic/LSB-Core-generic/iniscrptact.html>} """ status = self.status() if status == 3: # stopping while not running is also success! if debug: print("Transport already stopped") return elif status == 1: if debug: print("Transport already stopped, but PID file stil existed") os.remove(self.pid_file) return pid = self.get_pid() try: if debug: env.log("Attempting to kill PID: %s" % (pid)) os.kill(pid, signal.SIGTERM) time.sleep(0.2) for i in range(1, 10): if debug: env.log("Try again...") status = self.status() if status == 3 or status == 1: os.remove(self.pid_file) return 0 os.kill(pid, signal.SIGTERM) time.sleep(1) status = self.status() if status == 3 or status == 1: return 0 else: if debug: env.log("Attempting to kill with SIGABRT") os.kill(pid, signal.SIGABRT) else: env.log("Attempting to kill with SIGKILL") os.kill(pid, signal.SIGKILL) time.sleep(0.2) status = self.status() if status == 3 or status == 1: if debug: env.log("ok") return 0 else: raise RuntimeError("Spectrum did not die", 1) except OSError, e: env.log("pid file: '%s' (%s)" % (self.pid_file, debug)) raise RuntimeError("Failed to kill pid '%s'" % (pid), 1)
else: env.log("Attempting to kill with SIGKILL") os.kill(pid, signal.SIGKILL) time.sleep(0.2) status = self.status() if status == 3 or status == 1: if debug: env.log("ok") return 0 else: raise RuntimeError("Spectrum did not die", 1) except OSError, e: env.log("pid file: '%s' (%s)" % (self.pid_file, debug)) raise RuntimeError("Failed to kill pid '%s'" % (pid), 1) except Exception, e: env.log("%s: %s" % (type(e), e)) raise RuntimeError("Unknown Error occured", 1) def restart(self, no_daemon=False, debug=False): """ Restarts the instance (kills the process and starts it again). This method just calles L{stop} and L{start}. @param no_daemon: Passed to L{start}. @type no_daemon: boolean @param debug: Passed to L{start}. @type debug: boolean @raise RuntimeError: If stopping/starting the instance fails. @see: U{LSB standard for init script actions <http://refspecs.freestandards.org/LSB_3.1.0/LSB-Core-generic/LSB-Core-generic/iniscrptact.html>} """
if not instance.running(): continue try: iq = instance.get_stats() o = [ instance.get_jid() + ':' ] for stat in iq.getQueryChildren(): value = stat.getAttr( 'value' ) unit = stat.getAttr( 'units' ) name = stat.getAttr( 'name' ) o.append( name + ': ' + value + ' ' + unit ) output.append( "\n".join( o ) ) except RuntimeError, e: env.error( "%s: %s"%(instance.get_jid(), e.message) ) if output: env.log( "\n\n".join( output ) ) return 0 def upgrade_db( self ): """ Try to upgrade the database schema. @return: 0 @rtype: int """ return self._simple_action( 'upgrade_db', "Upgrading db for" ) def message_all( self, path=None ): """ Message all users that are currently online. The contents of the message come from the file located at I{path}. If I{path} is