From: Cameron Patrick Date: Sun, 1 Aug 2004 06:30:41 +0000 (+0000) Subject: new 'improved' (i.e. probably not working) code for rebooting dec server X-Git-Url: https://git.ucc.asn.au/?a=commitdiff_plain;h=2cbbcf280083a88aa882653d9b69f31948733036;p=zanchey%2Fdispense2.git new 'improved' (i.e. probably not working) code for rebooting dec server --- diff --git a/sql-edition/servers/LATClient.py b/sql-edition/servers/LATClient.py index 7c21d0d..fa2b297 100644 --- a/sql-edition/servers/LATClient.py +++ b/sql-edition/servers/LATClient.py @@ -1,6 +1,6 @@ from socket import * from select import select -from os import popen +from os import popen4 from time import sleep LATCP_SOCKET = '/var/run/latlogin' @@ -12,6 +12,28 @@ LATCP_CMD_ERRORMSG = 99 class LATClientException(Exception): pass +def read_for_a_bit(rfh): + message = '' + while 1: + r = select([rfh], [], [], 5.0)[0] + if r: + message = message + rfh.read(1) + else: + break + print "Received message: ", message + return message + +def write_expecting_echo(rfh, wfh, message): + print "Writing message:",message + while len(message) > 0: + wfh.write(message[0]) + wfh.flush() + echo = rfh.read(1) + if echo != message[0]: + print "Gah, expected echo when writing", message[0] + message = message[1:] + print " --> Sent and echoed." + class LATClient: def __init__(self, service = None, node = None, port = None, localport = None, password = None, is_queued = False, @@ -70,14 +92,21 @@ class LATClient: 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() + mopw, mopr = popen4('/usr/sbin/moprc '+self.server_name) + write_expecting_echo(mopr, mopw, '\n') + read_for_a_bit(mopr) + write_expecting_echo(mopr, mopw, self.connect_password+'\n') + read_for_a_bit(mopr) + write_expecting_echo(mopr, mopw, 'grim reaper\n') + read_for_a_bit(mopr) + write_expecting_echo(mopr, mopw, 'set priv\n') + read_for_a_bit(mopr) + write_expecting_echo(mopr, mopw, self.priv_password+'\n') + read_for_a_bit(mopr) + write_expecting_echo(mopr, mopw, 'init del 0\n') + read_for_a_bit(mopr) + mopr.close() + mopw.close() print print "Waiting 10 seconds for DEC server to come back to life..." sleep(10)