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. |
Simran Basi | 87d7a21 | 2012-09-27 10:41:05 -0700 | [diff] [blame] | 4 | import logging |
Simran Basi | af9b8e7 | 2012-10-12 15:02:36 -0700 | [diff] [blame] | 5 | import os |
Fang Deng | 7c2be10 | 2012-08-27 16:20:25 -0700 | [diff] [blame] | 6 | import re |
Simran Basi | af9b8e7 | 2012-10-12 15:02:36 -0700 | [diff] [blame] | 7 | import signal |
Scott Zawalski | 347a0b8 | 2012-03-30 16:39:21 -0400 | [diff] [blame] | 8 | import socket |
Simran Basi | af9b8e7 | 2012-10-12 15:02:36 -0700 | [diff] [blame] | 9 | import time |
Chris Masone | 6a0680f | 2012-03-02 08:40:00 -0800 | [diff] [blame] | 10 | |
Simran Basi | af9b8e7 | 2012-10-12 15:02:36 -0700 | [diff] [blame] | 11 | from autotest_lib.client.common_lib import base_utils, error, global_config |
| 12 | |
| 13 | |
| 14 | # Keep checking if the pid is alive every second until the timeout (in seconds) |
| 15 | CHECK_PID_IS_ALIVE_TIMEOUT = 6 |
| 16 | |
Chris Masone | 6a0680f | 2012-03-02 08:40:00 -0800 | [diff] [blame] | 17 | |
Gilad Arnold | 0ed760c | 2012-11-05 23:42:53 -0800 | [diff] [blame^] | 18 | |
| 19 | _LOCAL_HOST_LIST = ['localhost', '127.0.0.1'] |
| 20 | |
| 21 | |
Chris Masone | 6a0680f | 2012-03-02 08:40:00 -0800 | [diff] [blame] | 22 | def ping(host, deadline=None, tries=None, timeout=60): |
| 23 | """Attempt to ping |host|. |
| 24 | |
| 25 | Shell out to 'ping' to try to reach |host| for |timeout| seconds. |
| 26 | Returns exit code of ping. |
| 27 | |
| 28 | Per 'man ping', if you specify BOTH |deadline| and |tries|, ping only |
| 29 | returns 0 if we get responses to |tries| pings within |deadline| seconds. |
| 30 | |
| 31 | Specifying |deadline| or |count| alone should return 0 as long as |
| 32 | some packets receive responses. |
| 33 | |
| 34 | @param deadline: seconds within which |tries| pings must succeed. |
| 35 | @param tries: number of pings to send. |
| 36 | @param timeout: number of seconds after which to kill 'ping' command. |
| 37 | @return exit code of ping command. |
| 38 | """ |
| 39 | args = [host] |
| 40 | if deadline: |
| 41 | args.append('-w%d' % deadline) |
| 42 | if tries: |
| 43 | args.append('-c%d' % tries) |
| 44 | return base_utils.run('ping', args=args, |
| 45 | ignore_status=True, timeout=timeout, |
Scott Zawalski | ae84354 | 2012-03-20 09:51:29 -0400 | [diff] [blame] | 46 | stdout_tee=base_utils.TEE_TO_LOGS, |
| 47 | stderr_tee=base_utils.TEE_TO_LOGS).exit_status |
Scott Zawalski | 347a0b8 | 2012-03-30 16:39:21 -0400 | [diff] [blame] | 48 | |
| 49 | |
| 50 | def host_is_in_lab_zone(hostname): |
| 51 | """Check if the host is in the CROS.dns_zone. |
| 52 | |
| 53 | @param hostname: The hostname to check. |
| 54 | @returns True if hostname.dns_zone resolves, otherwise False. |
| 55 | """ |
| 56 | host_parts = hostname.split('.') |
| 57 | dns_zone = global_config.global_config.get_config_value('CROS', 'dns_zone', |
| 58 | default=None) |
| 59 | fqdn = '%s.%s' % (host_parts[0], dns_zone) |
| 60 | try: |
| 61 | socket.gethostbyname(fqdn) |
| 62 | return True |
| 63 | except socket.gaierror: |
| 64 | return False |
Fang Deng | 7c2be10 | 2012-08-27 16:20:25 -0700 | [diff] [blame] | 65 | |
| 66 | |
| 67 | def get_current_board(): |
| 68 | """Return the current board name. |
| 69 | |
| 70 | @return current board name, e.g "lumpy", None on fail. |
| 71 | """ |
| 72 | with open('/etc/lsb-release') as lsb_release_file: |
| 73 | for line in lsb_release_file: |
| 74 | m = re.match(r'^CHROMEOS_RELEASE_BOARD=(.+)$', line) |
| 75 | if m: |
| 76 | return m.group(1) |
| 77 | return None |
Simran Basi | 87d7a21 | 2012-09-27 10:41:05 -0700 | [diff] [blame] | 78 | |
| 79 | |
| 80 | # TODO(petermayo): crosbug.com/31826 Share this with _GsUpload in |
| 81 | # //chromite.git/buildbot/prebuilt.py somewhere/somehow |
| 82 | def gs_upload(local_file, remote_file, acl, result_dir=None, |
| 83 | transfer_timeout=300, acl_timeout=300): |
| 84 | """Upload to GS bucket. |
| 85 | |
| 86 | @param local_file: Local file to upload |
| 87 | @param remote_file: Remote location to upload the local_file to. |
| 88 | @param acl: name or file used for controlling access to the uploaded |
| 89 | file. |
| 90 | @param result_dir: Result directory if you want to add tracing to the |
| 91 | upload. |
| 92 | |
| 93 | @raise CmdError: the exit code of the gsutil call was not 0. |
| 94 | |
| 95 | @returns True/False - depending on if the upload succeeded or failed. |
| 96 | """ |
| 97 | # https://developers.google.com/storage/docs/accesscontrol#extension |
| 98 | CANNED_ACLS = ['project-private', 'private', 'public-read', |
| 99 | 'public-read-write', 'authenticated-read', |
| 100 | 'bucket-owner-read', 'bucket-owner-full-control'] |
| 101 | _GSUTIL_BIN = 'gsutil' |
| 102 | acl_cmd = None |
| 103 | if acl in CANNED_ACLS: |
| 104 | cmd = '%s cp -a %s %s %s' % (_GSUTIL_BIN, acl, local_file, remote_file) |
| 105 | else: |
| 106 | # For private uploads we assume that the overlay board is set up |
| 107 | # properly and a googlestore_acl.xml is present, if not this script |
| 108 | # errors |
| 109 | cmd = '%s cp -a private %s %s' % (_GSUTIL_BIN, local_file, remote_file) |
| 110 | if not os.path.exists(acl): |
| 111 | logging.error('Unable to find ACL File %s.', acl) |
| 112 | return False |
| 113 | acl_cmd = '%s setacl %s %s' % (_GSUTIL_BIN, acl, remote_file) |
| 114 | if not result_dir: |
| 115 | base_utils.run(cmd, timeout=transfer_timeout, verbose=True) |
| 116 | if acl_cmd: |
| 117 | base_utils.run(acl_cmd, timeout=acl_timeout, verbose=True) |
| 118 | return True |
| 119 | with open(os.path.join(result_dir, 'tracing'), 'w') as ftrace: |
| 120 | ftrace.write('Preamble\n') |
| 121 | base_utils.run(cmd, timeout=transfer_timeout, verbose=True, |
| 122 | stdout_tee=ftrace, stderr_tee=ftrace) |
| 123 | if acl_cmd: |
| 124 | ftrace.write('\nACL setting\n') |
| 125 | # Apply the passed in ACL xml file to the uploaded object. |
| 126 | base_utils.run(acl_cmd, timeout=acl_timeout, verbose=True, |
| 127 | stdout_tee=ftrace, stderr_tee=ftrace) |
| 128 | ftrace.write('Postamble\n') |
| 129 | return True |
Simran Basi | af9b8e7 | 2012-10-12 15:02:36 -0700 | [diff] [blame] | 130 | |
| 131 | |
Gilad Arnold | 0ed760c | 2012-11-05 23:42:53 -0800 | [diff] [blame^] | 132 | def gs_ls(uri_pattern): |
| 133 | """Returns a list of URIs that match a given pattern. |
| 134 | |
| 135 | @param uri_pattern: a GS URI pattern, may contain wildcards |
| 136 | |
| 137 | @return A list of URIs matching the given pattern. |
| 138 | |
| 139 | @raise CmdError: the gsutil command failed. |
| 140 | |
| 141 | """ |
| 142 | gs_cmd = ' '.join(['gsutil', 'ls', uri_pattern]) |
| 143 | result = base_utils.system_output(gs_cmd).splitlines() |
| 144 | return [path.rstrip() for path in result if path] |
| 145 | |
| 146 | |
Simran Basi | af9b8e7 | 2012-10-12 15:02:36 -0700 | [diff] [blame] | 147 | def nuke_pids(pid_list, signal_queue=[signal.SIGTERM, signal.SIGKILL]): |
| 148 | """ |
| 149 | Given a list of pid's, kill them via an esclating series of signals. |
| 150 | |
| 151 | @param pid_list: List of PID's to kill. |
| 152 | @param signal_queue: Queue of signals to send the PID's to terminate them. |
| 153 | """ |
| 154 | for sig in signal_queue: |
| 155 | logging.debug('Sending signal %s to the following pids:', sig) |
| 156 | for pid in pid_list: |
| 157 | logging.debug('Pid %d', pid) |
| 158 | try: |
| 159 | os.kill(pid, sig) |
| 160 | except OSError: |
| 161 | # The process may have died from a previous signal before we |
| 162 | # could kill it. |
| 163 | pass |
| 164 | time.sleep(CHECK_PID_IS_ALIVE_TIMEOUT) |
| 165 | failed_list = [] |
| 166 | if signal.SIGKILL in signal_queue: |
| 167 | return |
| 168 | for pid in pid_list: |
| 169 | if base_utils.pid_is_alive(pid): |
| 170 | failed_list.append('Could not kill %d for process name: %s.' % pid, |
| 171 | get_process_name(pid)) |
| 172 | if failed_list: |
| 173 | raise error.AutoservRunError('Following errors occured: %s' % |
| 174 | failed_list, None) |
Gilad Arnold | 0ed760c | 2012-11-05 23:42:53 -0800 | [diff] [blame^] | 175 | |
| 176 | |
| 177 | def externalize_host(host): |
| 178 | """Returns an externally accessible host name. |
| 179 | |
| 180 | @param host: a host name or address (string) |
| 181 | |
| 182 | @return An externally visible host name or address |
| 183 | |
| 184 | """ |
| 185 | return socket.gethostname() if host in _LOCAL_HOST_LIST else host |