be able to reboot the DEC server when it dies
authorCameron Patrick <cameron@ucc.asn.au>
Sun, 1 Aug 2004 04:24:23 +0000 (04:24 +0000)
committerCameron Patrick <cameron@ucc.asn.au>
Sun, 1 Aug 2004 04:24:23 +0000 (04:24 +0000)
sql-edition/servers/LATClient.py

index ead307a..362a452 100644 (file)
@@ -1,4 +1,7 @@
 from socket import *
+from select import select
+from os import popen
+from time import sleep
 
 LATCP_SOCKET = '/var/run/latlogin'
 
@@ -8,8 +11,14 @@ LATCP_CMD_TERMINALSESSION = 26
 LATCP_CMD_ERRORMSG = 99
 
 class LATClient:
-       def __init__(self, service = None, node = None, port = None, \
-                       localport = None, password = None, is_queued = False):
+       def __init__(self, service = None, node = None, port = None,
+                    localport = None, password = None, is_queued = False,
+                    server_name = '', connect_password='', priv_password=''):
+
+               self.server_name = server_name
+               self.connect_password = connect_password
+               self.priv_password = priv_password
+               
                self.sock = socket(AF_UNIX, SOCK_STREAM, 0);
                self.sock.connect(LATCP_SOCKET)
                self.send_msg(LATCP_CMD_VERSION, LAT_VERSION+'\000')
@@ -35,6 +44,13 @@ class LATClient:
                if ord(cmd) == LATCP_CMD_ERRORMSG:
                        raise Exception('LATClient: '+msg)
 
+               r = select([self.sock], [], [], 2.0)[0]
+               if r:
+                       l = self.sock.readline()
+                       if l.find('Service in use') >= 0:
+                               print "Service in use, apparently: restarting DEC server"
+                               self.reboot_server()
+
        def __del__(self):
                try:
                        self.sock.close()
@@ -46,6 +62,20 @@ class LATClient:
        def send_msg(self, cmd, msg):
                self.sock.send('%c%c%c%s'%(cmd, len(msg)/256, len(msg)%256, msg))
 
+       def reboot_server(self):
+               self.sock.shutdown(2)
+               self.sock.close()
+               mop = popen('/usr/sbin/moprc '+self.server_name, 'w')
+               mop.write('\n')
+               mop.write(self.connect_password+'\n')
+               mop.write('grim reaper\n')
+               mop.write('set priv\n')
+               mop.write(self.priv_password+'\n')
+               mop.write('init del 0\n')
+               mop.close()
+               print "Waiting 10 seconds for DEC server to come back to life..."
+               sleep(10)
+
        def read_reply(self):
                head = self.sock.recv(3)
                if len(head) != 3:

UCC git Repository :: git.ucc.asn.au