Chris Masone | 6a0680f | 2012-03-02 08:40:00 -0800 | [diff] [blame] | 1 | # Copyright (c) 2012 The Chromium 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. |
Fang Deng | 7c2be10 | 2012-08-27 16:20:25 -0700 | [diff] [blame] | 4 | import re |
Scott Zawalski | 347a0b8 | 2012-03-30 16:39:21 -0400 | [diff] [blame] | 5 | import socket |
Chris Masone | 6a0680f | 2012-03-02 08:40:00 -0800 | [diff] [blame] | 6 | |
Scott Zawalski | 347a0b8 | 2012-03-30 16:39:21 -0400 | [diff] [blame] | 7 | from autotest_lib.client.common_lib import base_utils, global_config |
Chris Masone | 6a0680f | 2012-03-02 08:40:00 -0800 | [diff] [blame] | 8 | |
| 9 | def ping(host, deadline=None, tries=None, timeout=60): |
| 10 | """Attempt to ping |host|. |
| 11 | |
| 12 | Shell out to 'ping' to try to reach |host| for |timeout| seconds. |
| 13 | Returns exit code of ping. |
| 14 | |
| 15 | Per 'man ping', if you specify BOTH |deadline| and |tries|, ping only |
| 16 | returns 0 if we get responses to |tries| pings within |deadline| seconds. |
| 17 | |
| 18 | Specifying |deadline| or |count| alone should return 0 as long as |
| 19 | some packets receive responses. |
| 20 | |
| 21 | @param deadline: seconds within which |tries| pings must succeed. |
| 22 | @param tries: number of pings to send. |
| 23 | @param timeout: number of seconds after which to kill 'ping' command. |
| 24 | @return exit code of ping command. |
| 25 | """ |
| 26 | args = [host] |
| 27 | if deadline: |
| 28 | args.append('-w%d' % deadline) |
| 29 | if tries: |
| 30 | args.append('-c%d' % tries) |
| 31 | return base_utils.run('ping', args=args, |
| 32 | ignore_status=True, timeout=timeout, |
Scott Zawalski | ae84354 | 2012-03-20 09:51:29 -0400 | [diff] [blame] | 33 | stdout_tee=base_utils.TEE_TO_LOGS, |
| 34 | stderr_tee=base_utils.TEE_TO_LOGS).exit_status |
Scott Zawalski | 347a0b8 | 2012-03-30 16:39:21 -0400 | [diff] [blame] | 35 | |
| 36 | |
| 37 | def host_is_in_lab_zone(hostname): |
| 38 | """Check if the host is in the CROS.dns_zone. |
| 39 | |
| 40 | @param hostname: The hostname to check. |
| 41 | @returns True if hostname.dns_zone resolves, otherwise False. |
| 42 | """ |
| 43 | host_parts = hostname.split('.') |
| 44 | dns_zone = global_config.global_config.get_config_value('CROS', 'dns_zone', |
| 45 | default=None) |
| 46 | fqdn = '%s.%s' % (host_parts[0], dns_zone) |
| 47 | try: |
| 48 | socket.gethostbyname(fqdn) |
| 49 | return True |
| 50 | except socket.gaierror: |
| 51 | return False |
Fang Deng | 7c2be10 | 2012-08-27 16:20:25 -0700 | [diff] [blame] | 52 | |
| 53 | |
| 54 | def get_current_board(): |
| 55 | """Return the current board name. |
| 56 | |
| 57 | @return current board name, e.g "lumpy", None on fail. |
| 58 | """ |
| 59 | with open('/etc/lsb-release') as lsb_release_file: |
| 60 | for line in lsb_release_file: |
| 61 | m = re.match(r'^CHROMEOS_RELEASE_BOARD=(.+)$', line) |
| 62 | if m: |
| 63 | return m.group(1) |
| 64 | return None |