mbligh | e8819cd | 2008-02-15 16:48:40 +0000 | [diff] [blame] | 1 | """\ |
| 2 | Utility functions for rpc_interface.py. We keep them in a separate file so that |
| 3 | only RPC interface functions go into that file. |
| 4 | """ |
| 5 | |
| 6 | __author__ = 'showard@google.com (Steve Howard)' |
| 7 | |
showard | 14374b1 | 2009-01-31 00:11:54 +0000 | [diff] [blame] | 8 | import datetime, os |
showard | 3d6ae11 | 2009-05-02 00:45:48 +0000 | [diff] [blame] | 9 | import django.http |
mbligh | ec5546d | 2008-06-16 16:51:28 +0000 | [diff] [blame] | 10 | from frontend.afe import models, model_logic |
mbligh | e8819cd | 2008-02-15 16:48:40 +0000 | [diff] [blame] | 11 | |
showard | a62866b | 2008-07-28 21:27:41 +0000 | [diff] [blame] | 12 | NULL_DATETIME = datetime.datetime.max |
| 13 | NULL_DATE = datetime.date.max |
| 14 | |
mbligh | e8819cd | 2008-02-15 16:48:40 +0000 | [diff] [blame] | 15 | def prepare_for_serialization(objects): |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 16 | """ |
| 17 | Prepare Python objects to be returned via RPC. |
| 18 | """ |
| 19 | if (isinstance(objects, list) and len(objects) and |
| 20 | isinstance(objects[0], dict) and 'id' in objects[0]): |
| 21 | objects = gather_unique_dicts(objects) |
| 22 | return _prepare_data(objects) |
showard | b8d3424 | 2008-04-25 18:11:16 +0000 | [diff] [blame] | 23 | |
| 24 | |
showard | c92da83 | 2009-04-07 18:14:34 +0000 | [diff] [blame] | 25 | def prepare_rows_as_nested_dicts(query, nested_dict_column_names): |
| 26 | """ |
| 27 | Prepare a Django query to be returned via RPC as a sequence of nested |
| 28 | dictionaries. |
| 29 | |
| 30 | @param query - A Django model query object with a select_related() method. |
| 31 | @param nested_dict_column_names - A list of column/attribute names for the |
| 32 | rows returned by query to expand into nested dictionaries using |
| 33 | their get_object_dict() method when not None. |
| 34 | |
| 35 | @returns An list suitable to returned in an RPC. |
| 36 | """ |
| 37 | all_dicts = [] |
| 38 | for row in query.select_related(): |
| 39 | row_dict = row.get_object_dict() |
| 40 | for column in nested_dict_column_names: |
| 41 | if row_dict[column] is not None: |
| 42 | row_dict[column] = getattr(row, column).get_object_dict() |
| 43 | all_dicts.append(row_dict) |
| 44 | return prepare_for_serialization(all_dicts) |
| 45 | |
| 46 | |
showard | b8d3424 | 2008-04-25 18:11:16 +0000 | [diff] [blame] | 47 | def _prepare_data(data): |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 48 | """ |
| 49 | Recursively process data structures, performing necessary type |
| 50 | conversions to values in data to allow for RPC serialization: |
| 51 | -convert datetimes to strings |
showard | 2b9a88b | 2008-06-13 20:55:03 +0000 | [diff] [blame] | 52 | -convert tuples and sets to lists |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 53 | """ |
| 54 | if isinstance(data, dict): |
| 55 | new_data = {} |
| 56 | for key, value in data.iteritems(): |
| 57 | new_data[key] = _prepare_data(value) |
| 58 | return new_data |
showard | 2b9a88b | 2008-06-13 20:55:03 +0000 | [diff] [blame] | 59 | elif (isinstance(data, list) or isinstance(data, tuple) or |
| 60 | isinstance(data, set)): |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 61 | return [_prepare_data(item) for item in data] |
showard | 9865997 | 2008-07-17 17:00:07 +0000 | [diff] [blame] | 62 | elif isinstance(data, datetime.date): |
showard | a62866b | 2008-07-28 21:27:41 +0000 | [diff] [blame] | 63 | if data is NULL_DATETIME or data is NULL_DATE: |
| 64 | return None |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 65 | return str(data) |
| 66 | else: |
| 67 | return data |
mbligh | e8819cd | 2008-02-15 16:48:40 +0000 | [diff] [blame] | 68 | |
| 69 | |
showard | 3d6ae11 | 2009-05-02 00:45:48 +0000 | [diff] [blame] | 70 | def raw_http_response(response_data, content_type=None): |
| 71 | response = django.http.HttpResponse(response_data, mimetype=content_type) |
| 72 | response['Content-length'] = str(len(response.content)) |
| 73 | return response |
| 74 | |
| 75 | |
showard | b0dfb9f | 2008-06-06 18:08:02 +0000 | [diff] [blame] | 76 | def gather_unique_dicts(dict_iterable): |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 77 | """\ |
| 78 | Pick out unique objects (by ID) from an iterable of object dicts. |
| 79 | """ |
| 80 | id_set = set() |
| 81 | result = [] |
| 82 | for obj in dict_iterable: |
| 83 | if obj['id'] not in id_set: |
| 84 | id_set.add(obj['id']) |
| 85 | result.append(obj) |
| 86 | return result |
showard | b0dfb9f | 2008-06-06 18:08:02 +0000 | [diff] [blame] | 87 | |
| 88 | |
mbligh | e8819cd | 2008-02-15 16:48:40 +0000 | [diff] [blame] | 89 | def extra_job_filters(not_yet_run=False, running=False, finished=False): |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 90 | """\ |
| 91 | Generate a SQL WHERE clause for job status filtering, and return it in |
| 92 | a dict of keyword args to pass to query.extra(). No more than one of |
| 93 | the parameters should be passed as True. |
| 94 | """ |
| 95 | assert not ((not_yet_run and running) or |
| 96 | (not_yet_run and finished) or |
| 97 | (running and finished)), ('Cannot specify more than one ' |
| 98 | 'filter to this function') |
| 99 | if not_yet_run: |
| 100 | where = ['id NOT IN (SELECT job_id FROM host_queue_entries ' |
| 101 | 'WHERE active OR complete)'] |
| 102 | elif running: |
| 103 | where = ['(id IN (SELECT job_id FROM host_queue_entries ' |
| 104 | 'WHERE active OR complete)) AND ' |
| 105 | '(id IN (SELECT job_id FROM host_queue_entries ' |
| 106 | 'WHERE not complete OR active))'] |
| 107 | elif finished: |
| 108 | where = ['id NOT IN (SELECT job_id FROM host_queue_entries ' |
| 109 | 'WHERE not complete OR active)'] |
| 110 | else: |
| 111 | return None |
| 112 | return {'where': where} |
mbligh | e8819cd | 2008-02-15 16:48:40 +0000 | [diff] [blame] | 113 | |
| 114 | |
showard | 8e3aa5e | 2008-04-08 19:42:32 +0000 | [diff] [blame] | 115 | def extra_host_filters(multiple_labels=[]): |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 116 | """\ |
| 117 | Generate SQL WHERE clauses for matching hosts in an intersection of |
| 118 | labels. |
| 119 | """ |
| 120 | extra_args = {} |
| 121 | where_str = ('hosts.id in (select host_id from hosts_labels ' |
| 122 | 'where label_id=%s)') |
| 123 | extra_args['where'] = [where_str] * len(multiple_labels) |
| 124 | extra_args['params'] = [models.Label.smart_get(label).id |
| 125 | for label in multiple_labels] |
| 126 | return extra_args |
showard | 8e3aa5e | 2008-04-08 19:42:32 +0000 | [diff] [blame] | 127 | |
| 128 | |
showard | 43a3d26 | 2008-11-12 18:17:05 +0000 | [diff] [blame] | 129 | def get_host_query(multiple_labels, exclude_only_if_needed_labels, filter_data): |
| 130 | query = models.Host.valid_objects.all() |
| 131 | if exclude_only_if_needed_labels: |
| 132 | only_if_needed_labels = models.Label.valid_objects.filter( |
| 133 | only_if_needed=True) |
showard | f7eac6f | 2008-11-13 21:18:01 +0000 | [diff] [blame] | 134 | if only_if_needed_labels.count() > 0: |
| 135 | only_if_needed_ids = ','.join(str(label['id']) for label |
| 136 | in only_if_needed_labels.values('id')) |
| 137 | query = models.Host.objects.add_join( |
| 138 | query, 'hosts_labels', join_key='host_id', |
| 139 | join_condition='hosts_labels_exclude.label_id IN (%s)' |
| 140 | % only_if_needed_ids, |
| 141 | suffix='_exclude', exclude=True) |
showard | 43a3d26 | 2008-11-12 18:17:05 +0000 | [diff] [blame] | 142 | filter_data['extra_args'] = (extra_host_filters(multiple_labels)) |
| 143 | return models.Host.query_objects(filter_data, initial_query=query) |
| 144 | |
| 145 | |
showard | 8fd5824 | 2008-03-10 21:29:07 +0000 | [diff] [blame] | 146 | class InconsistencyException(Exception): |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 147 | 'Raised when a list of objects does not have a consistent value' |
showard | 8fd5824 | 2008-03-10 21:29:07 +0000 | [diff] [blame] | 148 | |
| 149 | |
| 150 | def get_consistent_value(objects, field): |
mbligh | c5ddfd1 | 2008-08-04 17:15:00 +0000 | [diff] [blame] | 151 | if not objects: |
| 152 | # well a list of nothing is consistent |
| 153 | return None |
| 154 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 155 | value = getattr(objects[0], field) |
| 156 | for obj in objects: |
| 157 | this_value = getattr(obj, field) |
| 158 | if this_value != value: |
| 159 | raise InconsistencyException(objects[0], obj) |
| 160 | return value |
showard | 8fd5824 | 2008-03-10 21:29:07 +0000 | [diff] [blame] | 161 | |
| 162 | |
showard | 2b9a88b | 2008-06-13 20:55:03 +0000 | [diff] [blame] | 163 | def prepare_generate_control_file(tests, kernel, label, profilers): |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 164 | test_objects = [models.Test.smart_get(test) for test in tests] |
showard | 2b9a88b | 2008-06-13 20:55:03 +0000 | [diff] [blame] | 165 | profiler_objects = [models.Profiler.smart_get(profiler) |
| 166 | for profiler in profilers] |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 167 | # ensure tests are all the same type |
| 168 | try: |
| 169 | test_type = get_consistent_value(test_objects, 'test_type') |
| 170 | except InconsistencyException, exc: |
| 171 | test1, test2 = exc.args |
mbligh | ec5546d | 2008-06-16 16:51:28 +0000 | [diff] [blame] | 172 | raise model_logic.ValidationError( |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 173 | {'tests' : 'You cannot run both server- and client-side ' |
| 174 | 'tests together (tests %s and %s differ' % ( |
| 175 | test1.name, test2.name)}) |
showard | 8fd5824 | 2008-03-10 21:29:07 +0000 | [diff] [blame] | 176 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 177 | is_server = (test_type == models.Test.Types.SERVER) |
showard | 14374b1 | 2009-01-31 00:11:54 +0000 | [diff] [blame] | 178 | if test_objects: |
| 179 | synch_count = max(test.sync_count for test in test_objects) |
| 180 | else: |
| 181 | synch_count = 1 |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 182 | if label: |
| 183 | label = models.Label.smart_get(label) |
mbligh | e8819cd | 2008-02-15 16:48:40 +0000 | [diff] [blame] | 184 | |
showard | 989f25d | 2008-10-01 11:38:11 +0000 | [diff] [blame] | 185 | dependencies = set(label.name for label |
| 186 | in models.Label.objects.filter(test__in=test_objects)) |
| 187 | |
showard | 2bab8f4 | 2008-11-12 18:15:22 +0000 | [diff] [blame] | 188 | cf_info = dict(is_server=is_server, synch_count=synch_count, |
| 189 | dependencies=list(dependencies)) |
| 190 | return cf_info, test_objects, profiler_objects, label |
showard | 989f25d | 2008-10-01 11:38:11 +0000 | [diff] [blame] | 191 | |
| 192 | |
| 193 | def check_job_dependencies(host_objects, job_dependencies): |
| 194 | """ |
| 195 | Check that a set of machines satisfies a job's dependencies. |
| 196 | host_objects: list of models.Host objects |
| 197 | job_dependencies: list of names of labels |
| 198 | """ |
| 199 | # check that hosts satisfy dependencies |
| 200 | host_ids = [host.id for host in host_objects] |
| 201 | hosts_in_job = models.Host.objects.filter(id__in=host_ids) |
| 202 | ok_hosts = hosts_in_job |
| 203 | for index, dependency in enumerate(job_dependencies): |
| 204 | ok_hosts &= models.Host.objects.filter_custom_join( |
| 205 | '_label%d' % index, labels__name=dependency) |
| 206 | failing_hosts = (set(host.hostname for host in host_objects) - |
| 207 | set(host.hostname for host in ok_hosts)) |
| 208 | if failing_hosts: |
| 209 | raise model_logic.ValidationError( |
| 210 | {'hosts' : 'Host(s) failed to meet job dependencies: ' + |
| 211 | ', '.join(failing_hosts)}) |
| 212 | |
showard | 2bab8f4 | 2008-11-12 18:15:22 +0000 | [diff] [blame] | 213 | |
| 214 | def _execution_key_for(host_queue_entry): |
| 215 | return (host_queue_entry.job.id, host_queue_entry.execution_subdir) |
| 216 | |
| 217 | |
| 218 | def check_abort_synchronous_jobs(host_queue_entries): |
| 219 | # ensure user isn't aborting part of a synchronous autoserv execution |
| 220 | count_per_execution = {} |
| 221 | for queue_entry in host_queue_entries: |
| 222 | key = _execution_key_for(queue_entry) |
| 223 | count_per_execution.setdefault(key, 0) |
| 224 | count_per_execution[key] += 1 |
| 225 | |
| 226 | for queue_entry in host_queue_entries: |
| 227 | if not queue_entry.execution_subdir: |
| 228 | continue |
| 229 | execution_count = count_per_execution[_execution_key_for(queue_entry)] |
| 230 | if execution_count < queue_entry.job.synch_count: |
| 231 | raise model_logic.ValidationError( |
| 232 | {'' : 'You cannot abort part of a synchronous job execution ' |
showard | c92da83 | 2009-04-07 18:14:34 +0000 | [diff] [blame] | 233 | '(%d/%s), %d included, %d expected' |
showard | 2bab8f4 | 2008-11-12 18:15:22 +0000 | [diff] [blame] | 234 | % (queue_entry.job.id, queue_entry.execution_subdir, |
showard | 0174619 | 2008-11-13 21:18:14 +0000 | [diff] [blame] | 235 | execution_count, queue_entry.job.synch_count)}) |
showard | 8fbae65 | 2009-01-20 23:23:10 +0000 | [diff] [blame] | 236 | |
| 237 | |
showard | c92da83 | 2009-04-07 18:14:34 +0000 | [diff] [blame] | 238 | def check_atomic_group_create_job(synch_count, host_objects, metahost_objects, |
| 239 | dependencies, atomic_group, labels_by_name): |
| 240 | """ |
| 241 | Attempt to reject create_job requests with an atomic group that |
| 242 | will be impossible to schedule. The checks are not perfect but |
| 243 | should catch the most obvious issues. |
| 244 | |
| 245 | @param synch_count - The job's minimum synch count. |
| 246 | @param host_objects - A list of models.Host instances. |
| 247 | @param metahost_objects - A list of models.Label instances. |
| 248 | @param dependencies - A list of job dependency label names. |
| 249 | @param atomic_group - The models.AtomicGroup instance. |
| 250 | @param labels_by_name - A dictionary mapping label names to models.Label |
| 251 | instance. Used to look up instances for dependencies. |
| 252 | |
| 253 | @raises model_logic.ValidationError - When an issue is found. |
| 254 | """ |
| 255 | # If specific host objects were supplied with an atomic group, verify |
| 256 | # that there are enough to satisfy the synch_count. |
| 257 | minimum_required = synch_count or 1 |
| 258 | if (host_objects and not metahost_objects and |
| 259 | len(host_objects) < minimum_required): |
| 260 | raise model_logic.ValidationError( |
| 261 | {'hosts': |
| 262 | 'only %d hosts provided for job with synch_count = %d' % |
| 263 | (len(host_objects), synch_count)}) |
| 264 | |
| 265 | # Check that the atomic group has a hope of running this job |
| 266 | # given any supplied metahosts and dependancies that may limit. |
| 267 | |
| 268 | # Get a set of hostnames in the atomic group. |
| 269 | possible_hosts = set() |
| 270 | for label in atomic_group.label_set.all(): |
| 271 | possible_hosts.update(h.hostname for h in label.host_set.all()) |
| 272 | |
| 273 | # Filter out hosts that don't match all of the job dependency labels. |
| 274 | for label_name in set(dependencies): |
| 275 | label = labels_by_name[label_name] |
| 276 | hosts_in_label = (h.hostname for h in label.host_set.all()) |
| 277 | possible_hosts.intersection_update(hosts_in_label) |
| 278 | |
showard | 225bdc1 | 2009-04-13 16:09:21 +0000 | [diff] [blame] | 279 | if not host_objects and not metahost_objects: |
| 280 | # No hosts or metahosts are required to queue an atomic group Job. |
| 281 | # However, if they are given, we respect them below. |
| 282 | host_set = possible_hosts |
| 283 | else: |
| 284 | host_set = set(host.hostname for host in host_objects) |
| 285 | unusable_host_set = host_set.difference(possible_hosts) |
| 286 | if unusable_host_set: |
| 287 | raise model_logic.ValidationError( |
| 288 | {'hosts': 'Hosts "%s" are not in Atomic Group "%s"' % |
| 289 | (', '.join(sorted(unusable_host_set)), atomic_group.name)}) |
showard | c92da83 | 2009-04-07 18:14:34 +0000 | [diff] [blame] | 290 | |
| 291 | # Lookup hosts provided by each meta host and merge them into the |
| 292 | # host_set for final counting. |
| 293 | for meta_host in metahost_objects: |
| 294 | meta_possible = possible_hosts.copy() |
| 295 | hosts_in_meta_host = (h.hostname for h in meta_host.host_set.all()) |
| 296 | meta_possible.intersection_update(hosts_in_meta_host) |
| 297 | |
| 298 | # Count all hosts that this meta_host will provide. |
| 299 | host_set.update(meta_possible) |
| 300 | |
| 301 | if len(host_set) < minimum_required: |
| 302 | raise model_logic.ValidationError( |
| 303 | {'atomic_group_name': |
| 304 | 'Insufficient hosts in Atomic Group "%s" with the' |
| 305 | ' supplied dependencies and meta_hosts.' % |
| 306 | (atomic_group.name,)}) |
| 307 | |
| 308 | |
showard | 8fbae65 | 2009-01-20 23:23:10 +0000 | [diff] [blame] | 309 | def get_motd(): |
| 310 | dirname = os.path.dirname(__file__) |
| 311 | filename = os.path.join(dirname, "..", "..", "motd.txt") |
| 312 | text = '' |
| 313 | try: |
| 314 | fp = open(filename, "r") |
| 315 | try: |
| 316 | text = fp.read() |
| 317 | finally: |
| 318 | fp.close() |
| 319 | except: |
| 320 | pass |
| 321 | |
| 322 | return text |
showard | 29f7cd2 | 2009-04-29 21:16:24 +0000 | [diff] [blame] | 323 | |
| 324 | |
| 325 | def _get_metahost_counts(metahost_objects): |
| 326 | metahost_counts = {} |
| 327 | for metahost in metahost_objects: |
| 328 | metahost_counts.setdefault(metahost, 0) |
| 329 | metahost_counts[metahost] += 1 |
| 330 | return metahost_counts |
| 331 | |
| 332 | |
showard | 4d07756 | 2009-05-08 18:24:36 +0000 | [diff] [blame] | 333 | def get_job_info(job, preserve_metahosts=False, queue_entry_ids=None): |
showard | 29f7cd2 | 2009-04-29 21:16:24 +0000 | [diff] [blame] | 334 | hosts = [] |
| 335 | one_time_hosts = [] |
| 336 | meta_hosts = [] |
| 337 | atomic_group = None |
| 338 | |
showard | 4d07756 | 2009-05-08 18:24:36 +0000 | [diff] [blame] | 339 | queue_entries = job.hostqueueentry_set.all() |
| 340 | if queue_entry_ids is not None: |
| 341 | queue_entries = queue_entries.filter(id__in=queue_entry_ids) |
| 342 | |
| 343 | for queue_entry in queue_entries: |
showard | 29f7cd2 | 2009-04-29 21:16:24 +0000 | [diff] [blame] | 344 | if (queue_entry.host and (preserve_metahosts or |
| 345 | not queue_entry.meta_host)): |
| 346 | if queue_entry.deleted: |
| 347 | continue |
| 348 | if queue_entry.host.invalid: |
| 349 | one_time_hosts.append(queue_entry.host) |
| 350 | else: |
| 351 | hosts.append(queue_entry.host) |
| 352 | else: |
| 353 | meta_hosts.append(queue_entry.meta_host) |
| 354 | if atomic_group is None: |
| 355 | if queue_entry.atomic_group is not None: |
| 356 | atomic_group = queue_entry.atomic_group |
| 357 | else: |
| 358 | assert atomic_group.name == queue_entry.atomic_group.name, ( |
| 359 | 'DB inconsistency. HostQueueEntries with multiple atomic' |
| 360 | ' groups on job %s: %s != %s' % ( |
| 361 | id, atomic_group.name, queue_entry.atomic_group.name)) |
| 362 | |
| 363 | meta_host_counts = _get_metahost_counts(meta_hosts) |
| 364 | |
| 365 | info = dict(dependencies=[label.name for label |
| 366 | in job.dependency_labels.all()], |
| 367 | hosts=hosts, |
| 368 | meta_hosts=meta_hosts, |
| 369 | meta_host_counts=meta_host_counts, |
| 370 | one_time_hosts=one_time_hosts, |
| 371 | atomic_group=atomic_group) |
| 372 | return info |
| 373 | |
| 374 | |
| 375 | def create_new_job(owner, host_objects, metahost_objects, |
| 376 | name, priority, control_file, control_type, |
| 377 | is_template=False, timeout=None, synch_count=None, |
| 378 | run_verify=True, email_list='', dependencies=[], |
| 379 | reboot_before=None, reboot_after=None, atomic_group=None): |
| 380 | labels_by_name = dict((label.name, label) |
| 381 | for label in models.Label.objects.all()) |
| 382 | all_host_objects = host_objects + metahost_objects |
| 383 | metahost_counts = _get_metahost_counts(metahost_objects) |
| 384 | |
| 385 | # check that each metahost request has enough hosts under the label |
| 386 | for label, requested_count in metahost_counts.iteritems(): |
| 387 | available_count = label.host_set.count() |
| 388 | if requested_count > available_count: |
| 389 | error = ("You have requested %d %s's, but there are only %d." |
| 390 | % (requested_count, label.name, available_count)) |
| 391 | raise model_logic.ValidationError({'meta_hosts' : error}) |
| 392 | |
| 393 | if atomic_group: |
| 394 | check_atomic_group_create_job( |
| 395 | synch_count, host_objects, metahost_objects, |
| 396 | dependencies, atomic_group, labels_by_name) |
| 397 | else: |
| 398 | if synch_count is not None and synch_count > len(all_host_objects): |
| 399 | raise model_logic.ValidationError( |
| 400 | {'hosts': |
| 401 | 'only %d hosts provided for job with synch_count = %d' % |
| 402 | (len(all_host_objects), synch_count)}) |
| 403 | atomic_hosts = models.Host.objects.filter( |
| 404 | id__in=[host.id for host in host_objects], |
| 405 | labels__atomic_group=True) |
| 406 | unusable_host_names = [host.hostname for host in atomic_hosts] |
| 407 | if unusable_host_names: |
| 408 | raise model_logic.ValidationError( |
| 409 | {'hosts': |
| 410 | 'Host(s) "%s" are atomic group hosts but no ' |
| 411 | 'atomic group was specified for this job.' % |
| 412 | (', '.join(unusable_host_names),)}) |
| 413 | |
| 414 | |
| 415 | check_job_dependencies(host_objects, dependencies) |
| 416 | dependency_labels = [labels_by_name[label_name] |
| 417 | for label_name in dependencies] |
| 418 | |
| 419 | for label in metahost_objects + dependency_labels: |
| 420 | if label.atomic_group and not atomic_group: |
| 421 | raise model_logic.ValidationError( |
| 422 | {'atomic_group_name': |
| 423 | 'Some meta_hosts or dependencies require an atomic group ' |
| 424 | 'but no atomic_group_name was specified for this job.'}) |
| 425 | elif (label.atomic_group and |
| 426 | label.atomic_group.name != atomic_group.name): |
| 427 | raise model_logic.ValidationError( |
| 428 | {'atomic_group_name': |
| 429 | 'Some meta_hosts or dependencies require an atomic group ' |
| 430 | 'other than the one requested for this job.'}) |
| 431 | |
| 432 | job = models.Job.create(owner=owner, name=name, priority=priority, |
| 433 | control_file=control_file, |
| 434 | control_type=control_type, |
| 435 | synch_count=synch_count, |
| 436 | hosts=all_host_objects, |
| 437 | timeout=timeout, |
| 438 | run_verify=run_verify, |
| 439 | email_list=email_list.strip(), |
| 440 | dependencies=dependency_labels, |
| 441 | reboot_before=reboot_before, |
| 442 | reboot_after=reboot_after) |
| 443 | job.queue(all_host_objects, atomic_group=atomic_group, |
| 444 | is_template=is_template) |
| 445 | return job.id |