Module rhnreg_ks
[hide private]
[frames] | no frames]

Source Code for Module rhnreg_ks

  1  #!/usr/bin/python2 
  2  # 
  3  # Registration client for the Spacewalk for useage with kickstart 
  4  # Copyright (c) 1999--2020 Red Hat, Inc.  Distributed under GPLv2. 
  5  # 
  6  # Authors: 
  7  #       Adrian Likins <alikins@redhat.com> 
  8  #       James Bowes <jbowes@redhat.com> 
  9  # 
 10  #  see the output of "--help" for the valid options. 
 11  # 
 12  #  The contact info is in the form or a "key: value" one per line. 
 13  #  valid keys are: 
 14  #       title, first_name, last_name, company, 
 15  #       position, address1, address2, city, state, zip, 
 16  #       country, phone, fax, contact_email, contact_mail, 
 17  #       contact_phone, contact_fax, contact_special, 
 18  #       contact_newsletter 
 19  # 
 20  # 
 21  # 
 22   
 23  import sys 
 24  import os 
 25  from rhn.connections import idn_puny_to_unicode 
 26  from rhn.i18n import bstr, sstr 
 27   
 28  import gettext 
 29  t = gettext.translation('rhn-client-tools', fallback=True) 
 30  # Python 3 translations don't have a ugettext method 
 31  if not hasattr(t, 'ugettext'): 
 32      t.ugettext = t.gettext 
 33  _ = t.ugettext 
 34   
 35  from up2date_client import rhnreg 
 36  from up2date_client import hardware 
 37  from up2date_client import pkgUtils 
 38  from up2date_client import up2dateErrors 
 39  from up2date_client import rhncli 
 40  from up2date_client.pmPlugin import PM_PLUGIN_NAME, PM_PLUGIN_CONF 
41 42 43 -class RegisterKsCli(rhncli.RhnCli):
44
45 - def __init__(self):
46 super(RegisterKsCli, self).__init__() 47 48 self.optparser.add_option("--profilename", action="store", 49 help=_("Specify a profilename")), 50 self.optparser.add_option("--username", action="store", 51 help=_("Specify a username")), 52 self.optparser.add_option("--password", action="store", 53 help=_("Specify a password")), 54 self.optparser.add_option("--systemorgid", action="store", 55 help=_("Specify an organizational id for this system")), 56 self.optparser.add_option("--serverUrl", action="store", 57 help=_("Specify a url to use as a server")), 58 self.optparser.add_option("--sslCACert", action="store", 59 help=_("Specify a file to use as the ssl CA cert")), 60 self.optparser.add_option("--activationkey", action="store", 61 help=_("Specify an activation key")), 62 self.optparser.add_option("--use-eus-channel", action="store_true", 63 help=_("Subscribe this system to the EUS channel tied to the system's redhat-release")), 64 self.optparser.add_option("--contactinfo", action="store_true", 65 default=False, help=_("[Deprecated] Read contact info from stdin")), 66 self.optparser.add_option("--nohardware", action="store_true", 67 default=False, help=_("Do not probe or upload any hardware info")), 68 self.optparser.add_option("--nopackages", action="store_true", 69 default=False, help=_("Do not profile or upload any package info")), 70 self.optparser.add_option("--novirtinfo", action="store_true", 71 default=False, help=_("Do not upload any virtualization info")), 72 self.optparser.add_option("--norhnsd", action="store_true", 73 default=False, help=_("Do not start rhnsd after completion")), 74 self.optparser.add_option("--force", action="store_true", default=False, 75 help=_("Register the system even if it is already registered")),
76
77 - def main(self):
78 if self.options.serverUrl: 79 rhnreg.cfg.set("serverURL", self.options.serverUrl) 80 if self.options.sslCACert: 81 rhnreg.cfg.set("sslCACert", self.options.sslCACert) 82 83 if not (self.options.activationkey or 84 (self.options.username and self.options.password)): 85 print(_("A username and password are required "\ 86 "to register a system.")) 87 sys.exit(-1) 88 89 if rhnreg.registered() and not self.options.force: 90 print(_("This system is already registered. Use --force to override")) 91 sys.exit(-1) 92 93 rhnreg.getCaps() 94 95 if not self.options.nopackages: 96 getArch = 0 97 if rhnreg.cfg['supportsExtendedPackageProfile']: 98 getArch = 1 99 packageList = pkgUtils.getInstalledPackageList(getArch=getArch) 100 else: 101 packageList = [] 102 103 104 hardwareList = hardware.Hardware() 105 106 if self.options.profilename: 107 profilename = self.options.profilename 108 else: 109 profilename = RegisterKsCli.__generateProfileName(hardwareList) 110 111 other = {} 112 if self.options.systemorgid: 113 other['org_id'] = self.options.systemorgid 114 115 # Try to get the virt uuid and put it in "other". 116 (virt_uuid, virt_type) = rhnreg.get_virt_info() 117 if not virt_uuid is None: 118 other['virt_uuid'] = virt_uuid 119 other['virt_type'] = virt_type 120 121 # If specified, send up the EUS channel label for subscription. 122 if self.options.use_eus_channel: 123 if self.options.activationkey: 124 print(_("Usage of --use-eus-channel option with --activationkey is not supported. Please use username and password instead.")) 125 sys.exit(-1) 126 if not rhnreg.server_supports_eus(): 127 print(_("The server you are registering against does not support EUS.")) 128 sys.exit(-1) 129 130 channels = rhnreg.getAvailableChannels(self.options.username, 131 self.options.password) 132 other['channel'] = channels['default_channel'] 133 134 try: 135 systemId = rhnreg.registerSystem(self.options.username, 136 self.options.password, profilename, self.options.activationkey, other) 137 except (up2dateErrors.AuthenticationTicketError, 138 up2dateErrors.RhnUuidUniquenessError, 139 up2dateErrors.CommunicationError, 140 up2dateErrors.AuthenticationOrAccountCreationError): 141 e = sys.exc_info()[1] 142 print("%s" % e.errmsg) 143 sys.exit(1) 144 145 # collect hardware info, inluding hostname 146 if not self.options.nohardware: 147 rhnreg.sendHardware(systemId, hardwareList) 148 149 if not self.options.nopackages: 150 rhnreg.sendPackages(systemId, packageList) 151 152 if self.options.contactinfo: 153 print(_("Warning: --contactinfo option has been deprecated. Please login to the server web user Interface and update your contactinfo. ")) 154 155 # write out the new id 156 rhnreg.writeSystemId(bstr(systemId)) 157 158 # assume successful communication with server 159 # remember to save the config options 160 rhnreg.cfg.save() 161 162 # Send virtualization information to the server. We must do this 163 # *after* writing out the system id. 164 if not self.options.novirtinfo: 165 rhnreg.sendVirtInfo(systemId) 166 167 # do this after writing out system id, bug #147513 168 if not self.options.norhnsd: 169 rhnreg.startRhnsd() 170 171 try: 172 present, conf_changed = rhnreg.pluginEnable() 173 if not present: 174 sys.stderr.write(sstr(_("Warning: %s is not present, could not enable it.") % PM_PLUGIN_NAME)) 175 except IOError: 176 e = sys.exc_info()[1] 177 sys.stderr.write(sstr(_("Warning: Could not open %s\n%s is not enabled.\n") % (PM_PLUGIN_CONF, PM_PLUGIN_NAME) + e.errmsg)) 178 return RegisterKsCli.__runRhnCheck(self.options.verbose)
179 180 @staticmethod
181 - def __generateProfileName(hardwareList):
182 hostname = None 183 ipaddr = ip6addr = None 184 profileName = None 185 for hw in hardwareList: 186 if hw['class'] == 'NETINFO': 187 hostname = hw.get('hostname') 188 ipaddr = hw.get('ipaddr') 189 ip6addr = hw.get('ipaddr6') 190 191 if hostname: 192 profileName = idn_puny_to_unicode(hostname) 193 elif ipaddr: 194 profileName = ipaddr 195 elif ip6addr: 196 profileName = ip6addr 197 198 if not profileName: 199 print(_("A profilename was not specified, "\ 200 "and hostname and IP address could not be determined "\ 201 "to use as a profilename, please specify one.")) 202 sys.exit(-1) 203 204 return profileName
205 206 @staticmethod
207 - def __runRhnCheck(verbose):
208 cmd = "/usr/sbin/rhn_check" 209 if verbose: 210 cmd += ' -' + ('v' * verbose) 211 ret = os.system(cmd) 212 return ret >> 8
213 214 if __name__ == "__main__": 215 cli = RegisterKsCli() 216 cli.run() 217