1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 import sys
19
20 from spacewalk.common import rhnFlags
21 from spacewalk.common.usix import raise_with_tb
22 from spacewalk.common.rhnLog import log_debug, log_error
23 from spacewalk.common.rhnException import rhnFault, rhnException
24 from spacewalk.common.rhnTranslate import _
25 from spacewalk.server import rhnSQL, rhnChannel, rhnAction
26
27 from server_lib import join_server_group
28
29 VIRT_ENT_LABEL = 'virtualization_host'
30
32 """ Handle channel subscriptions for the registration token """
33 assert(isinstance(tokens_obj, ActivationTokens))
34
35 server_id, server_arch_id = server['id'], server['server_arch_id']
36
37
38
39 h = rhnSQL.prepare("""
40 select
41 rtc.channel_id id, c.name, c.label, c.parent_channel
42 from
43 rhnRegTokenChannels rtc,
44 rhnChannel c,
45 rhnServerChannelArchCompat scac
46 where rtc.token_id = :token_id
47 and rtc.channel_id = c.id
48 and c.channel_arch_id = scac.channel_arch_id
49 and scac.server_arch_id = :server_arch_id
50 """)
51
52 chash = {}
53 base_channel_token = None
54 base_channel_id = None
55
56 for token in tokens_obj.tokens:
57 token_id = token['token_id']
58 h.execute(token_id=token_id, server_arch_id=server_arch_id)
59 while 1:
60 row = h.fetchone_dict()
61 if not row:
62 break
63 channel_id = row['id']
64 chash[channel_id] = row
65 if row['parent_channel'] is not None:
66
67 continue
68
69
70 if base_channel_id is not None and channel_id != base_channel_id:
71
72
73 if base_channel_token == token:
74 log_error("Token has multiple base channels", token_id,
75 base_channel_id)
76 raise rhnFault(62,
77 _("Token `%s' has more than one base channel assigned")
78 % token['note'])
79 raise rhnFault(63, _("Conflicting base channels"))
80 base_channel_id = channel_id
81 base_channel_token = token
82
83 bc = chash.get(base_channel_id)
84 log_debug(4, "base channel", bc)
85
86
87
88
89
90 sbc = rhnChannel.get_base_channel(server_id, none_ok=1)
91
92
93 ret = []
94
95
96 if bc is None:
97 if sbc is None:
98
99 log_error("Server has invalid release and "
100 "token contains no base channels", server_id,
101 tokens_obj.tokens)
102 ret.append("System registered without a base channel")
103 ret.append("Unsupported release-architecture combination "
104 "(%s, %s)" % (server["release"], server_arch))
105 return ret
106 else:
107 if sbc and sbc["id"] != bc["id"]:
108
109 rhnChannel.unsubscribe_all_channels(server_id)
110 sbc = None
111 if sbc is None:
112
113 rhnChannel.subscribe_sql(server_id, bc["id"], commit=0)
114 ret.append("Subscribed to base channel '%s' (%s)" % (
115 bc["name"], bc["label"]))
116 sbc = bc
117
118
119
120 subscribe_channel = rhnSQL.Procedure("rhn_channel.subscribe_server")
121
122
123 channel_family_ids = set()
124
125 for c in [a for a in chash.values() if a["parent_channel"]]:
126
127 if str(c["parent_channel"]) != str(sbc["id"]):
128 ret.append("NOT subscribed to channel '%s' "
129 "(not a child of '%s')" % (
130 c["name"], sbc["name"]))
131 continue
132 try:
133
134
135
136 subscribe_channel(server_id, c["id"], 0, None)
137 child = rhnChannel.Channel()
138 child.load_by_id(c["id"])
139 child._load_channel_families()
140 cfamid = child._channel_families[0]
141 channel_family_ids.add(cfamid)
142 except rhnSQL.SQLError:
143 e = sys.exc_info()[1]
144 log_error("Failed channel subscription", server_id,
145 c["id"], c["label"], c["name"])
146 ret.append("FAILED to subscribe to channel '%s'" % c["name"])
147 else:
148 ret.append("Subscribed to channel '%s'" % c["name"])
149
150 log_debug(5, "cf ids: %s" % str(channel_family_ids))
151 log_debug(5, "Server org_id: %s" % str(server['org_id']))
152
153 return ret
154
155 _query_token_server_groups = rhnSQL.Statement("""
156 select rtg.server_group_id, sg.name
157 from rhnRegTokenGroups rtg, rhnServerGroup sg
158 where rtg.token_id = :token_id
159 and sg.id = rtg.server_group_id
160 """)
161
162
164 """ Handle server group subscriptions for the registration token """
165 assert(isinstance(tokens_obj, ActivationTokens))
166 h = rhnSQL.prepare(_query_token_server_groups)
167 server_groups = {}
168 for token in tokens_obj.tokens:
169 token_id = token['token_id']
170 h.execute(token_id=token_id)
171 while 1:
172 row = h.fetchone_dict()
173 if not row:
174 break
175 server_group_id = row['server_group_id']
176 server_groups[server_group_id] = row
177
178
179 ret = []
180 for server_group_id, sg in server_groups.items():
181 log_debug(4, "token server group", sg)
182
183 try:
184 join_server_group(server_id, server_group_id)
185 except rhnSQL.SQLError:
186 e = sys.exc_info()[1]
187 log_error("Failed to add server to group", server_id,
188 server_group_id, sg["name"])
189 raise_with_tb(rhnFault(80, _("Failed to add server to group %s") %
190 sg["name"]), sys.exc_info()[2])
191 else:
192 ret.append("Subscribed to server group '%s'" % sg["name"])
193 return ret
194
195
196 _query_token_packages = rhnSQL.Statement("""
197 select pn.id as name_id, pa.id as arch_id, pn.name
198 from rhnPackageName pn, rhnRegTokenPackages rtp
199 left outer join rhnPackageArch pa on rtp.arch_id = pa.id
200 where rtp.token_id = :token_id
201 and rtp.name_id = pn.id
202 order by upper(pn.name)
203 """)
204 _query_token_packages_insert = rhnSQL.Statement("""
205 insert into rhnActionPackage (id, action_id, name_id, parameter)
206 values (sequence_nextval('rhn_act_p_id_seq'), :action_id, :name_id, 'upgrade')
207 """)
208
209
211 assert(isinstance(tokens_obj, ActivationTokens))
212
213 h = rhnSQL.prepare(_query_token_packages)
214 package_names = {}
215 for token in tokens_obj.tokens:
216 token_id = token['token_id']
217 h.execute(token_id=token_id)
218 while True:
219 row = h.fetchone_dict()
220 if not row:
221 break
222 pn_id = row['name_id']
223 pa_id = row['arch_id']
224 package_names[(pn_id, pa_id)] = row['name']
225
226 ret = []
227 if not package_names:
228 return ret
229
230 package_arch_ids = list(package_names.keys())
231
232 last_action_id = rhnFlags.get('token_last_action_id')
233
234 action_id = rhnAction.schedule_server_packages_update_by_arch(server_id,
235 package_arch_ids, org_id=token['org_id'],
236 prerequisite=last_action_id,
237 action_name="Activation Key Package Auto-Install")
238
239
240 rhnFlags.set('token_last_action_id', action_id)
241
242 for p in package_names.values():
243 ret.append("Scheduled for install: '%s'" % p)
244
245 rhnSQL.commit()
246
247 return ret
248
249
250
251
252
253
254
255 _query_token_latest_revisions = rhnSQL.Statement("""
256 select cf.latest_config_revision_id revision_id,
257 cfn.path
258 from rhnConfigFileName cfn,
259 rhnConfigFile cf,
260 rhnConfigChannelType cct,
261 rhnConfigChannel cc,
262 rhnServerConfigChannel scc
263 where scc.server_id = :server_id
264 and scc.config_channel_id = cc.id
265 and cc.confchan_type_id = cct.id
266 and cct.label != 'server_import'
267 and cc.id = cf.config_channel_id
268 and cf.config_file_name_id = cfn.id
269 -- latest_config_revision_id should always be non-null but
270 -- we should protect ourselves
271 and cf.latest_config_revision_id is not null
272 order by cfn.path, cct.priority, scc.position
273 """)
274
275 _query_add_revision_to_action = rhnSQL.Statement("""
276 insert into rhnActionConfigRevision (id, action_id, server_id, config_revision_id)
277 values (sequence_nextval('rhn_actioncr_id_seq'), :action_id, :server_id, :config_revision_id)
278 """)
279
280
282 server_id = server['id']
283 log_debug(4, server_id)
284
285 revisions = {}
286
287 h = rhnSQL.prepare(_query_token_latest_revisions)
288 h.execute(server_id=server_id)
289
290 while 1:
291 row = h.fetchone_dict()
292 if not row:
293 break
294
295
296
297 if row['path'] not in revisions:
298 revisions[row['path']] = row['revision_id']
299
300 if not len(revisions):
301 return None
302
303
304 last_action_id = rhnFlags.get('token_last_action_id')
305
306 action_id = rhnAction.schedule_server_action(
307 server_id,
308 action_type='activation.schedule_deploy',
309 action_name="Activation Key Config File Deployment",
310 delta_time=0, scheduler=None,
311 org_id=server['org_id'],
312 prerequisite=last_action_id,
313 )
314
315
316 rhnFlags.set('token_last_action_id', action_id)
317
318 log_debug(4, "scheduled activation key config deploy")
319
320 h = rhnSQL.prepare(_query_add_revision_to_action)
321
322 for revision_id in revisions.values():
323 log_debug(5, action_id, revision_id)
324 h.execute(server_id=server_id,
325 action_id=action_id,
326 config_revision_id=revision_id)
327
328 return action_id
329
330
331 _query_token_config_channels = rhnSQL.Statement("""
332 select rtcc.config_channel_id,
333 rtcc.position, cc.name
334 from rhnConfigChannel cc,
335 rhnRegTokenConfigChannels rtcc
336 where rtcc.token_id = :token_id
337 and rtcc.config_channel_id = cc.id
338 order by rtcc.position
339 """)
340
341
342 _query_set_server_config_channels = rhnSQL.Statement("""
343 insert into rhnServerConfigChannel (server_id, config_channel_id, position)
344 values (:server_id, :config_channel_id, :position)
345 """)
346
347
353
354 _query_current_config_channels = rhnSQL.Statement("""
355 select config_channel_id
356 from rhnServerConfigChannel
357 where server_id = :server_id
358 and position is not null
359 """)
360
361
371
372
374 assert(isinstance(tokens_obj, ActivationTokens))
375 server_id = server['id']
376
377
378
379
380 if tokens_obj.is_rereg_token:
381 return []
382
383
384
385 config_channels = []
386 config_channels_hash = {}
387 deployment = 0
388 current_channels = []
389 if tokens_obj.forget_rereg_token:
390 current_channels = _get_current_config_channels(server_id)
391
392 for token in tokens_obj.tokens:
393 channels = _get_token_config_channels(token['token_id'])
394
395
396 deploy_configs = token['deploy_configs']
397 log_debug(2, "token_id: ", token['token_id'], " deploy_configs: ", deploy_configs)
398 if deploy_configs == 'Y':
399 log_debug(2, "At least one token set to deploy config files")
400 deployment = 1
401 for c in channels:
402 config_channel_id = c['config_channel_id']
403 if not c['config_channel_id'] in current_channels and\
404 config_channel_id not in config_channels_hash:
405 position = len(current_channels) + len(config_channels) + 1
406
407 c['position'] = position
408 config_channels.append(c)
409 config_channels_hash[config_channel_id] = None
410
411 ret = []
412 if config_channels:
413 h = rhnSQL.prepare(_query_set_server_config_channels)
414
415 h.execute_bulk({
416 'server_id': [server_id] * len(config_channels),
417 'config_channel_id': [c['config_channel_id'] for c in config_channels],
418 'position': [c['position'] for c in config_channels],
419 })
420
421 for channel in config_channels:
422 msg = "Subscribed to config channel %s" % channel['name']
423 log_debug(4, msg)
424 ret.append(msg)
425
426
427
428
429
430 if deployment:
431 log_debug(2, "At least one token has deploy_configs == Y, deploying configs")
432 deploy_configs_if_needed(server)
433
434 rhnSQL.commit()
435
436 return ret
437
438
439 _query_server_token_used = rhnSQL.Statement("""
440 insert into rhnServerTokenRegs (server_id, token_id)
441 values (:server_id, :token_id)
442 """)
443
444 _query_check_server_uses_token = rhnSQL.Statement("""
445 select 1 from rhnServerTokenRegs
446 where server_id = :server_id
447 and token_id = :token_id
448 """)
449
450
458
459 _query_check_token_limits = rhnSQL.Statement("""
460 select
461 rt.usage_limit max_nr,
462 ( select count(server_id) from rhnServerTokenRegs
463 where token_id = :token_id ) curr_nr
464 from rhnRegToken rt
465 where rt.id = :token_id
466 """)
467
468
483
484
486 token_id = token_rec["token_id"]
487
488
489 server_used_token(server_id, token_id)
490
491
492 h = rhnSQL.prepare(_query_check_token_limits)
493 h.execute(token_id=token_id)
494 ret = h.fetchone_dict()
495 if not ret:
496 raise rhnException("Could not check usage limits for token",
497 server_id, token_rec)
498
499 if ret["max_nr"] is not None and ret["max_nr"] < ret["curr_nr"]:
500 log_error("Token usage limit exceeded", token_rec,
501 ret["max_nr"], server_id)
502 raise rhnFault(61, _("Maximum usage count of %s reached") % ret["max_nr"])
503
504 return 0
505
506
508
509 """
510 An aggregation of activation tokens, exposing important information
511 like org_id, user_id etc in a unified manner.
512 """
513 is_rereg_token = 0
514 forget_rereg_token = 0
515
516 - def __init__(self, tokens, user_id=None, org_id=None,
517 kickstart_session_id=None, entitlements=[], deploy_configs=None):
518 self.tokens = tokens
519 self.user_id = user_id
520 self.org_id = org_id
521 self.kickstart_session_id = kickstart_session_id
522
523 self.deploy_configs = deploy_configs
524
525 self.entitlements = entitlements
526
528 return (len(self.tokens) > 0)
529
530 __bool__ = __nonzero__
531
533 if not self:
534 return None
535
536 for token in self.tokens:
537 server_id = token.get('server_id')
538 if server_id:
539 return server_id
540
541 return None
542
545
548
550 return self.kickstart_session_id
551
553 return self.entitlements
554
556 if entitlement in [x[0] for x in self.entitlements]:
557 return 1
558 return 0
559
561 return self.deploy_configs
562
564 """ Returns a string of the entitlement names that the token grants.
565 This function is poorly named.
566 """
567 token_names = [x[0] for x in self.entitlements]
568 if not token_names:
569 return None
570 return ",".join(token_names)
571
573 tokens = []
574 for token in self.tokens:
575 tokens.append(token['token'])
576
577 return tokens
578
579 - def entitle(self, server_id, history, virt_type=None):
580 """
581 Entitle a server according to the entitlements we have configured.
582 """
583 log_debug(3, self.entitlements)
584
585 entitle_server = rhnSQL.Procedure("rhn_entitlements.entitle_server")
586
587
588
589 can_entitle_server = rhnSQL.Function(
590 "rhn_entitlements.can_entitle_server", rhnSQL.types.NUMBER())
591
592 can_ent = None
593
594 history["entitlement"] = ""
595
596 for entitlement in self.entitlements:
597 if virt_type is not None and entitlement[0] == VIRT_ENT_LABEL:
598 continue
599
600 try:
601 can_ent = can_entitle_server(server_id, entitlement[0])
602 except rhnSQL.SQLSchemaError:
603 e = sys.exc_info()[1]
604 can_ent = 0
605
606 try:
607
608 if can_ent:
609 entitle_server(server_id, entitlement[0])
610 except rhnSQL.SQLSchemaError:
611 e = sys.exc_info()[1]
612 log_error("Token failed to entitle server", server_id,
613 self.get_names(), entitlement[0], e.errmsg)
614
615 raise_with_tb(rhnFault(90, e.errmsg), sys.exc_info()[2])
616 except rhnSQL.SQLError:
617 e = sys.exc_info()[1]
618 log_error("Token failed to entitle server", server_id,
619 self.get_names(), entitlement[0], e.args)
620 raise_with_tb(rhnFault(90, str(e)), sys.exc_info()[2])
621 else:
622 history["entitlement"] = "Entitled as a %s member" % entitlement[1]
623
624
626
627 """
628 Subclass for re-registration keys.
629
630 (i.e. used alone and not combined with other regular activation keys)
631 """
632 is_rereg_token = 1
633
634
636
637 """
638 Subclass for re-registration keys and activation keys used together.
639 """
640 forget_rereg_token = 1
641
642 - def __init__(self, tokens, user_id=None, org_id=None,
643 kickstart_session_id=None, entitlements=[],
644 remove_entitlements=[], deploy_configs=None):
645 ReRegistrationToken.__init__(self, tokens, user_id, org_id,
646 kickstart_session_id, entitlements, deploy_configs)
647 self.remove_entitlements = remove_entitlements
648
649 - def entitle(self, server_id, history, virt_type=None):
650 for ent in self.remove_entitlements:
651 unentitle_server = rhnSQL.Procedure(
652 "rhn_entitlements.remove_server_entitlement")
653 try:
654 unentitle_server(server_id, ent)
655 except rhnSQL.SQLSchemaError:
656 e = sys.exc_info()[1]
657 log_error("Failed to unentitle server", server_id,
658 ent, e.errmsg)
659 raise_with_tb(rhnFault(90, e.errmsg), sys.exc_info()[2])
660 except rhnSQL.SQLError:
661 e = sys.exc_info()[1]
662 log_error("Failed to unentitle server", server_id,
663 ent, e.args)
664 raise_with_tb(rhnFault(90, str(e)), sys.exc_info()[2])
665
666
667 ReRegistrationToken.entitle(self, server_id, history, virt_type)
668
669
671 """ Fetches a token from a prepared and executed cursor
672 Used by both fetch_token and fetch_org_token
673 """
674 token_entry = None
675 token_entitlements = {}
676 while 1:
677 row = cursor.fetchone_dict()
678 if not row:
679 break
680 tup = (row['token_type'], row['token_desc'], row['is_base'])
681 token_entitlements[tup] = None
682 if token_entry:
683
684
685
686
687
688 assert token_entry['token_id'] == row['token_id'], \
689 "Query returned different tokens - missing unique constraint" \
690 " on rhnActivationKey.token?"
691 continue
692
693
694 token_entry = row
695
696 return token_entry, token_entitlements
697
698
701 """ Given a hash token_entitlements, splits the base ones and puts them in
702 the entitlements_base hash, and the extras in entitlements_extra
703 """
704 for tup in token_entitlements.keys():
705 is_base = tup[2]
706 ent = (tup[0], tup[1])
707 if is_base == 'Y':
708 entitlements_base[ent] = None
709 else:
710 entitlements_extra[ent] = None
711
712 return entitlements_base, entitlements_extra
713
714
715 -def _validate_entitlements(token_string, rereg_ents, base_entitlements,
716 extra_entitlements, remove_entitlements):
717 """
718 Perform various checks on the final list of entitlements accumulated after
719 processing all activation keys.
720
721 rereg_ents passed in as a list of entitlement labels.
722
723 Extra/base entitlements passed in as a hash of tuples ('label', 'Friendly
724 Name') mapping to None. (i.e. seems to be used as just a set)
725
726 Remove entitlements being maintained as just a list of labels.
727 """
728
729 if len(list(base_entitlements.keys())) != 1:
730 log_error("Tokens with different base entitlements", token_string,
731 base_entitlements)
732 raise rhnFault(63,
733 _("Stacking of re-registration tokens with different base entitlements "
734 "is not supported"), explain=0)
735
736 _query_token = rhnSQL.Statement("""
737 select rt.id as token_id,
738 sgt.label as token_type,
739 sgt.name as token_desc,
740 sgt.is_base,
741 ak.token,
742 rt.user_id,
743 rt.org_id,
744 rt.note,
745 rt.usage_limit,
746 rt.server_id,
747 ak.ks_session_id kickstart_session_id,
748 rt.deploy_configs
749 from rhnActivationKey ak, rhnRegToken rt, rhnRegTokenEntitlement rte, rhnServerGroupType sgt
750 where ak.token = :token
751 and ak.reg_token_id = rt.id
752 and rt.disabled = 0
753 and rt.id = rte.reg_token_id
754 and rte.server_group_type_id = sgt.id
755 """)
756
757
759 """ Fetches a token from the database """
760 log_debug(3, token_string)
761
762 assert token_string
763 tokens = token_string.split(',')
764 h = rhnSQL.prepare(_query_token)
765 result = []
766 rereg_token_found = 0
767 num_of_rereg = 0
768
769 user_id = None
770 same_user_id = 1
771 org_id = None
772 ks_session_id_token = None
773 deploy_configs = None
774 entitlements_base = {}
775 entitlements_extra = {}
776
777
778 rereg_ents = []
779
780 for token in tokens:
781 h.execute(token=token)
782 token_entry, token_entitlements = _fetch_token_from_cursor(h)
783
784 if not token_entry:
785
786 log_error("Invalid token '%s'" % token)
787 raise rhnFault(60, _("Could not find token '%s'") % token, explain=0)
788
789 row = token_entry
790
791 if row.get('server_id'):
792 rereg_token_found = row
793 num_of_rereg += 1
794
795
796 for tup in token_entitlements.keys():
797 rereg_ents.append(tup[0])
798
799
800 token_user_id = row.get('user_id')
801
802
803
804
805
806
807 if same_user_id and user_id is not None and user_id != token_user_id:
808 log_debug(4, "Different user ids: %s, %s" % (same_user_id, user_id))
809
810 same_user_id = 0
811 else:
812 user_id = token_user_id
813
814
815 token_org_id = row.get('org_id')
816 assert(token_org_id is not None)
817 if org_id is not None and org_id != token_org_id:
818
819 raise rhnFault(63, _("Tokens from mismatching orgs"), explain=0)
820 org_id = token_org_id
821
822
823 token_ks_session_id = row.get('kickstart_session_id')
824 if token_ks_session_id is not None:
825 if ks_session_id_token is not None:
826 ks_session_id = ks_session_id_token['kickstart_session_id']
827 if ks_session_id != token_ks_session_id:
828
829 raise rhnFault(63, _("Kickstart session mismatch"),
830 explain=0)
831 else:
832
833 ks_session_id_token = row
834
835
836
837
838
839 _categorize_token_entitlements(token_entitlements, entitlements_base,
840 entitlements_extra)
841
842
843 deploy_configs = deploy_configs or (row['deploy_configs'] == 'Y')
844 result.append(row)
845
846
847 if num_of_rereg > 1:
848 raise rhnFault(63,
849 _("Stacking of re-registration tokens is not supported"), explain=0)
850
851 entitlements_remove = []
852 _validate_entitlements(token_string, rereg_ents, entitlements_base,
853 entitlements_extra, entitlements_remove)
854 log_debug(5, "entitlements_base = %s" % entitlements_base)
855 log_debug(5, "entitlements_extra = %s" % entitlements_extra)
856
857 if ks_session_id_token:
858 ks_session_id = ks_session_id_token['kickstart_session_id']
859 else:
860 ks_session_id = None
861
862
863 kwargs = {
864 'user_id': user_id,
865 'org_id': org_id,
866 'kickstart_session_id': ks_session_id,
867 'entitlements': list(entitlements_base.keys()) + list(entitlements_extra.keys()),
868 'deploy_configs': deploy_configs,
869 }
870 log_debug(4, "Values", kwargs)
871
872 if rereg_token_found and len(result) > 1:
873 log_debug(4, "re-activation stacked with activationkeys")
874 kwargs['remove_entitlements'] = entitlements_remove
875 return ReRegistrationActivationToken(result, **kwargs)
876 elif rereg_token_found:
877 log_debug(4, "simple re-activation")
878 return ReRegistrationToken([rereg_token_found], **kwargs)
879
880 return ActivationTokens(result, **kwargs)
881
882
883 _query_org_default_token = rhnSQL.Statement("""
884 select rt.id as token_id,
885 sgt.label as token_type,
886 sgt.name as token_desc,
887 sgt.is_base,
888 ak.token,
889 rt.user_id,
890 rt.org_id,
891 rt.note,
892 -- Default tokens have no usage limit
893 NULL usage_limit,
894 rt.server_id,
895 NULL kickstart_session_id,
896 rt.deploy_configs
897 from rhnServerGroupType sgt,
898 rhnActivationKey ak,
899 rhnRegToken rt,
900 rhnRegTokenOrgDefault rtod,
901 rhnRegTokenEntitlement rte
902 where rtod.org_id = :org_id
903 and rtod.reg_token_id = rt.id
904 and rt.id = rte.reg_token_id
905 and rt.disabled = 0
906 and rte.server_group_type_id = sgt.id
907 and ak.reg_token_id = rtod.reg_token_id
908 """)
909
910
912 log_debug(3, org_id)
913 h = rhnSQL.prepare(_query_org_default_token)
914 h.execute(org_id=org_id)
915 token_entry, token_entitlements = _fetch_token_from_cursor(h)
916 entitlements_base = {}
917 entitlements_extra = {}
918 _categorize_token_entitlements(token_entitlements, entitlements_base,
919 entitlements_extra)
920
921 kwargs = {}
922 tokens = []
923 if token_entry:
924 kwargs = {
925 'user_id': token_entry['user_id'],
926 'org_id': token_entry['org_id'],
927 'kickstart_session_id': token_entry['kickstart_session_id'],
928 'entitlements': list(entitlements_base.keys()) + list(entitlements_extra.keys()),
929 'deploy_configs': token_entry['deploy_configs'] == 'Y',
930 }
931 tokens.append(token_entry)
932
933 return ActivationTokens(tokens, **kwargs)
934
935
936 _query_disable_token = rhnSQL.Statement("""
937 update rhnRegToken
938 set disabled = 1
939 where id = :token_id
940 """)
941
942
950
951
952 -def process_token(server, server_arch, tokens_obj, virt_type=None):
953 """ perform registration tasks for a server as indicated by a token """
954 assert(isinstance(tokens_obj, ActivationTokens))
955 server_id = server['id']
956 log_debug(1, server_id, tokens_obj.get_names())
957
958
959 history = {}
960
961
962
963 check_token_limits(server_id, tokens_obj)
964
965 is_reactivation = rhnFlags.test('re_registration_token')
966
967
968 history["channels"] = token_channels(server, server_arch, tokens_obj)
969
970 if is_reactivation:
971
972 history["entitlement"] = "Re-activation: keeping previous entitlement level"
973 else:
974 tokens_obj.entitle(server_id, history, virt_type)
975
976 is_management_entitled = None
977
978 if tokens_obj.has_entitlement_label('enterprise_entitled'):
979 is_management_entitled = 1
980
981 if is_reactivation:
982 history["groups"] = ["Re-activation: keeping previous server groups"]
983 else:
984
985 if is_management_entitled:
986 history["groups"] = token_server_groups(server_id, tokens_obj)
987 else:
988
989 history["groups"] = [
990 "Not subscribed to any system groups: not entitled for "
991 "Management"
992 ]
993
994 if is_management_entitled:
995 history["packages"] = token_packages(server_id, tokens_obj)
996 history["config_channels"] = token_config_channels(server,
997 tokens_obj)
998 else:
999 history["packages"] = ["Insufficient service level for automatic package installation."]
1000 history["config_channels"] = ["Insufficient service level for config channel subscription."]
1001
1002
1003 return history_report(history)
1004
1005
1006 -def history_report(history):
1007 """ build a mildly html-ized version of the history as a report """
1008
1009 report = "Entitlement Information:\n"
1010 report += "<ul><li>%s</li></ul>" % history["entitlement"]
1011 report += "\n"
1012
1013 report += history_subreport(history, "groups",
1014 "Channel Subscription Information:",
1015 "The token does not include default Channel Subscriptions")
1016
1017
1018 report += history_subreport(history, "groups",
1019 "System Group Membership Information:",
1020 "The token does not include default System Group Membership")
1021
1022
1023 report += history_subreport(history, "packages",
1024 "Packages Scheduled for Installation:",
1025 "No packages scheduled for automatic installation")
1026
1027
1028 report += history_subreport(history, 'config_channels',
1029 "Config Channel Subscription Information:",
1030 "The token does not include default configuration channels")
1031
1032 return report
1033
1034
1035 -def history_subreport(history, key, title, emptymsg):
1036 if key in history:
1037 subreport = title + "\n"
1038 subreport += "<ul>\n"
1039
1040 for c in history[key]:
1041 subreport += "<li>%s</li>\n" % c
1042
1043 if len(history[key]) == 0:
1044 subreport += "<li>%s</li>\n" % emptymsg
1045
1046 subreport += "</ul>\n"
1047 return subreport
1048