blob: 760e06bac91fdb5f5e74df024b64ef39994614c8 [file] [log] [blame]
Chris Masone6a0680f2012-03-02 08:40:00 -08001# 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 Basi87d7a212012-09-27 10:41:05 -07004import logging
Fang Deng7c2be102012-08-27 16:20:25 -07005import re
Scott Zawalski347a0b82012-03-30 16:39:21 -04006import socket
Chris Masone6a0680f2012-03-02 08:40:00 -08007
Scott Zawalski347a0b82012-03-30 16:39:21 -04008from autotest_lib.client.common_lib import base_utils, global_config
Chris Masone6a0680f2012-03-02 08:40:00 -08009
10def ping(host, deadline=None, tries=None, timeout=60):
11 """Attempt to ping |host|.
12
13 Shell out to 'ping' to try to reach |host| for |timeout| seconds.
14 Returns exit code of ping.
15
16 Per 'man ping', if you specify BOTH |deadline| and |tries|, ping only
17 returns 0 if we get responses to |tries| pings within |deadline| seconds.
18
19 Specifying |deadline| or |count| alone should return 0 as long as
20 some packets receive responses.
21
22 @param deadline: seconds within which |tries| pings must succeed.
23 @param tries: number of pings to send.
24 @param timeout: number of seconds after which to kill 'ping' command.
25 @return exit code of ping command.
26 """
27 args = [host]
28 if deadline:
29 args.append('-w%d' % deadline)
30 if tries:
31 args.append('-c%d' % tries)
32 return base_utils.run('ping', args=args,
33 ignore_status=True, timeout=timeout,
Scott Zawalskiae843542012-03-20 09:51:29 -040034 stdout_tee=base_utils.TEE_TO_LOGS,
35 stderr_tee=base_utils.TEE_TO_LOGS).exit_status
Scott Zawalski347a0b82012-03-30 16:39:21 -040036
37
38def host_is_in_lab_zone(hostname):
39 """Check if the host is in the CROS.dns_zone.
40
41 @param hostname: The hostname to check.
42 @returns True if hostname.dns_zone resolves, otherwise False.
43 """
44 host_parts = hostname.split('.')
45 dns_zone = global_config.global_config.get_config_value('CROS', 'dns_zone',
46 default=None)
47 fqdn = '%s.%s' % (host_parts[0], dns_zone)
48 try:
49 socket.gethostbyname(fqdn)
50 return True
51 except socket.gaierror:
52 return False
Fang Deng7c2be102012-08-27 16:20:25 -070053
54
55def get_current_board():
56 """Return the current board name.
57
58 @return current board name, e.g "lumpy", None on fail.
59 """
60 with open('/etc/lsb-release') as lsb_release_file:
61 for line in lsb_release_file:
62 m = re.match(r'^CHROMEOS_RELEASE_BOARD=(.+)$', line)
63 if m:
64 return m.group(1)
65 return None
Simran Basi87d7a212012-09-27 10:41:05 -070066
67
68# TODO(petermayo): crosbug.com/31826 Share this with _GsUpload in
69# //chromite.git/buildbot/prebuilt.py somewhere/somehow
70def gs_upload(local_file, remote_file, acl, result_dir=None,
71 transfer_timeout=300, acl_timeout=300):
72 """Upload to GS bucket.
73
74 @param local_file: Local file to upload
75 @param remote_file: Remote location to upload the local_file to.
76 @param acl: name or file used for controlling access to the uploaded
77 file.
78 @param result_dir: Result directory if you want to add tracing to the
79 upload.
80
81 @raise CmdError: the exit code of the gsutil call was not 0.
82
83 @returns True/False - depending on if the upload succeeded or failed.
84 """
85 # https://developers.google.com/storage/docs/accesscontrol#extension
86 CANNED_ACLS = ['project-private', 'private', 'public-read',
87 'public-read-write', 'authenticated-read',
88 'bucket-owner-read', 'bucket-owner-full-control']
89 _GSUTIL_BIN = 'gsutil'
90 acl_cmd = None
91 if acl in CANNED_ACLS:
92 cmd = '%s cp -a %s %s %s' % (_GSUTIL_BIN, acl, local_file, remote_file)
93 else:
94 # For private uploads we assume that the overlay board is set up
95 # properly and a googlestore_acl.xml is present, if not this script
96 # errors
97 cmd = '%s cp -a private %s %s' % (_GSUTIL_BIN, local_file, remote_file)
98 if not os.path.exists(acl):
99 logging.error('Unable to find ACL File %s.', acl)
100 return False
101 acl_cmd = '%s setacl %s %s' % (_GSUTIL_BIN, acl, remote_file)
102 if not result_dir:
103 base_utils.run(cmd, timeout=transfer_timeout, verbose=True)
104 if acl_cmd:
105 base_utils.run(acl_cmd, timeout=acl_timeout, verbose=True)
106 return True
107 with open(os.path.join(result_dir, 'tracing'), 'w') as ftrace:
108 ftrace.write('Preamble\n')
109 base_utils.run(cmd, timeout=transfer_timeout, verbose=True,
110 stdout_tee=ftrace, stderr_tee=ftrace)
111 if acl_cmd:
112 ftrace.write('\nACL setting\n')
113 # Apply the passed in ACL xml file to the uploaded object.
114 base_utils.run(acl_cmd, timeout=acl_timeout, verbose=True,
115 stdout_tee=ftrace, stderr_tee=ftrace)
116 ftrace.write('Postamble\n')
117 return True