def register_system(client, minion): """ Adds a spacewalk registration for a minion. """ # ask for the minion data to get its id that tell us # if it is registered, and data to register it ret = client.cmd_iter(minion, GRAINS_ITEMS_CMD) for grains in ret: logger.info("Registering new minion: %s", minion) if minion in grains: values = grains[minion]['ret'] logger.debug("%s grains:\n%s", minion, pp.pformat(values)) username = client.cmd(minion, PILLAR_GET_CMD, [ADMIN_USER_PILLAR_KEY]) if not username[minion]: logger.error("Can't get admin user from pillar key '%s'", ADMIN_USER_PILLAR_KEY) continue user = rhnUser.search(username[minion]) rhnSQL.clear_log_id() newserv = rhnServer.Server(user, values['osarch']) token = client.cmd(minion, PILLAR_GET_CMD, [ACTIVATION_KEY_PILLAR_KEY]) if not token[minion]: tokens_obj = rhnServer.search_org_token(user.contact["org_id"]) rhnFlags.set("universal_registration_token", tokens_obj) else: tokens_obj = rhnServer.search_token(token[minion]) rhnFlags.set("registration_token", tokens_obj) # reserve the id newserv.getid() # overrite the digital id # FIXME: None of these values appear in the systems properties newserv.server['digital_server_id'] = 'SALT-ID-%s' % minion newserv.server['release'] = values['osrelease'] newserv.server['os'] = values['osfullname'] newserv.server['name'] = minion newserv.server['running_kernel'] = values['kernelrelease'] newserv.virt_uuid = None newserv.save() rhnSQL.commit() logger.info("%s registered as %s", minion, newserv.getid()) else: logger.error("Registration failed: Can't get grains for %s", minion)
def test_server_search(use_key=0): if use_key: user = None else: user = '******' u = rhnUser.search(user) s = rhnServer.Server(u, arch="athlon") s.server["release"] = "2.1AS" s.server["name"] = "test 1" if use_key: rhnFlags.set("registration_token", 'a02487cf77e72f86338f44212d23140d') s.save() print s.server["id"]
def new_server(user, org_id): serv = rhnServer.Server(user, org_id=org_id) # serv.default_description() params = build_sys_params_with_username(username=user.contact['login']) # print params serv.server['release'] = params['os_release'] serv.server['os'] = "Unittest Distro" serv.server['name'] = params['profile_name'] serv.set_arch('i386') serv.default_description() serv.getid() serv.gen_secret() serv.save() return serv
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]