Tom Wai-Hong Tam | efe1c7f | 2014-01-02 14:00:11 +0800 | [diff] [blame] | 1 | # Copyright (c) 2014 The Chromium OS Authors. All rights reserved. |
| 2 | # Use of this source code is governed by a BSD-style license that can be |
| 3 | # found in the LICENSE file. |
| 4 | # |
| 5 | |
| 6 | """This file provides core logic for connecting a Chameleon Daemon.""" |
| 7 | |
| 8 | |
Tom Wai-Hong Tam | efe1c7f | 2014-01-02 14:00:11 +0800 | [diff] [blame] | 9 | from autotest_lib.client.bin import utils |
Tom Wai-Hong Tam | 2588ce1 | 2014-08-21 06:24:05 +0800 | [diff] [blame] | 10 | from autotest_lib.client.cros.chameleon import chameleon |
Tom Wai-Hong Tam | efe1c7f | 2014-01-02 14:00:11 +0800 | [diff] [blame] | 11 | from autotest_lib.server.hosts import ssh_host |
| 12 | |
| 13 | |
Tom Wai-Hong Tam | efe1c7f | 2014-01-02 14:00:11 +0800 | [diff] [blame] | 14 | class ChameleonHost(ssh_host.SSHHost): |
| 15 | """Host class for a host that controls a Chameleon.""" |
| 16 | |
| 17 | # Chameleond process name. |
| 18 | CHAMELEOND_PROCESS = 'chameleond' |
| 19 | |
| 20 | |
| 21 | # TODO(waihong): Add verify and repair logic which are required while |
| 22 | # deploying to Cros Lab. |
| 23 | |
| 24 | |
| 25 | def _initialize(self, chameleon_host='localhost', chameleon_port=9992, |
| 26 | *args, **dargs): |
| 27 | """Initialize a ChameleonHost instance. |
| 28 | |
| 29 | A ChameleonHost instance represents a host that controls a Chameleon. |
| 30 | |
| 31 | @param chameleon_host: Name of the host where the chameleond process |
| 32 | is running. |
| 33 | @param chameleon_port: Port the chameleond process is listening on. |
| 34 | |
| 35 | """ |
| 36 | super(ChameleonHost, self)._initialize(hostname=chameleon_host, |
| 37 | *args, **dargs) |
| 38 | self._is_in_lab = utils.host_is_in_lab_zone(self.hostname) |
Tom Wai-Hong Tam | c3fb2f6 | 2014-08-21 08:10:34 +0800 | [diff] [blame^] | 39 | self._chameleon_connection = chameleon.ChameleonConnection( |
| 40 | self.hostname, chameleon_port) |
Tom Wai-Hong Tam | efe1c7f | 2014-01-02 14:00:11 +0800 | [diff] [blame] | 41 | |
| 42 | |
| 43 | def is_in_lab(self): |
| 44 | """Check whether the chameleon host is a lab device. |
| 45 | |
| 46 | @returns: True if the chameleon host is in Cros Lab, otherwise False. |
| 47 | |
| 48 | """ |
| 49 | return self._is_in_lab |
| 50 | |
| 51 | |
Tom Wai-Hong Tam | efe1c7f | 2014-01-02 14:00:11 +0800 | [diff] [blame] | 52 | def get_wait_up_processes(self): |
| 53 | """Get the list of local processes to wait for in wait_up. |
| 54 | |
| 55 | Override get_wait_up_processes in |
| 56 | autotest_lib.client.common_lib.hosts.base_classes.Host. |
| 57 | Wait for chameleond process to go up. Called by base class when |
| 58 | rebooting the device. |
| 59 | |
| 60 | """ |
| 61 | processes = [self.CHAMELEOND_PROCESS] |
| 62 | return processes |
| 63 | |
Tom Wai-Hong Tam | eaee340 | 2014-01-22 08:52:10 +0800 | [diff] [blame] | 64 | |
| 65 | def create_chameleon_board(self): |
| 66 | """Create a ChameleonBoard object.""" |
| 67 | # TODO(waihong): Add verify and repair logic which are required while |
| 68 | # deploying to Cros Lab. |
Tom Wai-Hong Tam | c3fb2f6 | 2014-08-21 08:10:34 +0800 | [diff] [blame^] | 69 | return chameleon.ChameleonBoard(self._chameleon_connection) |
Tom Wai-Hong Tam | 3d6790d | 2014-04-14 16:15:47 +0800 | [diff] [blame] | 70 | |
| 71 | |
| 72 | def create_chameleon_host(dut, chameleon_args): |
| 73 | """Create a ChameleonHost object. |
| 74 | |
| 75 | There three possible cases: |
| 76 | 1) If the DUT is in Cros Lab and has a chameleon board, then create |
| 77 | a ChameleonHost object pointing to the board. chameleon_args |
| 78 | is ignored. |
| 79 | 2) If not case 1) and chameleon_args is neither None nor empty, then |
| 80 | create a ChameleonHost object using chameleon_args. |
| 81 | 3) If neither case 1) or 2) applies, return None. |
| 82 | |
| 83 | @param dut: host name of the host that chameleon connects. It can be used |
| 84 | to lookup the chameleon in test lab using naming convention. |
| 85 | @param chameleon_args: A dictionary that contains args for creating |
| 86 | a ChameleonHost object, |
| 87 | e.g. {'chameleon_host': '172.11.11.112', |
| 88 | 'chameleon_port': 9992}. |
| 89 | |
| 90 | @returns: A ChameleonHost object or None. |
| 91 | |
| 92 | """ |
Tom Wai-Hong Tam | c3fb2f6 | 2014-08-21 08:10:34 +0800 | [diff] [blame^] | 93 | hostname = chameleon.make_chameleon_hostname(dut) |
Tom Wai-Hong Tam | 3d6790d | 2014-04-14 16:15:47 +0800 | [diff] [blame] | 94 | if utils.host_is_in_lab_zone(hostname): |
| 95 | return ChameleonHost(chameleon_host=hostname) |
| 96 | elif chameleon_args: |
| 97 | return ChameleonHost(**chameleon_args) |
| 98 | else: |
| 99 | return None |