Package backend :: Package server :: Package rhnServer :: Module server_kickstart
[hide private]
[frames] | no frames]

Source Code for Module backend.server.rhnServer.server_kickstart

  1  # 
  2  # Copyright (c) 2008--2016 Red Hat, Inc. 
  3  # 
  4  # This software is licensed to you under the GNU General Public License, 
  5  # version 2 (GPLv2). There is NO WARRANTY for this software, express or 
  6  # implied, including the implied warranties of MERCHANTABILITY or FITNESS 
  7  # FOR A PARTICULAR PURPOSE. You should have received a copy of GPLv2 
  8  # along with this software; if not, see 
  9  # http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt. 
 10  # 
 11  # Red Hat trademarks are not licensed under GPLv2. No permission is 
 12  # granted to use or replicate Red Hat trademarks that are incorporated 
 13  # in this software or its documentation. 
 14  # 
 15  # 
 16  # Kickstart-related operations 
 17  # 
 18   
 19  import sys 
 20   
 21  from spacewalk.common import rhnFlags 
 22  from spacewalk.common.rhnLog import log_debug, log_error 
 23  from spacewalk.common.rhnException import rhnException 
 24  from spacewalk.server import rhnSQL, rhnAction, rhnLib, rhnChannel 
 25   
 26   
27 -def update_kickstart_session(server_id, action_id, action_status, 28 kickstart_state, next_action_type):
29 log_debug(3, server_id, action_id, action_status, kickstart_state, next_action_type) 30 31 # Is this a kickstart-related action? 32 ks_session_id = get_kickstart_session_id(server_id, action_id) 33 if ks_session_id is None: 34 # Nothing more to do 35 log_debug(4, "Kickstart session not found") 36 return None 37 38 # Check the current action state 39 if action_status == 2: 40 # Completed 41 ks_status = kickstart_state 42 # Get the next action - it has to be of the right type 43 next_action_id = get_next_action_id(action_id, next_action_type) 44 elif action_status == 3: 45 # Failed 46 ks_status = 'failed' 47 next_action_id = None 48 else: 49 raise rhnException("Invalid action state %s" % action_status) 50 51 update_ks_session_table(ks_session_id, ks_status, next_action_id, 52 server_id) 53 return ks_session_id
54 55 _query_update_ks_session_table = rhnSQL.Statement(""" 56 update rhnKickstartSession 57 set action_id = :action_id, 58 state_id = :ks_status_id, 59 new_server_id = :server_id 60 where id = :ks_session_id 61 """) 62 63
64 -def update_ks_session_table(ks_session_id, ks_status, next_action_id, 65 server_id):
66 log_debug(4, ks_session_id, ks_status, next_action_id, server_id) 67 ks_table = rhnSQL.Table('rhnKickstartSessionState', 'label') 68 ks_status_id = ks_table[ks_status]['id'] 69 70 h = rhnSQL.prepare(_query_update_ks_session_table) 71 h.execute(ks_session_id=ks_session_id, ks_status_id=ks_status_id, 72 action_id=next_action_id, server_id=server_id) 73 74 if ks_status == 'complete': 75 delete_guests(server_id)
76 77 _query_lookup_guests_for_host = rhnSQL.Statement(""" 78 select virtual_system_id from rhnVirtualInstance 79 where host_system_id = :server_id 80 """) 81 _query_delete_virtual_instances = rhnSQL.Statement(""" 82 delete from rhnVirtualInstance where host_system_id = :server_id 83 """) 84 85
86 -def delete_guests(server_id):
87 """ 88 Callback used after a successful kickstart to remove any guest virtual 89 instances, as well as their associated servers. 90 """ 91 # First delete all the guest server objects: 92 h = rhnSQL.prepare(_query_lookup_guests_for_host) 93 h.execute(server_id=server_id) 94 delete_server = rhnSQL.Procedure("delete_server") 95 log_debug(4, "Deleting guests") 96 while 1: 97 row = h.fetchone_dict() 98 if not row: 99 break 100 guest_id = row['virtual_system_id'] 101 log_debug(4, 'Deleting guest server: %s' % guest_id) 102 try: 103 if guest_id is not None: 104 delete_server(guest_id) 105 except rhnSQL.SQLError: 106 log_error("Error deleting server: %s" % guest_id) 107 108 # Finally delete all the virtual instances: 109 log_debug(4, "Deleting all virtual instances for host") 110 h = rhnSQL.prepare(_query_delete_virtual_instances) 111 h.execute(server_id=server_id) 112 113 # Commit all changes: 114 try: 115 rhnSQL.commit() 116 except rhnSQL.SQLError: 117 e = sys.exc_info()[1] 118 log_error("Error committing transaction: %s" % e) 119 rhnSQL.rollback()
120 121 122 _query_get_next_action_id = rhnSQL.Statement(""" 123 select a.id 124 from rhnAction a, rhnActionType at 125 where a.prerequisite = :action_id 126 and a.action_type = at.id 127 and at.label = :next_action_type 128 """) 129 130
131 -def get_next_action_id(action_id, next_action_type=None):
132 if not next_action_type: 133 return None 134 h = rhnSQL.prepare(_query_get_next_action_id) 135 h.execute(action_id=action_id, next_action_type=next_action_type) 136 row = h.fetchone_dict() 137 if not row: 138 return None 139 return row['id']
140 141 _query_lookup_kickstart_session_id = rhnSQL.Statement(""" 142 select ks.id 143 from rhnKickstartSession ks 144 where ( 145 (ks.old_server_id = :server_id and ks.new_server_id is NULL) 146 or ks.new_server_id = :server_id 147 or ks.host_server_id = :server_id 148 ) 149 and ks.action_id = :action_id 150 """) 151 152
153 -def get_kickstart_session_id(server_id, action_id):
154 h = rhnSQL.prepare(_query_lookup_kickstart_session_id) 155 h.execute(server_id=server_id, action_id=action_id) 156 157 row = h.fetchone_dict() 158 if not row: 159 # Nothing to do 160 return None 161 return row['id']
162 163 164 _query_insert_package_delta = rhnSQL.Statement(""" 165 insert into rhnPackageDelta (id, label) 166 values (:package_delta_id, 'ks-delta-' || :package_delta_id) 167 """) 168 _query_insert_action_package_delta = rhnSQL.Statement(""" 169 insert into rhnActionPackageDelta (action_id, package_delta_id) 170 values (:action_id, :package_delta_id) 171 """) 172 _query_insert_package_delta_element = rhnSQL.Statement(""" 173 insert into rhnPackageDeltaElement 174 (package_delta_id, transaction_package_id) 175 values 176 (:package_delta_id, 177 lookup_transaction_package(:operation, :n, :e, :v, :r, :a)) 178 """) 179 180
181 -def schedule_kickstart_delta(server_id, kickstart_session_id, 182 installs, removes):
183 log_debug(3, server_id, kickstart_session_id) 184 row = get_kickstart_session_info(kickstart_session_id, server_id) 185 org_id = row['org_id'] 186 scheduler = row['scheduler'] 187 188 action_id = rhnAction.schedule_server_action( 189 server_id, 190 action_type='packages.runTransaction', action_name="Package delta", 191 delta_time=0, scheduler=scheduler, org_id=org_id, 192 ) 193 194 package_delta_id = rhnSQL.Sequence('rhn_packagedelta_id_seq').next() 195 196 h = rhnSQL.prepare(_query_insert_package_delta) 197 h.execute(package_delta_id=package_delta_id) 198 199 h = rhnSQL.prepare(_query_insert_action_package_delta) 200 h.execute(action_id=action_id, package_delta_id=package_delta_id) 201 202 h = rhnSQL.prepare(_query_insert_package_delta_element) 203 col_names = ['n', 'v', 'r', 'e'] 204 __execute_many(h, installs, col_names, operation='insert', a=None, 205 package_delta_id=package_delta_id) 206 __execute_many(h, removes, col_names, operation='delete', a=None, 207 package_delta_id=package_delta_id) 208 209 update_ks_session_table(kickstart_session_id, 'package_synch_scheduled', 210 action_id, server_id) 211 212 return action_id
213 214
215 -def schedule_kickstart_sync(server_id, kickstart_session_id):
216 row = get_kickstart_session_info(kickstart_session_id, server_id) 217 org_id = row['org_id'] 218 scheduler = row['scheduler'] 219 220 # Create a new action 221 action_id = rhnAction.schedule_server_action( 222 server_id, 223 action_type='kickstart.schedule_sync', 224 action_name="Schedule a package sync", 225 delta_time=0, scheduler=scheduler, org_id=org_id, 226 ) 227 return action_id
228 229
230 -def _get_ks_virt_type(type_id):
231 _query_kickstart_virt_type = rhnSQL.Statement(""" 232 select label 233 from rhnKickstartVirtualizationType kvt 234 where kvt.id = :id 235 """) 236 prepared_query = rhnSQL.prepare(_query_kickstart_virt_type) 237 prepared_query.execute(id=type_id) 238 row = prepared_query.fetchone_dict() 239 240 # XXX: we should have better constraints on the db so this doesn't happen. 241 if not row: 242 kstype = 'auto' 243 else: 244 kstype = row['label'] 245 log_debug(1, "KS_TYPE: %s" % kstype) 246 return kstype
247 248
249 -def get_kickstart_session_type(server_id, action_id):
250 ks_session_id = get_kickstart_session_id(server_id, action_id) 251 ks_session_info = get_kickstart_session_info(ks_session_id, server_id) 252 ks_type_id = ks_session_info['virtualization_type'] 253 ks_type = _get_ks_virt_type(ks_type_id) 254 return ks_type
255 256
257 -def subscribe_to_tools_channel(server_id, kickstart_session_id):
258 log_debug(3) 259 row = get_kickstart_session_info(kickstart_session_id, server_id) 260 org_id = row['org_id'] 261 scheduler = row['scheduler'] 262 ks_type_id = row['virtualization_type'] 263 ks_type = _get_ks_virt_type(ks_type_id) 264 265 if ks_type == 'para_host': 266 action_id = rhnAction.schedule_server_action( 267 server_id, 268 action_type='kickstart_host.add_tools_channel', 269 action_name='Subscribe server to RHN Tools channel.', 270 delta_time=0, scheduler=scheduler, org_id=org_id, 271 ) 272 elif ks_type == 'para_guest': 273 action_id = rhnAction.schedule_server_action( 274 server_id, 275 action_type='kickstart_guest.add_tools_channel', 276 action_name='Subscribe guest to RHN Tools channel.', 277 delta_time=0, scheduler=scheduler, org_id=org_id, 278 ) 279 else: 280 action_id = None 281 return action_id
282 283
284 -def schedule_virt_pkg_install(server_id, kickstart_session_id):
285 log_debug(3) 286 row = get_kickstart_session_info(kickstart_session_id, server_id) 287 org_id = row['org_id'] 288 scheduler = row['scheduler'] 289 ks_type_id = row['virtualization_type'] 290 log_debug(1, "VIRTUALIZATION_TYPE: %s" % str(ks_type_id)) 291 ks_type = _get_ks_virt_type(ks_type_id) 292 log_debug(1, "VIRTUALZIATION_TYPE_LABEL: %s" % str(ks_type)) 293 294 if ks_type == 'para_host': 295 log_debug(1, "SCHEDULING VIRT HOST PACKAGE INSTALL...") 296 action_id = rhnAction.schedule_server_action( 297 server_id, 298 action_type='kickstart_host.schedule_virt_host_pkg_install', 299 action_name="Schedule install of rhn-virtualization-host package.", 300 delta_time=0, scheduler=scheduler, org_id=org_id, 301 ) 302 elif ks_type == 'para_guest': 303 log_debug(1, "SCHEDULING VIRT GUEST PACKAGE INSTALL...") 304 action_id = rhnAction.schedule_server_action( 305 server_id, 306 action_type='kickstart_guest.schedule_virt_guest_pkg_install', 307 action_name="Schedule install of rhn-virtualization-guest package.", 308 delta_time=0, scheduler=scheduler, org_id=org_id, 309 ) 310 else: 311 log_debug(1, "NOT A VIRT KICKSTART") 312 action_id = None 313 314 return action_id
315 316 _query_ak_deploy_config = rhnSQL.Statement(""" 317 select rt.deploy_configs 318 from rhnKickstartSession ks, 319 rhnKickstartDefaultRegToken kdrt, 320 rhnRegToken rt 321 where ks.kickstart_id = kdrt.kickstart_id 322 and kdrt.regtoken_id = rt.id 323 and ks.id = :session_id 324 """) 325 # Make sure the activation keys associated with this kickstart profile 326 # have enabled deploying config files. Only deploy configs if at least one 327 # of them has. This is replacing code that didn't work because the 328 # rhnFlags('registration_token') could not be set during the rhn_check call. 329 330
331 -def ks_activation_key_deploy_config(kickstart_session_id):
332 h = rhnSQL.prepare(_query_ak_deploy_config) 333 h.execute(session_id=kickstart_session_id) 334 rows = h.fetchall_dict() 335 if rows: 336 for row in rows: 337 if row['deploy_configs'] and row['deploy_configs'] == 'Y': 338 return True 339 return False
340 341 _query_schedule_config_files = rhnSQL.Statement(""" 342 insert into rhnActionConfigRevision 343 (id, action_id, server_id, config_revision_id) 344 select sequence_nextval('rhn_actioncr_id_seq'), :action_id, 345 server_id, config_revision_id 346 from ( 347 select distinct scc.server_id, 348 cf.latest_config_revision_id config_revision_id 349 from rhnServerConfigChannel scc, 350 rhnConfigChannelType cct, 351 rhnConfigChannel cc, 352 rhnConfigFile cf, 353 rhnConfigFileState cfs 354 where scc.server_id = :server_id 355 and scc.config_channel_id = cf.config_channel_id 356 and cf.config_channel_id = cc.id 357 and cc.confchan_type_id = cct.id 358 and cct.label in ('normal', 'local_override') 359 and cf.latest_config_revision_id is not null 360 and cf.state_id = cfs.id 361 and cfs.label = 'alive' 362 ) X 363 """) 364 365
366 -def schedule_config_deploy(server_id, action_id, kickstart_session_id, 367 server_profile):
368 """ schedule a configfiles.deploy action dependent on the current action """ 369 log_debug(3, server_id, action_id, kickstart_session_id) 370 row = get_kickstart_session_info(kickstart_session_id, server_id) 371 org_id = row['org_id'] 372 scheduler = row['scheduler'] 373 deploy_configs = (row['deploy_configs'] == 'Y' 374 and ks_activation_key_deploy_config(kickstart_session_id)) 375 376 if not deploy_configs: 377 # Nothing more to do here 378 update_ks_session_table(kickstart_session_id, 'complete', 379 next_action_id=None, server_id=server_id) 380 return None 381 382 if server_profile: 383 # Have to schedule a package deploy action 384 aid = schedule_rhncfg_install(server_id, action_id, scheduler, 385 server_profile) 386 else: 387 aid = action_id 388 389 next_action_id = rhnAction.schedule_server_action( 390 server_id, 391 action_type='configfiles.deploy', 392 action_name='Deploy config files', 393 delta_time=0, scheduler=scheduler, org_id=org_id, 394 prerequisite=aid, 395 ) 396 # Deploy all of the config files that are part of this server's config 397 # channels 398 h = rhnSQL.prepare(_query_schedule_config_files) 399 h.execute(server_id=server_id, action_id=next_action_id) 400 update_ks_session_table(kickstart_session_id, 'configuration_deploy', 401 next_action_id, server_id) 402 return next_action_id
403 404
405 -class MissingBaseChannelError(Exception):
406 pass
407 408
409 -def schedule_rhncfg_install(server_id, action_id, scheduler, 410 server_profile=None):
411 capability = 'rhn-config-action' 412 try: 413 packages = _subscribe_server_to_capable_channels(server_id, scheduler, 414 capability) 415 except MissingBaseChannelError: 416 log_debug(2, "No base channel", server_id) 417 return action_id 418 419 if not packages: 420 # No channels offer this capability 421 log_debug(3, server_id, action_id, 422 "No channels to provide %s found" % capability) 423 # No new action needed here 424 return action_id 425 426 if not server_profile: 427 server_profile = get_server_package_profile(server_id) 428 429 # Make the package profile a hash, for easier checking 430 sphash = {} 431 for p in server_profile: 432 sphash[tuple(p)] = None 433 434 packages_to_install = [] 435 for p in packages: 436 key = (p['name'], p['version'], p['release'], p['epoch']) 437 if key not in sphash: 438 packages_to_install.append(p['package_id']) 439 440 if not packages_to_install: 441 # We already have these packages installed 442 log_debug(4, "No packages needed to be installed") 443 return action_id 444 445 log_debug(4, "Scheduling package install action") 446 new_action_id = schedule_package_install(server_id, action_id, scheduler, 447 packages_to_install) 448 return new_action_id
449 450 _query_lookup_subscribed_server_channels = rhnSQL.Statement(""" 451 select sc.channel_id, 452 case when c.parent_channel is not null then 0 else 1 end is_base_channel 453 from rhnServerChannel sc, rhnChannel c 454 where sc.server_id = :server_id 455 and sc.channel_id = c.id 456 """) 457 _query_lookup_unsubscribed_server_channels = rhnSQL.Statement(""" 458 select c.id 459 from 460 -- Get all the channels available to this org 461 ( select cfm.channel_id 462 from rhnChannelFamilyMembers cfm, 463 rhnPrivateChannelFamily pcf 464 where pcf.org_id = :org_id 465 and pcf.channel_family_id = cfm.channel_family_id 466 union 467 select cfm.channel_id 468 from rhnChannelFamilyMembers cfm, 469 rhnPublicChannelFamily pcf 470 where pcf.channel_family_id = cfm.channel_family_id) ac, 471 rhnChannel c 472 where c.parent_channel = :base_channel_id 473 and c.id = ac.channel_id 474 and not exists ( 475 select 1 476 from rhnServerChannel 477 where server_id = :server_id 478 and channel_id = c.id) 479 """) 480 481
482 -def _subscribe_server_to_capable_channels(server_id, scheduler, capability):
483 log_debug(4, server_id, scheduler, capability) 484 # Look through the channels this server is already subscribed to 485 h = rhnSQL.prepare(_query_lookup_subscribed_server_channels) 486 h.execute(server_id=server_id) 487 base_channel_id = None 488 channels = [] 489 while 1: 490 row = h.fetchone_dict() 491 if not row: 492 break 493 channel_id = row['channel_id'] 494 if row['is_base_channel']: 495 base_channel_id = channel_id 496 channels.append((channel_id, 1)) 497 if base_channel_id is None: 498 raise MissingBaseChannelError() 499 500 org_id = rhnSQL.Table('rhnServer', 'id')[server_id]['org_id'] 501 502 # Get the child channels this system is *not* subscribed to 503 h = rhnSQL.prepare(_query_lookup_unsubscribed_server_channels) 504 h.execute(server_id=server_id, org_id=org_id, 505 base_channel_id=base_channel_id) 506 l = [(x['id'], 0) for x in h.fetchall_dict() or []] 507 channels.extend(l) 508 # We now have a list of channels; look for one that provides the 509 # capability 510 for channel_id, is_subscribed in channels: 511 log_debug(5, "Checking channel:", channel_id, "; subscribed:", 512 is_subscribed) 513 packages = _channel_provides_capability(channel_id, capability) 514 if packages: 515 if is_subscribed: 516 log_debug(4, "Already subscribed; found packages", packages) 517 return packages 518 519 # Try to subscribe to it 520 rhnChannel.subscribe_sql(server_id, channel_id, 0) 521 log_debug(4, "Subscribed to", channel_id, 522 "Found packages", packages) 523 # We subscribed to this channel - we're done 524 return packages 525 526 # No channels provide this capability - we're done 527 log_debug(4, "No channels to provide capability", capability) 528 return None
529 530 _query_channel_provides_capability = rhnSQL.Statement(""" 531 select distinct pp.package_id, pn.name, pe.version, pe.release, pe.epoch 532 from rhnChannelNewestPackage cnp, 533 rhnPackageProvides pp, 534 rhnPackageCapability pc, 535 rhnPackageName pn, 536 rhnPackageEVR pe 537 where cnp.channel_id = :channel_id 538 and cnp.package_id = pp.package_id 539 and pp.capability_id = pc.id 540 and pc.name = :capability 541 and cnp.name_id = pn.id 542 and cnp.evr_id = pe.id 543 """) 544 545
546 -def _channel_provides_capability(channel_id, capability):
547 log_debug(4, channel_id, capability) 548 h = rhnSQL.prepare(_query_channel_provides_capability) 549 h.execute(channel_id=channel_id, capability=capability) 550 ret = h.fetchall_dict() 551 if not ret: 552 return ret 553 return ret
554 555 _query_insert_action_packages = rhnSQL.Statement(""" 556 insert into rhnActionPackage 557 (id, action_id, name_id, evr_id, package_arch_id, parameter) 558 select sequence_nextval('rhn_act_p_id_seq'), :action_id, name_id, evr_id, 559 package_arch_id, 'upgrade' 560 from rhnPackage 561 where id = :package_id 562 """) 563 564
565 -def schedule_package_install(server_id, action_id, scheduler, packages):
566 if not packages: 567 # Nothing to do 568 return action_id 569 new_action_id = rhnAction.schedule_server_action( 570 server_id, action_type='packages.update', 571 action_name="Package update to enable configuration deployment", 572 delta_time=0, scheduler=scheduler, prerequisite=action_id, 573 ) 574 # Add entries to rhnActionPackage 575 action_ids = [new_action_id] * len(packages) 576 h = rhnSQL.prepare(_query_insert_action_packages) 577 h.executemany(action_id=action_ids, package_id=packages) 578 return new_action_id
579 580
581 -def __execute_many(cursor, array, col_names, **kwargs):
582 """ Execute the cursor, with arguments extracted from the array 583 The array is converted into a hash having col_names as keys, and adds 584 whatever kwarg was specified too. 585 """ 586 linecount = len(array) 587 if not linecount: 588 return 589 # Transpose the array into a hash with col_names as keys 590 params = rhnLib.transpose_to_hash(array, col_names) 591 for k, v in kwargs.items(): 592 params[k] = [v] * linecount 593 594 cursor.executemany(**params)
595 596
597 -def _packages_from_cursor(cursor):
598 result = [] 599 while 1: 600 row = cursor.fetchone_dict() 601 if not row: 602 break 603 p_name = row['name'] 604 if p_name == 'gpg-pubkey': 605 # We ignore GPG public keys since they are too weird to schedule 606 # as a package delta 607 continue 608 result.append((p_name, row['version'], row['release'], row['epoch'])) 609 return result
610 611 _query_lookup_pending_kickstart_sessions = rhnSQL.Statement(""" 612 select ks.id, ks.action_id, NULL other_server_id 613 from rhnKickstartSessionState kss, 614 rhnKickstartSession ks 615 where ( 616 (ks.old_server_id = :server_id and ks.new_server_id is null) 617 or ks.new_server_id = :server_id 618 ) 619 and ks.state_id = kss.id 620 and kss.label not in ('complete', 'failed') 621 and (:ks_session_id is null or ks.id != :ks_session_id) 622 """) 623 624 _query_terminate_pending_kickstart_sessions = rhnSQL.Statement(""" 625 update rhnKickstartSession 626 set action_id = NULL, 627 state_id = :state_id 628 where id = :kickstart_session_id 629 """) 630 631
632 -def terminate_kickstart_sessions(server_id):
633 log_debug(3, server_id) 634 history = [] 635 tokens_obj = rhnFlags.get('registration_token') 636 current_ks_session_id = tokens_obj.get_kickstart_session_id() 637 # ks_session_id can be null 638 h = rhnSQL.prepare(_query_lookup_pending_kickstart_sessions) 639 h.execute(server_id=server_id, ks_session_id=current_ks_session_id) 640 log_debug(4, "current_ks_session_id", current_ks_session_id) 641 ks_session_ids = [] 642 action_ids = [] 643 while 1: 644 row = h.fetchone_dict() 645 if not row: 646 break 647 ks_session_ids.append(row['id']) 648 action_ids.append(row['action_id']) 649 650 if not ks_session_ids: 651 # Nothing to do 652 log_debug(4, "Nothing to do", server_id, current_ks_session_id) 653 return [] 654 655 ks_session_table = rhnSQL.Table('rhnKickstartSessionState', 'label') 656 state_id_failed = ks_session_table['failed']['id'] 657 state_ids = [state_id_failed] * len(ks_session_ids) 658 659 # Add a history item 660 for ks_session_id in ks_session_ids: 661 log_debug(4, "Adding history entry for session id", ks_session_id) 662 history.append(("Kickstart session canceled", 663 "A kickstart session for this system was canceled because " 664 "the system was re-registered with token <strong>%s</strong>" % 665 tokens_obj.get_names())) 666 667 h = rhnSQL.prepare(_query_terminate_pending_kickstart_sessions) 668 669 params = { 670 'kickstart_session_id': ks_session_ids, 671 'state_id': state_ids, 672 } 673 # Terminate pending actions 674 log_debug(4, "Terminating sessions", params) 675 h.execute_bulk(params) 676 677 # Invalidate pending actions 678 for action_id in action_ids: 679 if action_id is None: 680 continue 681 rhnAction.invalidate_action(server_id, action_id) 682 return history
683 684
685 -def get_kickstart_profile_package_profile(kickstart_session_id):
686 """ Fetches the package profile from the kickstart profile (Not the session) """ 687 h = rhnSQL.prepare(""" 688 select pn.name, pe.version, pe.release, pe.epoch, pa.label 689 from rhnKickstartSession ks, 690 rhnKickstartDefaults kd, 691 rhnServerProfilePackage spp, 692 rhnPackageName pn, 693 rhnPackageEVR pe, 694 rhnPackageArch pa 695 where ks.id = :kickstart_session_id 696 and kd.server_profile_id = spp.server_profile_id 697 and spp.name_id = pn.id 698 and spp.evr_id = pe.id 699 and spp.package_arch_id = pa.id 700 and kd.kickstart_id = ks.kickstart_id 701 """) 702 h.execute(kickstart_session_id=kickstart_session_id) 703 return _packages_from_cursor(h)
704 705
706 -def get_kisckstart_session_package_profile(kickstart_session_id):
707 """ Fetches the package profile from the kickstart session """ 708 h = rhnSQL.prepare(""" 709 select pn.name, pe.version, pe.release, pe.epoch, pa.label 710 from rhnKickstartSession ks, 711 rhnServerProfilePackage spp, 712 rhnPackageName pn, 713 rhnPackageEVR pe, 714 rhnPackageArch pa 715 where ks.id = :kickstart_session_id 716 and ks.server_profile_id = spp.server_profile_id 717 and spp.name_id = pn.id 718 and spp.evr_id = pe.id 719 and spp.package_arch_id = pa.id 720 """) 721 h.execute(kickstart_session_id=kickstart_session_id) 722 return _packages_from_cursor(h)
723 724
725 -def get_server_package_profile(server_id):
726 # XXX misa 2005-05-25 May need to look at package arches too 727 h = rhnSQL.prepare(""" 728 select pn.name, pe.version, pe.release, pe.epoch, pa.label 729 from rhnServerPackage sp, 730 rhnPackageName pn, 731 rhnPackageEVR pe, 732 rhnPackageArch pa 733 where sp.server_id = :server_id 734 and sp.name_id = pn.id 735 and sp.evr_id = pe.id 736 and sp.package_arch_id = pa.id 737 """) 738 h.execute(server_id=server_id) 739 return _packages_from_cursor(h)
740 741 _query_get_kickstart_session_info = rhnSQL.Statement(""" 742 select org_id, scheduler, deploy_configs, virtualization_type 743 from rhnKickstartSession 744 where id = :kickstart_session_id 745 """) 746 747
748 -def get_kickstart_session_info(kickstart_session_id, server_id):
749 h = rhnSQL.prepare(_query_get_kickstart_session_info) 750 h.execute(kickstart_session_id=kickstart_session_id) 751 row = h.fetchone_dict() 752 if not row: 753 raise rhnException("Could not fetch kickstart session id %s " 754 "for server %s" % (kickstart_session_id, server_id)) 755 756 return row
757 758 _query_lookup_ks_server_profile = rhnSQL.Statement(""" 759 select kss.server_profile_id 760 from rhnServerProfileType spt, 761 rhnServerProfile sp, 762 rhnKickstartSession kss 763 where kss.id = :ks_session_id 764 and kss.server_profile_id = sp.id 765 and sp.profile_type_id = spt.id 766 and spt.label = :profile_type_label 767 """) 768 _query_delete_server_profile = rhnSQL.Statement(""" 769 delete from rhnServerProfile where id = :server_profile_id 770 """) 771 772
773 -def cleanup_profile(server_id, action_id, ks_session_id, action_status):
774 if ks_session_id is None: 775 log_debug(4, "No kickstart session") 776 return 777 if action_status != 2: 778 log_debug(4, "Action status: %s; nothing to do" % action_status) 779 return 780 781 h = rhnSQL.prepare(_query_lookup_ks_server_profile) 782 h.execute(ks_session_id=ks_session_id, profile_type_label='sync_profile') 783 row = h.fetchone_dict() 784 if not row: 785 log_debug(4, "No server profile of the right type found; nothing to do") 786 return 787 788 server_profile_id = row['server_profile_id'] 789 if server_profile_id is None: 790 log_debug(4, "No server profile associated with this kickstart session") 791 return 792 793 # There is an "on delete cascade" constraint on 794 # rhnKickstartSession.server_profile_id and on 795 # rhnServerProfilePacakge.server_profile_id 796 h = rhnSQL.prepare(_query_delete_server_profile) 797 h.execute(server_profile_id=server_profile_id)
798