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 |
| 25 | atest job [create|list|stat|abort] <options>""" |
| 26 | usage_action = '[create|list|stat|abort]' |
| 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 | |
| 40 | class job_help(job): |
| 41 | """Just here to get the atest logic working. |
| 42 | Usage is set by its parent""" |
| 43 | pass |
| 44 | |
| 45 | |
| 46 | class job_list_stat(action_common.atest_list, job): |
| 47 | def __split_jobs_between_ids_names(self): |
| 48 | job_ids = [] |
| 49 | job_names = [] |
| 50 | |
| 51 | # Sort between job IDs and names |
| 52 | for job_id in self.jobs: |
| 53 | if job_id.isdigit(): |
| 54 | job_ids.append(job_id) |
| 55 | else: |
| 56 | job_names.append(job_id) |
| 57 | return (job_ids, job_names) |
| 58 | |
| 59 | |
| 60 | def execute_on_ids_and_names(self, op, filters={}, |
| 61 | check_results={'id__in': 'id', |
| 62 | 'name__in': 'id'}, |
| 63 | tag_id='id__in', tag_name='name__in'): |
| 64 | if not self.jobs: |
| 65 | # Want everything |
| 66 | return super(job_list_stat, self).execute(op=op, filters=filters) |
| 67 | |
| 68 | all_jobs = [] |
| 69 | (job_ids, job_names) = self.__split_jobs_between_ids_names() |
| 70 | |
| 71 | for items, tag in [(job_ids, tag_id), |
| 72 | (job_names, tag_name)]: |
| 73 | if items: |
| 74 | new_filters = filters.copy() |
| 75 | new_filters[tag] = items |
| 76 | jobs = super(job_list_stat, |
| 77 | self).execute(op=op, |
| 78 | filters=new_filters, |
| 79 | check_results=check_results) |
| 80 | all_jobs.extend(jobs) |
| 81 | |
| 82 | return all_jobs |
| 83 | |
| 84 | |
| 85 | class job_list(job_list_stat): |
| 86 | """atest job list [<jobs>] [--all] [--running] [--user <username>]""" |
| 87 | def __init__(self): |
| 88 | super(job_list, self).__init__() |
| 89 | self.parser.add_option('-a', '--all', help='List jobs for all ' |
| 90 | 'users.', action='store_true', default=False) |
| 91 | self.parser.add_option('-r', '--running', help='List only running ' |
| 92 | 'jobs', action='store_true') |
| 93 | self.parser.add_option('-u', '--user', help='List jobs for given ' |
| 94 | 'user', type='string') |
| 95 | |
| 96 | |
| 97 | def parse(self): |
| 98 | (options, leftover) = self.parse_with_flist([('jobs', '', '', True)], |
| 99 | None) |
| 100 | self.all = options.all |
| 101 | self.data['running'] = options.running |
| 102 | if options.user: |
| 103 | if options.all: |
| 104 | self.invalid_syntax('Only specify --all or --user, not both.') |
| 105 | else: |
| 106 | self.data['owner'] = options.user |
| 107 | elif not options.all and not self.jobs: |
| 108 | self.data['owner'] = getpass.getuser() |
| 109 | |
| 110 | return (options, leftover) |
| 111 | |
| 112 | |
| 113 | def execute(self): |
| 114 | return self.execute_on_ids_and_names(op='get_jobs_summary', |
| 115 | filters=self.data) |
| 116 | |
| 117 | |
| 118 | def output(self, results): |
| 119 | keys = ['id', 'owner', 'name', 'status_counts'] |
| 120 | if self.verbose: |
| 121 | keys.extend(['priority', 'control_type', 'created_on']) |
| 122 | self._convert_status(results) |
| 123 | super(job_list, self).output(results, keys) |
| 124 | |
| 125 | |
| 126 | |
| 127 | class job_stat(job_list_stat): |
| 128 | """atest job stat <job>""" |
| 129 | usage_action = 'stat' |
| 130 | |
| 131 | def __init__(self): |
| 132 | super(job_stat, self).__init__() |
| 133 | self.parser.add_option('-f', '--control-file', |
| 134 | help='Display the control file', |
| 135 | action='store_true', default=False) |
| 136 | |
| 137 | |
| 138 | def parse(self): |
| 139 | (options, leftover) = self.parse_with_flist(flists=[('jobs', '', '', |
| 140 | True)], |
| 141 | req_items='jobs') |
| 142 | if not self.jobs: |
| 143 | self.invalid_syntax('Must specify at least one job.') |
| 144 | |
| 145 | self.show_control_file = options.control_file |
| 146 | |
| 147 | return (options, leftover) |
| 148 | |
| 149 | |
| 150 | def _merge_results(self, summary, qes): |
| 151 | hosts_status = {} |
| 152 | for qe in qes: |
| 153 | if qe['host']: |
| 154 | job_id = qe['job']['id'] |
| 155 | hostname = qe['host']['hostname'] |
| 156 | hosts_status.setdefault(job_id, |
| 157 | {}).setdefault(qe['status'], |
| 158 | []).append(hostname) |
| 159 | |
| 160 | for job in summary: |
| 161 | job_id = job['id'] |
| 162 | if hosts_status.has_key(job_id): |
| 163 | this_job = hosts_status[job_id] |
mbligh | 47dc4d2 | 2009-02-12 21:48:34 +0000 | [diff] [blame^] | 164 | host_per_status = ['%s=%s' %(status, ','.join(host)) |
mbligh | be630eb | 2008-08-01 16:41:48 +0000 | [diff] [blame] | 165 | for status, host in this_job.iteritems()] |
| 166 | job['hosts_status'] = ', '.join(host_per_status) |
| 167 | else: |
| 168 | job['hosts_status'] = '' |
| 169 | return summary |
| 170 | |
| 171 | |
| 172 | def execute(self): |
| 173 | summary = self.execute_on_ids_and_names(op='get_jobs_summary') |
| 174 | |
| 175 | # Get the real hostnames |
| 176 | qes = self.execute_on_ids_and_names(op='get_host_queue_entries', |
| 177 | check_results={}, |
| 178 | tag_id='job__in', |
| 179 | tag_name='job__name__in') |
| 180 | |
| 181 | self._convert_status(summary) |
| 182 | |
| 183 | return self._merge_results(summary, qes) |
| 184 | |
| 185 | |
| 186 | def output(self, results): |
| 187 | if not self.verbose: |
| 188 | keys = ['id', 'name', 'priority', 'status_counts', 'hosts_status'] |
| 189 | else: |
| 190 | keys = ['id', 'name', 'priority', 'status_counts', 'hosts_status', |
showard | 2bab8f4 | 2008-11-12 18:15:22 +0000 | [diff] [blame] | 191 | 'owner', 'control_type', 'synch_count', 'created_on', |
showard | 21baa45 | 2008-10-21 00:08:39 +0000 | [diff] [blame] | 192 | 'run_verify', 'reboot_before', 'reboot_after'] |
mbligh | be630eb | 2008-08-01 16:41:48 +0000 | [diff] [blame] | 193 | |
| 194 | if self.show_control_file: |
| 195 | keys.append('control_file') |
| 196 | |
| 197 | super(job_stat, self).output(results, keys) |
| 198 | |
| 199 | |
| 200 | class job_create(action_common.atest_create, job): |
| 201 | """atest job create [--priority <Low|Medium|High|Urgent>] |
mbligh | a212d71 | 2009-02-11 01:22:36 +0000 | [diff] [blame] | 202 | [--synch_count] [--control-file </path/to/cfile>] |
mbligh | be630eb | 2008-08-01 16:41:48 +0000 | [diff] [blame] | 203 | [--on-server] [--test <test1,test2>] [--kernel <http://kernel>] |
| 204 | [--mlist </path/to/machinelist>] [--machine <host1 host2 host3>] |
mbligh | b9a8b16 | 2008-10-29 16:47:29 +0000 | [diff] [blame] | 205 | [--labels <labels this job is dependent on>] |
showard | 21baa45 | 2008-10-21 00:08:39 +0000 | [diff] [blame] | 206 | [--reboot_before <option>] [--reboot_after <option>] |
mbligh | 5d0b4b3 | 2008-12-22 14:43:01 +0000 | [diff] [blame] | 207 | [--noverify] [--timeout <timeout>] |
mbligh | ae64d3a | 2008-10-15 04:13:52 +0000 | [diff] [blame] | 208 | job_name |
| 209 | |
| 210 | Creating a job is rather different from the other create operations, |
| 211 | so it only uses the __init__() and output() from its superclass. |
| 212 | """ |
mbligh | be630eb | 2008-08-01 16:41:48 +0000 | [diff] [blame] | 213 | op_action = 'create' |
| 214 | msg_items = 'job_name' |
mbligh | be630eb | 2008-08-01 16:41:48 +0000 | [diff] [blame] | 215 | |
| 216 | def __init__(self): |
| 217 | super(job_create, self).__init__() |
| 218 | self.hosts = [] |
| 219 | self.ctrl_file_data = {} |
| 220 | self.data_item_key = 'name' |
| 221 | self.parser.add_option('-p', '--priority', help='Job priority (low, ' |
| 222 | 'medium, high, urgent), default=medium', |
| 223 | type='choice', choices=('low', 'medium', 'high', |
| 224 | 'urgent'), default='medium') |
showard | 7bce102 | 2008-11-14 22:51:05 +0000 | [diff] [blame] | 225 | self.parser.add_option('-y', '--synch_count', type=int, |
showard | 2bab8f4 | 2008-11-12 18:15:22 +0000 | [diff] [blame] | 226 | help='Number of machines to use per autoserv ' |
mbligh | 7ffdb8b | 2009-01-21 19:01:51 +0000 | [diff] [blame] | 227 | 'execution') |
mbligh | be630eb | 2008-08-01 16:41:48 +0000 | [diff] [blame] | 228 | self.parser.add_option('-f', '--control-file', |
| 229 | help='use this control file', metavar='FILE') |
| 230 | self.parser.add_option('-s', '--server', |
| 231 | help='This is server-side job', |
| 232 | action='store_true', default=False) |
| 233 | self.parser.add_option('-t', '--test', |
mbligh | 51148c7 | 2008-08-11 20:23:58 +0000 | [diff] [blame] | 234 | help='List of tests to run') |
mbligh | be630eb | 2008-08-01 16:41:48 +0000 | [diff] [blame] | 235 | self.parser.add_option('-k', '--kernel', help='Install kernel from this' |
| 236 | ' URL before beginning job') |
mbligh | b9a8b16 | 2008-10-29 16:47:29 +0000 | [diff] [blame] | 237 | self.parser.add_option('-b', '--labels', help='Comma separated list of ' |
| 238 | 'labels this job is dependent on.', default='') |
mbligh | be630eb | 2008-08-01 16:41:48 +0000 | [diff] [blame] | 239 | self.parser.add_option('-m', '--machine', help='List of machines to ' |
| 240 | 'run on') |
| 241 | self.parser.add_option('-M', '--mlist', |
| 242 | help='File listing machines to use', |
| 243 | type='string', metavar='MACHINE_FLIST') |
mbligh | 6fee7fd | 2008-10-10 15:44:39 +0000 | [diff] [blame] | 244 | self.parser.add_option('-e', '--email', help='A comma seperated list ' |
| 245 | 'of email addresses to notify of job completion', |
| 246 | default='') |
mbligh | b9a8b16 | 2008-10-29 16:47:29 +0000 | [diff] [blame] | 247 | self.parser.add_option('-B', '--reboot_before', |
showard | 21baa45 | 2008-10-21 00:08:39 +0000 | [diff] [blame] | 248 | help='Whether or not to reboot the machine ' |
| 249 | 'before the job (never/if dirty/always)', |
| 250 | type='choice', |
| 251 | choices=('never', 'if dirty', 'always')) |
| 252 | self.parser.add_option('-a', '--reboot_after', |
| 253 | help='Whether or not to reboot the machine ' |
| 254 | 'after the job (never/if all tests passed/' |
| 255 | 'always)', |
| 256 | type='choice', |
| 257 | choices=('never', 'if all tests passed', |
| 258 | 'always')) |
mbligh | fb8f0ab | 2008-11-13 01:11:48 +0000 | [diff] [blame] | 259 | self.parser.add_option('-l', '--clone', help='Clone an existing job. ' |
| 260 | 'This will discard all other options except ' |
| 261 | '--reuse-hosts.', default=False, |
| 262 | metavar='JOB_ID') |
| 263 | self.parser.add_option('-r', '--reuse-hosts', help='Use the exact same ' |
| 264 | 'hosts as cloned job. Only for use with ' |
| 265 | '--clone.', action='store_true', default=False) |
mbligh | 5d0b4b3 | 2008-12-22 14:43:01 +0000 | [diff] [blame] | 266 | self.parser.add_option('-n', '--noverify', |
| 267 | help='Do not run verify for job', |
| 268 | default=False, action='store_true') |
| 269 | self.parser.add_option('-o', '--timeout', help='Job timeout in hours.', |
| 270 | metavar='TIMEOUT') |
mbligh | be630eb | 2008-08-01 16:41:48 +0000 | [diff] [blame] | 271 | |
| 272 | |
| 273 | def parse(self): |
| 274 | flists = [('hosts', 'mlist', 'machine', False), |
| 275 | ('jobname', '', '', True)] |
| 276 | (options, leftover) = self.parse_with_flist(flists, |
| 277 | req_items='jobname') |
| 278 | self.data = {} |
mbligh | fb8f0ab | 2008-11-13 01:11:48 +0000 | [diff] [blame] | 279 | if len(self.jobname) > 1: |
| 280 | self.invalid_syntax('Too many arguments specified, only expected ' |
| 281 | 'to receive job name: %s' % self.jobname) |
| 282 | self.jobname = self.jobname[0] |
| 283 | |
| 284 | if options.reuse_hosts and not options.clone: |
| 285 | self.invalid_syntax('--reuse-hosts only to be used with --clone.') |
| 286 | # If cloning skip parse, parsing is done in execute |
| 287 | self.clone_id = options.clone |
| 288 | if options.clone: |
| 289 | self.op_action = 'clone' |
| 290 | self.msg_items = 'jobid' |
| 291 | self.reuse_hosts = options.reuse_hosts |
| 292 | return (options, leftover) |
mbligh | be630eb | 2008-08-01 16:41:48 +0000 | [diff] [blame] | 293 | |
| 294 | if len(self.hosts) == 0: |
mbligh | 120351e | 2009-01-24 01:40:45 +0000 | [diff] [blame] | 295 | self.invalid_syntax('Must specify at least one machine (-m or -M).') |
mbligh | be630eb | 2008-08-01 16:41:48 +0000 | [diff] [blame] | 296 | if not options.control_file and not options.test: |
| 297 | self.invalid_syntax('Must specify either --test or --control-file' |
| 298 | ' to create a job.') |
| 299 | if options.control_file and options.test: |
| 300 | self.invalid_syntax('Can only specify one of --control-file or ' |
| 301 | '--test, not both.') |
mbligh | 120351e | 2009-01-24 01:40:45 +0000 | [diff] [blame] | 302 | if options.kernel: |
| 303 | self.ctrl_file_data['kernel'] = options.kernel |
| 304 | self.ctrl_file_data['do_push_packages'] = True |
mbligh | be630eb | 2008-08-01 16:41:48 +0000 | [diff] [blame] | 305 | if options.control_file: |
mbligh | be630eb | 2008-08-01 16:41:48 +0000 | [diff] [blame] | 306 | try: |
mbligh | 120351e | 2009-01-24 01:40:45 +0000 | [diff] [blame] | 307 | control_file_f = open(options.control_file) |
| 308 | try: |
| 309 | control_file_data = control_file_f.read() |
| 310 | finally: |
| 311 | control_file_f.close() |
mbligh | be630eb | 2008-08-01 16:41:48 +0000 | [diff] [blame] | 312 | except IOError: |
| 313 | self.generic_error('Unable to read from specified ' |
| 314 | 'control-file: %s' % options.control_file) |
mbligh | 120351e | 2009-01-24 01:40:45 +0000 | [diff] [blame] | 315 | if options.kernel: |
| 316 | if options.server: |
| 317 | self.invalid_syntax( |
| 318 | 'A control file and a kernel may only be specified' |
| 319 | ' together on client side jobs.') |
| 320 | # execute() will pass this to the AFE server to wrap this |
| 321 | # control file up to include the kernel installation steps. |
| 322 | self.ctrl_file_data['client_control_file'] = control_file_data |
| 323 | else: |
| 324 | self.data['control_file'] = control_file_data |
mbligh | 4eae22a | 2008-10-10 16:09:46 +0000 | [diff] [blame] | 325 | if options.test: |
showard | 2bab8f4 | 2008-11-12 18:15:22 +0000 | [diff] [blame] | 326 | if options.server: |
mbligh | b9a8b16 | 2008-10-29 16:47:29 +0000 | [diff] [blame] | 327 | self.invalid_syntax('If you specify tests, then the ' |
showard | 2bab8f4 | 2008-11-12 18:15:22 +0000 | [diff] [blame] | 328 | 'client/server setting is implicit and ' |
| 329 | 'cannot be overriden.') |
mbligh | 4eae22a | 2008-10-10 16:09:46 +0000 | [diff] [blame] | 330 | tests = [t.strip() for t in options.test.split(',') if t.strip()] |
mbligh | 120351e | 2009-01-24 01:40:45 +0000 | [diff] [blame] | 331 | self.ctrl_file_data['tests'] = tests |
mbligh | 4eae22a | 2008-10-10 16:09:46 +0000 | [diff] [blame] | 332 | |
mbligh | be630eb | 2008-08-01 16:41:48 +0000 | [diff] [blame] | 333 | |
| 334 | if options.priority: |
| 335 | self.data['priority'] = options.priority.capitalize() |
showard | 21baa45 | 2008-10-21 00:08:39 +0000 | [diff] [blame] | 336 | if options.reboot_before: |
| 337 | self.data['reboot_before'] = options.reboot_before.capitalize() |
| 338 | if options.reboot_after: |
| 339 | self.data['reboot_after'] = options.reboot_after.capitalize() |
mbligh | 5d0b4b3 | 2008-12-22 14:43:01 +0000 | [diff] [blame] | 340 | if options.noverify: |
| 341 | self.data['run_verify'] = False |
| 342 | if options.timeout: |
| 343 | self.data['timeout'] = options.timeout |
mbligh | be630eb | 2008-08-01 16:41:48 +0000 | [diff] [blame] | 344 | |
mbligh | be630eb | 2008-08-01 16:41:48 +0000 | [diff] [blame] | 345 | self.data['name'] = self.jobname |
| 346 | |
| 347 | (self.data['hosts'], |
| 348 | self.data['meta_hosts']) = self.parse_hosts(self.hosts) |
| 349 | |
mbligh | b9a8b16 | 2008-10-29 16:47:29 +0000 | [diff] [blame] | 350 | deps = options.labels.split(',') |
| 351 | deps = [dep.strip() for dep in deps if dep.strip()] |
| 352 | self.data['dependencies'] = deps |
mbligh | be630eb | 2008-08-01 16:41:48 +0000 | [diff] [blame] | 353 | |
mbligh | 6fee7fd | 2008-10-10 15:44:39 +0000 | [diff] [blame] | 354 | self.data['email_list'] = options.email |
mbligh | 7ffdb8b | 2009-01-21 19:01:51 +0000 | [diff] [blame] | 355 | if options.synch_count: |
| 356 | self.data['synch_count'] = options.synch_count |
mbligh | be630eb | 2008-08-01 16:41:48 +0000 | [diff] [blame] | 357 | if options.server: |
| 358 | self.data['control_type'] = 'Server' |
| 359 | else: |
| 360 | self.data['control_type'] = 'Client' |
| 361 | |
mbligh | be630eb | 2008-08-01 16:41:48 +0000 | [diff] [blame] | 362 | return (options, leftover) |
| 363 | |
| 364 | |
| 365 | def execute(self): |
| 366 | if self.ctrl_file_data: |
mbligh | 120351e | 2009-01-24 01:40:45 +0000 | [diff] [blame] | 367 | uploading_kernel = 'kernel' in self.ctrl_file_data |
| 368 | if uploading_kernel: |
mbligh | be630eb | 2008-08-01 16:41:48 +0000 | [diff] [blame] | 369 | socket.setdefaulttimeout(topic_common.UPLOAD_SOCKET_TIMEOUT) |
| 370 | print 'Uploading Kernel: this may take a while...', |
mbligh | 120351e | 2009-01-24 01:40:45 +0000 | [diff] [blame] | 371 | sys.stdout.flush() |
| 372 | try: |
| 373 | cf_info = self.execute_rpc(op='generate_control_file', |
| 374 | item=self.jobname, |
| 375 | **self.ctrl_file_data) |
| 376 | finally: |
| 377 | if uploading_kernel: |
| 378 | socket.setdefaulttimeout( |
| 379 | topic_common.DEFAULT_SOCKET_TIMEOUT) |
| 380 | if uploading_kernel: |
mbligh | be630eb | 2008-08-01 16:41:48 +0000 | [diff] [blame] | 381 | print 'Done' |
showard | 989f25d | 2008-10-01 11:38:11 +0000 | [diff] [blame] | 382 | self.data['control_file'] = cf_info['control_file'] |
mbligh | 7ffdb8b | 2009-01-21 19:01:51 +0000 | [diff] [blame] | 383 | if 'synch_count' not in self.data: |
| 384 | self.data['synch_count'] = cf_info['synch_count'] |
showard | 989f25d | 2008-10-01 11:38:11 +0000 | [diff] [blame] | 385 | if cf_info['is_server']: |
mbligh | be630eb | 2008-08-01 16:41:48 +0000 | [diff] [blame] | 386 | self.data['control_type'] = 'Server' |
| 387 | else: |
| 388 | self.data['control_type'] = 'Client' |
mbligh | ae64d3a | 2008-10-15 04:13:52 +0000 | [diff] [blame] | 389 | |
mbligh | b9a8b16 | 2008-10-29 16:47:29 +0000 | [diff] [blame] | 390 | # Get the union of the 2 sets of dependencies |
| 391 | deps = set(self.data['dependencies']) |
showard | a6fe9c6 | 2008-11-03 19:04:25 +0000 | [diff] [blame] | 392 | deps = sorted(deps.union(cf_info['dependencies'])) |
mbligh | b9a8b16 | 2008-10-29 16:47:29 +0000 | [diff] [blame] | 393 | self.data['dependencies'] = list(deps) |
mbligh | ae64d3a | 2008-10-15 04:13:52 +0000 | [diff] [blame] | 394 | |
mbligh | 7ffdb8b | 2009-01-21 19:01:51 +0000 | [diff] [blame] | 395 | if 'synch_count' not in self.data: |
| 396 | self.data['synch_count'] = 1 |
| 397 | |
mbligh | fb8f0ab | 2008-11-13 01:11:48 +0000 | [diff] [blame] | 398 | if self.clone_id: |
| 399 | clone_info = self.execute_rpc(op='get_info_for_clone', |
| 400 | id=self.clone_id, |
| 401 | preserve_metahosts=self.reuse_hosts) |
| 402 | self.data = clone_info['job'] |
| 403 | |
| 404 | # Remove fields from clone data that cannot be reused |
| 405 | unused_fields = ('name', 'created_on', 'id', 'owner') |
| 406 | for field in unused_fields: |
| 407 | del self.data[field] |
| 408 | |
| 409 | # Keyword args cannot be unicode strings |
| 410 | for key, val in self.data.iteritems(): |
| 411 | del self.data[key] |
| 412 | self.data[str(key)] = val |
| 413 | |
| 414 | # Convert host list from clone info that can be used for job_create |
| 415 | host_list = [] |
| 416 | if clone_info['meta_host_counts']: |
| 417 | # Creates a dictionary of meta_hosts, e.g. |
| 418 | # {u'label1': 3, u'label2': 2, u'label3': 5} |
| 419 | meta_hosts = clone_info['meta_host_counts'] |
| 420 | # Create a list of formatted metahosts, e.g. |
| 421 | # [u'3*label1', u'2*label2', u'5*label3'] |
| 422 | meta_host_list = ['%s*%s' % (str(val), key) for key,val in |
| 423 | meta_hosts.items()] |
| 424 | host_list.extend(meta_host_list) |
| 425 | if clone_info['hosts']: |
| 426 | # Creates a list of hosts, e.g. [u'host1', u'host2'] |
| 427 | hosts = [host['hostname'] for host in clone_info['hosts']] |
| 428 | host_list.extend(hosts) |
| 429 | |
| 430 | (self.data['hosts'], |
| 431 | self.data['meta_hosts']) = self.parse_hosts(host_list) |
| 432 | self.data['name'] = self.jobname |
| 433 | |
mbligh | 7b8d1f9 | 2008-10-21 16:24:08 +0000 | [diff] [blame] | 434 | socket.setdefaulttimeout(topic_common.LIST_SOCKET_TIMEOUT) |
| 435 | # This RPC takes a while when there are lots of hosts. |
| 436 | # We don't set it back to default because it's the last RPC. |
mbligh | fb8f0ab | 2008-11-13 01:11:48 +0000 | [diff] [blame] | 437 | |
mbligh | 982170a | 2008-11-20 00:57:39 +0000 | [diff] [blame] | 438 | socket.setdefaulttimeout(topic_common.LIST_SOCKET_TIMEOUT) |
| 439 | # This RPC takes a while when there are lots of hosts. |
| 440 | # We don't set it back to default because it's the last RPC. |
mbligh | ae64d3a | 2008-10-15 04:13:52 +0000 | [diff] [blame] | 441 | job_id = self.execute_rpc(op='create_job', **self.data) |
| 442 | return ['%s (id %s)' % (self.jobname, job_id)] |
mbligh | be630eb | 2008-08-01 16:41:48 +0000 | [diff] [blame] | 443 | |
| 444 | |
| 445 | def get_items(self): |
| 446 | return [self.jobname] |
| 447 | |
| 448 | |
| 449 | class job_abort(job, action_common.atest_delete): |
| 450 | """atest job abort <job(s)>""" |
| 451 | usage_action = op_action = 'abort' |
| 452 | msg_done = 'Aborted' |
| 453 | |
| 454 | def parse(self): |
| 455 | (options, leftover) = self.parse_with_flist([('jobids', '', '', True)], |
| 456 | req_items='jobids') |
| 457 | |
| 458 | |
mbligh | 206d50a | 2008-11-13 01:19:25 +0000 | [diff] [blame] | 459 | def execute(self): |
| 460 | data = {'job__id__in': self.jobids} |
| 461 | self.execute_rpc(op='abort_host_queue_entries', **data) |
| 462 | print 'Aborting jobs: %s' % ', '.join(self.jobids) |
| 463 | |
| 464 | |
mbligh | be630eb | 2008-08-01 16:41:48 +0000 | [diff] [blame] | 465 | def get_items(self): |
| 466 | return self.jobids |