Aviv Keshet | 53bd44e | 2013-01-31 13:08:41 -0800 | [diff] [blame^] | 1 | #!/usr/bin/python |
| 2 | |
| 3 | # Copyright (c) 2013 The Chromium OS Authors. All rights reserved. |
| 4 | # Use of this source code is governed by a BSD-style license that can be |
| 5 | # found in the LICENSE file. |
| 6 | """A small wrapper script, iterates through |
| 7 | the known hosts and tries to call get_labels() |
| 8 | to discover host functionality, and adds these |
| 9 | detected labels to host. |
| 10 | |
| 11 | Limitations: |
| 12 | - Does not keep a count of how many labels were |
| 13 | actually added. |
| 14 | - If a label is added by this script because it |
| 15 | is detected as supported by get_labels, but later becomes |
| 16 | unsupported, this script has no way to know that it |
| 17 | should be removed, so it will remain attached to the host. |
| 18 | See crosbug.com/38569 |
| 19 | """ |
| 20 | |
| 21 | |
| 22 | import logging |
| 23 | import socket |
| 24 | import argparse |
| 25 | import sys |
| 26 | |
| 27 | import common |
| 28 | |
| 29 | from autotest_lib.server.hosts import ssh_host |
| 30 | from autotest_lib.server import frontend |
| 31 | from autotest_lib.client.common_lib import error |
| 32 | |
| 33 | |
| 34 | def add_missing_labels(hostname, afe): |
| 35 | """ |
| 36 | Queries the detectable labels supported by the given host, |
| 37 | and adds those labels to the host. |
| 38 | |
| 39 | @param hostname: The host to query and update. |
| 40 | @param afe: A frontend.AFE() instance. |
| 41 | |
| 42 | @return: True on success. |
| 43 | False on failure to fetch labels or to add any individual label. |
| 44 | """ |
| 45 | |
| 46 | try: |
| 47 | ssh_obj = ssh_host.SSHHost(hostname) |
| 48 | labels = ssh_obj.get_labels() |
| 49 | except socket.gaierror: |
| 50 | logging.warning('Unable to establish ssh connection to hostname ' |
| 51 | '%s. Skipping.', hostname) |
| 52 | return False |
| 53 | except error.AutoservRunError: |
| 54 | logging.warning('Unable to query labels on hostname %s. Skipping.', |
| 55 | hostname) |
| 56 | return False |
| 57 | |
| 58 | success = True |
| 59 | |
| 60 | |
| 61 | for label_name in labels: |
| 62 | label_matches = afe.get_labels(name=label_name) |
| 63 | |
| 64 | if not label_matches: |
| 65 | success = False |
| 66 | logging.warning('Unable to add label %s to host %s. ' |
| 67 | 'Skipping unknown label.', label_name, |
| 68 | hostname) |
| 69 | continue |
| 70 | |
| 71 | label_matches[0].add_hosts(hosts=[hostname]) |
| 72 | |
| 73 | return success |
| 74 | |
| 75 | |
| 76 | def main(): |
| 77 | """" |
| 78 | Entry point for add_detected_host_labels script. |
| 79 | """ |
| 80 | |
| 81 | parser = argparse.ArgumentParser() |
| 82 | parser.add_argument('-s', '--silent', dest='silent', action='store_true', |
| 83 | help='Suppress logging messages below.') |
| 84 | parser.add_argument('-i', '--info', dest='info_only', action='store_true', |
| 85 | help='Suppress logging messages below INFO priority.') |
| 86 | options = parser.parse_args() |
| 87 | |
| 88 | if options.silent and options.info_only: |
| 89 | print 'The -i and -s flags cannot be used together.' |
| 90 | parser.print_help() |
| 91 | return 0 |
| 92 | |
| 93 | |
| 94 | if options.silent: |
| 95 | logging.disable(logging.CRITICAL) |
| 96 | |
| 97 | if options.info_only: |
| 98 | logging.disable(logging.DEBUG) |
| 99 | |
| 100 | afe = frontend.AFE() |
| 101 | labels = afe.get_labels() |
| 102 | |
| 103 | hostnames = afe.get_hostnames() |
| 104 | failures = 0 |
| 105 | attempts = 0 |
| 106 | for hostname in hostnames: |
| 107 | if not add_missing_labels(hostname, afe): |
| 108 | failures += 1 |
| 109 | |
| 110 | attempts = len(hostnames) |
| 111 | |
| 112 | logging.info('Label updating finished. Failed update on %d out of %d ' |
| 113 | 'hosts.', failures, attempts) |
| 114 | |
| 115 | return 0 |
| 116 | |
| 117 | |
| 118 | if __name__ == '__main__': |
| 119 | sys.exit(main()) |