mbligh | be630eb | 2008-08-01 16:41:48 +0000 | [diff] [blame] | 1 | # |
| 2 | # Copyright 2008 Google Inc. All Rights Reserved. |
| 3 | |
| 4 | """ |
| 5 | The job module contains the objects and methods used to |
| 6 | manage jobs in Autotest. |
| 7 | |
| 8 | The valid actions are: |
| 9 | list: lists job(s) |
| 10 | create: create a job |
| 11 | abort: abort job(s) |
| 12 | stat: detailed listing of job(s) |
| 13 | |
| 14 | The common options are: |
| 15 | |
| 16 | See topic_common.py for a High Level Design and Algorithm. |
| 17 | """ |
| 18 | |
| 19 | import getpass, os, pwd, re, socket, sys |
| 20 | from autotest_lib.cli import topic_common, action_common |
| 21 | |
| 22 | |
| 23 | class job(topic_common.atest): |
| 24 | """Job class |
mbligh | 5a49608 | 2009-08-03 16:44:54 +0000 | [diff] [blame] | 25 | atest job [create|clone|list|stat|abort] <options>""" |
| 26 | usage_action = '[create|clone|list|stat|abort]' |
mbligh | be630eb | 2008-08-01 16:41:48 +0000 | [diff] [blame] | 27 | topic = msg_topic = 'job' |
| 28 | msg_items = '<job_ids>' |
| 29 | |
| 30 | |
| 31 | def _convert_status(self, results): |
| 32 | for result in results: |
mbligh | 10a4733 | 2008-08-11 19:37:46 +0000 | [diff] [blame] | 33 | total = sum(result['status_counts'].values()) |
mbligh | 47dc4d2 | 2009-02-12 21:48:34 +0000 | [diff] [blame] | 34 | status = ['%s=%s(%.1f%%)' % (key, val, 100.0*float(val)/total) |
mbligh | 10a4733 | 2008-08-11 19:37:46 +0000 | [diff] [blame] | 35 | for key, val in result['status_counts'].iteritems()] |
mbligh | be630eb | 2008-08-01 16:41:48 +0000 | [diff] [blame] | 36 | status.sort() |
| 37 | result['status_counts'] = ', '.join(status) |
| 38 | |
| 39 | |
mbligh | 5a49608 | 2009-08-03 16:44:54 +0000 | [diff] [blame] | 40 | def backward_compatibility(self, action, argv): |
| 41 | """ 'job create --clone' became 'job clone --id' """ |
| 42 | if action == 'create': |
| 43 | for option in ['-l', '--clone']: |
| 44 | if option in argv: |
| 45 | argv[argv.index(option)] = '--id' |
| 46 | action = 'clone' |
| 47 | return action |
| 48 | |
| 49 | |
mbligh | be630eb | 2008-08-01 16:41:48 +0000 | [diff] [blame] | 50 | class job_help(job): |
| 51 | """Just here to get the atest logic working. |
| 52 | Usage is set by its parent""" |
| 53 | pass |
| 54 | |
| 55 | |
| 56 | class job_list_stat(action_common.atest_list, job): |
mbligh | 9deeefa | 2009-05-01 23:11:08 +0000 | [diff] [blame] | 57 | def __init__(self): |
| 58 | super(job_list_stat, self).__init__() |
| 59 | |
| 60 | self.topic_parse_info = topic_common.item_parse_info( |
| 61 | attribute_name='jobs', |
| 62 | use_leftover=True) |
| 63 | |
| 64 | |
mbligh | be630eb | 2008-08-01 16:41:48 +0000 | [diff] [blame] | 65 | def __split_jobs_between_ids_names(self): |
| 66 | job_ids = [] |
| 67 | job_names = [] |
| 68 | |
| 69 | # Sort between job IDs and names |
| 70 | for job_id in self.jobs: |
| 71 | if job_id.isdigit(): |
| 72 | job_ids.append(job_id) |
| 73 | else: |
| 74 | job_names.append(job_id) |
| 75 | return (job_ids, job_names) |
| 76 | |
| 77 | |
| 78 | def execute_on_ids_and_names(self, op, filters={}, |
| 79 | check_results={'id__in': 'id', |
| 80 | 'name__in': 'id'}, |
| 81 | tag_id='id__in', tag_name='name__in'): |
| 82 | if not self.jobs: |
| 83 | # Want everything |
| 84 | return super(job_list_stat, self).execute(op=op, filters=filters) |
| 85 | |
| 86 | all_jobs = [] |
| 87 | (job_ids, job_names) = self.__split_jobs_between_ids_names() |
| 88 | |
| 89 | for items, tag in [(job_ids, tag_id), |
| 90 | (job_names, tag_name)]: |
| 91 | if items: |
| 92 | new_filters = filters.copy() |
| 93 | new_filters[tag] = items |
| 94 | jobs = super(job_list_stat, |
| 95 | self).execute(op=op, |
| 96 | filters=new_filters, |
| 97 | check_results=check_results) |
| 98 | all_jobs.extend(jobs) |
| 99 | |
| 100 | return all_jobs |
| 101 | |
| 102 | |
| 103 | class job_list(job_list_stat): |
| 104 | """atest job list [<jobs>] [--all] [--running] [--user <username>]""" |
| 105 | def __init__(self): |
| 106 | super(job_list, self).__init__() |
| 107 | self.parser.add_option('-a', '--all', help='List jobs for all ' |
| 108 | 'users.', action='store_true', default=False) |
| 109 | self.parser.add_option('-r', '--running', help='List only running ' |
| 110 | 'jobs', action='store_true') |
| 111 | self.parser.add_option('-u', '--user', help='List jobs for given ' |
| 112 | 'user', type='string') |
| 113 | |
| 114 | |
| 115 | def parse(self): |
mbligh | 9deeefa | 2009-05-01 23:11:08 +0000 | [diff] [blame] | 116 | options, leftover = super(job_list, self).parse() |
mbligh | be630eb | 2008-08-01 16:41:48 +0000 | [diff] [blame] | 117 | self.all = options.all |
| 118 | self.data['running'] = options.running |
| 119 | if options.user: |
| 120 | if options.all: |
| 121 | self.invalid_syntax('Only specify --all or --user, not both.') |
| 122 | else: |
| 123 | self.data['owner'] = options.user |
| 124 | elif not options.all and not self.jobs: |
| 125 | self.data['owner'] = getpass.getuser() |
| 126 | |
mbligh | 9deeefa | 2009-05-01 23:11:08 +0000 | [diff] [blame] | 127 | return options, leftover |
mbligh | be630eb | 2008-08-01 16:41:48 +0000 | [diff] [blame] | 128 | |
| 129 | |
| 130 | def execute(self): |
| 131 | return self.execute_on_ids_and_names(op='get_jobs_summary', |
| 132 | filters=self.data) |
| 133 | |
| 134 | |
| 135 | def output(self, results): |
| 136 | keys = ['id', 'owner', 'name', 'status_counts'] |
| 137 | if self.verbose: |
| 138 | keys.extend(['priority', 'control_type', 'created_on']) |
| 139 | self._convert_status(results) |
| 140 | super(job_list, self).output(results, keys) |
| 141 | |
| 142 | |
| 143 | |
| 144 | class job_stat(job_list_stat): |
| 145 | """atest job stat <job>""" |
| 146 | usage_action = 'stat' |
| 147 | |
| 148 | def __init__(self): |
| 149 | super(job_stat, self).__init__() |
| 150 | self.parser.add_option('-f', '--control-file', |
| 151 | help='Display the control file', |
| 152 | action='store_true', default=False) |
| 153 | |
| 154 | |
| 155 | def parse(self): |
mbligh | 9deeefa | 2009-05-01 23:11:08 +0000 | [diff] [blame] | 156 | options, leftover = super(job_stat, self).parse(req_items='jobs') |
mbligh | be630eb | 2008-08-01 16:41:48 +0000 | [diff] [blame] | 157 | if not self.jobs: |
| 158 | self.invalid_syntax('Must specify at least one job.') |
| 159 | |
| 160 | self.show_control_file = options.control_file |
| 161 | |
mbligh | 9deeefa | 2009-05-01 23:11:08 +0000 | [diff] [blame] | 162 | return options, leftover |
mbligh | be630eb | 2008-08-01 16:41:48 +0000 | [diff] [blame] | 163 | |
| 164 | |
| 165 | def _merge_results(self, summary, qes): |
| 166 | hosts_status = {} |
| 167 | for qe in qes: |
| 168 | if qe['host']: |
| 169 | job_id = qe['job']['id'] |
| 170 | hostname = qe['host']['hostname'] |
| 171 | hosts_status.setdefault(job_id, |
| 172 | {}).setdefault(qe['status'], |
| 173 | []).append(hostname) |
| 174 | |
| 175 | for job in summary: |
| 176 | job_id = job['id'] |
| 177 | if hosts_status.has_key(job_id): |
| 178 | this_job = hosts_status[job_id] |
mbligh | 47dc4d2 | 2009-02-12 21:48:34 +0000 | [diff] [blame] | 179 | host_per_status = ['%s=%s' %(status, ','.join(host)) |
mbligh | be630eb | 2008-08-01 16:41:48 +0000 | [diff] [blame] | 180 | for status, host in this_job.iteritems()] |
| 181 | job['hosts_status'] = ', '.join(host_per_status) |
| 182 | else: |
| 183 | job['hosts_status'] = '' |
| 184 | return summary |
| 185 | |
| 186 | |
| 187 | def execute(self): |
| 188 | summary = self.execute_on_ids_and_names(op='get_jobs_summary') |
| 189 | |
| 190 | # Get the real hostnames |
| 191 | qes = self.execute_on_ids_and_names(op='get_host_queue_entries', |
| 192 | check_results={}, |
| 193 | tag_id='job__in', |
| 194 | tag_name='job__name__in') |
| 195 | |
| 196 | self._convert_status(summary) |
| 197 | |
| 198 | return self._merge_results(summary, qes) |
| 199 | |
| 200 | |
| 201 | def output(self, results): |
| 202 | if not self.verbose: |
| 203 | keys = ['id', 'name', 'priority', 'status_counts', 'hosts_status'] |
| 204 | else: |
| 205 | keys = ['id', 'name', 'priority', 'status_counts', 'hosts_status', |
showard | 2bab8f4 | 2008-11-12 18:15:22 +0000 | [diff] [blame] | 206 | 'owner', 'control_type', 'synch_count', 'created_on', |
showard | a1e74b3 | 2009-05-12 17:32:04 +0000 | [diff] [blame] | 207 | 'run_verify', 'reboot_before', 'reboot_after', |
| 208 | 'parse_failed_repair'] |
mbligh | be630eb | 2008-08-01 16:41:48 +0000 | [diff] [blame] | 209 | |
| 210 | if self.show_control_file: |
| 211 | keys.append('control_file') |
| 212 | |
| 213 | super(job_stat, self).output(results, keys) |
| 214 | |
| 215 | |
mbligh | 5a49608 | 2009-08-03 16:44:54 +0000 | [diff] [blame] | 216 | class job_create_or_clone(action_common.atest_create, job): |
| 217 | """Class containing the code common to the job create and clone actions""" |
| 218 | msg_items = 'job_name' |
| 219 | |
| 220 | def __init__(self): |
| 221 | super(job_create_or_clone, self).__init__() |
| 222 | self.hosts = [] |
| 223 | self.data_item_key = 'name' |
| 224 | self.parser.add_option('-p', '--priority', help='Job priority (low, ' |
| 225 | 'medium, high, urgent), default=medium', |
| 226 | type='choice', choices=('low', 'medium', 'high', |
| 227 | 'urgent'), default='medium') |
| 228 | self.parser.add_option('-b', '--labels', |
| 229 | help='Comma separated list of labels ' |
| 230 | 'to get machine list from.', default='') |
| 231 | self.parser.add_option('-m', '--machine', help='List of machines to ' |
| 232 | 'run on') |
| 233 | self.parser.add_option('-M', '--mlist', |
| 234 | help='File listing machines to use', |
| 235 | type='string', metavar='MACHINE_FLIST') |
| 236 | self.parser.add_option('--one-time-hosts', |
| 237 | help='List of one time hosts') |
| 238 | self.parser.add_option('-e', '--email', |
| 239 | help='A comma seperated list of ' |
| 240 | 'email addresses to notify of job completion', |
| 241 | default='') |
| 242 | |
| 243 | |
mbligh | 56f1f4a | 2009-08-03 16:45:12 +0000 | [diff] [blame^] | 244 | def _parse_hosts(self, args): |
| 245 | """ Parses the arguments to generate a list of hosts and meta_hosts |
| 246 | A host is a regular name, a meta_host is n*label or *label. |
| 247 | These can be mixed on the CLI, and separated by either commas or |
| 248 | spaces, e.g.: 5*Machine_Label host0 5*Machine_Label2,host2 """ |
| 249 | |
| 250 | hosts = [] |
| 251 | meta_hosts = [] |
| 252 | |
| 253 | for arg in args: |
| 254 | for host in arg.split(','): |
| 255 | if re.match('^[0-9]+[*]', host): |
| 256 | num, host = host.split('*', 1) |
| 257 | meta_hosts += int(num) * [host] |
| 258 | elif re.match('^[*](\w*)', host): |
| 259 | meta_hosts += [re.match('^[*](\w*)', host).group(1)] |
| 260 | elif host != '' and host not in hosts: |
| 261 | # Real hostname and not a duplicate |
| 262 | hosts.append(host) |
| 263 | |
| 264 | return (hosts, meta_hosts) |
| 265 | |
| 266 | |
mbligh | 5a49608 | 2009-08-03 16:44:54 +0000 | [diff] [blame] | 267 | def parse(self): |
| 268 | host_info = topic_common.item_parse_info(attribute_name='hosts', |
| 269 | inline_option='machine', |
| 270 | filename_option='mlist') |
| 271 | job_info = topic_common.item_parse_info(attribute_name='jobname', |
| 272 | use_leftover=True) |
| 273 | oth_info = topic_common.item_parse_info(attribute_name='one_time_hosts', |
| 274 | inline_option='one_time_hosts') |
| 275 | |
| 276 | options, leftover = super(job_create_or_clone, |
| 277 | self).parse([host_info, job_info, oth_info], |
| 278 | req_items='jobname') |
| 279 | self.data = {} |
| 280 | if len(self.jobname) > 1: |
| 281 | self.invalid_syntax('Too many arguments specified, only expected ' |
| 282 | 'to receive job name: %s' % self.jobname) |
| 283 | self.jobname = self.jobname[0] |
| 284 | |
| 285 | if options.priority: |
| 286 | self.data['priority'] = options.priority.capitalize() |
| 287 | |
| 288 | if self.one_time_hosts: |
| 289 | self.data['one_time_hosts'] = self.one_time_hosts |
| 290 | |
| 291 | if options.labels: |
| 292 | labels = options.labels.split(',') |
| 293 | labels = [label.strip() for label in labels if label.strip()] |
| 294 | label_hosts = self.execute_rpc(op='get_hosts', |
| 295 | multiple_labels=labels) |
| 296 | for host in label_hosts: |
| 297 | self.hosts.append(host['hostname']) |
| 298 | |
| 299 | self.data['name'] = self.jobname |
| 300 | |
| 301 | (self.data['hosts'], |
mbligh | 56f1f4a | 2009-08-03 16:45:12 +0000 | [diff] [blame^] | 302 | self.data['meta_hosts']) = self._parse_hosts(self.hosts) |
mbligh | 5a49608 | 2009-08-03 16:44:54 +0000 | [diff] [blame] | 303 | |
| 304 | self.data['email_list'] = options.email |
| 305 | |
| 306 | return options, leftover |
| 307 | |
| 308 | |
| 309 | def create_job(self): |
| 310 | job_id = self.execute_rpc(op='create_job', **self.data) |
| 311 | return ['%s (id %s)' % (self.jobname, job_id)] |
| 312 | |
| 313 | |
| 314 | def get_items(self): |
| 315 | return [self.jobname] |
| 316 | |
| 317 | |
| 318 | |
| 319 | class job_create(job_create_or_clone): |
mbligh | be630eb | 2008-08-01 16:41:48 +0000 | [diff] [blame] | 320 | """atest job create [--priority <Low|Medium|High|Urgent>] |
mbligh | a212d71 | 2009-02-11 01:22:36 +0000 | [diff] [blame] | 321 | [--synch_count] [--control-file </path/to/cfile>] |
mbligh | be630eb | 2008-08-01 16:41:48 +0000 | [diff] [blame] | 322 | [--on-server] [--test <test1,test2>] [--kernel <http://kernel>] |
| 323 | [--mlist </path/to/machinelist>] [--machine <host1 host2 host3>] |
showard | b27f4ad | 2009-05-01 00:08:26 +0000 | [diff] [blame] | 324 | [--labels <list of labels of machines to run on>] |
showard | 21baa45 | 2008-10-21 00:08:39 +0000 | [diff] [blame] | 325 | [--reboot_before <option>] [--reboot_after <option>] |
showard | 12f3e32 | 2009-05-13 21:27:42 +0000 | [diff] [blame] | 326 | [--noverify] [--timeout <timeout>] [--max_runtime <max runtime>] |
| 327 | [--one-time-hosts <hosts>] [--email <email>] |
| 328 | [--dependencies <labels this job is dependent on>] |
showard | a1e74b3 | 2009-05-12 17:32:04 +0000 | [diff] [blame] | 329 | [--atomic_group <atomic group name>] [--parse-failed-repair <option>] |
mbligh | ae64d3a | 2008-10-15 04:13:52 +0000 | [diff] [blame] | 330 | job_name |
| 331 | |
| 332 | Creating a job is rather different from the other create operations, |
| 333 | so it only uses the __init__() and output() from its superclass. |
| 334 | """ |
mbligh | be630eb | 2008-08-01 16:41:48 +0000 | [diff] [blame] | 335 | op_action = 'create' |
mbligh | be630eb | 2008-08-01 16:41:48 +0000 | [diff] [blame] | 336 | |
| 337 | def __init__(self): |
| 338 | super(job_create, self).__init__() |
mbligh | be630eb | 2008-08-01 16:41:48 +0000 | [diff] [blame] | 339 | self.ctrl_file_data = {} |
showard | 7bce102 | 2008-11-14 22:51:05 +0000 | [diff] [blame] | 340 | self.parser.add_option('-y', '--synch_count', type=int, |
showard | 2bab8f4 | 2008-11-12 18:15:22 +0000 | [diff] [blame] | 341 | help='Number of machines to use per autoserv ' |
mbligh | 7ffdb8b | 2009-01-21 19:01:51 +0000 | [diff] [blame] | 342 | 'execution') |
mbligh | be630eb | 2008-08-01 16:41:48 +0000 | [diff] [blame] | 343 | self.parser.add_option('-f', '--control-file', |
| 344 | help='use this control file', metavar='FILE') |
| 345 | self.parser.add_option('-s', '--server', |
| 346 | help='This is server-side job', |
| 347 | action='store_true', default=False) |
| 348 | self.parser.add_option('-t', '--test', |
mbligh | 51148c7 | 2008-08-11 20:23:58 +0000 | [diff] [blame] | 349 | help='List of tests to run') |
mbligh | be630eb | 2008-08-01 16:41:48 +0000 | [diff] [blame] | 350 | self.parser.add_option('-k', '--kernel', help='Install kernel from this' |
| 351 | ' URL before beginning job') |
mbligh | 5a49608 | 2009-08-03 16:44:54 +0000 | [diff] [blame] | 352 | |
showard | b27f4ad | 2009-05-01 00:08:26 +0000 | [diff] [blame] | 353 | self.parser.add_option('-d', '--dependencies', help='Comma separated ' |
| 354 | 'list of labels this job is dependent on.', |
| 355 | default='') |
showard | 648a35c | 2009-05-01 00:08:42 +0000 | [diff] [blame] | 356 | self.parser.add_option('-G', '--atomic_group', help='Name of an Atomic ' |
| 357 | 'Group to schedule this job on.', |
| 358 | default='') |
mbligh | 5a49608 | 2009-08-03 16:44:54 +0000 | [diff] [blame] | 359 | |
mbligh | b9a8b16 | 2008-10-29 16:47:29 +0000 | [diff] [blame] | 360 | self.parser.add_option('-B', '--reboot_before', |
showard | 21baa45 | 2008-10-21 00:08:39 +0000 | [diff] [blame] | 361 | help='Whether or not to reboot the machine ' |
| 362 | 'before the job (never/if dirty/always)', |
| 363 | type='choice', |
| 364 | choices=('never', 'if dirty', 'always')) |
| 365 | self.parser.add_option('-a', '--reboot_after', |
| 366 | help='Whether or not to reboot the machine ' |
| 367 | 'after the job (never/if all tests passed/' |
| 368 | 'always)', |
| 369 | type='choice', |
| 370 | choices=('never', 'if all tests passed', |
| 371 | 'always')) |
mbligh | 5a49608 | 2009-08-03 16:44:54 +0000 | [diff] [blame] | 372 | |
showard | a1e74b3 | 2009-05-12 17:32:04 +0000 | [diff] [blame] | 373 | self.parser.add_option('--parse-failed-repair', |
| 374 | help='Whether or not to parse failed repair ' |
| 375 | 'results as part of the job', |
| 376 | type='choice', |
| 377 | choices=('true', 'false')) |
mbligh | 5d0b4b3 | 2008-12-22 14:43:01 +0000 | [diff] [blame] | 378 | self.parser.add_option('-n', '--noverify', |
| 379 | help='Do not run verify for job', |
| 380 | default=False, action='store_true') |
| 381 | self.parser.add_option('-o', '--timeout', help='Job timeout in hours.', |
| 382 | metavar='TIMEOUT') |
showard | 12f3e32 | 2009-05-13 21:27:42 +0000 | [diff] [blame] | 383 | self.parser.add_option('--max_runtime', |
| 384 | help='Job maximum runtime in hours') |
mbligh | be630eb | 2008-08-01 16:41:48 +0000 | [diff] [blame] | 385 | |
| 386 | |
| 387 | def parse(self): |
mbligh | 5a49608 | 2009-08-03 16:44:54 +0000 | [diff] [blame] | 388 | options, leftover = super(job_create, self).parse() |
mbligh | be630eb | 2008-08-01 16:41:48 +0000 | [diff] [blame] | 389 | |
mbligh | 9deeefa | 2009-05-01 23:11:08 +0000 | [diff] [blame] | 390 | if (len(self.hosts) == 0 and not self.one_time_hosts |
showard | 648a35c | 2009-05-01 00:08:42 +0000 | [diff] [blame] | 391 | and not options.labels and not options.atomic_group): |
mbligh | ce34864 | 2009-02-12 21:50:39 +0000 | [diff] [blame] | 392 | self.invalid_syntax('Must specify at least one machine ' |
showard | 648a35c | 2009-05-01 00:08:42 +0000 | [diff] [blame] | 393 | 'or an atomic group ' |
| 394 | '(-m, -M, -b, -G or --one-time-hosts).') |
mbligh | be630eb | 2008-08-01 16:41:48 +0000 | [diff] [blame] | 395 | if not options.control_file and not options.test: |
| 396 | self.invalid_syntax('Must specify either --test or --control-file' |
| 397 | ' to create a job.') |
| 398 | if options.control_file and options.test: |
| 399 | self.invalid_syntax('Can only specify one of --control-file or ' |
| 400 | '--test, not both.') |
mbligh | 120351e | 2009-01-24 01:40:45 +0000 | [diff] [blame] | 401 | if options.kernel: |
| 402 | self.ctrl_file_data['kernel'] = options.kernel |
| 403 | self.ctrl_file_data['do_push_packages'] = True |
mbligh | be630eb | 2008-08-01 16:41:48 +0000 | [diff] [blame] | 404 | if options.control_file: |
mbligh | be630eb | 2008-08-01 16:41:48 +0000 | [diff] [blame] | 405 | try: |
mbligh | 120351e | 2009-01-24 01:40:45 +0000 | [diff] [blame] | 406 | control_file_f = open(options.control_file) |
| 407 | try: |
| 408 | control_file_data = control_file_f.read() |
| 409 | finally: |
| 410 | control_file_f.close() |
mbligh | be630eb | 2008-08-01 16:41:48 +0000 | [diff] [blame] | 411 | except IOError: |
| 412 | self.generic_error('Unable to read from specified ' |
| 413 | 'control-file: %s' % options.control_file) |
mbligh | 120351e | 2009-01-24 01:40:45 +0000 | [diff] [blame] | 414 | if options.kernel: |
| 415 | if options.server: |
| 416 | self.invalid_syntax( |
| 417 | 'A control file and a kernel may only be specified' |
| 418 | ' together on client side jobs.') |
| 419 | # execute() will pass this to the AFE server to wrap this |
| 420 | # control file up to include the kernel installation steps. |
| 421 | self.ctrl_file_data['client_control_file'] = control_file_data |
| 422 | else: |
| 423 | self.data['control_file'] = control_file_data |
mbligh | 4eae22a | 2008-10-10 16:09:46 +0000 | [diff] [blame] | 424 | if options.test: |
showard | 2bab8f4 | 2008-11-12 18:15:22 +0000 | [diff] [blame] | 425 | if options.server: |
mbligh | b9a8b16 | 2008-10-29 16:47:29 +0000 | [diff] [blame] | 426 | self.invalid_syntax('If you specify tests, then the ' |
showard | 2bab8f4 | 2008-11-12 18:15:22 +0000 | [diff] [blame] | 427 | 'client/server setting is implicit and ' |
| 428 | 'cannot be overriden.') |
mbligh | 4eae22a | 2008-10-10 16:09:46 +0000 | [diff] [blame] | 429 | tests = [t.strip() for t in options.test.split(',') if t.strip()] |
mbligh | 120351e | 2009-01-24 01:40:45 +0000 | [diff] [blame] | 430 | self.ctrl_file_data['tests'] = tests |
mbligh | 4eae22a | 2008-10-10 16:09:46 +0000 | [diff] [blame] | 431 | |
mbligh | be630eb | 2008-08-01 16:41:48 +0000 | [diff] [blame] | 432 | |
showard | 21baa45 | 2008-10-21 00:08:39 +0000 | [diff] [blame] | 433 | if options.reboot_before: |
| 434 | self.data['reboot_before'] = options.reboot_before.capitalize() |
| 435 | if options.reboot_after: |
| 436 | self.data['reboot_after'] = options.reboot_after.capitalize() |
showard | a1e74b3 | 2009-05-12 17:32:04 +0000 | [diff] [blame] | 437 | if options.parse_failed_repair: |
| 438 | self.data['parse_failed_repair'] = ( |
| 439 | options.parse_failed_repair == 'true') |
mbligh | 5d0b4b3 | 2008-12-22 14:43:01 +0000 | [diff] [blame] | 440 | if options.noverify: |
| 441 | self.data['run_verify'] = False |
| 442 | if options.timeout: |
| 443 | self.data['timeout'] = options.timeout |
showard | 12f3e32 | 2009-05-13 21:27:42 +0000 | [diff] [blame] | 444 | if options.max_runtime: |
| 445 | self.data['max_runtime_hrs'] = options.max_runtime |
mbligh | be630eb | 2008-08-01 16:41:48 +0000 | [diff] [blame] | 446 | |
showard | 648a35c | 2009-05-01 00:08:42 +0000 | [diff] [blame] | 447 | if options.atomic_group: |
| 448 | self.data['atomic_group_name'] = options.atomic_group |
| 449 | |
showard | b27f4ad | 2009-05-01 00:08:26 +0000 | [diff] [blame] | 450 | deps = options.dependencies.split(',') |
mbligh | b9a8b16 | 2008-10-29 16:47:29 +0000 | [diff] [blame] | 451 | deps = [dep.strip() for dep in deps if dep.strip()] |
| 452 | self.data['dependencies'] = deps |
mbligh | be630eb | 2008-08-01 16:41:48 +0000 | [diff] [blame] | 453 | |
mbligh | 7ffdb8b | 2009-01-21 19:01:51 +0000 | [diff] [blame] | 454 | if options.synch_count: |
| 455 | self.data['synch_count'] = options.synch_count |
mbligh | be630eb | 2008-08-01 16:41:48 +0000 | [diff] [blame] | 456 | if options.server: |
| 457 | self.data['control_type'] = 'Server' |
| 458 | else: |
| 459 | self.data['control_type'] = 'Client' |
| 460 | |
mbligh | 9deeefa | 2009-05-01 23:11:08 +0000 | [diff] [blame] | 461 | return options, leftover |
mbligh | be630eb | 2008-08-01 16:41:48 +0000 | [diff] [blame] | 462 | |
| 463 | |
| 464 | def execute(self): |
| 465 | if self.ctrl_file_data: |
mbligh | 120351e | 2009-01-24 01:40:45 +0000 | [diff] [blame] | 466 | uploading_kernel = 'kernel' in self.ctrl_file_data |
| 467 | if uploading_kernel: |
mbligh | 8c7b04c | 2009-03-25 18:01:56 +0000 | [diff] [blame] | 468 | default_timeout = socket.getdefaulttimeout() |
mbligh | be630eb | 2008-08-01 16:41:48 +0000 | [diff] [blame] | 469 | socket.setdefaulttimeout(topic_common.UPLOAD_SOCKET_TIMEOUT) |
| 470 | print 'Uploading Kernel: this may take a while...', |
mbligh | 120351e | 2009-01-24 01:40:45 +0000 | [diff] [blame] | 471 | sys.stdout.flush() |
| 472 | try: |
| 473 | cf_info = self.execute_rpc(op='generate_control_file', |
| 474 | item=self.jobname, |
| 475 | **self.ctrl_file_data) |
| 476 | finally: |
| 477 | if uploading_kernel: |
mbligh | 8c7b04c | 2009-03-25 18:01:56 +0000 | [diff] [blame] | 478 | socket.setdefaulttimeout(default_timeout) |
| 479 | |
mbligh | 120351e | 2009-01-24 01:40:45 +0000 | [diff] [blame] | 480 | if uploading_kernel: |
mbligh | be630eb | 2008-08-01 16:41:48 +0000 | [diff] [blame] | 481 | print 'Done' |
showard | 989f25d | 2008-10-01 11:38:11 +0000 | [diff] [blame] | 482 | self.data['control_file'] = cf_info['control_file'] |
mbligh | 7ffdb8b | 2009-01-21 19:01:51 +0000 | [diff] [blame] | 483 | if 'synch_count' not in self.data: |
| 484 | self.data['synch_count'] = cf_info['synch_count'] |
showard | 989f25d | 2008-10-01 11:38:11 +0000 | [diff] [blame] | 485 | if cf_info['is_server']: |
mbligh | be630eb | 2008-08-01 16:41:48 +0000 | [diff] [blame] | 486 | self.data['control_type'] = 'Server' |
| 487 | else: |
| 488 | self.data['control_type'] = 'Client' |
mbligh | ae64d3a | 2008-10-15 04:13:52 +0000 | [diff] [blame] | 489 | |
mbligh | b9a8b16 | 2008-10-29 16:47:29 +0000 | [diff] [blame] | 490 | # Get the union of the 2 sets of dependencies |
| 491 | deps = set(self.data['dependencies']) |
showard | a6fe9c6 | 2008-11-03 19:04:25 +0000 | [diff] [blame] | 492 | deps = sorted(deps.union(cf_info['dependencies'])) |
mbligh | b9a8b16 | 2008-10-29 16:47:29 +0000 | [diff] [blame] | 493 | self.data['dependencies'] = list(deps) |
mbligh | ae64d3a | 2008-10-15 04:13:52 +0000 | [diff] [blame] | 494 | |
mbligh | 7ffdb8b | 2009-01-21 19:01:51 +0000 | [diff] [blame] | 495 | if 'synch_count' not in self.data: |
| 496 | self.data['synch_count'] = 1 |
| 497 | |
mbligh | 5a49608 | 2009-08-03 16:44:54 +0000 | [diff] [blame] | 498 | return self.create_job() |
mbligh | be630eb | 2008-08-01 16:41:48 +0000 | [diff] [blame] | 499 | |
| 500 | |
mbligh | 5a49608 | 2009-08-03 16:44:54 +0000 | [diff] [blame] | 501 | class job_clone(job_create_or_clone): |
| 502 | """atest job clone [--priority <Low|Medium|High|Urgent>] |
| 503 | [--mlist </path/to/machinelist>] [--machine <host1 host2 host3>] |
| 504 | [--labels <list of labels of machines to run on>] |
| 505 | [--one-time-hosts <hosts>] [--email <email>] |
| 506 | job_name |
| 507 | |
| 508 | Cloning a job is rather different from the other create operations, |
| 509 | so it only uses the __init__() and output() from its superclass. |
| 510 | """ |
| 511 | op_action = 'clone' |
| 512 | usage_action = 'clone' |
| 513 | |
| 514 | def __init__(self): |
| 515 | super(job_clone, self).__init__() |
| 516 | self.parser.add_option('-i', '--id', help='Job id to clone', |
| 517 | default=False, |
| 518 | metavar='JOB_ID') |
| 519 | self.parser.add_option('-r', '--reuse-hosts', |
| 520 | help='Use the exact same hosts as the ' |
| 521 | 'cloned job.', |
| 522 | action='store_true', default=False) |
| 523 | |
| 524 | |
| 525 | def parse(self): |
| 526 | options, leftover = super(job_clone, self).parse() |
| 527 | |
| 528 | self.clone_id = options.id |
| 529 | self.reuse_hosts = options.reuse_hosts |
| 530 | |
mbligh | 56f1f4a | 2009-08-03 16:45:12 +0000 | [diff] [blame^] | 531 | host_specified = self.hosts or self.one_time_hosts or options.labels |
| 532 | if self.reuse_hosts and host_specified: |
| 533 | self.invalid_syntax('Cannot specify hosts and reuse the same ' |
| 534 | 'ones as the cloned job.') |
| 535 | |
| 536 | if not (self.reuse_hosts or host_specified): |
| 537 | self.invalid_syntax('Must reuse or specify at least one ' |
| 538 | 'machine (-r, -m, -M, -b or ' |
| 539 | '--one-time-hosts).') |
mbligh | 5a49608 | 2009-08-03 16:44:54 +0000 | [diff] [blame] | 540 | |
| 541 | return options, leftover |
| 542 | |
| 543 | |
| 544 | def execute(self): |
| 545 | clone_info = self.execute_rpc(op='get_info_for_clone', |
| 546 | id=self.clone_id, |
| 547 | preserve_metahosts=self.reuse_hosts) |
mbligh | 5a49608 | 2009-08-03 16:44:54 +0000 | [diff] [blame] | 548 | |
| 549 | # Remove fields from clone data that cannot be reused |
mbligh | 56f1f4a | 2009-08-03 16:45:12 +0000 | [diff] [blame^] | 550 | for field in ('name', 'created_on', 'id', 'owner'): |
| 551 | del clone_info['job'][field] |
mbligh | 5a49608 | 2009-08-03 16:44:54 +0000 | [diff] [blame] | 552 | |
| 553 | # Keyword args cannot be unicode strings |
mbligh | 56f1f4a | 2009-08-03 16:45:12 +0000 | [diff] [blame^] | 554 | self.data.update((str(key), val) |
| 555 | for key, val in clone_info['job'].iteritems()) |
mbligh | 5a49608 | 2009-08-03 16:44:54 +0000 | [diff] [blame] | 556 | |
mbligh | 56f1f4a | 2009-08-03 16:45:12 +0000 | [diff] [blame^] | 557 | if self.reuse_hosts: |
| 558 | # Convert host list from clone info that can be used for job_create |
| 559 | for label, qty in clone_info['meta_host_counts'].iteritems(): |
| 560 | self.data['meta_hosts'].extend([label]*qty) |
mbligh | 5a49608 | 2009-08-03 16:44:54 +0000 | [diff] [blame] | 561 | |
mbligh | 56f1f4a | 2009-08-03 16:45:12 +0000 | [diff] [blame^] | 562 | self.data['hosts'].extend(host['hostname'] |
| 563 | for host in clone_info['hosts']) |
mbligh | 5a49608 | 2009-08-03 16:44:54 +0000 | [diff] [blame] | 564 | |
| 565 | return self.create_job() |
mbligh | be630eb | 2008-08-01 16:41:48 +0000 | [diff] [blame] | 566 | |
| 567 | |
| 568 | class job_abort(job, action_common.atest_delete): |
| 569 | """atest job abort <job(s)>""" |
| 570 | usage_action = op_action = 'abort' |
| 571 | msg_done = 'Aborted' |
| 572 | |
| 573 | def parse(self): |
mbligh | 9deeefa | 2009-05-01 23:11:08 +0000 | [diff] [blame] | 574 | job_info = topic_common.item_parse_info(attribute_name='jobids', |
| 575 | use_leftover=True) |
| 576 | options, leftover = super(job_abort, self).parse([job_info], |
| 577 | req_items='jobids') |
mbligh | be630eb | 2008-08-01 16:41:48 +0000 | [diff] [blame] | 578 | |
| 579 | |
mbligh | 206d50a | 2008-11-13 01:19:25 +0000 | [diff] [blame] | 580 | def execute(self): |
| 581 | data = {'job__id__in': self.jobids} |
| 582 | self.execute_rpc(op='abort_host_queue_entries', **data) |
| 583 | print 'Aborting jobs: %s' % ', '.join(self.jobids) |
| 584 | |
| 585 | |
mbligh | be630eb | 2008-08-01 16:41:48 +0000 | [diff] [blame] | 586 | def get_items(self): |
| 587 | return self.jobids |