YANZ UPLOAD ANJG

#!/usr/bin/python3.6 # # Registration client for the Spacewalk for useage with kickstart # Copyright (c) 1999--2020 Red Hat, Inc. Distributed under GPLv2. # # Authors: # Adrian Likins # James Bowes # # see the output of "--help" for the valid options. # # The contact info is in the form or a "key: value" one per line. # valid keys are: # title, first_name, last_name, company, # position, address1, address2, city, state, zip, # country, phone, fax, contact_email, contact_mail, # contact_phone, contact_fax, contact_special, # contact_newsletter # # # import sys import os from rhn.connections import idn_puny_to_unicode from rhn.i18n import bstr, sstr import gettext t = gettext.translation('rhn-client-tools', fallback=True) # Python 3 translations don't have a ugettext method if not hasattr(t, 'ugettext'): t.ugettext = t.gettext _ = t.ugettext from up2date_client import rhnreg from up2date_client import hardware from up2date_client import up2dateErrors from up2date_client import rhncli def _read_activation_key_from_stdin(stream=None): """Read an activation key from *stream* (stdin by default). Used when --activationkey=- is given so the key is not placed on the command line (process argument list). Only the first line is consumed and the single trailing newline the writer appends is stripped; the key is otherwise passed through verbatim. """ if stream is None: stream = sys.stdin return stream.readline().rstrip('\n') class RegisterKsCli(rhncli.RhnCli): def __init__(self): super(RegisterKsCli, self).__init__() self.optparser.add_option("--profilename", action="store", help=_("Specify a profilename")), self.optparser.add_option("--systemorgid", action="store", help=_("Specify an organizational id for this system")), self.optparser.add_option("--serverUrl", action="store", help=_("Specify a url to use as a server")), self.optparser.add_option("--serverURLipv6", action="store", help=_("Specify a url to use as a server over IPv6")), self.optparser.add_option("--sslCACert", action="store", help=_("Specify a file to use as the ssl CA cert")), self.optparser.add_option("--activationkey", action="store", help=_("Specify an activation key. Use --activationkey=- to read " "the key from standard input.")), self.optparser.add_option("--contactinfo", action="store_true", default=False, help=_("[Deprecated] Read contact info from stdin")), self.optparser.add_option("--strict-edition", action="store_true", default=False, help=_("Do not start registration if edition does not match the key")), self.optparser.add_option("--migrate-silently", dest='silent_migration', action="store_true", default=False, help=_("Do not start interactive prompts for user, " "start migration to different edition automatically")), self.optparser.add_option("--force", action="store_true", default=False, help=_("Register the system even if it is already registered")), self.optparser.add_option('--edition', action='store', help='Specify one of the supported editions: solo, shared, admin', choices=['solo', 'shared', 'admin', 'auto'], default='auto'), def main(self): if self.options.serverUrl: rhnreg.cfg.set("serverURL", self.options.serverUrl) if self.options.serverURLipv6: rhnreg.cfg.set("serverURLipv6", self.options.serverURLipv6) if self.options.sslCACert: rhnreg.cfg.set("sslCACert", self.options.sslCACert) if rhnreg.registered() and not self.options.force: print(_("This system is already registered. Use --force to override")) sys.exit(-1) if self.options.activationkey == '-': # '-' is the stdin sentinel: read the key from stdin so it is not # placed on the command line (argv). See CLOS-4594/F-50. Done after the # already-registered guard so a key piped in is never drained from stdin # only to be discarded when registration is skipped. self.options.activationkey = _read_activation_key_from_stdin() if not self.options.activationkey: print(_("An activationkey is required " "to register a system.")) sys.exit(-1) hardwareList = hardware.Hardware() if self.options.profilename: profilename = self.options.profilename else: profilename = RegisterKsCli.__generateProfileName(hardwareList) other = {} if self.options.systemorgid: other['org_id'] = self.options.systemorgid rhnreg.checkLicenseKey(self.options.activationkey, self.options.strict_edition, self.options.silent_migration) try: systemId = rhnreg.registerSystem( profileName=profilename, token=self.options.activationkey, other=other, edition=self.options.edition ) # we run rhn_check below, no need to ask for JWT and Rollout here except (up2dateErrors.AuthenticationTicketError, up2dateErrors.RhnUuidUniquenessError, up2dateErrors.CommunicationError, up2dateErrors.AuthenticationOrAccountCreationError): e = sys.exc_info()[1] print("%s" % e.errmsg) sys.exit(1) if self.options.contactinfo: print(_("Warning: --contactinfo option has been deprecated. " "Please login to the server web user " "Interface and update your contactinfo. ")) # write out the new id rhnreg.writeSystemId(bstr(systemId)) # assume successful communication with server # remember to save the config options rhnreg.cfg.save() return RegisterKsCli.__runRhnCheck(self.options.verbose, allowTransition=True) @staticmethod def __generateProfileName(hardwareList): hostname = None ipaddr = ip6addr = None profileName = None for hw in hardwareList: if hw['class'] == 'NETINFO': hostname = hw.get('hostname') ipaddr = hw.get('ipaddr') ip6addr = hw.get('ipaddr6') if hostname: profileName = idn_puny_to_unicode(hostname) elif ipaddr: profileName = ipaddr elif ip6addr: profileName = ip6addr if not profileName: print(_("A profilename was not specified, "\ "and hostname and IP address could not be determined "\ "to use as a profilename, please specify one.")) sys.exit(-1) return profileName @staticmethod def __runRhnCheck(verbose, allowTransition): cmd = "/usr/sbin/rhn_check" if verbose: cmd += ' -' + ('v' * verbose) if allowTransition: cmd += ' --allow-transition' ret = os.system(cmd) return ret >> 8 if __name__ == "__main__": cli = RegisterKsCli() cli.run()