def load_token(self): # Fetch token tokens_obj = rhnFlags.get("registration_token") if not tokens_obj: # No tokens present return 0 # make sure we have reserved a server_id. most likely if this # is a new server object (just created from # registration.new_system) then we have no associated a # server["id"] yet -- and getid() will reserve that for us. self.getid() # pull in the extra information needed to fill in the # required registration fields using tokens user_id = tokens_obj.get_user_id() org_id = tokens_obj.get_org_id() self.user = rhnUser.User("", "") if user_id is not None: self.user.reload(user_id) self.server["creator_id"] = user_id self.server["org_id"] = org_id self.server["contact_method_id"] = tokens_obj.get_contact_method_id() return 0
def create_activation_key(org_id=None, user_id=None, groups=None, channels=None, entitlement_level=None, note=None, server_id=None): if org_id is None: need_user = 1 org_id = create_new_org() else: need_user = 0 if user_id is None: if need_user: u = create_new_user(org_id=org_id) user_id = u.getid() else: u = rhnUser.User("", "") u.reload(user_id) if groups is None: groups = [] for i in range(3): params = build_server_group_params(org_id=org_id) sg = create_server_group(params) groups.append(sg.get_id()) if channels is None: channels = ['rhel-i386-as-3-beta', 'rhel-i386-as-2.1-beta'] if entitlement_level is None: entitlement_level = 'provisioning_entitled' if note is None: note = "Test activation key %d" % int(time.time()) a = rhnActivationKey.ActivationKey() a.set_user_id(user_id) a.set_org_id(org_id) a.set_entitlement_level(entitlement_level) a.set_note(note) a.set_server_groups(groups) a.set_channels(channels) a.set_server_id(server_id) a.save() rhnSQL.commit() return a
def __init__(self, hw=None, guest=None): log_debug(4, hw, guest) if not hw or "identifier" not in hw or not guest: # incomplete data log_debug(1, "incomplete data") return host = rhnSQL.Row("rhnServer", "digital_server_id") host.load(hw['identifier']) hid = host.get('id') guest.user = rhnUser.User("", "") guest.user.reload(guest.server['creator_id']) guestid = guest.getid() if not hid: # create a new host entry host = server_class.Server(guest.user, hw.get('arch')) host.server["name"] = hw.get('name') host.server["os"] = hw.get('os') host.server["release"] = hw.get('type') host.server["last_boot"] = time.time() host.default_description() host.virt_type = rhnVirtualization.VirtualizationType.FULLY host.virt_uuid = None fake_token = False if not rhnFlags.test("registration_token"): # we need to fake it rhnFlags.set("registration_token", 'fake') fake_token = True host.save(1, None) host.server["digital_server_id"] = hw['identifier'] entitle_server = rhnSQL.Procedure( "rhn_entitlements.entitle_server") entitle_server(host.getid(), 'foreign_entitled') host.save(1, None) if fake_token: rhnFlags.set("registration_token", None) hid = host.getid() host.reload(hid) log_debug(4, "New host created: ", host) else: host = server_class.Server(None) host.reload(hid) host.checkin(commit=0) log_debug(4, "Found host: ", host) host.reload_hardware() hostcpu = host.hardware_by_class(CPUDevice) if hostcpu and len(hostcpu) > 0: hostcpu = hostcpu[0].data else: hostcpu = None if not hostcpu or str(hostcpu.get('nrsocket')) != hw.get('total_ifls'): # update only if the number has changed log_debug(1, "update host cpu:", hw.get('total_ifls')) cpu = { 'class': 'CPU', 'desc': 'Processor', 'count': hw.get('total_ifls'), 'model_ver': '', 'speed': '0', 'cache': '', 'model_number': '', 'bogomips': '', 'socket_count': hw.get('total_ifls'), 'platform': hw.get('arch'), 'other': '', 'model_rev': '', 'model': hw.get('arch'), 'type': hw.get('type') } host.delete_hardware() host.add_hardware(cpu) host.save_hardware() h = rhnSQL.prepare(""" select host_system_id from rhnVirtualInstance where virtual_system_id = :guestid""") h.execute(guestid=guestid) row = h.fetchone_dict() if not row or not row['host_system_id']: self._insert_virtual_instance(hid, None, fakeuuid=False) self._insert_virtual_instance(hid, guestid, fakeuuid=True) elif row['host_system_id'] != hid: log_debug(4, "update_virtual_instance", hid, guestid) q_update = rhnSQL.prepare(""" UPDATE rhnVirtualInstance SET host_system_id = :host_id WHERE virtual_system_id = :guest_id AND host_system_id = :old_host_id """) q_update.execute(host_id=hid, guest_id=guestid, old_host_id=row['host_system_id'])
def create_system(self, user, profile_name, release_version, architecture, data): """ Create a system based on the input parameters. Return dict containing a server object for now. Called by new_system (< rhel5) and new_system_user_pass | new_system_activation_key (>= rhel5) """ if profile_name is not None and not \ rhnFlags.test("re_registration_token") and \ len(profile_name) < 1: raise rhnFault(800) # log entry point if data.has_key("token"): log_item = "token = '%s'" % data["token"] else: log_item = "username = '******'" % user.username log_debug(1, log_item, release_version, architecture) # Fetch the applet's UUID if data.has_key("uuid"): applet_uuid = data['uuid'] log_debug(3, "applet uuid", applet_uuid) else: applet_uuid = None # Fetch the up2date UUID if data.has_key("rhnuuid"): up2date_uuid = data['rhnuuid'] log_debug(3, "up2date uuid", up2date_uuid) # XXX Should somehow check the uuid uniqueness #raise rhnFault(105, "A system cannot be registered multiple times") else: up2date_uuid = None release = str(release_version) if data.has_key('token'): token_string = data['token'] # Look the token up; if the token does not exist or is invalid, # stop right here (search_token raises the appropriate rhnFault) tokens_obj = rhnServer.search_token(token_string) log_user_id = tokens_obj.get_user_id() else: # user should not be null here log_user_id = user.getid() tokens_obj = rhnServer.search_org_token(user.contact["org_id"]) log_debug(3,"universal_registration_token set as %s" % str(tokens_obj.get_tokens())) rhnFlags.set("universal_registration_token", tokens_obj) if data.has_key('channel') and len(data['channel']) > 0: channel = data['channel'] log_debug(3, "requested EUS channel: %s" % str(channel)) else: channel = None newserv = None if tokens_obj: # Only set registration_token if we have token(s) available. # server_token.ActivationTokens.__nonzero__ should do the right # thing of filtering the case of no tokens rhnFlags.set("registration_token", tokens_obj) # Is the token associated with a server? if tokens_obj.is_rereg_token: # Also flag it's a re-registration token rhnFlags.set("re_registration_token", tokens_obj) # Load the server object newserv = rhnServer.search(tokens_obj.get_server_id()) newserv.disable_token() # The old hardware info no longer applies newserv.delete_hardware() # Update the arch - it may have changed; we know the field was # provided for us newserv.set_arch(architecture) newserv.user = rhnUser.User("", "") newserv.user.reload(newserv.server['creator_id']) # Generate a new secret for this server newserv.gen_secret() # Get rid of the old package profile - it's bogus in this case newserv.dispose_packages() # The new server may have a different base channel newserv.change_base_channel(release) if newserv is None: # Not a re-registration token, we need a fresh server object rhnSQL.set_log_auth(log_user_id) newserv = rhnServer.Server(user, architecture) # Proceed with using the rest of the data newserv.server["release"] = release if data.has_key('release_name'): newserv.server["os"] = data['release_name'] ## add the package list if data.has_key('packages'): for package in data['packages']: newserv.add_package(package) # add the hardware profile if data.has_key('hardware_profile'): for hw in data['hardware_profile']: newserv.add_hardware(hw) # fill in the other details from the data dictionary if profile_name is not None and not \ rhnFlags.test("re_registration_token"): newserv.server["name"] = profile_name[:128] if data.has_key("os"): newserv.server["os"] = data["os"][:64] if data.has_key("description"): newserv.server["description"] = data["description"][:256] else: newserv.default_description() # Check for virt params # Get the uuid, if there is one. if data.has_key('virt_uuid'): virt_uuid = data['virt_uuid'] if virt_uuid is not None \ and not rhnVirtualization.is_host_uuid(virt_uuid): # If we don't have a virt_type key, we'll assume PARA. virt_type = None if data.has_key('virt_type'): virt_type = data['virt_type'] if virt_type == 'para': virt_type = rhnVirtualization.VirtualizationType.PARA elif virt_type == 'fully': virt_type = rhnVirtualization.VirtualizationType.FULLY else: raise Exception( "Unknown virtualization type: %s" % virt_type) else: raise Exception("Virtualization type not provided") newserv.virt_uuid = virt_uuid newserv.virt_type = virt_type else: newserv.virt_uuid = None newserv.virt_type = None else: newserv.virt_uuid = None newserv.virt_type = None # If we didn't find virt info from xen, check smbios if data.has_key('smbios') and newserv.virt_uuid is None: (newserv.virt_type, newserv.virt_uuid) = \ parse_smbios(data['smbios']) if tokens_obj.forget_rereg_token: # At this point we retained the server with re-activation # let the stacked activation keys do their magic tokens_obj.is_rereg_token = 0 rhnFlags.set("re_registration_token", 0) # now if we have a token, load the extra registration # information from the token if rhnFlags.test("registration_token"): # Keep the original "info" field newserv.load_token() # we need to flush the registration information into the # database so we can proceed with processing the rest of # the token information (like subscribing the server to # groups, channels, etc) # bretm 02/19/2007 -- this shouldn't throw any of the following: # SubscriptionCountExceeded # BaseChannelDeniedError # NoBaseChannelError # since we have the token object, and underneath the hood, we have none_ok=have_token # BUT - it does. So catch them and throw. this will make rhnreg_ks # die out, but oh well. at least they don't end up registered, and # without a base channel. try: # don't commit newserv.save(0, channel) except (rhnChannel.SubscriptionCountExceeded, rhnChannel.NoBaseChannelError), channel_error: raise rhnFault(70), None, sys.exc_info()[2] except rhnChannel.BaseChannelDeniedError, channel_error: raise rhnFault(71), None, sys.exc_info()[2]