mbligh | be630eb | 2008-08-01 16:41:48 +0000 | [diff] [blame] | 1 | # Copyright 2008 Google Inc. All Rights Reserved. |
| 2 | |
| 3 | """ |
| 4 | The host module contains the objects and method used to |
| 5 | manage a host in Autotest. |
| 6 | |
| 7 | The valid actions are: |
| 8 | create: adds host(s) |
| 9 | delete: deletes host(s) |
| 10 | list: lists host(s) |
| 11 | stat: displays host(s) information |
| 12 | mod: modifies host(s) |
| 13 | jobs: lists all jobs that ran on host(s) |
| 14 | |
| 15 | The common options are: |
| 16 | -M|--mlist: file containing a list of machines |
| 17 | |
| 18 | |
mbligh | be630eb | 2008-08-01 16:41:48 +0000 | [diff] [blame] | 19 | See topic_common.py for a High Level Design and Algorithm. |
| 20 | |
| 21 | """ |
Justin Giorgi | 16bba56 | 2016-06-22 10:42:13 -0700 | [diff] [blame] | 22 | import common |
Simran Basi | 0739d68 | 2015-02-25 16:22:56 -0800 | [diff] [blame] | 23 | import re |
Justin Giorgi | 16bba56 | 2016-06-22 10:42:13 -0700 | [diff] [blame] | 24 | import socket |
mbligh | be630eb | 2008-08-01 16:41:48 +0000 | [diff] [blame] | 25 | |
Ningning Xia | ea02ab1 | 2018-05-11 15:08:57 -0700 | [diff] [blame] | 26 | from autotest_lib.cli import action_common, rpc, topic_common, skylab_utils |
Allen Li | 2c32d6b | 2017-02-03 15:28:10 -0800 | [diff] [blame] | 27 | from autotest_lib.client.bin import utils as bin_utils |
Justin Giorgi | 16bba56 | 2016-06-22 10:42:13 -0700 | [diff] [blame] | 28 | from autotest_lib.client.common_lib import error, host_protections |
Justin Giorgi | 5208eaa | 2016-07-02 20:12:12 -0700 | [diff] [blame] | 29 | from autotest_lib.server import frontend, hosts |
Prathmesh Prabhu | d252d26 | 2017-05-31 11:46:34 -0700 | [diff] [blame] | 30 | from autotest_lib.server.hosts import host_info |
mbligh | be630eb | 2008-08-01 16:41:48 +0000 | [diff] [blame] | 31 | |
| 32 | |
Ningning Xia | ea02ab1 | 2018-05-11 15:08:57 -0700 | [diff] [blame] | 33 | try: |
| 34 | from skylab_inventory import text_manager |
| 35 | from skylab_inventory.lib import device |
| 36 | except ImportError: |
| 37 | pass |
| 38 | |
| 39 | |
mbligh | be630eb | 2008-08-01 16:41:48 +0000 | [diff] [blame] | 40 | class host(topic_common.atest): |
| 41 | """Host class |
| 42 | atest host [create|delete|list|stat|mod|jobs] <options>""" |
| 43 | usage_action = '[create|delete|list|stat|mod|jobs]' |
| 44 | topic = msg_topic = 'host' |
| 45 | msg_items = '<hosts>' |
| 46 | |
mbligh | aed47e8 | 2009-03-17 19:06:18 +0000 | [diff] [blame] | 47 | protections = host_protections.Protection.names |
| 48 | |
mbligh | be630eb | 2008-08-01 16:41:48 +0000 | [diff] [blame] | 49 | |
| 50 | def __init__(self): |
| 51 | """Add to the parser the options common to all the |
| 52 | host actions""" |
| 53 | super(host, self).__init__() |
| 54 | |
| 55 | self.parser.add_option('-M', '--mlist', |
| 56 | help='File listing the machines', |
| 57 | type='string', |
| 58 | default=None, |
| 59 | metavar='MACHINE_FLIST') |
| 60 | |
mbligh | 9deeefa | 2009-05-01 23:11:08 +0000 | [diff] [blame] | 61 | self.topic_parse_info = topic_common.item_parse_info( |
| 62 | attribute_name='hosts', |
| 63 | filename_option='mlist', |
| 64 | use_leftover=True) |
mbligh | be630eb | 2008-08-01 16:41:48 +0000 | [diff] [blame] | 65 | |
| 66 | |
| 67 | def _parse_lock_options(self, options): |
| 68 | if options.lock and options.unlock: |
| 69 | self.invalid_syntax('Only specify one of ' |
| 70 | '--lock and --unlock.') |
| 71 | |
Ningning Xia | ef35cb5 | 2018-05-04 17:58:20 -0700 | [diff] [blame] | 72 | if options.skylab and options.unlock and options.unlock_lock_id is None: |
| 73 | self.invalid_syntax('Must provide --unlock-lock-id with "--skylab ' |
| 74 | '--unlock".') |
| 75 | |
| 76 | if (not (options.skylab and options.unlock) and |
| 77 | options.unlock_lock_id is not None): |
| 78 | self.invalid_syntax('--unlock-lock-id is only valid with ' |
| 79 | '"--skylab --unlock".') |
| 80 | |
| 81 | self.lock = options.lock |
| 82 | self.unlock = options.unlock |
| 83 | self.lock_reason = options.lock_reason |
| 84 | self.unlock_lock_id = options.unlock_lock_id |
| 85 | |
mbligh | be630eb | 2008-08-01 16:41:48 +0000 | [diff] [blame] | 86 | if options.lock: |
| 87 | self.data['locked'] = True |
| 88 | self.messages.append('Locked host') |
| 89 | elif options.unlock: |
| 90 | self.data['locked'] = False |
Matthew Sartori | 6818633 | 2015-04-27 17:19:53 -0700 | [diff] [blame] | 91 | self.data['lock_reason'] = '' |
mbligh | be630eb | 2008-08-01 16:41:48 +0000 | [diff] [blame] | 92 | self.messages.append('Unlocked host') |
| 93 | |
Matthew Sartori | 6818633 | 2015-04-27 17:19:53 -0700 | [diff] [blame] | 94 | if options.lock and options.lock_reason: |
| 95 | self.data['lock_reason'] = options.lock_reason |
| 96 | |
mbligh | be630eb | 2008-08-01 16:41:48 +0000 | [diff] [blame] | 97 | |
| 98 | def _cleanup_labels(self, labels, platform=None): |
| 99 | """Removes the platform label from the overall labels""" |
| 100 | if platform: |
| 101 | return [label for label in labels |
| 102 | if label != platform] |
| 103 | else: |
| 104 | try: |
| 105 | return [label for label in labels |
| 106 | if not label['platform']] |
| 107 | except TypeError: |
| 108 | # This is a hack - the server will soon |
| 109 | # do this, so all this code should be removed. |
| 110 | return labels |
| 111 | |
| 112 | |
| 113 | def get_items(self): |
| 114 | return self.hosts |
| 115 | |
| 116 | |
| 117 | class host_help(host): |
| 118 | """Just here to get the atest logic working. |
| 119 | Usage is set by its parent""" |
| 120 | pass |
| 121 | |
| 122 | |
| 123 | class host_list(action_common.atest_list, host): |
| 124 | """atest host list [--mlist <file>|<hosts>] [--label <label>] |
mbligh | 536a524 | 2008-10-18 14:35:54 +0000 | [diff] [blame] | 125 | [--status <status1,status2>] [--acl <ACL>] [--user <user>]""" |
mbligh | be630eb | 2008-08-01 16:41:48 +0000 | [diff] [blame] | 126 | |
| 127 | def __init__(self): |
| 128 | super(host_list, self).__init__() |
| 129 | |
| 130 | self.parser.add_option('-b', '--label', |
mbligh | 6444c6b | 2008-10-27 20:55:13 +0000 | [diff] [blame] | 131 | default='', |
| 132 | help='Only list hosts with all these labels ' |
Ningning Xia | ea02ab1 | 2018-05-11 15:08:57 -0700 | [diff] [blame] | 133 | '(comma separated). When --skylab is provided, ' |
| 134 | 'a label must be in the format of ' |
| 135 | 'label-key:label-value (e.g., board:lumpy).') |
mbligh | be630eb | 2008-08-01 16:41:48 +0000 | [diff] [blame] | 136 | self.parser.add_option('-s', '--status', |
mbligh | 6444c6b | 2008-10-27 20:55:13 +0000 | [diff] [blame] | 137 | default='', |
| 138 | help='Only list hosts with any of these ' |
| 139 | 'statuses (comma separated)') |
mbligh | 536a524 | 2008-10-18 14:35:54 +0000 | [diff] [blame] | 140 | self.parser.add_option('-a', '--acl', |
mbligh | 6444c6b | 2008-10-27 20:55:13 +0000 | [diff] [blame] | 141 | default='', |
Ningning Xia | ea02ab1 | 2018-05-11 15:08:57 -0700 | [diff] [blame] | 142 | help=('Only list hosts within this ACL. %s' % |
| 143 | skylab_utils.MSG_INVALID_IN_SKYLAB)) |
mbligh | 536a524 | 2008-10-18 14:35:54 +0000 | [diff] [blame] | 144 | self.parser.add_option('-u', '--user', |
mbligh | 6444c6b | 2008-10-27 20:55:13 +0000 | [diff] [blame] | 145 | default='', |
Ningning Xia | ea02ab1 | 2018-05-11 15:08:57 -0700 | [diff] [blame] | 146 | help=('Only list hosts available to this user. ' |
| 147 | '%s' % skylab_utils.MSG_INVALID_IN_SKYLAB)) |
mbligh | 70b9bf4 | 2008-11-18 15:00:46 +0000 | [diff] [blame] | 148 | self.parser.add_option('-N', '--hostnames-only', help='Only return ' |
| 149 | 'hostnames for the machines queried.', |
| 150 | action='store_true') |
mbligh | 91e0efd | 2009-02-26 01:02:16 +0000 | [diff] [blame] | 151 | self.parser.add_option('--locked', |
| 152 | default=False, |
| 153 | help='Only list locked hosts', |
| 154 | action='store_true') |
| 155 | self.parser.add_option('--unlocked', |
| 156 | default=False, |
| 157 | help='Only list unlocked hosts', |
| 158 | action='store_true') |
Ningning Xia | ea02ab1 | 2018-05-11 15:08:57 -0700 | [diff] [blame] | 159 | self.parser.add_option('--full-output', |
| 160 | default=False, |
| 161 | help=('Print out the full content of the hosts. ' |
| 162 | 'Only supported with --skylab.'), |
| 163 | action='store_true', |
| 164 | dest='full_output') |
mbligh | 91e0efd | 2009-02-26 01:02:16 +0000 | [diff] [blame] | 165 | |
Ningning Xia | ea02ab1 | 2018-05-11 15:08:57 -0700 | [diff] [blame] | 166 | self.add_skylab_options() |
mbligh | be630eb | 2008-08-01 16:41:48 +0000 | [diff] [blame] | 167 | |
| 168 | |
| 169 | def parse(self): |
| 170 | """Consume the specific options""" |
jamesren | c286316 | 2010-07-12 21:20:51 +0000 | [diff] [blame] | 171 | label_info = topic_common.item_parse_info(attribute_name='labels', |
| 172 | inline_option='label') |
| 173 | |
| 174 | (options, leftover) = super(host_list, self).parse([label_info]) |
| 175 | |
mbligh | be630eb | 2008-08-01 16:41:48 +0000 | [diff] [blame] | 176 | self.status = options.status |
mbligh | 536a524 | 2008-10-18 14:35:54 +0000 | [diff] [blame] | 177 | self.acl = options.acl |
| 178 | self.user = options.user |
mbligh | 70b9bf4 | 2008-11-18 15:00:46 +0000 | [diff] [blame] | 179 | self.hostnames_only = options.hostnames_only |
mbligh | 91e0efd | 2009-02-26 01:02:16 +0000 | [diff] [blame] | 180 | |
| 181 | if options.locked and options.unlocked: |
| 182 | self.invalid_syntax('--locked and --unlocked are ' |
| 183 | 'mutually exclusive') |
Ningning Xia | ea02ab1 | 2018-05-11 15:08:57 -0700 | [diff] [blame] | 184 | |
mbligh | 91e0efd | 2009-02-26 01:02:16 +0000 | [diff] [blame] | 185 | self.locked = options.locked |
| 186 | self.unlocked = options.unlocked |
Ningning Xia | 64ced00 | 2018-05-16 17:36:20 -0700 | [diff] [blame] | 187 | self.label_map = None |
Ningning Xia | ea02ab1 | 2018-05-11 15:08:57 -0700 | [diff] [blame] | 188 | |
| 189 | if self.skylab: |
| 190 | if options.user or options.acl or options.status: |
| 191 | self.invalid_syntax('--user, --acl or --status is not ' |
| 192 | 'supported with --skylab.') |
| 193 | self.full_output = options.full_output |
| 194 | if self.full_output and self.hostnames_only: |
| 195 | self.invalid_syntax('--full-output is conflicted with ' |
| 196 | '--hostnames-only.') |
Ningning Xia | 64ced00 | 2018-05-16 17:36:20 -0700 | [diff] [blame] | 197 | |
| 198 | if self.labels: |
| 199 | self.label_map = device.convert_to_label_map(self.labels) |
Ningning Xia | ea02ab1 | 2018-05-11 15:08:57 -0700 | [diff] [blame] | 200 | else: |
| 201 | if options.full_output: |
| 202 | self.invalid_syntax('--full_output is only supported with ' |
| 203 | '--skylab.') |
| 204 | |
mbligh | be630eb | 2008-08-01 16:41:48 +0000 | [diff] [blame] | 205 | return (options, leftover) |
| 206 | |
| 207 | |
Ningning Xia | ea02ab1 | 2018-05-11 15:08:57 -0700 | [diff] [blame] | 208 | def execute_skylab(self): |
| 209 | """Execute 'atest host list' with --skylab.""" |
| 210 | inventory_repo = skylab_utils.InventoryRepo(self.inventory_repo_dir) |
| 211 | inventory_repo.initialize() |
| 212 | lab = text_manager.load_lab(inventory_repo.get_data_dir()) |
| 213 | |
| 214 | # TODO(nxia): support filtering on run-time labels and status. |
| 215 | return device.get_devices( |
| 216 | lab, |
| 217 | 'duts', |
| 218 | self.environment, |
Ningning Xia | 64ced00 | 2018-05-16 17:36:20 -0700 | [diff] [blame] | 219 | label_map=self.label_map, |
Ningning Xia | ea02ab1 | 2018-05-11 15:08:57 -0700 | [diff] [blame] | 220 | hostnames=self.hosts, |
| 221 | locked=self.locked, |
| 222 | unlocked=self.unlocked) |
| 223 | |
| 224 | |
mbligh | be630eb | 2008-08-01 16:41:48 +0000 | [diff] [blame] | 225 | def execute(self): |
xixuan | d6011f1 | 2016-12-08 15:01:58 -0800 | [diff] [blame] | 226 | """Execute 'atest host list'.""" |
Ningning Xia | ea02ab1 | 2018-05-11 15:08:57 -0700 | [diff] [blame] | 227 | if self.skylab: |
| 228 | return self.execute_skylab() |
| 229 | |
mbligh | be630eb | 2008-08-01 16:41:48 +0000 | [diff] [blame] | 230 | filters = {} |
| 231 | check_results = {} |
| 232 | if self.hosts: |
| 233 | filters['hostname__in'] = self.hosts |
| 234 | check_results['hostname__in'] = 'hostname' |
mbligh | 6444c6b | 2008-10-27 20:55:13 +0000 | [diff] [blame] | 235 | |
| 236 | if self.labels: |
jamesren | c286316 | 2010-07-12 21:20:51 +0000 | [diff] [blame] | 237 | if len(self.labels) == 1: |
mbligh | f703fb4 | 2009-01-30 00:35:05 +0000 | [diff] [blame] | 238 | # This is needed for labels with wildcards (x86*) |
jamesren | c286316 | 2010-07-12 21:20:51 +0000 | [diff] [blame] | 239 | filters['labels__name__in'] = self.labels |
mbligh | f703fb4 | 2009-01-30 00:35:05 +0000 | [diff] [blame] | 240 | check_results['labels__name__in'] = None |
| 241 | else: |
jamesren | c286316 | 2010-07-12 21:20:51 +0000 | [diff] [blame] | 242 | filters['multiple_labels'] = self.labels |
mbligh | f703fb4 | 2009-01-30 00:35:05 +0000 | [diff] [blame] | 243 | check_results['multiple_labels'] = None |
mbligh | 6444c6b | 2008-10-27 20:55:13 +0000 | [diff] [blame] | 244 | |
mbligh | be630eb | 2008-08-01 16:41:48 +0000 | [diff] [blame] | 245 | if self.status: |
mbligh | 6444c6b | 2008-10-27 20:55:13 +0000 | [diff] [blame] | 246 | statuses = self.status.split(',') |
| 247 | statuses = [status.strip() for status in statuses |
| 248 | if status.strip()] |
| 249 | |
| 250 | filters['status__in'] = statuses |
mbligh | cd8eb97 | 2008-08-25 19:20:39 +0000 | [diff] [blame] | 251 | check_results['status__in'] = None |
mbligh | 6444c6b | 2008-10-27 20:55:13 +0000 | [diff] [blame] | 252 | |
mbligh | 536a524 | 2008-10-18 14:35:54 +0000 | [diff] [blame] | 253 | if self.acl: |
showard | d9ac445 | 2009-02-07 02:04:37 +0000 | [diff] [blame] | 254 | filters['aclgroup__name'] = self.acl |
| 255 | check_results['aclgroup__name'] = None |
mbligh | 536a524 | 2008-10-18 14:35:54 +0000 | [diff] [blame] | 256 | if self.user: |
showard | d9ac445 | 2009-02-07 02:04:37 +0000 | [diff] [blame] | 257 | filters['aclgroup__users__login'] = self.user |
| 258 | check_results['aclgroup__users__login'] = None |
mbligh | 91e0efd | 2009-02-26 01:02:16 +0000 | [diff] [blame] | 259 | |
| 260 | if self.locked or self.unlocked: |
| 261 | filters['locked'] = self.locked |
| 262 | check_results['locked'] = None |
| 263 | |
mbligh | be630eb | 2008-08-01 16:41:48 +0000 | [diff] [blame] | 264 | return super(host_list, self).execute(op='get_hosts', |
| 265 | filters=filters, |
| 266 | check_results=check_results) |
| 267 | |
| 268 | |
| 269 | def output(self, results): |
xixuan | d6011f1 | 2016-12-08 15:01:58 -0800 | [diff] [blame] | 270 | """Print output of 'atest host list'. |
| 271 | |
| 272 | @param results: the results to be printed. |
| 273 | """ |
Ningning Xia | ea02ab1 | 2018-05-11 15:08:57 -0700 | [diff] [blame] | 274 | if results and not self.skylab: |
mbligh | be630eb | 2008-08-01 16:41:48 +0000 | [diff] [blame] | 275 | # Remove the platform from the labels. |
| 276 | for result in results: |
| 277 | result['labels'] = self._cleanup_labels(result['labels'], |
| 278 | result['platform']) |
Ningning Xia | ea02ab1 | 2018-05-11 15:08:57 -0700 | [diff] [blame] | 279 | if self.skylab and self.full_output: |
| 280 | print results |
| 281 | return |
| 282 | |
| 283 | if self.skylab: |
| 284 | results = device.convert_to_autotest_hosts(results) |
| 285 | |
mbligh | 70b9bf4 | 2008-11-18 15:00:46 +0000 | [diff] [blame] | 286 | if self.hostnames_only: |
mbligh | df75f8b | 2008-11-18 19:07:42 +0000 | [diff] [blame] | 287 | self.print_list(results, key='hostname') |
mbligh | 70b9bf4 | 2008-11-18 15:00:46 +0000 | [diff] [blame] | 288 | else: |
Ningning Xia | ea02ab1 | 2018-05-11 15:08:57 -0700 | [diff] [blame] | 289 | keys = ['hostname', 'status', 'shard', 'locked', 'lock_reason', |
| 290 | 'locked_by', 'platform', 'labels'] |
Prashanth Balasubramanian | a504856 | 2014-12-12 10:14:11 -0800 | [diff] [blame] | 291 | super(host_list, self).output(results, keys=keys) |
mbligh | be630eb | 2008-08-01 16:41:48 +0000 | [diff] [blame] | 292 | |
| 293 | |
| 294 | class host_stat(host): |
| 295 | """atest host stat --mlist <file>|<hosts>""" |
| 296 | usage_action = 'stat' |
| 297 | |
| 298 | def execute(self): |
xixuan | d6011f1 | 2016-12-08 15:01:58 -0800 | [diff] [blame] | 299 | """Execute 'atest host stat'.""" |
mbligh | be630eb | 2008-08-01 16:41:48 +0000 | [diff] [blame] | 300 | results = [] |
| 301 | # Convert wildcards into real host stats. |
| 302 | existing_hosts = [] |
| 303 | for host in self.hosts: |
| 304 | if host.endswith('*'): |
| 305 | stats = self.execute_rpc('get_hosts', |
| 306 | hostname__startswith=host.rstrip('*')) |
| 307 | if len(stats) == 0: |
| 308 | self.failure('No hosts matching %s' % host, item=host, |
| 309 | what_failed='Failed to stat') |
| 310 | continue |
| 311 | else: |
| 312 | stats = self.execute_rpc('get_hosts', hostname=host) |
| 313 | if len(stats) == 0: |
| 314 | self.failure('Unknown host %s' % host, item=host, |
| 315 | what_failed='Failed to stat') |
| 316 | continue |
| 317 | existing_hosts.extend(stats) |
| 318 | |
| 319 | for stat in existing_hosts: |
| 320 | host = stat['hostname'] |
| 321 | # The host exists, these should succeed |
| 322 | acls = self.execute_rpc('get_acl_groups', hosts__hostname=host) |
| 323 | |
| 324 | labels = self.execute_rpc('get_labels', host__hostname=host) |
Simran Basi | 0739d68 | 2015-02-25 16:22:56 -0800 | [diff] [blame] | 325 | results.append([[stat], acls, labels, stat['attributes']]) |
mbligh | be630eb | 2008-08-01 16:41:48 +0000 | [diff] [blame] | 326 | return results |
| 327 | |
| 328 | |
| 329 | def output(self, results): |
xixuan | d6011f1 | 2016-12-08 15:01:58 -0800 | [diff] [blame] | 330 | """Print output of 'atest host stat'. |
| 331 | |
| 332 | @param results: the results to be printed. |
| 333 | """ |
Simran Basi | 0739d68 | 2015-02-25 16:22:56 -0800 | [diff] [blame] | 334 | for stats, acls, labels, attributes in results: |
mbligh | be630eb | 2008-08-01 16:41:48 +0000 | [diff] [blame] | 335 | print '-'*5 |
| 336 | self.print_fields(stats, |
Aviv Keshet | a40d127 | 2017-11-16 00:56:35 -0800 | [diff] [blame] | 337 | keys=['hostname', 'id', 'platform', |
mbligh | e163b03 | 2008-10-18 14:30:27 +0000 | [diff] [blame] | 338 | 'status', 'locked', 'locked_by', |
Matthew Sartori | 6818633 | 2015-04-27 17:19:53 -0700 | [diff] [blame] | 339 | 'lock_time', 'lock_reason', 'protection',]) |
mbligh | be630eb | 2008-08-01 16:41:48 +0000 | [diff] [blame] | 340 | self.print_by_ids(acls, 'ACLs', line_before=True) |
| 341 | labels = self._cleanup_labels(labels) |
| 342 | self.print_by_ids(labels, 'Labels', line_before=True) |
Simran Basi | 0739d68 | 2015-02-25 16:22:56 -0800 | [diff] [blame] | 343 | self.print_dict(attributes, 'Host Attributes', line_before=True) |
mbligh | be630eb | 2008-08-01 16:41:48 +0000 | [diff] [blame] | 344 | |
| 345 | |
| 346 | class host_jobs(host): |
mbligh | 1494eca | 2008-08-13 21:24:22 +0000 | [diff] [blame] | 347 | """atest host jobs [--max-query] --mlist <file>|<hosts>""" |
mbligh | be630eb | 2008-08-01 16:41:48 +0000 | [diff] [blame] | 348 | usage_action = 'jobs' |
| 349 | |
mbligh | 6996fe8 | 2008-08-13 00:32:27 +0000 | [diff] [blame] | 350 | def __init__(self): |
| 351 | super(host_jobs, self).__init__() |
| 352 | self.parser.add_option('-q', '--max-query', |
| 353 | help='Limits the number of results ' |
| 354 | '(20 by default)', |
| 355 | type='int', default=20) |
| 356 | |
| 357 | |
| 358 | def parse(self): |
| 359 | """Consume the specific options""" |
mbligh | 9deeefa | 2009-05-01 23:11:08 +0000 | [diff] [blame] | 360 | (options, leftover) = super(host_jobs, self).parse() |
mbligh | 6996fe8 | 2008-08-13 00:32:27 +0000 | [diff] [blame] | 361 | self.max_queries = options.max_query |
| 362 | return (options, leftover) |
| 363 | |
| 364 | |
mbligh | be630eb | 2008-08-01 16:41:48 +0000 | [diff] [blame] | 365 | def execute(self): |
xixuan | d6011f1 | 2016-12-08 15:01:58 -0800 | [diff] [blame] | 366 | """Execute 'atest host jobs'.""" |
mbligh | be630eb | 2008-08-01 16:41:48 +0000 | [diff] [blame] | 367 | results = [] |
| 368 | real_hosts = [] |
| 369 | for host in self.hosts: |
| 370 | if host.endswith('*'): |
| 371 | stats = self.execute_rpc('get_hosts', |
| 372 | hostname__startswith=host.rstrip('*')) |
| 373 | if len(stats) == 0: |
| 374 | self.failure('No host matching %s' % host, item=host, |
| 375 | what_failed='Failed to stat') |
| 376 | [real_hosts.append(stat['hostname']) for stat in stats] |
| 377 | else: |
| 378 | real_hosts.append(host) |
| 379 | |
| 380 | for host in real_hosts: |
| 381 | queue_entries = self.execute_rpc('get_host_queue_entries', |
mbligh | 6996fe8 | 2008-08-13 00:32:27 +0000 | [diff] [blame] | 382 | host__hostname=host, |
mbligh | 1494eca | 2008-08-13 21:24:22 +0000 | [diff] [blame] | 383 | query_limit=self.max_queries, |
showard | 6958c72 | 2009-09-23 20:03:16 +0000 | [diff] [blame] | 384 | sort_by=['-job__id']) |
mbligh | be630eb | 2008-08-01 16:41:48 +0000 | [diff] [blame] | 385 | jobs = [] |
| 386 | for entry in queue_entries: |
| 387 | job = {'job_id': entry['job']['id'], |
| 388 | 'job_owner': entry['job']['owner'], |
| 389 | 'job_name': entry['job']['name'], |
| 390 | 'status': entry['status']} |
| 391 | jobs.append(job) |
| 392 | results.append((host, jobs)) |
| 393 | return results |
| 394 | |
| 395 | |
| 396 | def output(self, results): |
xixuan | d6011f1 | 2016-12-08 15:01:58 -0800 | [diff] [blame] | 397 | """Print output of 'atest host jobs'. |
| 398 | |
| 399 | @param results: the results to be printed. |
| 400 | """ |
mbligh | be630eb | 2008-08-01 16:41:48 +0000 | [diff] [blame] | 401 | for host, jobs in results: |
| 402 | print '-'*5 |
| 403 | print 'Hostname: %s' % host |
| 404 | self.print_table(jobs, keys_header=['job_id', |
showard | be0d869 | 2009-08-20 23:42:44 +0000 | [diff] [blame] | 405 | 'job_owner', |
| 406 | 'job_name', |
| 407 | 'status']) |
mbligh | be630eb | 2008-08-01 16:41:48 +0000 | [diff] [blame] | 408 | |
Justin Giorgi | 16bba56 | 2016-06-22 10:42:13 -0700 | [diff] [blame] | 409 | class BaseHostModCreate(host): |
xixuan | d6011f1 | 2016-12-08 15:01:58 -0800 | [diff] [blame] | 410 | """The base class for host_mod and host_create""" |
Justin Giorgi | 9261f9f | 2016-07-11 17:48:52 -0700 | [diff] [blame] | 411 | # Matches one attribute=value pair |
| 412 | attribute_regex = r'(?P<attribute>\w+)=(?P<value>.+)?' |
mbligh | be630eb | 2008-08-01 16:41:48 +0000 | [diff] [blame] | 413 | |
| 414 | def __init__(self): |
Justin Giorgi | 16bba56 | 2016-06-22 10:42:13 -0700 | [diff] [blame] | 415 | """Add the options shared between host mod and host create actions.""" |
mbligh | be630eb | 2008-08-01 16:41:48 +0000 | [diff] [blame] | 416 | self.messages = [] |
Justin Giorgi | 16bba56 | 2016-06-22 10:42:13 -0700 | [diff] [blame] | 417 | self.host_ids = {} |
| 418 | super(BaseHostModCreate, self).__init__() |
mbligh | be630eb | 2008-08-01 16:41:48 +0000 | [diff] [blame] | 419 | self.parser.add_option('-l', '--lock', |
Ningning Xia | ef35cb5 | 2018-05-04 17:58:20 -0700 | [diff] [blame] | 420 | help='Lock hosts.', |
mbligh | be630eb | 2008-08-01 16:41:48 +0000 | [diff] [blame] | 421 | action='store_true') |
Matthew Sartori | 6818633 | 2015-04-27 17:19:53 -0700 | [diff] [blame] | 422 | self.parser.add_option('-r', '--lock_reason', |
Ningning Xia | ef35cb5 | 2018-05-04 17:58:20 -0700 | [diff] [blame] | 423 | help='Reason for locking hosts.', |
Matthew Sartori | 6818633 | 2015-04-27 17:19:53 -0700 | [diff] [blame] | 424 | default='') |
Ningning Xia | ef35cb5 | 2018-05-04 17:58:20 -0700 | [diff] [blame] | 425 | self.parser.add_option('-u', '--unlock', |
| 426 | help='Unlock hosts.', |
| 427 | action='store_true') |
| 428 | self.parser.add_option('--unlock-lock-id', |
| 429 | help=('Unlock the lock with the lock-id. Only ' |
| 430 | 'useful when unlocking skylab hosts.'), |
| 431 | default=None) |
mbligh | e163b03 | 2008-10-18 14:30:27 +0000 | [diff] [blame] | 432 | self.parser.add_option('-p', '--protection', type='choice', |
mbligh | aed47e8 | 2009-03-17 19:06:18 +0000 | [diff] [blame] | 433 | help=('Set the protection level on a host. ' |
Ningning Xia | ef35cb5 | 2018-05-04 17:58:20 -0700 | [diff] [blame] | 434 | 'Must be one of: %s. %s' % |
| 435 | (', '.join('"%s"' % p |
| 436 | for p in self.protections), |
| 437 | skylab_utils.MSG_INVALID_IN_SKYLAB)), |
mbligh | aed47e8 | 2009-03-17 19:06:18 +0000 | [diff] [blame] | 438 | choices=self.protections) |
Justin Giorgi | 9261f9f | 2016-07-11 17:48:52 -0700 | [diff] [blame] | 439 | self._attributes = [] |
| 440 | self.parser.add_option('--attribute', '-i', |
| 441 | help=('Host attribute to add or change. Format ' |
| 442 | 'is <attribute>=<value>. Multiple ' |
| 443 | 'attributes can be set by passing the ' |
| 444 | 'argument multiple times. Attributes can ' |
| 445 | 'be unset by providing an empty value.'), |
| 446 | action='append') |
mbligh | be630eb | 2008-08-01 16:41:48 +0000 | [diff] [blame] | 447 | self.parser.add_option('-b', '--labels', |
Ningning Xia | ef35cb5 | 2018-05-04 17:58:20 -0700 | [diff] [blame] | 448 | help=('Comma separated list of labels. ' |
| 449 | 'When --skylab is provided, a label must ' |
| 450 | 'be in the format of label-key:label-value' |
| 451 | ' (e.g., board:lumpy).')) |
mbligh | be630eb | 2008-08-01 16:41:48 +0000 | [diff] [blame] | 452 | self.parser.add_option('-B', '--blist', |
| 453 | help='File listing the labels', |
| 454 | type='string', |
| 455 | metavar='LABEL_FLIST') |
| 456 | self.parser.add_option('-a', '--acls', |
Ningning Xia | ef35cb5 | 2018-05-04 17:58:20 -0700 | [diff] [blame] | 457 | help=('Comma separated list of ACLs. %s' % |
| 458 | skylab_utils.MSG_INVALID_IN_SKYLAB)) |
mbligh | be630eb | 2008-08-01 16:41:48 +0000 | [diff] [blame] | 459 | self.parser.add_option('-A', '--alist', |
Ningning Xia | ef35cb5 | 2018-05-04 17:58:20 -0700 | [diff] [blame] | 460 | help=('File listing the acls. %s' % |
| 461 | skylab_utils.MSG_INVALID_IN_SKYLAB), |
mbligh | be630eb | 2008-08-01 16:41:48 +0000 | [diff] [blame] | 462 | type='string', |
| 463 | metavar='ACL_FLIST') |
Justin Giorgi | 16bba56 | 2016-06-22 10:42:13 -0700 | [diff] [blame] | 464 | self.parser.add_option('-t', '--platform', |
Ningning Xia | ef35cb5 | 2018-05-04 17:58:20 -0700 | [diff] [blame] | 465 | help=('Sets the platform label. This is ' |
| 466 | 'deprecated for --skylab. Please set ' |
| 467 | 'platform in labels (e.g., ' |
| 468 | '-b platform:platform_name).')) |
| 469 | |
| 470 | self.add_skylab_options() |
mbligh | be630eb | 2008-08-01 16:41:48 +0000 | [diff] [blame] | 471 | |
| 472 | |
| 473 | def parse(self): |
Justin Giorgi | 16bba56 | 2016-06-22 10:42:13 -0700 | [diff] [blame] | 474 | """Consume the options common to host create and host mod. |
| 475 | """ |
mbligh | 9deeefa | 2009-05-01 23:11:08 +0000 | [diff] [blame] | 476 | label_info = topic_common.item_parse_info(attribute_name='labels', |
| 477 | inline_option='labels', |
| 478 | filename_option='blist') |
| 479 | acl_info = topic_common.item_parse_info(attribute_name='acls', |
| 480 | inline_option='acls', |
| 481 | filename_option='alist') |
| 482 | |
Justin Giorgi | 16bba56 | 2016-06-22 10:42:13 -0700 | [diff] [blame] | 483 | (options, leftover) = super(BaseHostModCreate, self).parse([label_info, |
mbligh | 9deeefa | 2009-05-01 23:11:08 +0000 | [diff] [blame] | 484 | acl_info], |
| 485 | req_items='hosts') |
mbligh | be630eb | 2008-08-01 16:41:48 +0000 | [diff] [blame] | 486 | |
| 487 | self._parse_lock_options(options) |
Justin Giorgi | 16bba56 | 2016-06-22 10:42:13 -0700 | [diff] [blame] | 488 | |
Ningning Xia | 64ced00 | 2018-05-16 17:36:20 -0700 | [diff] [blame] | 489 | self.label_map = None |
| 490 | if self.skylab: |
Ningning Xia | ef35cb5 | 2018-05-04 17:58:20 -0700 | [diff] [blame] | 491 | # TODO(nxia): drop these flags when all hosts are migrated to skylab |
Ningning Xia | 64ced00 | 2018-05-16 17:36:20 -0700 | [diff] [blame] | 492 | if (options.protection or options.acls or options.alist or |
| 493 | options.platform): |
| 494 | self.invalid_syntax( |
| 495 | '--protection, --acls, --alist or --platform is not ' |
| 496 | 'supported with --skylab.') |
| 497 | |
| 498 | if self.labels: |
| 499 | self.label_map = device.convert_to_label_map(self.labels) |
Ningning Xia | ef35cb5 | 2018-05-04 17:58:20 -0700 | [diff] [blame] | 500 | |
mbligh | aed47e8 | 2009-03-17 19:06:18 +0000 | [diff] [blame] | 501 | if options.protection: |
| 502 | self.data['protection'] = options.protection |
Justin Giorgi | 16bba56 | 2016-06-22 10:42:13 -0700 | [diff] [blame] | 503 | self.messages.append('Protection set to "%s"' % options.protection) |
| 504 | |
| 505 | self.attributes = {} |
Justin Giorgi | 9261f9f | 2016-07-11 17:48:52 -0700 | [diff] [blame] | 506 | if options.attribute: |
| 507 | for pair in options.attribute: |
| 508 | m = re.match(self.attribute_regex, pair) |
| 509 | if not m: |
| 510 | raise topic_common.CliError('Attribute must be in key=value ' |
| 511 | 'syntax.') |
| 512 | elif m.group('attribute') in self.attributes: |
xixuan | d6011f1 | 2016-12-08 15:01:58 -0800 | [diff] [blame] | 513 | raise topic_common.CliError( |
| 514 | 'Multiple values provided for attribute ' |
| 515 | '%s.' % m.group('attribute')) |
Justin Giorgi | 9261f9f | 2016-07-11 17:48:52 -0700 | [diff] [blame] | 516 | self.attributes[m.group('attribute')] = m.group('value') |
Justin Giorgi | 16bba56 | 2016-06-22 10:42:13 -0700 | [diff] [blame] | 517 | |
| 518 | self.platform = options.platform |
mbligh | be630eb | 2008-08-01 16:41:48 +0000 | [diff] [blame] | 519 | return (options, leftover) |
| 520 | |
| 521 | |
Justin Giorgi | 16bba56 | 2016-06-22 10:42:13 -0700 | [diff] [blame] | 522 | def _set_acls(self, hosts, acls): |
| 523 | """Add hosts to acls (and remove from all other acls). |
| 524 | |
| 525 | @param hosts: list of hostnames |
| 526 | @param acls: list of acl names |
| 527 | """ |
| 528 | # Remove from all ACLs except 'Everyone' and ACLs in list |
| 529 | # Skip hosts that don't exist |
| 530 | for host in hosts: |
| 531 | if host not in self.host_ids: |
| 532 | continue |
| 533 | host_id = self.host_ids[host] |
| 534 | for a in self.execute_rpc('get_acl_groups', hosts=host_id): |
| 535 | if a['name'] not in self.acls and a['id'] != 1: |
| 536 | self.execute_rpc('acl_group_remove_hosts', id=a['id'], |
| 537 | hosts=self.hosts) |
| 538 | |
| 539 | # Add hosts to the ACLs |
| 540 | self.check_and_create_items('get_acl_groups', 'add_acl_group', |
| 541 | self.acls) |
| 542 | for a in acls: |
| 543 | self.execute_rpc('acl_group_add_hosts', id=a, hosts=hosts) |
| 544 | |
| 545 | |
| 546 | def _remove_labels(self, host, condition): |
| 547 | """Remove all labels from host that meet condition(label). |
| 548 | |
| 549 | @param host: hostname |
| 550 | @param condition: callable that returns bool when given a label |
| 551 | """ |
| 552 | if host in self.host_ids: |
| 553 | host_id = self.host_ids[host] |
| 554 | labels_to_remove = [] |
| 555 | for l in self.execute_rpc('get_labels', host=host_id): |
| 556 | if condition(l): |
| 557 | labels_to_remove.append(l['id']) |
| 558 | if labels_to_remove: |
| 559 | self.execute_rpc('host_remove_labels', id=host_id, |
| 560 | labels=labels_to_remove) |
| 561 | |
| 562 | |
| 563 | def _set_labels(self, host, labels): |
| 564 | """Apply labels to host (and remove all other labels). |
| 565 | |
| 566 | @param host: hostname |
| 567 | @param labels: list of label names |
| 568 | """ |
| 569 | condition = lambda l: l['name'] not in labels and not l['platform'] |
| 570 | self._remove_labels(host, condition) |
| 571 | self.check_and_create_items('get_labels', 'add_label', labels) |
| 572 | self.execute_rpc('host_add_labels', id=host, labels=labels) |
| 573 | |
| 574 | |
| 575 | def _set_platform_label(self, host, platform_label): |
| 576 | """Apply the platform label to host (and remove existing). |
| 577 | |
| 578 | @param host: hostname |
| 579 | @param platform_label: platform label's name |
| 580 | """ |
| 581 | self._remove_labels(host, lambda l: l['platform']) |
| 582 | self.check_and_create_items('get_labels', 'add_label', [platform_label], |
| 583 | platform=True) |
| 584 | self.execute_rpc('host_add_labels', id=host, labels=[platform_label]) |
| 585 | |
| 586 | |
| 587 | def _set_attributes(self, host, attributes): |
| 588 | """Set attributes on host. |
| 589 | |
| 590 | @param host: hostname |
| 591 | @param attributes: attribute dictionary |
| 592 | """ |
| 593 | for attr, value in self.attributes.iteritems(): |
| 594 | self.execute_rpc('set_host_attribute', attribute=attr, |
| 595 | value=value, hostname=host) |
| 596 | |
| 597 | |
| 598 | class host_mod(BaseHostModCreate): |
| 599 | """atest host mod [--lock|--unlock --force_modify_locking |
| 600 | --platform <arch> |
| 601 | --labels <labels>|--blist <label_file> |
| 602 | --acls <acls>|--alist <acl_file> |
| 603 | --protection <protection_type> |
| 604 | --attributes <attr>=<value>;<attr>=<value> |
| 605 | --mlist <mach_file>] <hosts>""" |
| 606 | usage_action = 'mod' |
Justin Giorgi | 16bba56 | 2016-06-22 10:42:13 -0700 | [diff] [blame] | 607 | |
| 608 | def __init__(self): |
| 609 | """Add the options specific to the mod action""" |
| 610 | super(host_mod, self).__init__() |
| 611 | self.parser.add_option('-f', '--force_modify_locking', |
| 612 | help='Forcefully lock\unlock a host', |
| 613 | action='store_true') |
| 614 | self.parser.add_option('--remove_acls', |
Ningning Xia | ef35cb5 | 2018-05-04 17:58:20 -0700 | [diff] [blame] | 615 | help=('Remove all active acls. %s' % |
| 616 | skylab_utils.MSG_INVALID_IN_SKYLAB), |
Justin Giorgi | 16bba56 | 2016-06-22 10:42:13 -0700 | [diff] [blame] | 617 | action='store_true') |
| 618 | self.parser.add_option('--remove_labels', |
| 619 | help='Remove all labels.', |
| 620 | action='store_true') |
| 621 | |
| 622 | |
| 623 | def parse(self): |
| 624 | """Consume the specific options""" |
| 625 | (options, leftover) = super(host_mod, self).parse() |
| 626 | |
| 627 | if options.force_modify_locking: |
| 628 | self.data['force_modify_locking'] = True |
| 629 | |
Ningning Xia | ef35cb5 | 2018-05-04 17:58:20 -0700 | [diff] [blame] | 630 | if self.skylab and options.remove_acls: |
| 631 | # TODO(nxia): drop the flag when all hosts are migrated to skylab |
| 632 | self.invalid_syntax('--remove_acls is not supported with --skylab.') |
| 633 | |
Justin Giorgi | 16bba56 | 2016-06-22 10:42:13 -0700 | [diff] [blame] | 634 | self.remove_acls = options.remove_acls |
| 635 | self.remove_labels = options.remove_labels |
| 636 | |
| 637 | return (options, leftover) |
| 638 | |
| 639 | |
Ningning Xia | ef35cb5 | 2018-05-04 17:58:20 -0700 | [diff] [blame] | 640 | def execute_skylab(self): |
| 641 | """Execute atest host mod with --skylab. |
| 642 | |
| 643 | @return A list of hostnames which have been successfully modified. |
| 644 | """ |
| 645 | inventory_repo = skylab_utils.InventoryRepo(self.inventory_repo_dir) |
| 646 | inventory_repo.initialize() |
| 647 | data_dir = inventory_repo.get_data_dir() |
| 648 | lab = text_manager.load_lab(data_dir) |
| 649 | |
| 650 | locked_by = None |
| 651 | if self.lock: |
| 652 | locked_by = inventory_repo.git_repo.config('user.email') |
| 653 | |
| 654 | successes = [] |
| 655 | for hostname in self.hosts: |
| 656 | try: |
| 657 | device.modify( |
| 658 | lab, |
| 659 | 'duts', |
| 660 | hostname, |
| 661 | self.environment, |
| 662 | lock=self.lock, |
| 663 | locked_by=locked_by, |
| 664 | lock_reason = self.lock_reason, |
| 665 | unlock=self.unlock, |
| 666 | unlock_lock_id=self.unlock_lock_id, |
| 667 | attributes=self.attributes, |
| 668 | remove_labels=self.remove_labels, |
Ningning Xia | 64ced00 | 2018-05-16 17:36:20 -0700 | [diff] [blame] | 669 | label_map=self.label_map) |
Ningning Xia | ef35cb5 | 2018-05-04 17:58:20 -0700 | [diff] [blame] | 670 | successes.append(hostname) |
| 671 | except device.SkylabDeviceActionError as e: |
| 672 | print('Cannot modify host %s: %s' % (hostname, e)) |
| 673 | |
| 674 | if successes: |
| 675 | text_manager.dump_lab(data_dir, lab) |
| 676 | |
| 677 | status = inventory_repo.git_repo.status() |
| 678 | if not status: |
| 679 | print('Nothing is changed for hosts %s.' % successes) |
| 680 | return [] |
| 681 | |
| 682 | message = skylab_utils.construct_commit_message( |
| 683 | 'Modify %d hosts.\n\n%s' % (len(successes), successes)) |
| 684 | self.change_number = inventory_repo.upload_change( |
| 685 | message, draft=self.draft, dryrun=self.dryrun, |
| 686 | submit=self.submit) |
| 687 | |
| 688 | return successes |
| 689 | |
| 690 | |
Justin Giorgi | 16bba56 | 2016-06-22 10:42:13 -0700 | [diff] [blame] | 691 | def execute(self): |
xixuan | d6011f1 | 2016-12-08 15:01:58 -0800 | [diff] [blame] | 692 | """Execute 'atest host mod'.""" |
Ningning Xia | ef35cb5 | 2018-05-04 17:58:20 -0700 | [diff] [blame] | 693 | if self.skylab: |
| 694 | return self.execute_skylab() |
| 695 | |
Justin Giorgi | 16bba56 | 2016-06-22 10:42:13 -0700 | [diff] [blame] | 696 | successes = [] |
| 697 | for host in self.execute_rpc('get_hosts', hostname__in=self.hosts): |
| 698 | self.host_ids[host['hostname']] = host['id'] |
| 699 | for host in self.hosts: |
| 700 | if host not in self.host_ids: |
| 701 | self.failure('Cannot modify non-existant host %s.' % host) |
| 702 | continue |
| 703 | host_id = self.host_ids[host] |
| 704 | |
| 705 | try: |
| 706 | if self.data: |
| 707 | self.execute_rpc('modify_host', item=host, |
| 708 | id=host, **self.data) |
| 709 | |
| 710 | if self.attributes: |
| 711 | self._set_attributes(host, self.attributes) |
| 712 | |
| 713 | if self.labels or self.remove_labels: |
| 714 | self._set_labels(host, self.labels) |
| 715 | |
| 716 | if self.platform: |
| 717 | self._set_platform_label(host, self.platform) |
| 718 | |
| 719 | # TODO: Make the AFE return True or False, |
| 720 | # especially for lock |
| 721 | successes.append(host) |
| 722 | except topic_common.CliError, full_error: |
| 723 | # Already logged by execute_rpc() |
| 724 | pass |
| 725 | |
| 726 | if self.acls or self.remove_acls: |
| 727 | self._set_acls(self.hosts, self.acls) |
| 728 | |
| 729 | return successes |
| 730 | |
| 731 | |
| 732 | def output(self, hosts): |
xixuan | d6011f1 | 2016-12-08 15:01:58 -0800 | [diff] [blame] | 733 | """Print output of 'atest host mod'. |
| 734 | |
| 735 | @param hosts: the host list to be printed. |
| 736 | """ |
Justin Giorgi | 16bba56 | 2016-06-22 10:42:13 -0700 | [diff] [blame] | 737 | for msg in self.messages: |
| 738 | self.print_wrapped(msg, hosts) |
| 739 | |
Ningning Xia | ef35cb5 | 2018-05-04 17:58:20 -0700 | [diff] [blame] | 740 | if hosts and self.skylab: |
Ningning Xia | c8b430d | 2018-05-17 15:10:25 -0700 | [diff] [blame^] | 741 | print('Modified hosts: %s.' % ', '.join(hosts)) |
Ningning Xia | ef35cb5 | 2018-05-04 17:58:20 -0700 | [diff] [blame] | 742 | if self.skylab and not self.dryrun and not self.submit: |
Ningning Xia | c8b430d | 2018-05-17 15:10:25 -0700 | [diff] [blame^] | 743 | print(skylab_utils.get_cl_message(self.change_number)) |
Ningning Xia | ef35cb5 | 2018-05-04 17:58:20 -0700 | [diff] [blame] | 744 | |
Justin Giorgi | 16bba56 | 2016-06-22 10:42:13 -0700 | [diff] [blame] | 745 | |
| 746 | class HostInfo(object): |
| 747 | """Store host information so we don't have to keep looking it up.""" |
| 748 | def __init__(self, hostname, platform, labels): |
| 749 | self.hostname = hostname |
| 750 | self.platform = platform |
| 751 | self.labels = labels |
| 752 | |
| 753 | |
| 754 | class host_create(BaseHostModCreate): |
| 755 | """atest host create [--lock|--unlock --platform <arch> |
| 756 | --labels <labels>|--blist <label_file> |
| 757 | --acls <acls>|--alist <acl_file> |
| 758 | --protection <protection_type> |
| 759 | --attributes <attr>=<value>;<attr>=<value> |
| 760 | --mlist <mach_file>] <hosts>""" |
| 761 | usage_action = 'create' |
| 762 | |
| 763 | def parse(self): |
| 764 | """Option logic specific to create action. |
| 765 | """ |
| 766 | (options, leftovers) = super(host_create, self).parse() |
| 767 | self.locked = options.lock |
| 768 | if 'serials' in self.attributes: |
| 769 | if len(self.hosts) > 1: |
| 770 | raise topic_common.CliError('Can not specify serials with ' |
| 771 | 'multiple hosts.') |
| 772 | |
| 773 | |
| 774 | @classmethod |
| 775 | def construct_without_parse( |
| 776 | cls, web_server, hosts, platform=None, |
| 777 | locked=False, lock_reason='', labels=[], acls=[], |
| 778 | protection=host_protections.Protection.NO_PROTECTION): |
| 779 | """Construct a host_create object and fill in data from args. |
| 780 | |
| 781 | Do not need to call parse after the construction. |
| 782 | |
| 783 | Return an object of site_host_create ready to execute. |
| 784 | |
| 785 | @param web_server: A string specifies the autotest webserver url. |
| 786 | It is needed to setup comm to make rpc. |
| 787 | @param hosts: A list of hostnames as strings. |
| 788 | @param platform: A string or None. |
| 789 | @param locked: A boolean. |
| 790 | @param lock_reason: A string. |
| 791 | @param labels: A list of labels as strings. |
| 792 | @param acls: A list of acls as strings. |
| 793 | @param protection: An enum defined in host_protections. |
| 794 | """ |
| 795 | obj = cls() |
| 796 | obj.web_server = web_server |
| 797 | try: |
| 798 | # Setup stuff needed for afe comm. |
| 799 | obj.afe = rpc.afe_comm(web_server) |
| 800 | except rpc.AuthError, s: |
| 801 | obj.failure(str(s), fatal=True) |
| 802 | obj.hosts = hosts |
| 803 | obj.platform = platform |
| 804 | obj.locked = locked |
| 805 | if locked and lock_reason.strip(): |
| 806 | obj.data['lock_reason'] = lock_reason.strip() |
| 807 | obj.labels = labels |
| 808 | obj.acls = acls |
| 809 | if protection: |
| 810 | obj.data['protection'] = protection |
| 811 | obj.attributes = {} |
| 812 | return obj |
| 813 | |
| 814 | |
Justin Giorgi | 5208eaa | 2016-07-02 20:12:12 -0700 | [diff] [blame] | 815 | def _detect_host_info(self, host): |
| 816 | """Detect platform and labels from the host. |
| 817 | |
| 818 | @param host: hostname |
| 819 | |
| 820 | @return: HostInfo object |
| 821 | """ |
| 822 | # Mock an afe_host object so that the host is constructed as if the |
| 823 | # data was already in afe |
| 824 | data = {'attributes': self.attributes, 'labels': self.labels} |
| 825 | afe_host = frontend.Host(None, data) |
Prathmesh Prabhu | d252d26 | 2017-05-31 11:46:34 -0700 | [diff] [blame] | 826 | store = host_info.InMemoryHostInfoStore( |
| 827 | host_info.HostInfo(labels=self.labels, |
| 828 | attributes=self.attributes)) |
| 829 | machine = { |
| 830 | 'hostname': host, |
| 831 | 'afe_host': afe_host, |
| 832 | 'host_info_store': store |
| 833 | } |
Justin Giorgi | 5208eaa | 2016-07-02 20:12:12 -0700 | [diff] [blame] | 834 | try: |
Allen Li | 2c32d6b | 2017-02-03 15:28:10 -0800 | [diff] [blame] | 835 | if bin_utils.ping(host, tries=1, deadline=1) == 0: |
Justin Giorgi | 5208eaa | 2016-07-02 20:12:12 -0700 | [diff] [blame] | 836 | serials = self.attributes.get('serials', '').split(',') |
Richard Barnette | 9db8068 | 2018-04-26 00:55:15 +0000 | [diff] [blame] | 837 | adb_serial = self.attributes.get('serials') |
| 838 | host_dut = hosts.create_host(machine, |
| 839 | adb_serial=adb_serial) |
xixuan | d6011f1 | 2016-12-08 15:01:58 -0800 | [diff] [blame] | 840 | |
Prathmesh Prabhu | d252d26 | 2017-05-31 11:46:34 -0700 | [diff] [blame] | 841 | info = HostInfo(host, host_dut.get_platform(), |
| 842 | host_dut.get_labels()) |
xixuan | d6011f1 | 2016-12-08 15:01:58 -0800 | [diff] [blame] | 843 | # Clean host to make sure nothing left after calling it, |
| 844 | # e.g. tunnels. |
| 845 | if hasattr(host_dut, 'close'): |
| 846 | host_dut.close() |
Justin Giorgi | 5208eaa | 2016-07-02 20:12:12 -0700 | [diff] [blame] | 847 | else: |
| 848 | # Can't ping the host, use default information. |
Prathmesh Prabhu | d252d26 | 2017-05-31 11:46:34 -0700 | [diff] [blame] | 849 | info = HostInfo(host, None, []) |
Justin Giorgi | 5208eaa | 2016-07-02 20:12:12 -0700 | [diff] [blame] | 850 | except (socket.gaierror, error.AutoservRunError, |
| 851 | error.AutoservSSHTimeout): |
| 852 | # We may be adding a host that does not exist yet or we can't |
| 853 | # reach due to hostname/address issues or if the host is down. |
Prathmesh Prabhu | d252d26 | 2017-05-31 11:46:34 -0700 | [diff] [blame] | 854 | info = HostInfo(host, None, []) |
| 855 | return info |
Justin Giorgi | 5208eaa | 2016-07-02 20:12:12 -0700 | [diff] [blame] | 856 | |
| 857 | |
mbligh | be630eb | 2008-08-01 16:41:48 +0000 | [diff] [blame] | 858 | def _execute_add_one_host(self, host): |
mbligh | 719e14a | 2008-12-04 01:17:08 +0000 | [diff] [blame] | 859 | # Always add the hosts as locked to avoid the host |
Matthew Sartori | 6818633 | 2015-04-27 17:19:53 -0700 | [diff] [blame] | 860 | # being picked up by the scheduler before it's ACL'ed. |
mbligh | 719e14a | 2008-12-04 01:17:08 +0000 | [diff] [blame] | 861 | self.data['locked'] = True |
Matthew Sartori | 6818633 | 2015-04-27 17:19:53 -0700 | [diff] [blame] | 862 | if not self.locked: |
| 863 | self.data['lock_reason'] = 'Forced lock on device creation' |
Justin Giorgi | 16bba56 | 2016-06-22 10:42:13 -0700 | [diff] [blame] | 864 | self.execute_rpc('add_host', hostname=host, status="Ready", **self.data) |
mbligh | be630eb | 2008-08-01 16:41:48 +0000 | [diff] [blame] | 865 | |
Justin Giorgi | 16bba56 | 2016-06-22 10:42:13 -0700 | [diff] [blame] | 866 | # If there are labels avaliable for host, use them. |
Prathmesh Prabhu | d252d26 | 2017-05-31 11:46:34 -0700 | [diff] [blame] | 867 | info = self._detect_host_info(host) |
Justin Giorgi | 16bba56 | 2016-06-22 10:42:13 -0700 | [diff] [blame] | 868 | labels = set(self.labels) |
Prathmesh Prabhu | d252d26 | 2017-05-31 11:46:34 -0700 | [diff] [blame] | 869 | if info.labels: |
| 870 | labels.update(info.labels) |
Justin Giorgi | 16bba56 | 2016-06-22 10:42:13 -0700 | [diff] [blame] | 871 | |
| 872 | if labels: |
| 873 | self._set_labels(host, list(labels)) |
| 874 | |
| 875 | # Now add the platform label. |
| 876 | # If a platform was not provided and we were able to retrieve it |
| 877 | # from the host, use the retrieved platform. |
Prathmesh Prabhu | d252d26 | 2017-05-31 11:46:34 -0700 | [diff] [blame] | 878 | platform = self.platform if self.platform else info.platform |
Justin Giorgi | 16bba56 | 2016-06-22 10:42:13 -0700 | [diff] [blame] | 879 | if platform: |
| 880 | self._set_platform_label(host, platform) |
| 881 | |
| 882 | if self.attributes: |
| 883 | self._set_attributes(host, self.attributes) |
mbligh | be630eb | 2008-08-01 16:41:48 +0000 | [diff] [blame] | 884 | |
| 885 | |
Ningning Xia | 73868fb | 2018-05-16 19:05:52 -0700 | [diff] [blame] | 886 | def execute_skylab(self): |
| 887 | """Execute atest host create with --skylab. |
| 888 | |
| 889 | @return A list of hostnames which have been successfully created. |
| 890 | """ |
| 891 | inventory_repo = skylab_utils.InventoryRepo(self.inventory_repo_dir) |
| 892 | inventory_repo.initialize() |
| 893 | data_dir = inventory_repo.get_data_dir() |
| 894 | lab = text_manager.load_lab(data_dir) |
| 895 | |
| 896 | locked_by = None |
| 897 | if self.lock: |
| 898 | locked_by = inventory_repo.git_repo.config('user.email') |
| 899 | |
| 900 | successes = [] |
| 901 | for hostname in self.hosts: |
| 902 | try: |
| 903 | device.create( |
| 904 | lab, |
| 905 | 'duts', |
| 906 | hostname, |
| 907 | self.environment, |
| 908 | lock=self.lock, |
| 909 | locked_by=locked_by, |
| 910 | lock_reason = self.lock_reason, |
| 911 | attributes=self.attributes, |
| 912 | label_map=self.label_map) |
| 913 | successes.append(hostname) |
| 914 | except device.SkylabDeviceActionError as e: |
| 915 | print('Cannot create host %s: %s' % (hostname, e)) |
| 916 | |
| 917 | if successes: |
| 918 | text_manager.dump_lab(data_dir, lab) |
| 919 | message = skylab_utils.construct_commit_message( |
| 920 | 'Create %d hosts.\n\n%s' % (len(successes), successes)) |
| 921 | self.change_number = inventory_repo.upload_change( |
| 922 | message, draft=self.draft, dryrun=self.dryrun, |
| 923 | submit=self.submit) |
| 924 | |
| 925 | return successes |
| 926 | |
| 927 | |
Justin Giorgi | 5208eaa | 2016-07-02 20:12:12 -0700 | [diff] [blame] | 928 | def execute(self): |
xixuan | d6011f1 | 2016-12-08 15:01:58 -0800 | [diff] [blame] | 929 | """Execute 'atest host create'.""" |
Ningning Xia | 73868fb | 2018-05-16 19:05:52 -0700 | [diff] [blame] | 930 | if self.skylab: |
| 931 | return self.execute_skylab() |
| 932 | |
Justin Giorgi | 16bba56 | 2016-06-22 10:42:13 -0700 | [diff] [blame] | 933 | successful_hosts = [] |
| 934 | for host in self.hosts: |
| 935 | try: |
| 936 | self._execute_add_one_host(host) |
| 937 | successful_hosts.append(host) |
| 938 | except topic_common.CliError: |
| 939 | pass |
Jiaxi Luo | c342f9f | 2014-05-19 16:22:03 -0700 | [diff] [blame] | 940 | |
| 941 | if successful_hosts: |
Justin Giorgi | 16bba56 | 2016-06-22 10:42:13 -0700 | [diff] [blame] | 942 | self._set_acls(successful_hosts, self.acls) |
Jiaxi Luo | c342f9f | 2014-05-19 16:22:03 -0700 | [diff] [blame] | 943 | |
| 944 | if not self.locked: |
| 945 | for host in successful_hosts: |
Matthew Sartori | 6818633 | 2015-04-27 17:19:53 -0700 | [diff] [blame] | 946 | self.execute_rpc('modify_host', id=host, locked=False, |
| 947 | lock_reason='') |
Jiaxi Luo | c342f9f | 2014-05-19 16:22:03 -0700 | [diff] [blame] | 948 | return successful_hosts |
| 949 | |
| 950 | |
mbligh | be630eb | 2008-08-01 16:41:48 +0000 | [diff] [blame] | 951 | def output(self, hosts): |
xixuan | d6011f1 | 2016-12-08 15:01:58 -0800 | [diff] [blame] | 952 | """Print output of 'atest host create'. |
| 953 | |
| 954 | @param hosts: the added host list to be printed. |
| 955 | """ |
mbligh | be630eb | 2008-08-01 16:41:48 +0000 | [diff] [blame] | 956 | self.print_wrapped('Added host', hosts) |
| 957 | |
| 958 | |
| 959 | class host_delete(action_common.atest_delete, host): |
| 960 | """atest host delete [--mlist <mach_file>] <hosts>""" |
Ningning Xia | c8b430d | 2018-05-17 15:10:25 -0700 | [diff] [blame^] | 961 | |
| 962 | def __init__(self): |
| 963 | super(host_delete, self).__init__() |
| 964 | |
| 965 | self.add_skylab_options() |
| 966 | |
| 967 | |
| 968 | def execute_skylab(self): |
| 969 | """Execute 'atest host delete' with '--skylab'. |
| 970 | |
| 971 | @return A list of hostnames which have been successfully deleted. |
| 972 | """ |
| 973 | inventory_repo = skylab_utils.InventoryRepo(self.inventory_repo_dir) |
| 974 | inventory_repo.initialize() |
| 975 | data_dir = inventory_repo.get_data_dir() |
| 976 | lab = text_manager.load_lab(data_dir) |
| 977 | |
| 978 | successes = [] |
| 979 | for hostname in self.hosts: |
| 980 | try: |
| 981 | device.delete( |
| 982 | lab, |
| 983 | 'duts', |
| 984 | hostname, |
| 985 | self.environment) |
| 986 | successes.append(hostname) |
| 987 | except device.SkylabDeviceActionError as e: |
| 988 | print('Cannot delete host %s: %s' % (hostname, e)) |
| 989 | |
| 990 | if successes: |
| 991 | text_manager.dump_lab(data_dir, lab) |
| 992 | message = skylab_utils.construct_commit_message( |
| 993 | 'Delete %d hosts.\n\n%s' % (len(successes), successes)) |
| 994 | self.change_number = inventory_repo.upload_change( |
| 995 | message, draft=self.draft, dryrun=self.dryrun, |
| 996 | submit=self.submit) |
| 997 | |
| 998 | return successes |
| 999 | |
| 1000 | |
| 1001 | def execute(self): |
| 1002 | """Execute 'atest host delete'. |
| 1003 | |
| 1004 | @return A list of hostnames which have been successfully deleted. |
| 1005 | """ |
| 1006 | if self.skylab: |
| 1007 | return self.execute_skylab() |
| 1008 | |
| 1009 | return super(host_delete, self).execute() |