1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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
29 log_debug(3, server_id, action_id, action_status, kickstart_state, next_action_type)
30
31
32 ks_session_id = get_kickstart_session_id(server_id, action_id)
33 if ks_session_id is None:
34
35 log_debug(4, "Kickstart session not found")
36 return None
37
38
39 if action_status == 2:
40
41 ks_status = kickstart_state
42
43 next_action_id = get_next_action_id(action_id, next_action_type)
44 elif action_status == 3:
45
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
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
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
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
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
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
216 row = get_kickstart_session_info(kickstart_session_id, server_id)
217 org_id = row['org_id']
218 scheduler = row['scheduler']
219
220
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
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
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
255
256
282
283
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
326
327
328
329
330
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
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
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
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
397
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
407
408
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
421 log_debug(3, server_id, action_id,
422 "No channels to provide %s found" % capability)
423
424 return action_id
425
426 if not server_profile:
427 server_profile = get_server_package_profile(server_id)
428
429
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
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
483 log_debug(4, server_id, scheduler, capability)
484
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
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
509
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
520 rhnChannel.subscribe_sql(server_id, channel_id, 0)
521 log_debug(4, "Subscribed to", channel_id,
522 "Found packages", packages)
523
524 return packages
525
526
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
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
579
580
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
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
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
606
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
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
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
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
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
674 log_debug(4, "Terminating sessions", params)
675 h.execute_bulk(params)
676
677
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
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
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
726
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
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
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
794
795
796 h = rhnSQL.prepare(_query_delete_server_profile)
797 h.execute(server_profile_id=server_profile_id)
798