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