J. Richard Barnette | 67ccb87 | 2012-04-19 16:34:56 -0700 | [diff] [blame] | 1 | # Copyright (c) 2012 The Chromium OS Authors. All rights reserved. |
Craig Harrison | 2b6c6fc | 2011-06-23 10:34:02 -0700 | [diff] [blame] | 2 | # Use of this source code is governed by a BSD-style license that can be |
| 3 | # found in the LICENSE file. |
| 4 | |
Craig Harrison | 9194455 | 2011-08-04 14:09:55 -0700 | [diff] [blame] | 5 | import logging |
Tom Wai-Hong Tam | 4a257e5 | 2011-11-12 08:36:22 +0800 | [diff] [blame] | 6 | import os |
ctchang | 74816ac | 2012-08-31 11:12:43 +0800 | [diff] [blame] | 7 | import socket |
Craig Harrison | 2b6c6fc | 2011-06-23 10:34:02 -0700 | [diff] [blame] | 8 | import subprocess |
Craig Harrison | 9194455 | 2011-08-04 14:09:55 -0700 | [diff] [blame] | 9 | import time |
| 10 | import xmlrpclib |
Craig Harrison | 2b6c6fc | 2011-06-23 10:34:02 -0700 | [diff] [blame] | 11 | |
Chrome Bot | 9a1137d | 2011-07-19 14:35:00 -0700 | [diff] [blame] | 12 | from autotest_lib.client.common_lib import error |
Vadim Bendebury | bb73195 | 2012-12-05 17:30:36 -0800 | [diff] [blame] | 13 | from autotest_lib.server import autotest, test |
Chris Sosa | 8ee1d59 | 2011-08-14 16:50:31 -0700 | [diff] [blame] | 14 | from autotest_lib.server.cros import servo |
Craig Harrison | 9194455 | 2011-08-04 14:09:55 -0700 | [diff] [blame] | 15 | |
Craig Harrison | 2b6c6fc | 2011-06-23 10:34:02 -0700 | [diff] [blame] | 16 | class ServoTest(test.test): |
| 17 | """AutoTest test class that creates and destroys a servo object. |
| 18 | |
Tom Wai-Hong Tam | bea57b3 | 2011-09-02 18:27:47 +0800 | [diff] [blame] | 19 | Servo-based server side AutoTests can inherit from this object. |
| 20 | There are 2 remote clients supported: |
| 21 | If use_pyauto flag is True, a remote PyAuto client will be launched; |
| 22 | If use_faft flag is Ture, a remote FAFT client will be launched. |
Craig Harrison | 2b6c6fc | 2011-06-23 10:34:02 -0700 | [diff] [blame] | 23 | """ |
Tom Wai-Hong Tam | bea57b3 | 2011-09-02 18:27:47 +0800 | [diff] [blame] | 24 | version = 2 |
J. Richard Barnette | 67ccb87 | 2012-04-19 16:34:56 -0700 | [diff] [blame] | 25 | |
Craig Harrison | 9194455 | 2011-08-04 14:09:55 -0700 | [diff] [blame] | 26 | # Exposes RPC access to a remote PyAuto client. |
| 27 | pyauto = None |
Tom Wai-Hong Tam | bea57b3 | 2011-09-02 18:27:47 +0800 | [diff] [blame] | 28 | # Exposes RPC access to a remote FAFT client. |
| 29 | faft_client = None |
| 30 | |
Craig Harrison | 9194455 | 2011-08-04 14:09:55 -0700 | [diff] [blame] | 31 | # Autotest references to the client. |
Tom Wai-Hong Tam | bea57b3 | 2011-09-02 18:27:47 +0800 | [diff] [blame] | 32 | _autotest_client = None |
| 33 | # Remote client info list. |
| 34 | _remote_infos = { |
| 35 | 'pyauto': { |
| 36 | # Used or not. |
| 37 | 'used': False, |
| 38 | # Reference name of RPC object in this class. |
| 39 | 'ref_name': 'pyauto', |
| 40 | # Port number of the remote RPC. |
| 41 | 'port': 9988, |
Tom Wai-Hong Tam | bea57b3 | 2011-09-02 18:27:47 +0800 | [diff] [blame] | 42 | # The remote command to be run. |
Scott Zawalski | 9a98556 | 2012-04-03 17:47:30 -0400 | [diff] [blame] | 43 | 'remote_command': 'python /usr/local/autotest/cros/remote_pyauto.py' |
Tom Wai-Hong Tam | bea57b3 | 2011-09-02 18:27:47 +0800 | [diff] [blame] | 44 | ' --no-http-server', |
| 45 | # The short form of remote command, used by pkill. |
Scott Zawalski | 9a98556 | 2012-04-03 17:47:30 -0400 | [diff] [blame] | 46 | 'remote_command_short': 'remote_pyauto', |
Tom Wai-Hong Tam | bea57b3 | 2011-09-02 18:27:47 +0800 | [diff] [blame] | 47 | # The remote process info. |
| 48 | 'remote_process': None, |
| 49 | # The ssh tunnel process info. |
| 50 | 'ssh_tunnel': None, |
| 51 | # Polling RPC function name for testing the server availability. |
| 52 | 'polling_rpc': 'IsLinux', |
Tom Wai-Hong Tam | e2c6612 | 2011-10-25 17:21:35 +0800 | [diff] [blame] | 53 | # Additional SSH options. |
| 54 | 'ssh_config': '-o StrictHostKeyChecking=no ', |
Tom Wai-Hong Tam | bea57b3 | 2011-09-02 18:27:47 +0800 | [diff] [blame] | 55 | }, |
| 56 | 'faft': { |
| 57 | 'used': False, |
| 58 | 'ref_name': 'faft_client', |
| 59 | 'port': 9990, |
Tom Wai-Hong Tam | c1e0b9a | 2012-11-01 13:52:39 +0800 | [diff] [blame] | 60 | 'remote_command': '/usr/local/autotest/cros/faft_client.py', |
Tom Wai-Hong Tam | bea57b3 | 2011-09-02 18:27:47 +0800 | [diff] [blame] | 61 | 'remote_command_short': 'faft_client', |
Vic Yang | f8fd454 | 2012-08-01 11:37:46 +0800 | [diff] [blame] | 62 | 'remote_log_file': '/tmp/faft_client.log', |
Tom Wai-Hong Tam | bea57b3 | 2011-09-02 18:27:47 +0800 | [diff] [blame] | 63 | 'remote_process': None, |
| 64 | 'ssh_tunnel': None, |
Chun-ting Chang | d43aa9b | 2012-11-16 10:12:05 +0800 | [diff] [blame] | 65 | 'polling_rpc': 'system.is_available', |
Tom Wai-Hong Tam | e2c6612 | 2011-10-25 17:21:35 +0800 | [diff] [blame] | 66 | 'ssh_config': '-o StrictHostKeyChecking=no ' |
| 67 | '-o UserKnownHostsFile=/dev/null ', |
Tom Wai-Hong Tam | bea57b3 | 2011-09-02 18:27:47 +0800 | [diff] [blame] | 68 | }, |
| 69 | } |
Craig Harrison | 2b6c6fc | 2011-06-23 10:34:02 -0700 | [diff] [blame] | 70 | |
Vadim Bendebury | bb73195 | 2012-12-05 17:30:36 -0800 | [diff] [blame] | 71 | def _init_servo(self, host): |
J. Richard Barnette | 67ccb87 | 2012-04-19 16:34:56 -0700 | [diff] [blame] | 72 | """Initialize `self.servo`. |
Craig Harrison | 9194455 | 2011-08-04 14:09:55 -0700 | [diff] [blame] | 73 | |
J. Richard Barnette | 67ccb87 | 2012-04-19 16:34:56 -0700 | [diff] [blame] | 74 | If the host has an attached servo object, use that. |
| 75 | Otherwise assume that there's a locally attached servo |
Vadim Bendebury | bb73195 | 2012-12-05 17:30:36 -0800 | [diff] [blame] | 76 | device, and use it. |
J. Richard Barnette | 67ccb87 | 2012-04-19 16:34:56 -0700 | [diff] [blame] | 77 | |
Todd Broch | f24d278 | 2011-08-19 10:55:41 -0700 | [diff] [blame] | 78 | """ |
J. Richard Barnette | 67ccb87 | 2012-04-19 16:34:56 -0700 | [diff] [blame] | 79 | if host.servo: |
| 80 | self.servo = host.servo |
Vadim Bendebury | bb73195 | 2012-12-05 17:30:36 -0800 | [diff] [blame] | 81 | else: |
| 82 | self.servo = servo.Servo() |
J. Richard Barnette | 67ccb87 | 2012-04-19 16:34:56 -0700 | [diff] [blame] | 83 | |
| 84 | |
Vadim Bendebury | bb73195 | 2012-12-05 17:30:36 -0800 | [diff] [blame] | 85 | def initialize(self, host, _, use_pyauto=False, use_faft=False): |
J. Richard Barnette | 67ccb87 | 2012-04-19 16:34:56 -0700 | [diff] [blame] | 86 | """Create a Servo object and install the dependency. |
| 87 | |
| 88 | If use_pyauto/use_faft is True the PyAuto/FAFTClient dependency is |
| 89 | installed on the client and a remote PyAuto/FAFTClient server is |
| 90 | launched and connected. |
| 91 | """ |
Chris Sosa | 33320a8 | 2011-10-24 14:28:32 -0700 | [diff] [blame] | 92 | # Initialize servotest args. |
Vic Yang | 3a7cf60 | 2012-11-07 17:28:39 +0800 | [diff] [blame] | 93 | self._client = host |
Chris Sosa | 33320a8 | 2011-10-24 14:28:32 -0700 | [diff] [blame] | 94 | self._remote_infos['pyauto']['used'] = use_pyauto |
| 95 | self._remote_infos['faft']['used'] = use_faft |
| 96 | |
Vadim Bendebury | bb73195 | 2012-12-05 17:30:36 -0800 | [diff] [blame] | 97 | self._init_servo(host) |
J. Richard Barnette | 67ccb87 | 2012-04-19 16:34:56 -0700 | [diff] [blame] | 98 | |
Chrome Bot | 9a1137d | 2011-07-19 14:35:00 -0700 | [diff] [blame] | 99 | # Initializes dut, may raise AssertionError if pre-defined gpio |
| 100 | # sequence to set GPIO's fail. Autotest does not handle exception |
| 101 | # throwing in initialize and will cause a test to hang. |
| 102 | try: |
| 103 | self.servo.initialize_dut() |
Chris Sosa | 33320a8 | 2011-10-24 14:28:32 -0700 | [diff] [blame] | 104 | except (AssertionError, xmlrpclib.Fault) as e: |
Chrome Bot | 9a1137d | 2011-07-19 14:35:00 -0700 | [diff] [blame] | 105 | raise error.TestFail(e) |
| 106 | |
Tom Wai-Hong Tam | bea57b3 | 2011-09-02 18:27:47 +0800 | [diff] [blame] | 107 | # Install PyAuto/FAFTClient dependency. |
| 108 | for info in self._remote_infos.itervalues(): |
| 109 | if info['used']: |
| 110 | if not self._autotest_client: |
| 111 | self._autotest_client = autotest.Autotest(self._client) |
Tom Wai-Hong Tam | 00d1051 | 2012-11-09 15:37:51 +0800 | [diff] [blame] | 112 | self._autotest_client.install() |
Tom Wai-Hong Tam | bea57b3 | 2011-09-02 18:27:47 +0800 | [diff] [blame] | 113 | self.launch_client(info) |
Craig Harrison | 2b6c6fc | 2011-06-23 10:34:02 -0700 | [diff] [blame] | 114 | |
| 115 | |
J. Richard Barnette | 134ec2c | 2012-04-25 12:59:37 -0700 | [diff] [blame] | 116 | def _ping_test(self, hostname, timeout=5): |
Craig Harrison | 2b6c6fc | 2011-06-23 10:34:02 -0700 | [diff] [blame] | 117 | """Verify whether a host responds to a ping. |
| 118 | |
| 119 | Args: |
| 120 | hostname: Hostname to ping. |
| 121 | timeout: Time in seconds to wait for a response. |
| 122 | """ |
Tom Wai-Hong Tam | 4a257e5 | 2011-11-12 08:36:22 +0800 | [diff] [blame] | 123 | with open(os.devnull, 'w') as fnull: |
| 124 | return subprocess.call( |
| 125 | ['ping', '-c', '1', '-W', str(timeout), hostname], |
| 126 | stdout=fnull, stderr=fnull) == 0 |
Craig Harrison | 2b6c6fc | 2011-06-23 10:34:02 -0700 | [diff] [blame] | 127 | |
| 128 | |
ctchang | 74816ac | 2012-08-31 11:12:43 +0800 | [diff] [blame] | 129 | def _sshd_test(self, hostname, timeout=5): |
| 130 | """Verify whether sshd is running in host. |
| 131 | |
| 132 | Args: |
| 133 | hostname: Hostname to verify. |
| 134 | timeout: Time in seconds to wait for a response. |
| 135 | """ |
| 136 | try: |
| 137 | sock = socket.create_connection((hostname, 22), timeout=timeout) |
| 138 | sock.close() |
| 139 | return True |
Tom Wai-Hong Tam | 711a7aa | 2012-09-18 10:30:43 +0800 | [diff] [blame] | 140 | except socket.timeout: |
| 141 | return False |
ctchang | 74816ac | 2012-08-31 11:12:43 +0800 | [diff] [blame] | 142 | except socket.error: |
Tom Wai-Hong Tam | 711a7aa | 2012-09-18 10:30:43 +0800 | [diff] [blame] | 143 | time.sleep(timeout) |
ctchang | 74816ac | 2012-08-31 11:12:43 +0800 | [diff] [blame] | 144 | return False |
| 145 | |
| 146 | |
Tom Wai-Hong Tam | bea57b3 | 2011-09-02 18:27:47 +0800 | [diff] [blame] | 147 | def launch_client(self, info): |
Tom Wai-Hong Tam | e97cb4a | 2012-11-22 09:52:46 +0800 | [diff] [blame] | 148 | """Launch a remote XML RPC connection on client with retrials. |
| 149 | |
| 150 | Args: |
| 151 | info: A dict of remote info, see the definition of self._remote_infos. |
| 152 | """ |
| 153 | retry = 3 |
Tom Wai-Hong Tam | b3db3b2 | 2012-12-04 11:59:54 +0800 | [diff] [blame] | 154 | while retry: |
| 155 | try: |
| 156 | self._launch_client_once(info) |
| 157 | break |
| 158 | except AssertionError: |
Tom Wai-Hong Tam | e97cb4a | 2012-11-22 09:52:46 +0800 | [diff] [blame] | 159 | retry -= 1 |
Tom Wai-Hong Tam | b3db3b2 | 2012-12-04 11:59:54 +0800 | [diff] [blame] | 160 | if retry: |
| 161 | logging.info('Retry again...') |
| 162 | time.sleep(5) |
| 163 | else: |
| 164 | raise |
Tom Wai-Hong Tam | e97cb4a | 2012-11-22 09:52:46 +0800 | [diff] [blame] | 165 | |
| 166 | |
| 167 | def _launch_client_once(self, info): |
Tom Wai-Hong Tam | bea57b3 | 2011-09-02 18:27:47 +0800 | [diff] [blame] | 168 | """Launch a remote process on client and set up an xmlrpc connection. |
| 169 | |
| 170 | Args: |
| 171 | info: A dict of remote info, see the definition of self._remote_infos. |
| 172 | """ |
| 173 | assert info['used'], \ |
| 174 | 'Remote %s dependency not installed.' % info['ref_name'] |
| 175 | if not info['ssh_tunnel'] or info['ssh_tunnel'].poll() is not None: |
| 176 | self._launch_ssh_tunnel(info) |
| 177 | assert info['ssh_tunnel'] and info['ssh_tunnel'].poll() is None, \ |
Craig Harrison | 9194455 | 2011-08-04 14:09:55 -0700 | [diff] [blame] | 178 | 'The SSH tunnel is not up.' |
Tom Wai-Hong Tam | bea57b3 | 2011-09-02 18:27:47 +0800 | [diff] [blame] | 179 | |
| 180 | # Launch RPC server remotely. |
| 181 | self._kill_remote_process(info) |
Vic Yang | 3a7cf60 | 2012-11-07 17:28:39 +0800 | [diff] [blame] | 182 | logging.info('Client command: %s', info['remote_command']) |
Vic Yang | f8fd454 | 2012-08-01 11:37:46 +0800 | [diff] [blame] | 183 | if 'remote_log_file' in info: |
Vic Yang | 3a7cf60 | 2012-11-07 17:28:39 +0800 | [diff] [blame] | 184 | log_file = info['remote_log_file'] |
Vic Yang | f8fd454 | 2012-08-01 11:37:46 +0800 | [diff] [blame] | 185 | else: |
Vic Yang | 3a7cf60 | 2012-11-07 17:28:39 +0800 | [diff] [blame] | 186 | log_file = '/dev/null' |
Vic Yang | f8fd454 | 2012-08-01 11:37:46 +0800 | [diff] [blame] | 187 | logging.info("Logging to %s", log_file) |
Tom Wai-Hong Tam | bea57b3 | 2011-09-02 18:27:47 +0800 | [diff] [blame] | 188 | info['remote_process'] = subprocess.Popen([ |
Vic Yang | f8fd454 | 2012-08-01 11:37:46 +0800 | [diff] [blame] | 189 | 'ssh -n -q %s root@%s \'%s &> %s\'' % (info['ssh_config'], |
| 190 | self._client.ip, info['remote_command'], log_file)], |
| 191 | shell=True) |
Tom Wai-Hong Tam | bea57b3 | 2011-09-02 18:27:47 +0800 | [diff] [blame] | 192 | |
| 193 | # Connect to RPC object. |
| 194 | logging.info('Connecting to client RPC server...') |
| 195 | remote_url = 'http://localhost:%s' % info['port'] |
| 196 | setattr(self, info['ref_name'], |
| 197 | xmlrpclib.ServerProxy(remote_url, allow_none=True)) |
Vic Yang | 3a7cf60 | 2012-11-07 17:28:39 +0800 | [diff] [blame] | 198 | logging.info('Server proxy: %s', remote_url) |
Tom Wai-Hong Tam | bea57b3 | 2011-09-02 18:27:47 +0800 | [diff] [blame] | 199 | |
Craig Harrison | 9194455 | 2011-08-04 14:09:55 -0700 | [diff] [blame] | 200 | # Poll for client RPC server to come online. |
Vic Yang | 6c1dc15 | 2012-09-13 14:56:11 +0800 | [diff] [blame] | 201 | timeout = 20 |
Craig Harrison | 9194455 | 2011-08-04 14:09:55 -0700 | [diff] [blame] | 202 | succeed = False |
Vic Yang | 3a7cf60 | 2012-11-07 17:28:39 +0800 | [diff] [blame] | 203 | rpc_error = None |
Craig Harrison | 9194455 | 2011-08-04 14:09:55 -0700 | [diff] [blame] | 204 | while timeout > 0 and not succeed: |
Vic Yang | 6c1dc15 | 2012-09-13 14:56:11 +0800 | [diff] [blame] | 205 | time.sleep(1) |
Craig Harrison | 9194455 | 2011-08-04 14:09:55 -0700 | [diff] [blame] | 206 | try: |
Tom Wai-Hong Tam | bea57b3 | 2011-09-02 18:27:47 +0800 | [diff] [blame] | 207 | remote_object = getattr(self, info['ref_name']) |
| 208 | polling_rpc = getattr(remote_object, info['polling_rpc']) |
| 209 | polling_rpc() |
Craig Harrison | 9194455 | 2011-08-04 14:09:55 -0700 | [diff] [blame] | 210 | succeed = True |
Tom Wai-Hong Tam | ac35f08 | 2012-11-06 15:00:35 +0800 | [diff] [blame] | 211 | except (socket.error, xmlrpclib.ProtocolError) as e: |
| 212 | # The client RPC server may not come online fast enough. Retry. |
Craig Harrison | 9194455 | 2011-08-04 14:09:55 -0700 | [diff] [blame] | 213 | timeout -= 1 |
Vic Yang | 3a7cf60 | 2012-11-07 17:28:39 +0800 | [diff] [blame] | 214 | rpc_error = e |
Vic Yang | f8fd454 | 2012-08-01 11:37:46 +0800 | [diff] [blame] | 215 | |
| 216 | if not succeed: |
Vic Yang | 3a7cf60 | 2012-11-07 17:28:39 +0800 | [diff] [blame] | 217 | if isinstance(rpc_error, xmlrpclib.ProtocolError): |
Tom Wai-Hong Tam | ac35f08 | 2012-11-06 15:00:35 +0800 | [diff] [blame] | 218 | logging.info("A protocol error occurred") |
Vic Yang | 3a7cf60 | 2012-11-07 17:28:39 +0800 | [diff] [blame] | 219 | logging.info("URL: %s", rpc_error.url) |
| 220 | logging.info("HTTP/HTTPS headers: %s", rpc_error.headers) |
| 221 | logging.info("Error code: %d", rpc_error.errcode) |
| 222 | logging.info("Error message: %s", rpc_error.errmsg) |
Vic Yang | f8fd454 | 2012-08-01 11:37:46 +0800 | [diff] [blame] | 223 | if 'remote_log_file' in info: |
| 224 | p = subprocess.Popen([ |
| 225 | 'ssh -n -q %s root@%s \'cat %s\'' % (info['ssh_config'], |
| 226 | self._client.ip, info['remote_log_file'])], shell=True, |
| 227 | stdout=subprocess.PIPE) |
Tom Wai-Hong Tam | c1e0b9a | 2012-11-01 13:52:39 +0800 | [diff] [blame] | 228 | logging.info('Log of running remote %s:', info['ref_name']) |
Vic Yang | f8fd454 | 2012-08-01 11:37:46 +0800 | [diff] [blame] | 229 | logging.info(p.communicate()[0]) |
Tom Wai-Hong Tam | bea57b3 | 2011-09-02 18:27:47 +0800 | [diff] [blame] | 230 | assert succeed, 'Timed out connecting to client RPC server.' |
Craig Harrison | 9194455 | 2011-08-04 14:09:55 -0700 | [diff] [blame] | 231 | |
| 232 | |
Vic Yang | 8bbc1c3 | 2012-09-13 11:47:44 +0800 | [diff] [blame] | 233 | def wait_for_client(self, install_deps=False, timeout=100): |
Craig Harrison | 9194455 | 2011-08-04 14:09:55 -0700 | [diff] [blame] | 234 | """Wait for the client to come back online. |
| 235 | |
Tom Wai-Hong Tam | bea57b3 | 2011-09-02 18:27:47 +0800 | [diff] [blame] | 236 | New remote processes will be launched if their used flags are enabled. |
Tom Wai-Hong Tam | 7c17ff2 | 2011-10-26 09:44:09 +0800 | [diff] [blame] | 237 | |
| 238 | Args: |
| 239 | install_deps: If True, install the Autotest dependency when ready. |
Vic Yang | 8bbc1c3 | 2012-09-13 11:47:44 +0800 | [diff] [blame] | 240 | timeout: Time in seconds to wait for the client SSH daemon to |
| 241 | come up. |
Craig Harrison | 9194455 | 2011-08-04 14:09:55 -0700 | [diff] [blame] | 242 | """ |
Craig Harrison | 9194455 | 2011-08-04 14:09:55 -0700 | [diff] [blame] | 243 | # Ensure old ssh connections are terminated. |
| 244 | self._terminate_all_ssh() |
| 245 | # Wait for the client to come up. |
Vic Yang | 6c1dc15 | 2012-09-13 14:56:11 +0800 | [diff] [blame] | 246 | while timeout > 0 and not self._sshd_test(self._client.ip, timeout=2): |
| 247 | timeout -= 2 |
Vadim Bendebury | 69f047c | 2012-10-11 15:20:31 -0700 | [diff] [blame] | 248 | assert (timeout > 0), 'Timed out waiting for client to reboot.' |
Tom Wai-Hong Tam | bea57b3 | 2011-09-02 18:27:47 +0800 | [diff] [blame] | 249 | logging.info('Server: Client machine is up.') |
| 250 | # Relaunch remote clients. |
| 251 | for name, info in self._remote_infos.iteritems(): |
| 252 | if info['used']: |
Tom Wai-Hong Tam | 7c17ff2 | 2011-10-26 09:44:09 +0800 | [diff] [blame] | 253 | if install_deps: |
| 254 | if not self._autotest_client: |
| 255 | self._autotest_client = autotest.Autotest(self._client) |
Tom Wai-Hong Tam | 00d1051 | 2012-11-09 15:37:51 +0800 | [diff] [blame] | 256 | self._autotest_client.install() |
Tom Wai-Hong Tam | bea57b3 | 2011-09-02 18:27:47 +0800 | [diff] [blame] | 257 | self.launch_client(info) |
Vic Yang | 3a7cf60 | 2012-11-07 17:28:39 +0800 | [diff] [blame] | 258 | logging.info('Server: Relaunched remote %s.', name) |
Craig Harrison | 9194455 | 2011-08-04 14:09:55 -0700 | [diff] [blame] | 259 | |
| 260 | |
Vic Yang | 8bbc1c3 | 2012-09-13 11:47:44 +0800 | [diff] [blame] | 261 | def wait_for_client_offline(self, timeout=60): |
Tom Wai-Hong Tam | a70f0fe | 2011-09-02 18:28:47 +0800 | [diff] [blame] | 262 | """Wait for the client to come offline. |
| 263 | |
| 264 | Args: |
| 265 | timeout: Time in seconds to wait the client to come offline. |
| 266 | """ |
| 267 | # Wait for the client to come offline. |
J. Richard Barnette | 134ec2c | 2012-04-25 12:59:37 -0700 | [diff] [blame] | 268 | while timeout > 0 and self._ping_test(self._client.ip, timeout=1): |
Vic Yang | cf5d6fc | 2012-09-14 15:22:44 +0800 | [diff] [blame] | 269 | time.sleep(1) |
Tom Wai-Hong Tam | a70f0fe | 2011-09-02 18:28:47 +0800 | [diff] [blame] | 270 | timeout -= 1 |
| 271 | assert timeout, 'Timed out waiting for client offline.' |
| 272 | logging.info('Server: Client machine is offline.') |
| 273 | |
| 274 | |
Vic Yang | 4e0d1f7 | 2012-05-24 15:11:11 +0800 | [diff] [blame] | 275 | def kill_remote(self): |
| 276 | """Call remote cleanup and kill ssh.""" |
Tom Wai-Hong Tam | bea57b3 | 2011-09-02 18:27:47 +0800 | [diff] [blame] | 277 | for info in self._remote_infos.itervalues(): |
| 278 | if info['remote_process'] and info['remote_process'].poll() is None: |
| 279 | remote_object = getattr(self, info['ref_name']) |
Vadim Bendebury | 5b82cc5 | 2012-10-09 19:05:01 -0700 | [diff] [blame] | 280 | try: |
| 281 | remote_object.cleanup() |
| 282 | logging.info('Cleanup succeeded.') |
| 283 | except xmlrpclib.ProtocolError, e: |
| 284 | logging.info('Cleanup returned protocol error: ' + str(e)) |
Craig Harrison | 9194455 | 2011-08-04 14:09:55 -0700 | [diff] [blame] | 285 | self._terminate_all_ssh() |
| 286 | |
| 287 | |
Vic Yang | 4e0d1f7 | 2012-05-24 15:11:11 +0800 | [diff] [blame] | 288 | def cleanup(self): |
| 289 | """Delete the Servo object, call remote cleanup, and kill ssh.""" |
Vic Yang | 4e0d1f7 | 2012-05-24 15:11:11 +0800 | [diff] [blame] | 290 | self.kill_remote() |
| 291 | |
| 292 | |
Tom Wai-Hong Tam | bea57b3 | 2011-09-02 18:27:47 +0800 | [diff] [blame] | 293 | def _launch_ssh_tunnel(self, info): |
| 294 | """Establish an ssh tunnel for connecting to the remote RPC server. |
| 295 | |
| 296 | Args: |
| 297 | info: A dict of remote info, see the definition of self._remote_infos. |
| 298 | """ |
| 299 | if not info['ssh_tunnel'] or info['ssh_tunnel'].poll() is not None: |
Tom Wai-Hong Tam | e2c6612 | 2011-10-25 17:21:35 +0800 | [diff] [blame] | 300 | info['ssh_tunnel'] = subprocess.Popen([ |
Tom Wai-Hong Tam | 4a257e5 | 2011-11-12 08:36:22 +0800 | [diff] [blame] | 301 | 'ssh -N -n -q %s -L %s:localhost:%s root@%s' % |
Tom Wai-Hong Tam | e2c6612 | 2011-10-25 17:21:35 +0800 | [diff] [blame] | 302 | (info['ssh_config'], info['port'], info['port'], |
| 303 | self._client.ip)], shell=True) |
Craig Harrison | 9194455 | 2011-08-04 14:09:55 -0700 | [diff] [blame] | 304 | |
| 305 | |
Tom Wai-Hong Tam | bea57b3 | 2011-09-02 18:27:47 +0800 | [diff] [blame] | 306 | def _kill_remote_process(self, info): |
| 307 | """Ensure the remote process and local ssh process are terminated. |
| 308 | |
| 309 | Args: |
| 310 | info: A dict of remote info, see the definition of self._remote_infos. |
| 311 | """ |
| 312 | kill_cmd = 'pkill -f %s' % info['remote_command_short'] |
Tom Wai-Hong Tam | 4a257e5 | 2011-11-12 08:36:22 +0800 | [diff] [blame] | 313 | subprocess.call(['ssh -n -q %s root@%s \'%s\'' % |
Tom Wai-Hong Tam | e2c6612 | 2011-10-25 17:21:35 +0800 | [diff] [blame] | 314 | (info['ssh_config'], self._client.ip, kill_cmd)], |
Craig Harrison | 9194455 | 2011-08-04 14:09:55 -0700 | [diff] [blame] | 315 | shell=True) |
Tom Wai-Hong Tam | bea57b3 | 2011-09-02 18:27:47 +0800 | [diff] [blame] | 316 | if info['remote_process'] and info['remote_process'].poll() is None: |
| 317 | info['remote_process'].terminate() |
Craig Harrison | 9194455 | 2011-08-04 14:09:55 -0700 | [diff] [blame] | 318 | |
| 319 | |
| 320 | def _terminate_all_ssh(self): |
Tom Wai-Hong Tam | bea57b3 | 2011-09-02 18:27:47 +0800 | [diff] [blame] | 321 | """Terminate all ssh connections associated with remote processes.""" |
| 322 | for info in self._remote_infos.itervalues(): |
| 323 | if info['ssh_tunnel'] and info['ssh_tunnel'].poll() is None: |
| 324 | info['ssh_tunnel'].terminate() |
| 325 | self._kill_remote_process(info) |