diff mbox

[Branch,~linaro-validation/lava-dispatcher/trunk] Rev 312: watch for various messages from the connection_command that indicate

Message ID 20120601051911.16541.52172.launchpad@ackee.canonical.com
State Accepted
Headers show

Commit Message

Michael-Doyle Hudson June 1, 2012, 5:19 a.m. UTC
Merge authors:
  Michael Hudson-Doyle (mwhudson)
------------------------------------------------------------
revno: 312 [merge]
committer: Michael Hudson-Doyle <michael.hudson@linaro.org>
branch nick: trunk
timestamp: Fri 2012-06-01 17:17:36 +1200
message:
  watch for various messages from the connection_command that indicate
  how successful the connection attempt has been, and do various things in response.
  
  apologies for the lack of review, the code has been well tested on staging though
modified:
  lava_dispatcher/client/master.py


--
lp:lava-dispatcher
https://code.launchpad.net/~linaro-validation/lava-dispatcher/trunk

You are subscribed to branch lp:lava-dispatcher.
To unsubscribe from this branch go to https://code.launchpad.net/~linaro-validation/lava-dispatcher/trunk/+edit-subscription
diff mbox

Patch

=== modified file 'lava_dispatcher/client/master.py'
--- lava_dispatcher/client/master.py	2012-05-31 08:26:23 +0000
+++ lava_dispatcher/client/master.py	2012-06-01 04:40:24 +0000
@@ -297,13 +297,58 @@ 
         pre_connect = self.device_option("pre_connect_command")
         if pre_connect:
             logging_system(pre_connect)
+        self.proc = self._connect_carefully()
+        atexit.register(self._close_logging_spawn)
+
+    def _connect_carefully(self):
         cmd = self.device_option("connection_command")
-        proc = logging_spawn(cmd, timeout=1200)
-        proc.logfile_read = self.sio
-        #serial can be slow, races do funny things if you don't increase delay
-        proc.delaybeforesend=1
-        self.proc = proc
-        atexit.register(self._close_logging_spawn)
+
+        retry_count = 0
+        retry_limit = 3
+
+        port_stuck_message = 'Data Buffering Suspended\.'
+        hot_key_message = 'Type the hot key to suspend the connection:.*\r'
+        conn_closed_message = 'Connection closed by foreign host\.'
+
+        expectations = {
+            port_stuck_message: 'reset-port',
+            'Connected\.\r': 'all-good',
+            hot_key_message: 'all-good',
+            conn_closed_message: 'retry',
+            pexpect.TIMEOUT: 'all-good',
+            }
+        patterns = []
+        results = []
+        for pattern, result in expectations.items():
+            patterns.append(pattern)
+            results.append(result)
+
+        while retry_count < retry_limit:
+            proc = logging_spawn(cmd, timeout=1200)
+            proc.logfile_read = self.sio
+            #serial can be slow, races do funny things if you don't increase delay
+            proc.delaybeforesend=1
+            logging.info('Attempting to connect to device')
+            match = proc.expect(patterns, timeout=10)
+            result = results[match]
+            logging.info('Matched %r which means %s', patterns[match], result)
+            if result == 'retry':
+                proc.close(True)
+                retry_count += 1
+                time.sleep(5)
+                continue
+            elif result == 'all-good':
+                return proc
+            elif result == 'reset-port':
+                reset_port = self.device_option("reset_port_command")
+                if reset_port:
+                    logging_system(reset_port)
+                else:
+                    raise OperationFailed("no reset_port command configured")
+                proc.close(True)
+                retry_count += 1
+                time.sleep(5)
+        raise OperationFailed("could execute connection_command successfully")
 
     @property
     def master_str(self):