mbligh | be630eb | 2008-08-01 16:41:48 +0000 | [diff] [blame] | 1 | # |
| 2 | # Copyright 2008 Google Inc. All Rights Reserved. |
| 3 | |
| 4 | """ |
| 5 | The host module contains the objects and method used to |
| 6 | manage a host in Autotest. |
| 7 | |
| 8 | The valid actions are: |
| 9 | create: adds host(s) |
| 10 | delete: deletes host(s) |
| 11 | list: lists host(s) |
| 12 | stat: displays host(s) information |
| 13 | mod: modifies host(s) |
| 14 | jobs: lists all jobs that ran on host(s) |
| 15 | |
| 16 | The common options are: |
| 17 | -M|--mlist: file containing a list of machines |
| 18 | |
| 19 | |
mbligh | be630eb | 2008-08-01 16:41:48 +0000 | [diff] [blame] | 20 | See topic_common.py for a High Level Design and Algorithm. |
| 21 | |
| 22 | """ |
Simran Basi | 0739d68 | 2015-02-25 16:22:56 -0800 | [diff] [blame^] | 23 | import re |
mbligh | be630eb | 2008-08-01 16:41:48 +0000 | [diff] [blame] | 24 | |
Jiaxi Luo | c342f9f | 2014-05-19 16:22:03 -0700 | [diff] [blame] | 25 | from autotest_lib.cli import action_common, topic_common |
mbligh | aed47e8 | 2009-03-17 19:06:18 +0000 | [diff] [blame] | 26 | from autotest_lib.client.common_lib import host_protections |
mbligh | be630eb | 2008-08-01 16:41:48 +0000 | [diff] [blame] | 27 | |
| 28 | |
| 29 | class host(topic_common.atest): |
| 30 | """Host class |
| 31 | atest host [create|delete|list|stat|mod|jobs] <options>""" |
| 32 | usage_action = '[create|delete|list|stat|mod|jobs]' |
| 33 | topic = msg_topic = 'host' |
| 34 | msg_items = '<hosts>' |
| 35 | |
mbligh | aed47e8 | 2009-03-17 19:06:18 +0000 | [diff] [blame] | 36 | protections = host_protections.Protection.names |
| 37 | |
mbligh | be630eb | 2008-08-01 16:41:48 +0000 | [diff] [blame] | 38 | |
| 39 | def __init__(self): |
| 40 | """Add to the parser the options common to all the |
| 41 | host actions""" |
| 42 | super(host, self).__init__() |
| 43 | |
| 44 | self.parser.add_option('-M', '--mlist', |
| 45 | help='File listing the machines', |
| 46 | type='string', |
| 47 | default=None, |
| 48 | metavar='MACHINE_FLIST') |
| 49 | |
mbligh | 9deeefa | 2009-05-01 23:11:08 +0000 | [diff] [blame] | 50 | self.topic_parse_info = topic_common.item_parse_info( |
| 51 | attribute_name='hosts', |
| 52 | filename_option='mlist', |
| 53 | use_leftover=True) |
mbligh | be630eb | 2008-08-01 16:41:48 +0000 | [diff] [blame] | 54 | |
| 55 | |
| 56 | def _parse_lock_options(self, options): |
| 57 | if options.lock and options.unlock: |
| 58 | self.invalid_syntax('Only specify one of ' |
| 59 | '--lock and --unlock.') |
| 60 | |
| 61 | if options.lock: |
| 62 | self.data['locked'] = True |
| 63 | self.messages.append('Locked host') |
| 64 | elif options.unlock: |
| 65 | self.data['locked'] = False |
| 66 | self.messages.append('Unlocked host') |
| 67 | |
| 68 | |
| 69 | def _cleanup_labels(self, labels, platform=None): |
| 70 | """Removes the platform label from the overall labels""" |
| 71 | if platform: |
| 72 | return [label for label in labels |
| 73 | if label != platform] |
| 74 | else: |
| 75 | try: |
| 76 | return [label for label in labels |
| 77 | if not label['platform']] |
| 78 | except TypeError: |
| 79 | # This is a hack - the server will soon |
| 80 | # do this, so all this code should be removed. |
| 81 | return labels |
| 82 | |
| 83 | |
| 84 | def get_items(self): |
| 85 | return self.hosts |
| 86 | |
| 87 | |
| 88 | class host_help(host): |
| 89 | """Just here to get the atest logic working. |
| 90 | Usage is set by its parent""" |
| 91 | pass |
| 92 | |
| 93 | |
| 94 | class host_list(action_common.atest_list, host): |
| 95 | """atest host list [--mlist <file>|<hosts>] [--label <label>] |
mbligh | 536a524 | 2008-10-18 14:35:54 +0000 | [diff] [blame] | 96 | [--status <status1,status2>] [--acl <ACL>] [--user <user>]""" |
mbligh | be630eb | 2008-08-01 16:41:48 +0000 | [diff] [blame] | 97 | |
| 98 | def __init__(self): |
| 99 | super(host_list, self).__init__() |
| 100 | |
| 101 | self.parser.add_option('-b', '--label', |
mbligh | 6444c6b | 2008-10-27 20:55:13 +0000 | [diff] [blame] | 102 | default='', |
| 103 | help='Only list hosts with all these labels ' |
| 104 | '(comma separated)') |
mbligh | be630eb | 2008-08-01 16:41:48 +0000 | [diff] [blame] | 105 | self.parser.add_option('-s', '--status', |
mbligh | 6444c6b | 2008-10-27 20:55:13 +0000 | [diff] [blame] | 106 | default='', |
| 107 | help='Only list hosts with any of these ' |
| 108 | 'statuses (comma separated)') |
mbligh | 536a524 | 2008-10-18 14:35:54 +0000 | [diff] [blame] | 109 | self.parser.add_option('-a', '--acl', |
mbligh | 6444c6b | 2008-10-27 20:55:13 +0000 | [diff] [blame] | 110 | default='', |
mbligh | 536a524 | 2008-10-18 14:35:54 +0000 | [diff] [blame] | 111 | help='Only list hosts within this ACL') |
| 112 | self.parser.add_option('-u', '--user', |
mbligh | 6444c6b | 2008-10-27 20:55:13 +0000 | [diff] [blame] | 113 | default='', |
mbligh | 536a524 | 2008-10-18 14:35:54 +0000 | [diff] [blame] | 114 | help='Only list hosts available to this user') |
mbligh | 70b9bf4 | 2008-11-18 15:00:46 +0000 | [diff] [blame] | 115 | self.parser.add_option('-N', '--hostnames-only', help='Only return ' |
| 116 | 'hostnames for the machines queried.', |
| 117 | action='store_true') |
mbligh | 91e0efd | 2009-02-26 01:02:16 +0000 | [diff] [blame] | 118 | self.parser.add_option('--locked', |
| 119 | default=False, |
| 120 | help='Only list locked hosts', |
| 121 | action='store_true') |
| 122 | self.parser.add_option('--unlocked', |
| 123 | default=False, |
| 124 | help='Only list unlocked hosts', |
| 125 | action='store_true') |
| 126 | |
mbligh | be630eb | 2008-08-01 16:41:48 +0000 | [diff] [blame] | 127 | |
| 128 | |
| 129 | def parse(self): |
| 130 | """Consume the specific options""" |
jamesren | c286316 | 2010-07-12 21:20:51 +0000 | [diff] [blame] | 131 | label_info = topic_common.item_parse_info(attribute_name='labels', |
| 132 | inline_option='label') |
| 133 | |
| 134 | (options, leftover) = super(host_list, self).parse([label_info]) |
| 135 | |
mbligh | be630eb | 2008-08-01 16:41:48 +0000 | [diff] [blame] | 136 | self.status = options.status |
mbligh | 536a524 | 2008-10-18 14:35:54 +0000 | [diff] [blame] | 137 | self.acl = options.acl |
| 138 | self.user = options.user |
mbligh | 70b9bf4 | 2008-11-18 15:00:46 +0000 | [diff] [blame] | 139 | self.hostnames_only = options.hostnames_only |
mbligh | 91e0efd | 2009-02-26 01:02:16 +0000 | [diff] [blame] | 140 | |
| 141 | if options.locked and options.unlocked: |
| 142 | self.invalid_syntax('--locked and --unlocked are ' |
| 143 | 'mutually exclusive') |
| 144 | self.locked = options.locked |
| 145 | self.unlocked = options.unlocked |
mbligh | be630eb | 2008-08-01 16:41:48 +0000 | [diff] [blame] | 146 | return (options, leftover) |
| 147 | |
| 148 | |
| 149 | def execute(self): |
| 150 | filters = {} |
| 151 | check_results = {} |
| 152 | if self.hosts: |
| 153 | filters['hostname__in'] = self.hosts |
| 154 | check_results['hostname__in'] = 'hostname' |
mbligh | 6444c6b | 2008-10-27 20:55:13 +0000 | [diff] [blame] | 155 | |
| 156 | if self.labels: |
jamesren | c286316 | 2010-07-12 21:20:51 +0000 | [diff] [blame] | 157 | if len(self.labels) == 1: |
mbligh | f703fb4 | 2009-01-30 00:35:05 +0000 | [diff] [blame] | 158 | # This is needed for labels with wildcards (x86*) |
jamesren | c286316 | 2010-07-12 21:20:51 +0000 | [diff] [blame] | 159 | filters['labels__name__in'] = self.labels |
mbligh | f703fb4 | 2009-01-30 00:35:05 +0000 | [diff] [blame] | 160 | check_results['labels__name__in'] = None |
| 161 | else: |
jamesren | c286316 | 2010-07-12 21:20:51 +0000 | [diff] [blame] | 162 | filters['multiple_labels'] = self.labels |
mbligh | f703fb4 | 2009-01-30 00:35:05 +0000 | [diff] [blame] | 163 | check_results['multiple_labels'] = None |
mbligh | 6444c6b | 2008-10-27 20:55:13 +0000 | [diff] [blame] | 164 | |
mbligh | be630eb | 2008-08-01 16:41:48 +0000 | [diff] [blame] | 165 | if self.status: |
mbligh | 6444c6b | 2008-10-27 20:55:13 +0000 | [diff] [blame] | 166 | statuses = self.status.split(',') |
| 167 | statuses = [status.strip() for status in statuses |
| 168 | if status.strip()] |
| 169 | |
| 170 | filters['status__in'] = statuses |
mbligh | cd8eb97 | 2008-08-25 19:20:39 +0000 | [diff] [blame] | 171 | check_results['status__in'] = None |
mbligh | 6444c6b | 2008-10-27 20:55:13 +0000 | [diff] [blame] | 172 | |
mbligh | 536a524 | 2008-10-18 14:35:54 +0000 | [diff] [blame] | 173 | if self.acl: |
showard | d9ac445 | 2009-02-07 02:04:37 +0000 | [diff] [blame] | 174 | filters['aclgroup__name'] = self.acl |
| 175 | check_results['aclgroup__name'] = None |
mbligh | 536a524 | 2008-10-18 14:35:54 +0000 | [diff] [blame] | 176 | if self.user: |
showard | d9ac445 | 2009-02-07 02:04:37 +0000 | [diff] [blame] | 177 | filters['aclgroup__users__login'] = self.user |
| 178 | check_results['aclgroup__users__login'] = None |
mbligh | 91e0efd | 2009-02-26 01:02:16 +0000 | [diff] [blame] | 179 | |
| 180 | if self.locked or self.unlocked: |
| 181 | filters['locked'] = self.locked |
| 182 | check_results['locked'] = None |
| 183 | |
mbligh | be630eb | 2008-08-01 16:41:48 +0000 | [diff] [blame] | 184 | return super(host_list, self).execute(op='get_hosts', |
| 185 | filters=filters, |
| 186 | check_results=check_results) |
| 187 | |
| 188 | |
| 189 | def output(self, results): |
| 190 | if results: |
| 191 | # Remove the platform from the labels. |
| 192 | for result in results: |
| 193 | result['labels'] = self._cleanup_labels(result['labels'], |
| 194 | result['platform']) |
mbligh | 70b9bf4 | 2008-11-18 15:00:46 +0000 | [diff] [blame] | 195 | if self.hostnames_only: |
mbligh | df75f8b | 2008-11-18 19:07:42 +0000 | [diff] [blame] | 196 | self.print_list(results, key='hostname') |
mbligh | 70b9bf4 | 2008-11-18 15:00:46 +0000 | [diff] [blame] | 197 | else: |
Prashanth Balasubramanian | a504856 | 2014-12-12 10:14:11 -0800 | [diff] [blame] | 198 | keys = ['hostname', 'status', |
| 199 | 'shard', 'locked', 'platform', 'labels'] |
| 200 | super(host_list, self).output(results, keys=keys) |
mbligh | be630eb | 2008-08-01 16:41:48 +0000 | [diff] [blame] | 201 | |
| 202 | |
| 203 | class host_stat(host): |
| 204 | """atest host stat --mlist <file>|<hosts>""" |
| 205 | usage_action = 'stat' |
| 206 | |
| 207 | def execute(self): |
| 208 | results = [] |
| 209 | # Convert wildcards into real host stats. |
| 210 | existing_hosts = [] |
| 211 | for host in self.hosts: |
| 212 | if host.endswith('*'): |
| 213 | stats = self.execute_rpc('get_hosts', |
| 214 | hostname__startswith=host.rstrip('*')) |
| 215 | if len(stats) == 0: |
| 216 | self.failure('No hosts matching %s' % host, item=host, |
| 217 | what_failed='Failed to stat') |
| 218 | continue |
| 219 | else: |
| 220 | stats = self.execute_rpc('get_hosts', hostname=host) |
| 221 | if len(stats) == 0: |
| 222 | self.failure('Unknown host %s' % host, item=host, |
| 223 | what_failed='Failed to stat') |
| 224 | continue |
| 225 | existing_hosts.extend(stats) |
| 226 | |
| 227 | for stat in existing_hosts: |
| 228 | host = stat['hostname'] |
| 229 | # The host exists, these should succeed |
| 230 | acls = self.execute_rpc('get_acl_groups', hosts__hostname=host) |
| 231 | |
| 232 | labels = self.execute_rpc('get_labels', host__hostname=host) |
Simran Basi | 0739d68 | 2015-02-25 16:22:56 -0800 | [diff] [blame^] | 233 | results.append([[stat], acls, labels, stat['attributes']]) |
mbligh | be630eb | 2008-08-01 16:41:48 +0000 | [diff] [blame] | 234 | return results |
| 235 | |
| 236 | |
| 237 | def output(self, results): |
Simran Basi | 0739d68 | 2015-02-25 16:22:56 -0800 | [diff] [blame^] | 238 | for stats, acls, labels, attributes in results: |
mbligh | be630eb | 2008-08-01 16:41:48 +0000 | [diff] [blame] | 239 | print '-'*5 |
| 240 | self.print_fields(stats, |
| 241 | keys=['hostname', 'platform', |
mbligh | e163b03 | 2008-10-18 14:30:27 +0000 | [diff] [blame] | 242 | 'status', 'locked', 'locked_by', |
| 243 | 'lock_time', 'protection',]) |
mbligh | be630eb | 2008-08-01 16:41:48 +0000 | [diff] [blame] | 244 | self.print_by_ids(acls, 'ACLs', line_before=True) |
| 245 | labels = self._cleanup_labels(labels) |
| 246 | self.print_by_ids(labels, 'Labels', line_before=True) |
Simran Basi | 0739d68 | 2015-02-25 16:22:56 -0800 | [diff] [blame^] | 247 | self.print_dict(attributes, 'Host Attributes', line_before=True) |
mbligh | be630eb | 2008-08-01 16:41:48 +0000 | [diff] [blame] | 248 | |
| 249 | |
| 250 | class host_jobs(host): |
mbligh | 1494eca | 2008-08-13 21:24:22 +0000 | [diff] [blame] | 251 | """atest host jobs [--max-query] --mlist <file>|<hosts>""" |
mbligh | be630eb | 2008-08-01 16:41:48 +0000 | [diff] [blame] | 252 | usage_action = 'jobs' |
| 253 | |
mbligh | 6996fe8 | 2008-08-13 00:32:27 +0000 | [diff] [blame] | 254 | def __init__(self): |
| 255 | super(host_jobs, self).__init__() |
| 256 | self.parser.add_option('-q', '--max-query', |
| 257 | help='Limits the number of results ' |
| 258 | '(20 by default)', |
| 259 | type='int', default=20) |
| 260 | |
| 261 | |
| 262 | def parse(self): |
| 263 | """Consume the specific options""" |
mbligh | 9deeefa | 2009-05-01 23:11:08 +0000 | [diff] [blame] | 264 | (options, leftover) = super(host_jobs, self).parse() |
mbligh | 6996fe8 | 2008-08-13 00:32:27 +0000 | [diff] [blame] | 265 | self.max_queries = options.max_query |
| 266 | return (options, leftover) |
| 267 | |
| 268 | |
mbligh | be630eb | 2008-08-01 16:41:48 +0000 | [diff] [blame] | 269 | def execute(self): |
| 270 | results = [] |
| 271 | real_hosts = [] |
| 272 | for host in self.hosts: |
| 273 | if host.endswith('*'): |
| 274 | stats = self.execute_rpc('get_hosts', |
| 275 | hostname__startswith=host.rstrip('*')) |
| 276 | if len(stats) == 0: |
| 277 | self.failure('No host matching %s' % host, item=host, |
| 278 | what_failed='Failed to stat') |
| 279 | [real_hosts.append(stat['hostname']) for stat in stats] |
| 280 | else: |
| 281 | real_hosts.append(host) |
| 282 | |
| 283 | for host in real_hosts: |
| 284 | queue_entries = self.execute_rpc('get_host_queue_entries', |
mbligh | 6996fe8 | 2008-08-13 00:32:27 +0000 | [diff] [blame] | 285 | host__hostname=host, |
mbligh | 1494eca | 2008-08-13 21:24:22 +0000 | [diff] [blame] | 286 | query_limit=self.max_queries, |
showard | 6958c72 | 2009-09-23 20:03:16 +0000 | [diff] [blame] | 287 | sort_by=['-job__id']) |
mbligh | be630eb | 2008-08-01 16:41:48 +0000 | [diff] [blame] | 288 | jobs = [] |
| 289 | for entry in queue_entries: |
| 290 | job = {'job_id': entry['job']['id'], |
| 291 | 'job_owner': entry['job']['owner'], |
| 292 | 'job_name': entry['job']['name'], |
| 293 | 'status': entry['status']} |
| 294 | jobs.append(job) |
| 295 | results.append((host, jobs)) |
| 296 | return results |
| 297 | |
| 298 | |
| 299 | def output(self, results): |
| 300 | for host, jobs in results: |
| 301 | print '-'*5 |
| 302 | print 'Hostname: %s' % host |
| 303 | self.print_table(jobs, keys_header=['job_id', |
showard | be0d869 | 2009-08-20 23:42:44 +0000 | [diff] [blame] | 304 | 'job_owner', |
| 305 | 'job_name', |
| 306 | 'status']) |
mbligh | be630eb | 2008-08-01 16:41:48 +0000 | [diff] [blame] | 307 | |
| 308 | |
| 309 | class host_mod(host): |
showard | be0d869 | 2009-08-20 23:42:44 +0000 | [diff] [blame] | 310 | """atest host mod --lock|--unlock|--protection |
mbligh | be630eb | 2008-08-01 16:41:48 +0000 | [diff] [blame] | 311 | --mlist <file>|<hosts>""" |
| 312 | usage_action = 'mod' |
Simran Basi | 0739d68 | 2015-02-25 16:22:56 -0800 | [diff] [blame^] | 313 | attribute_regex = r'^(?P<attribute>\w+)=(?P<value>.+)?' |
mbligh | be630eb | 2008-08-01 16:41:48 +0000 | [diff] [blame] | 314 | |
| 315 | def __init__(self): |
| 316 | """Add the options specific to the mod action""" |
| 317 | self.data = {} |
| 318 | self.messages = [] |
Simran Basi | 0739d68 | 2015-02-25 16:22:56 -0800 | [diff] [blame^] | 319 | self.attribute = None |
| 320 | self.value = None |
mbligh | be630eb | 2008-08-01 16:41:48 +0000 | [diff] [blame] | 321 | super(host_mod, self).__init__() |
mbligh | be630eb | 2008-08-01 16:41:48 +0000 | [diff] [blame] | 322 | self.parser.add_option('-l', '--lock', |
| 323 | help='Lock hosts', |
| 324 | action='store_true') |
| 325 | self.parser.add_option('-u', '--unlock', |
| 326 | help='Unlock hosts', |
| 327 | action='store_true') |
mbligh | e163b03 | 2008-10-18 14:30:27 +0000 | [diff] [blame] | 328 | self.parser.add_option('-p', '--protection', type='choice', |
mbligh | aed47e8 | 2009-03-17 19:06:18 +0000 | [diff] [blame] | 329 | help=('Set the protection level on a host. ' |
| 330 | 'Must be one of: %s' % |
| 331 | ', '.join('"%s"' % p |
| 332 | for p in self.protections)), |
| 333 | choices=self.protections) |
Simran Basi | 0739d68 | 2015-02-25 16:22:56 -0800 | [diff] [blame^] | 334 | self.parser.add_option('--attribute', '-a', default='', |
| 335 | help=('Host attribute to add or change. Format ' |
| 336 | 'is <attribute>=<value>. Value can be ' |
| 337 | 'blank to delete attribute.')) |
mbligh | be630eb | 2008-08-01 16:41:48 +0000 | [diff] [blame] | 338 | |
| 339 | |
| 340 | def parse(self): |
| 341 | """Consume the specific options""" |
| 342 | (options, leftover) = super(host_mod, self).parse() |
| 343 | |
| 344 | self._parse_lock_options(options) |
| 345 | |
mbligh | e163b03 | 2008-10-18 14:30:27 +0000 | [diff] [blame] | 346 | if options.protection: |
| 347 | self.data['protection'] = options.protection |
showard | be0d869 | 2009-08-20 23:42:44 +0000 | [diff] [blame] | 348 | self.messages.append('Protection set to "%s"' % options.protection) |
mbligh | e163b03 | 2008-10-18 14:30:27 +0000 | [diff] [blame] | 349 | |
Simran Basi | 0739d68 | 2015-02-25 16:22:56 -0800 | [diff] [blame^] | 350 | if len(self.data) == 0 and not options.attribute: |
mbligh | be630eb | 2008-08-01 16:41:48 +0000 | [diff] [blame] | 351 | self.invalid_syntax('No modification requested') |
Simran Basi | 0739d68 | 2015-02-25 16:22:56 -0800 | [diff] [blame^] | 352 | |
| 353 | if options.attribute: |
| 354 | match = re.match(self.attribute_regex, options.attribute) |
| 355 | if not match: |
| 356 | self.invalid_syntax('Attributes must be in <attribute>=<value>' |
| 357 | ' syntax!') |
| 358 | |
| 359 | self.attribute = match.group('attribute') |
| 360 | self.value = match.group('value') |
| 361 | |
mbligh | be630eb | 2008-08-01 16:41:48 +0000 | [diff] [blame] | 362 | return (options, leftover) |
| 363 | |
| 364 | |
| 365 | def execute(self): |
| 366 | successes = [] |
| 367 | for host in self.hosts: |
mbligh | 6eca460 | 2009-01-07 16:48:50 +0000 | [diff] [blame] | 368 | try: |
| 369 | res = self.execute_rpc('modify_host', item=host, |
| 370 | id=host, **self.data) |
Simran Basi | 0739d68 | 2015-02-25 16:22:56 -0800 | [diff] [blame^] | 371 | if self.attribute: |
| 372 | self.execute_rpc('set_host_attribute', |
| 373 | attribute=self.attribute, |
| 374 | value=self.value, hostname=host) |
mbligh | 6eca460 | 2009-01-07 16:48:50 +0000 | [diff] [blame] | 375 | # TODO: Make the AFE return True or False, |
| 376 | # especially for lock |
mbligh | be630eb | 2008-08-01 16:41:48 +0000 | [diff] [blame] | 377 | successes.append(host) |
mbligh | 6eca460 | 2009-01-07 16:48:50 +0000 | [diff] [blame] | 378 | except topic_common.CliError, full_error: |
| 379 | # Already logged by execute_rpc() |
| 380 | pass |
| 381 | |
mbligh | be630eb | 2008-08-01 16:41:48 +0000 | [diff] [blame] | 382 | return successes |
| 383 | |
| 384 | |
| 385 | def output(self, hosts): |
| 386 | for msg in self.messages: |
| 387 | self.print_wrapped(msg, hosts) |
| 388 | |
| 389 | |
mbligh | be630eb | 2008-08-01 16:41:48 +0000 | [diff] [blame] | 390 | class host_create(host): |
| 391 | """atest host create [--lock|--unlock --platform <arch> |
| 392 | --labels <labels>|--blist <label_file> |
| 393 | --acls <acls>|--alist <acl_file> |
mbligh | aed47e8 | 2009-03-17 19:06:18 +0000 | [diff] [blame] | 394 | --protection <protection_type> |
mbligh | be630eb | 2008-08-01 16:41:48 +0000 | [diff] [blame] | 395 | --mlist <mach_file>] <hosts>""" |
| 396 | usage_action = 'create' |
| 397 | |
| 398 | def __init__(self): |
| 399 | self.messages = [] |
| 400 | super(host_create, self).__init__() |
| 401 | self.parser.add_option('-l', '--lock', |
| 402 | help='Create the hosts as locked', |
mbligh | 719e14a | 2008-12-04 01:17:08 +0000 | [diff] [blame] | 403 | action='store_true', default=False) |
mbligh | be630eb | 2008-08-01 16:41:48 +0000 | [diff] [blame] | 404 | self.parser.add_option('-u', '--unlock', |
| 405 | help='Create the hosts as ' |
| 406 | 'unlocked (default)', |
| 407 | action='store_true') |
| 408 | self.parser.add_option('-t', '--platform', |
| 409 | help='Sets the platform label') |
| 410 | self.parser.add_option('-b', '--labels', |
| 411 | help='Comma separated list of labels') |
| 412 | self.parser.add_option('-B', '--blist', |
| 413 | help='File listing the labels', |
| 414 | type='string', |
| 415 | metavar='LABEL_FLIST') |
| 416 | self.parser.add_option('-a', '--acls', |
| 417 | help='Comma separated list of ACLs') |
| 418 | self.parser.add_option('-A', '--alist', |
| 419 | help='File listing the acls', |
| 420 | type='string', |
| 421 | metavar='ACL_FLIST') |
mbligh | aed47e8 | 2009-03-17 19:06:18 +0000 | [diff] [blame] | 422 | self.parser.add_option('-p', '--protection', type='choice', |
| 423 | help=('Set the protection level on a host. ' |
| 424 | 'Must be one of: %s' % |
| 425 | ', '.join('"%s"' % p |
| 426 | for p in self.protections)), |
| 427 | choices=self.protections) |
mbligh | be630eb | 2008-08-01 16:41:48 +0000 | [diff] [blame] | 428 | |
| 429 | |
| 430 | def parse(self): |
mbligh | 9deeefa | 2009-05-01 23:11:08 +0000 | [diff] [blame] | 431 | label_info = topic_common.item_parse_info(attribute_name='labels', |
| 432 | inline_option='labels', |
| 433 | filename_option='blist') |
| 434 | acl_info = topic_common.item_parse_info(attribute_name='acls', |
| 435 | inline_option='acls', |
| 436 | filename_option='alist') |
| 437 | |
| 438 | (options, leftover) = super(host_create, self).parse([label_info, |
| 439 | acl_info], |
| 440 | req_items='hosts') |
mbligh | be630eb | 2008-08-01 16:41:48 +0000 | [diff] [blame] | 441 | |
| 442 | self._parse_lock_options(options) |
mbligh | 719e14a | 2008-12-04 01:17:08 +0000 | [diff] [blame] | 443 | self.locked = options.lock |
mbligh | be630eb | 2008-08-01 16:41:48 +0000 | [diff] [blame] | 444 | self.platform = getattr(options, 'platform', None) |
mbligh | aed47e8 | 2009-03-17 19:06:18 +0000 | [diff] [blame] | 445 | if options.protection: |
| 446 | self.data['protection'] = options.protection |
mbligh | be630eb | 2008-08-01 16:41:48 +0000 | [diff] [blame] | 447 | return (options, leftover) |
| 448 | |
| 449 | |
| 450 | def _execute_add_one_host(self, host): |
mbligh | 719e14a | 2008-12-04 01:17:08 +0000 | [diff] [blame] | 451 | # Always add the hosts as locked to avoid the host |
mbligh | ef87cad | 2009-03-11 18:18:10 +0000 | [diff] [blame] | 452 | # being picked up by the scheduler before it's ACL'ed |
mbligh | 719e14a | 2008-12-04 01:17:08 +0000 | [diff] [blame] | 453 | self.data['locked'] = True |
mbligh | be630eb | 2008-08-01 16:41:48 +0000 | [diff] [blame] | 454 | self.execute_rpc('add_host', hostname=host, |
| 455 | status="Ready", **self.data) |
| 456 | |
| 457 | # Now add the platform label |
| 458 | labels = self.labels[:] |
| 459 | if self.platform: |
| 460 | labels.append(self.platform) |
| 461 | if len (labels): |
| 462 | self.execute_rpc('host_add_labels', id=host, labels=labels) |
| 463 | |
| 464 | |
Jiaxi Luo | c342f9f | 2014-05-19 16:22:03 -0700 | [diff] [blame] | 465 | def _execute_add_hosts(self): |
| 466 | successful_hosts = self.site_create_hosts_hook() |
| 467 | |
| 468 | if successful_hosts: |
| 469 | for acl in self.acls: |
| 470 | self.execute_rpc('acl_group_add_hosts', |
| 471 | id=acl, |
| 472 | hosts=successful_hosts) |
| 473 | |
| 474 | if not self.locked: |
| 475 | for host in successful_hosts: |
| 476 | self.execute_rpc('modify_host', id=host, locked=False) |
| 477 | return successful_hosts |
| 478 | |
| 479 | |
mbligh | be630eb | 2008-08-01 16:41:48 +0000 | [diff] [blame] | 480 | def execute(self): |
| 481 | # We need to check if these labels & ACLs exist, |
| 482 | # and create them if not. |
| 483 | if self.platform: |
| 484 | self.check_and_create_items('get_labels', 'add_label', |
| 485 | [self.platform], |
| 486 | platform=True) |
| 487 | |
| 488 | if self.labels: |
| 489 | self.check_and_create_items('get_labels', 'add_label', |
| 490 | self.labels, |
| 491 | platform=False) |
| 492 | |
| 493 | if self.acls: |
| 494 | self.check_and_create_items('get_acl_groups', |
| 495 | 'add_acl_group', |
| 496 | self.acls) |
| 497 | |
Jiaxi Luo | c342f9f | 2014-05-19 16:22:03 -0700 | [diff] [blame] | 498 | return self._execute_add_hosts() |
mbligh | be630eb | 2008-08-01 16:41:48 +0000 | [diff] [blame] | 499 | |
| 500 | |
| 501 | def site_create_hosts_hook(self): |
Jiaxi Luo | c342f9f | 2014-05-19 16:22:03 -0700 | [diff] [blame] | 502 | successful_hosts = [] |
mbligh | be630eb | 2008-08-01 16:41:48 +0000 | [diff] [blame] | 503 | for host in self.hosts: |
| 504 | try: |
| 505 | self._execute_add_one_host(host) |
Jiaxi Luo | c342f9f | 2014-05-19 16:22:03 -0700 | [diff] [blame] | 506 | successful_hosts.append(host) |
mbligh | be630eb | 2008-08-01 16:41:48 +0000 | [diff] [blame] | 507 | except topic_common.CliError: |
| 508 | pass |
| 509 | |
Jiaxi Luo | c342f9f | 2014-05-19 16:22:03 -0700 | [diff] [blame] | 510 | return successful_hosts |
mbligh | be630eb | 2008-08-01 16:41:48 +0000 | [diff] [blame] | 511 | |
| 512 | |
| 513 | def output(self, hosts): |
| 514 | self.print_wrapped('Added host', hosts) |
| 515 | |
| 516 | |
| 517 | class host_delete(action_common.atest_delete, host): |
| 518 | """atest host delete [--mlist <mach_file>] <hosts>""" |
| 519 | pass |