blob: 51ceb9225fbbe3036cbc2e8085103fba9413d3cd [file] [log] [blame]
mblighbe630eb2008-08-01 16:41:48 +00001#
2# Copyright 2008 Google Inc. All Rights Reserved.
3
4"""
5The job module contains the objects and methods used to
6manage jobs in Autotest.
7
8The valid actions are:
9list: lists job(s)
10create: create a job
11abort: abort job(s)
12stat: detailed listing of job(s)
13
14The common options are:
15
16See topic_common.py for a High Level Design and Algorithm.
17"""
18
19import getpass, os, pwd, re, socket, sys
20from autotest_lib.cli import topic_common, action_common
21
22
23class job(topic_common.atest):
24 """Job class
mbligh5a496082009-08-03 16:44:54 +000025 atest job [create|clone|list|stat|abort] <options>"""
26 usage_action = '[create|clone|list|stat|abort]'
mblighbe630eb2008-08-01 16:41:48 +000027 topic = msg_topic = 'job'
28 msg_items = '<job_ids>'
29
30
31 def _convert_status(self, results):
32 for result in results:
mbligh10a47332008-08-11 19:37:46 +000033 total = sum(result['status_counts'].values())
mbligh47dc4d22009-02-12 21:48:34 +000034 status = ['%s=%s(%.1f%%)' % (key, val, 100.0*float(val)/total)
mbligh10a47332008-08-11 19:37:46 +000035 for key, val in result['status_counts'].iteritems()]
mblighbe630eb2008-08-01 16:41:48 +000036 status.sort()
37 result['status_counts'] = ', '.join(status)
38
39
mbligh5a496082009-08-03 16:44:54 +000040 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
mblighbe630eb2008-08-01 16:41:48 +000050class job_help(job):
51 """Just here to get the atest logic working.
52 Usage is set by its parent"""
53 pass
54
55
56class job_list_stat(action_common.atest_list, job):
mbligh9deeefa2009-05-01 23:11:08 +000057 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
mblighbe630eb2008-08-01 16:41:48 +000065 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
103class 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):
mbligh9deeefa2009-05-01 23:11:08 +0000116 options, leftover = super(job_list, self).parse()
mblighbe630eb2008-08-01 16:41:48 +0000117 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
mbligh9deeefa2009-05-01 23:11:08 +0000127 return options, leftover
mblighbe630eb2008-08-01 16:41:48 +0000128
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
144class 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)
mblighfca5ed12009-11-06 02:59:56 +0000153 self.parser.add_option('-N', '--list-hosts',
154 help='Display only a list of hosts',
155 action='store_true')
156 self.parser.add_option('-s', '--list-hosts-status',
157 help='Display only the hosts in these statuses '
158 'for a job.', action='store')
mblighbe630eb2008-08-01 16:41:48 +0000159
160
161 def parse(self):
mblighfca5ed12009-11-06 02:59:56 +0000162 status_list = topic_common.item_parse_info(
163 attribute_name='status_list',
164 inline_option='list_hosts_status')
165 options, leftover = super(job_stat, self).parse([status_list],
166 req_items='jobs')
167
mblighbe630eb2008-08-01 16:41:48 +0000168 if not self.jobs:
169 self.invalid_syntax('Must specify at least one job.')
170
171 self.show_control_file = options.control_file
mblighfca5ed12009-11-06 02:59:56 +0000172 self.list_hosts = options.list_hosts
173
174 if self.list_hosts and self.status_list:
175 self.invalid_syntax('--list-hosts is implicit when using '
176 '--list-hosts-status.')
177 if len(self.jobs) > 1 and (self.list_hosts or self.status_list):
178 self.invalid_syntax('--list-hosts and --list-hosts-status should '
179 'only be used on a single job.')
mblighbe630eb2008-08-01 16:41:48 +0000180
mbligh9deeefa2009-05-01 23:11:08 +0000181 return options, leftover
mblighbe630eb2008-08-01 16:41:48 +0000182
183
184 def _merge_results(self, summary, qes):
185 hosts_status = {}
186 for qe in qes:
187 if qe['host']:
188 job_id = qe['job']['id']
189 hostname = qe['host']['hostname']
190 hosts_status.setdefault(job_id,
191 {}).setdefault(qe['status'],
192 []).append(hostname)
193
194 for job in summary:
195 job_id = job['id']
196 if hosts_status.has_key(job_id):
197 this_job = hosts_status[job_id]
mblighfca5ed12009-11-06 02:59:56 +0000198 job['hosts'] = ' '.join(' '.join(host) for host in
199 this_job.itervalues())
200 host_per_status = ['%s="%s"' %(status, ' '.join(host))
mblighbe630eb2008-08-01 16:41:48 +0000201 for status, host in this_job.iteritems()]
202 job['hosts_status'] = ', '.join(host_per_status)
mblighfca5ed12009-11-06 02:59:56 +0000203 if self.status_list:
204 statuses = set(s.lower() for s in self.status_list)
205 all_hosts = [s for s in host_per_status if s.split('=',
206 1)[0].lower() in statuses]
207 job['hosts_selected_status'] = '\n'.join(all_hosts)
mblighbe630eb2008-08-01 16:41:48 +0000208 else:
209 job['hosts_status'] = ''
mblighfca5ed12009-11-06 02:59:56 +0000210
211 if not job.get('hosts'):
212 self.generic_error('Job has unassigned meta-hosts, '
213 'try again shortly.')
214
mblighbe630eb2008-08-01 16:41:48 +0000215 return summary
216
217
218 def execute(self):
219 summary = self.execute_on_ids_and_names(op='get_jobs_summary')
220
221 # Get the real hostnames
222 qes = self.execute_on_ids_and_names(op='get_host_queue_entries',
223 check_results={},
224 tag_id='job__in',
225 tag_name='job__name__in')
226
227 self._convert_status(summary)
228
229 return self._merge_results(summary, qes)
230
231
232 def output(self, results):
mblighfca5ed12009-11-06 02:59:56 +0000233 if self.list_hosts:
234 keys = ['hosts']
235 elif self.status_list:
236 keys = ['hosts_selected_status']
237 elif not self.verbose:
mblighbe630eb2008-08-01 16:41:48 +0000238 keys = ['id', 'name', 'priority', 'status_counts', 'hosts_status']
239 else:
240 keys = ['id', 'name', 'priority', 'status_counts', 'hosts_status',
showard2bab8f42008-11-12 18:15:22 +0000241 'owner', 'control_type', 'synch_count', 'created_on',
showarda1e74b32009-05-12 17:32:04 +0000242 'run_verify', 'reboot_before', 'reboot_after',
243 'parse_failed_repair']
mblighbe630eb2008-08-01 16:41:48 +0000244
245 if self.show_control_file:
246 keys.append('control_file')
247
248 super(job_stat, self).output(results, keys)
249
250
mbligh5a496082009-08-03 16:44:54 +0000251class job_create_or_clone(action_common.atest_create, job):
252 """Class containing the code common to the job create and clone actions"""
253 msg_items = 'job_name'
254
255 def __init__(self):
256 super(job_create_or_clone, self).__init__()
257 self.hosts = []
258 self.data_item_key = 'name'
259 self.parser.add_option('-p', '--priority', help='Job priority (low, '
260 'medium, high, urgent), default=medium',
261 type='choice', choices=('low', 'medium', 'high',
262 'urgent'), default='medium')
263 self.parser.add_option('-b', '--labels',
264 help='Comma separated list of labels '
265 'to get machine list from.', default='')
266 self.parser.add_option('-m', '--machine', help='List of machines to '
267 'run on')
268 self.parser.add_option('-M', '--mlist',
269 help='File listing machines to use',
270 type='string', metavar='MACHINE_FLIST')
271 self.parser.add_option('--one-time-hosts',
272 help='List of one time hosts')
273 self.parser.add_option('-e', '--email',
274 help='A comma seperated list of '
275 'email addresses to notify of job completion',
276 default='')
277
278
mbligh56f1f4a2009-08-03 16:45:12 +0000279 def _parse_hosts(self, args):
280 """ Parses the arguments to generate a list of hosts and meta_hosts
281 A host is a regular name, a meta_host is n*label or *label.
282 These can be mixed on the CLI, and separated by either commas or
283 spaces, e.g.: 5*Machine_Label host0 5*Machine_Label2,host2 """
284
285 hosts = []
286 meta_hosts = []
287
288 for arg in args:
289 for host in arg.split(','):
290 if re.match('^[0-9]+[*]', host):
291 num, host = host.split('*', 1)
292 meta_hosts += int(num) * [host]
293 elif re.match('^[*](\w*)', host):
294 meta_hosts += [re.match('^[*](\w*)', host).group(1)]
295 elif host != '' and host not in hosts:
296 # Real hostname and not a duplicate
297 hosts.append(host)
298
299 return (hosts, meta_hosts)
300
301
Eric Li8a12e802011-02-17 14:24:13 -0800302 def parse(self, parse_info=[]):
mbligh5a496082009-08-03 16:44:54 +0000303 host_info = topic_common.item_parse_info(attribute_name='hosts',
304 inline_option='machine',
305 filename_option='mlist')
306 job_info = topic_common.item_parse_info(attribute_name='jobname',
307 use_leftover=True)
308 oth_info = topic_common.item_parse_info(attribute_name='one_time_hosts',
309 inline_option='one_time_hosts')
jamesrenc2863162010-07-12 21:20:51 +0000310 label_info = topic_common.item_parse_info(attribute_name='labels',
311 inline_option='labels')
mbligh5a496082009-08-03 16:44:54 +0000312
Eric Li8a12e802011-02-17 14:24:13 -0800313 options, leftover = super(job_create_or_clone, self).parse(
314 [host_info, job_info, oth_info, label_info] + parse_info,
315 req_items='jobname')
mbligh5a496082009-08-03 16:44:54 +0000316 self.data = {}
Dale Curtis8adf7892011-09-08 16:13:36 -0700317 jobname = getattr(self, 'jobname')
318 if len(jobname) > 1:
mbligh5a496082009-08-03 16:44:54 +0000319 self.invalid_syntax('Too many arguments specified, only expected '
Dale Curtis8adf7892011-09-08 16:13:36 -0700320 'to receive job name: %s' % jobname)
321 self.jobname = jobname[0]
mbligh5a496082009-08-03 16:44:54 +0000322
323 if options.priority:
324 self.data['priority'] = options.priority.capitalize()
325
326 if self.one_time_hosts:
327 self.data['one_time_hosts'] = self.one_time_hosts
328
jamesrenc2863162010-07-12 21:20:51 +0000329 if self.labels:
mbligh5a496082009-08-03 16:44:54 +0000330 label_hosts = self.execute_rpc(op='get_hosts',
jamesrenc2863162010-07-12 21:20:51 +0000331 multiple_labels=self.labels)
mbligh5a496082009-08-03 16:44:54 +0000332 for host in label_hosts:
333 self.hosts.append(host['hostname'])
334
335 self.data['name'] = self.jobname
336
337 (self.data['hosts'],
mbligh56f1f4a2009-08-03 16:45:12 +0000338 self.data['meta_hosts']) = self._parse_hosts(self.hosts)
mbligh5a496082009-08-03 16:44:54 +0000339
340 self.data['email_list'] = options.email
341
342 return options, leftover
343
344
345 def create_job(self):
346 job_id = self.execute_rpc(op='create_job', **self.data)
347 return ['%s (id %s)' % (self.jobname, job_id)]
348
349
350 def get_items(self):
351 return [self.jobname]
352
353
354
355class job_create(job_create_or_clone):
mblighbe630eb2008-08-01 16:41:48 +0000356 """atest job create [--priority <Low|Medium|High|Urgent>]
mbligha212d712009-02-11 01:22:36 +0000357 [--synch_count] [--control-file </path/to/cfile>]
mblighbe630eb2008-08-01 16:41:48 +0000358 [--on-server] [--test <test1,test2>] [--kernel <http://kernel>]
359 [--mlist </path/to/machinelist>] [--machine <host1 host2 host3>]
showardb27f4ad2009-05-01 00:08:26 +0000360 [--labels <list of labels of machines to run on>]
showard21baa452008-10-21 00:08:39 +0000361 [--reboot_before <option>] [--reboot_after <option>]
showard12f3e322009-05-13 21:27:42 +0000362 [--noverify] [--timeout <timeout>] [--max_runtime <max runtime>]
363 [--one-time-hosts <hosts>] [--email <email>]
364 [--dependencies <labels this job is dependent on>]
showarda1e74b32009-05-12 17:32:04 +0000365 [--atomic_group <atomic group name>] [--parse-failed-repair <option>]
Paul Pendlebury5a8c6ad2011-02-01 07:20:17 -0800366 [--image <http://path/to/image>]
mblighae64d3a2008-10-15 04:13:52 +0000367 job_name
368
369 Creating a job is rather different from the other create operations,
370 so it only uses the __init__() and output() from its superclass.
371 """
mblighbe630eb2008-08-01 16:41:48 +0000372 op_action = 'create'
mblighbe630eb2008-08-01 16:41:48 +0000373
374 def __init__(self):
375 super(job_create, self).__init__()
mblighbe630eb2008-08-01 16:41:48 +0000376 self.ctrl_file_data = {}
showard7bce1022008-11-14 22:51:05 +0000377 self.parser.add_option('-y', '--synch_count', type=int,
showard2bab8f42008-11-12 18:15:22 +0000378 help='Number of machines to use per autoserv '
mbligh7ffdb8b2009-01-21 19:01:51 +0000379 'execution')
mblighbe630eb2008-08-01 16:41:48 +0000380 self.parser.add_option('-f', '--control-file',
381 help='use this control file', metavar='FILE')
382 self.parser.add_option('-s', '--server',
383 help='This is server-side job',
384 action='store_true', default=False)
385 self.parser.add_option('-t', '--test',
mbligh51148c72008-08-11 20:23:58 +0000386 help='List of tests to run')
mbligha3c58d22009-08-24 22:01:51 +0000387
388 self.parser.add_option('-k', '--kernel', help='A comma separated list'
389 ' of kernel versions/URLs/filenames to run the'
390 ' job on')
391 self.parser.add_option('--kernel-cmdline', help='A string that will be'
392 ' given as cmdline to the booted kernel(s)'
393 ' specified by the -k option')
mbligh5a496082009-08-03 16:44:54 +0000394
showardb27f4ad2009-05-01 00:08:26 +0000395 self.parser.add_option('-d', '--dependencies', help='Comma separated '
396 'list of labels this job is dependent on.',
397 default='')
showard648a35c2009-05-01 00:08:42 +0000398 self.parser.add_option('-G', '--atomic_group', help='Name of an Atomic '
399 'Group to schedule this job on.',
400 default='')
mbligh5a496082009-08-03 16:44:54 +0000401
mblighb9a8b162008-10-29 16:47:29 +0000402 self.parser.add_option('-B', '--reboot_before',
showard21baa452008-10-21 00:08:39 +0000403 help='Whether or not to reboot the machine '
404 'before the job (never/if dirty/always)',
405 type='choice',
406 choices=('never', 'if dirty', 'always'))
407 self.parser.add_option('-a', '--reboot_after',
408 help='Whether or not to reboot the machine '
409 'after the job (never/if all tests passed/'
410 'always)',
411 type='choice',
412 choices=('never', 'if all tests passed',
413 'always'))
mbligh5a496082009-08-03 16:44:54 +0000414
showarda1e74b32009-05-12 17:32:04 +0000415 self.parser.add_option('--parse-failed-repair',
416 help='Whether or not to parse failed repair '
417 'results as part of the job',
418 type='choice',
419 choices=('true', 'false'))
mbligh5d0b4b32008-12-22 14:43:01 +0000420 self.parser.add_option('-n', '--noverify',
421 help='Do not run verify for job',
422 default=False, action='store_true')
423 self.parser.add_option('-o', '--timeout', help='Job timeout in hours.',
424 metavar='TIMEOUT')
showard12f3e322009-05-13 21:27:42 +0000425 self.parser.add_option('--max_runtime',
426 help='Job maximum runtime in hours')
mblighbe630eb2008-08-01 16:41:48 +0000427
Paul Pendlebury5a8c6ad2011-02-01 07:20:17 -0800428 self.parser.add_option('-i', '--image',
429 help='OS image to install before running the '
430 'test.')
431
mblighbe630eb2008-08-01 16:41:48 +0000432
mbligha3c58d22009-08-24 22:01:51 +0000433 @staticmethod
434 def _get_kernel_data(kernel_list, cmdline):
435 # the RPC supports cmdline per kernel version in a dictionary
436 kernels = []
mbligh6aaab2e2009-09-03 20:25:19 +0000437 for version in re.split(r'[, ]+', kernel_list):
438 if not version:
439 continue
mbligha3c58d22009-08-24 22:01:51 +0000440 kernel_info = {'version': version}
441 if cmdline:
442 kernel_info['cmdline'] = cmdline
443 kernels.append(kernel_info)
444
445 return kernels
446
447
mblighbe630eb2008-08-01 16:41:48 +0000448 def parse(self):
Eric Li8a12e802011-02-17 14:24:13 -0800449 deps_info = topic_common.item_parse_info(attribute_name='dependencies',
450 inline_option='dependencies')
451 options, leftover = super(job_create, self).parse(
452 parse_info=[deps_info])
mblighbe630eb2008-08-01 16:41:48 +0000453
mbligh9deeefa2009-05-01 23:11:08 +0000454 if (len(self.hosts) == 0 and not self.one_time_hosts
showard648a35c2009-05-01 00:08:42 +0000455 and not options.labels and not options.atomic_group):
mblighce348642009-02-12 21:50:39 +0000456 self.invalid_syntax('Must specify at least one machine '
showard648a35c2009-05-01 00:08:42 +0000457 'or an atomic group '
458 '(-m, -M, -b, -G or --one-time-hosts).')
mblighbe630eb2008-08-01 16:41:48 +0000459 if not options.control_file and not options.test:
460 self.invalid_syntax('Must specify either --test or --control-file'
461 ' to create a job.')
462 if options.control_file and options.test:
463 self.invalid_syntax('Can only specify one of --control-file or '
464 '--test, not both.')
mbligh120351e2009-01-24 01:40:45 +0000465 if options.kernel:
mbligha3c58d22009-08-24 22:01:51 +0000466 self.ctrl_file_data['kernel'] = self._get_kernel_data(
467 options.kernel, options.kernel_cmdline)
mblighbe630eb2008-08-01 16:41:48 +0000468 if options.control_file:
mblighbe630eb2008-08-01 16:41:48 +0000469 try:
mbligh120351e2009-01-24 01:40:45 +0000470 control_file_f = open(options.control_file)
471 try:
472 control_file_data = control_file_f.read()
473 finally:
474 control_file_f.close()
mblighbe630eb2008-08-01 16:41:48 +0000475 except IOError:
476 self.generic_error('Unable to read from specified '
477 'control-file: %s' % options.control_file)
mbligh120351e2009-01-24 01:40:45 +0000478 if options.kernel:
mbligh120351e2009-01-24 01:40:45 +0000479 # execute() will pass this to the AFE server to wrap this
480 # control file up to include the kernel installation steps.
481 self.ctrl_file_data['client_control_file'] = control_file_data
482 else:
483 self.data['control_file'] = control_file_data
mbligh4eae22a2008-10-10 16:09:46 +0000484 if options.test:
showard2bab8f42008-11-12 18:15:22 +0000485 if options.server:
mblighb9a8b162008-10-29 16:47:29 +0000486 self.invalid_syntax('If you specify tests, then the '
showard2bab8f42008-11-12 18:15:22 +0000487 'client/server setting is implicit and '
488 'cannot be overriden.')
mbligh4eae22a2008-10-10 16:09:46 +0000489 tests = [t.strip() for t in options.test.split(',') if t.strip()]
mbligh120351e2009-01-24 01:40:45 +0000490 self.ctrl_file_data['tests'] = tests
mbligh4eae22a2008-10-10 16:09:46 +0000491
Paul Pendlebury5a8c6ad2011-02-01 07:20:17 -0800492 if options.image:
493 self.data['image'] = options.image
mblighbe630eb2008-08-01 16:41:48 +0000494
showard21baa452008-10-21 00:08:39 +0000495 if options.reboot_before:
496 self.data['reboot_before'] = options.reboot_before.capitalize()
497 if options.reboot_after:
498 self.data['reboot_after'] = options.reboot_after.capitalize()
showarda1e74b32009-05-12 17:32:04 +0000499 if options.parse_failed_repair:
500 self.data['parse_failed_repair'] = (
501 options.parse_failed_repair == 'true')
mbligh5d0b4b32008-12-22 14:43:01 +0000502 if options.noverify:
503 self.data['run_verify'] = False
504 if options.timeout:
505 self.data['timeout'] = options.timeout
showard12f3e322009-05-13 21:27:42 +0000506 if options.max_runtime:
507 self.data['max_runtime_hrs'] = options.max_runtime
mblighbe630eb2008-08-01 16:41:48 +0000508
showard648a35c2009-05-01 00:08:42 +0000509 if options.atomic_group:
510 self.data['atomic_group_name'] = options.atomic_group
511
Eric Li8a12e802011-02-17 14:24:13 -0800512 self.data['dependencies'] = self.dependencies
mblighbe630eb2008-08-01 16:41:48 +0000513
mbligh7ffdb8b2009-01-21 19:01:51 +0000514 if options.synch_count:
515 self.data['synch_count'] = options.synch_count
mblighbe630eb2008-08-01 16:41:48 +0000516 if options.server:
517 self.data['control_type'] = 'Server'
518 else:
519 self.data['control_type'] = 'Client'
520
mbligh9deeefa2009-05-01 23:11:08 +0000521 return options, leftover
mblighbe630eb2008-08-01 16:41:48 +0000522
523
524 def execute(self):
525 if self.ctrl_file_data:
mbligh120351e2009-01-24 01:40:45 +0000526 uploading_kernel = 'kernel' in self.ctrl_file_data
527 if uploading_kernel:
mbligh8c7b04c2009-03-25 18:01:56 +0000528 default_timeout = socket.getdefaulttimeout()
mblighbe630eb2008-08-01 16:41:48 +0000529 socket.setdefaulttimeout(topic_common.UPLOAD_SOCKET_TIMEOUT)
530 print 'Uploading Kernel: this may take a while...',
mbligh120351e2009-01-24 01:40:45 +0000531 sys.stdout.flush()
532 try:
533 cf_info = self.execute_rpc(op='generate_control_file',
534 item=self.jobname,
535 **self.ctrl_file_data)
536 finally:
537 if uploading_kernel:
mbligh8c7b04c2009-03-25 18:01:56 +0000538 socket.setdefaulttimeout(default_timeout)
539
mbligh120351e2009-01-24 01:40:45 +0000540 if uploading_kernel:
mblighbe630eb2008-08-01 16:41:48 +0000541 print 'Done'
showard989f25d2008-10-01 11:38:11 +0000542 self.data['control_file'] = cf_info['control_file']
mbligh7ffdb8b2009-01-21 19:01:51 +0000543 if 'synch_count' not in self.data:
544 self.data['synch_count'] = cf_info['synch_count']
showard989f25d2008-10-01 11:38:11 +0000545 if cf_info['is_server']:
mblighbe630eb2008-08-01 16:41:48 +0000546 self.data['control_type'] = 'Server'
547 else:
548 self.data['control_type'] = 'Client'
mblighae64d3a2008-10-15 04:13:52 +0000549
mblighb9a8b162008-10-29 16:47:29 +0000550 # Get the union of the 2 sets of dependencies
551 deps = set(self.data['dependencies'])
showarda6fe9c62008-11-03 19:04:25 +0000552 deps = sorted(deps.union(cf_info['dependencies']))
mblighb9a8b162008-10-29 16:47:29 +0000553 self.data['dependencies'] = list(deps)
mblighae64d3a2008-10-15 04:13:52 +0000554
mbligh7ffdb8b2009-01-21 19:01:51 +0000555 if 'synch_count' not in self.data:
556 self.data['synch_count'] = 1
557
mbligh5a496082009-08-03 16:44:54 +0000558 return self.create_job()
mblighbe630eb2008-08-01 16:41:48 +0000559
560
mbligh5a496082009-08-03 16:44:54 +0000561class job_clone(job_create_or_clone):
562 """atest job clone [--priority <Low|Medium|High|Urgent>]
563 [--mlist </path/to/machinelist>] [--machine <host1 host2 host3>]
564 [--labels <list of labels of machines to run on>]
565 [--one-time-hosts <hosts>] [--email <email>]
566 job_name
567
568 Cloning a job is rather different from the other create operations,
569 so it only uses the __init__() and output() from its superclass.
570 """
571 op_action = 'clone'
572 usage_action = 'clone'
573
574 def __init__(self):
575 super(job_clone, self).__init__()
576 self.parser.add_option('-i', '--id', help='Job id to clone',
577 default=False,
578 metavar='JOB_ID')
579 self.parser.add_option('-r', '--reuse-hosts',
580 help='Use the exact same hosts as the '
581 'cloned job.',
582 action='store_true', default=False)
583
584
585 def parse(self):
586 options, leftover = super(job_clone, self).parse()
587
588 self.clone_id = options.id
589 self.reuse_hosts = options.reuse_hosts
590
mbligh56f1f4a2009-08-03 16:45:12 +0000591 host_specified = self.hosts or self.one_time_hosts or options.labels
592 if self.reuse_hosts and host_specified:
593 self.invalid_syntax('Cannot specify hosts and reuse the same '
594 'ones as the cloned job.')
595
596 if not (self.reuse_hosts or host_specified):
597 self.invalid_syntax('Must reuse or specify at least one '
598 'machine (-r, -m, -M, -b or '
599 '--one-time-hosts).')
mbligh5a496082009-08-03 16:44:54 +0000600
601 return options, leftover
602
603
604 def execute(self):
605 clone_info = self.execute_rpc(op='get_info_for_clone',
606 id=self.clone_id,
607 preserve_metahosts=self.reuse_hosts)
mbligh5a496082009-08-03 16:44:54 +0000608
609 # Remove fields from clone data that cannot be reused
mbligh56f1f4a2009-08-03 16:45:12 +0000610 for field in ('name', 'created_on', 'id', 'owner'):
611 del clone_info['job'][field]
mbligh5a496082009-08-03 16:44:54 +0000612
Eric Li861b2d52011-02-04 14:50:35 -0800613 # Also remove parameterized_job field, as the feature still is
614 # incomplete, this tool does not attempt to support it for now,
615 # it uses a different API function and it breaks create_job()
616 if clone_info['job'].has_key('parameterized_job'):
617 del clone_info['job']['parameterized_job']
618
mbligh5a496082009-08-03 16:44:54 +0000619 # Keyword args cannot be unicode strings
mbligh56f1f4a2009-08-03 16:45:12 +0000620 self.data.update((str(key), val)
621 for key, val in clone_info['job'].iteritems())
mbligh5a496082009-08-03 16:44:54 +0000622
mbligh56f1f4a2009-08-03 16:45:12 +0000623 if self.reuse_hosts:
624 # Convert host list from clone info that can be used for job_create
625 for label, qty in clone_info['meta_host_counts'].iteritems():
626 self.data['meta_hosts'].extend([label]*qty)
mbligh5a496082009-08-03 16:44:54 +0000627
mbligh56f1f4a2009-08-03 16:45:12 +0000628 self.data['hosts'].extend(host['hostname']
629 for host in clone_info['hosts'])
mbligh5a496082009-08-03 16:44:54 +0000630
631 return self.create_job()
mblighbe630eb2008-08-01 16:41:48 +0000632
633
634class job_abort(job, action_common.atest_delete):
635 """atest job abort <job(s)>"""
636 usage_action = op_action = 'abort'
637 msg_done = 'Aborted'
638
639 def parse(self):
mbligh9deeefa2009-05-01 23:11:08 +0000640 job_info = topic_common.item_parse_info(attribute_name='jobids',
641 use_leftover=True)
642 options, leftover = super(job_abort, self).parse([job_info],
643 req_items='jobids')
mblighbe630eb2008-08-01 16:41:48 +0000644
645
mbligh206d50a2008-11-13 01:19:25 +0000646 def execute(self):
647 data = {'job__id__in': self.jobids}
648 self.execute_rpc(op='abort_host_queue_entries', **data)
649 print 'Aborting jobs: %s' % ', '.join(self.jobids)
650
651
mblighbe630eb2008-08-01 16:41:48 +0000652 def get_items(self):
653 return self.jobids