????

Your IP : 18.226.93.104


Current Path : /proc/335989/root/usr/sbin/
Upload File :
Current File : //proc/335989/root/usr/sbin/rhnreg_ks

#!/usr/bin/python
#
# Registration client for the CloudLinux Network for useage with kickstart 
# Copyright (c) 1999-2006 Red Hat, Inc.  Distributed under GPL.
#
# Authors:
#       Adrian Likins <alikins@redhat.com>
#       James Bowes <jbowes@redhat.com>
#
#  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:
#       reg_num, 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

import gettext
_ = gettext.gettext

sys.path.append("/usr/share/rhn/")

from up2date_client import rhnreg
from up2date_client import hardware
from up2date_client import rpmUtils
from up2date_client import up2dateErrors
from up2date_client import rhncli


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("--username", action="store",
            help=_("Specify a username")),
        self.optparser.add_option("--password", action="store",
            help=_("Specify a password")),
        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("--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")),
        self.optparser.add_option("--use-eus-channel", action="store_true",
            help=_("Subscribe this system to the EUS channel tied to the system's redhat-release")),
        self.optparser.add_option("--contactinfo", action="store_true",
            default=False, help=_("[Deprecated] Read contact info from stdin")),
        self.optparser.add_option("--hardware", dest="nohardware", action="store_false",
            default=True, help=_("Probe and upload hardware info [deprecated]")),
        self.optparser.add_option("--packages", dest="nopackages", action="store_false",
            default=True, help=_("Profile and upload package info [deprecated]")),
        self.optparser.add_option("--virtinfo", dest="novirtinfo", action="store_false",
            default=True, help=_("Upload virtualization info [deprecated]")),
        self.optparser.add_option("--norhnsd", action="store_true",
            default=False, help=_("Do not start rhnsd after completion")),
        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',
            choices=['solo', 'shared']),

    def main(self):

        if self.options.edition is None and isinstance(self.options.activationkey, str):
            RegisterKsCli.__activation_check(self.options.activationkey)

        if self.options.serverUrl:
            rhnreg.cfg.set("serverURL", self.options.serverUrl)

        if self.options.sslCACert:
            rhnreg.cfg.set("sslCACert", self.options.sslCACert)

        if not (self.options.activationkey or 
                (self.options.username and self.options.password)):
            print _("A username and password are required "\
                    "to register a system.")
            sys.exit(-1)

        if rhnreg.registered() and not self.options.force:
            print _("This system is already registered. Use --force to override")
            sys.exit(-1)

        rhnreg.getCaps()
        
        if not self.options.nopackages:
            getArch = 0
            if rhnreg.cfg['supportsExtendedPackageProfile']:
                getArch = 1
            packageList = rpmUtils.getInstalledPackageList(getArch=getArch)
        else:
            packageList = []

        
        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

        # Try to get the virt uuid and put it in "other".
        (virt_uuid, virt_type) = rhnreg.get_virt_info()
        if not virt_uuid is None:
            other['virt_uuid'] = virt_uuid
            other['virt_type'] = virt_type
            
        # If specified, send up the EUS channel label for subscription.
        if self.options.use_eus_channel:
            if self.options.activationkey:
                print _("Usage of --use-eus-channel option with --activationkey is not supported. Please use username and password instead.")
  	        sys.exit(-1)
            if not rhnreg.server_supports_eus():
                print _("The server you are registering against does not support EUS.")
                sys.exit(-1)
            
            channels = rhnreg.getAvailableChannels(self.options.username,
                                                   self.options.password)
            other['channel'] = channels['default_channel']

        try:
            if self.options.activationkey:
                systemId = rhnreg.registerSystem(
                    token=self.options.activationkey,
                    profileName=profilename,
                    other=other,
                    edition=self.options.edition
                )
            else:
                systemId = rhnreg.registerSystem(
                    self.options.username,
                    self.options.password, profilename,
                    other=other,
                    edition=self.options.edition
                )
            # get and write rollout items to files
            system_id = rhnreg.extract_system_id()
            rhnreg.prepareGradualRolloutCertsIfNeeded(system_id)
            # update JWT token
            rhnreg.getAndWriteJWTTokenToFile(systemId)
        except (up2dateErrors.AuthenticationTicketError,
                up2dateErrors.RhnUuidUniquenessError,
                up2dateErrors.CommunicationError,
                up2dateErrors.AuthenticationOrAccountCreationError), e:
            print "%s" % e.errmsg
            sys.exit(1)

        # collect hardware info, inluding hostname
        if not self.options.nohardware:
            rhnreg.sendHardware(systemId, hardwareList)

        if not self.options.nopackages:
            rhnreg.sendPackages(systemId, packageList)

        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
        if isinstance(systemId, unicode):
            rhnreg.writeSystemId(unicode.encode(systemId, 'utf-8'))
        else:
            rhnreg.writeSystemId(systemId)

        # assume successful communication with server
        # remember to save the config options
        rhnreg.cfg.save()

        # Send virtualization information to the server.  We must do this
        # *after* writing out the system id.
        if not self.options.novirtinfo:
            rhnreg.sendVirtInfo(systemId)

        # do this after writing out system id, bug #147513
        if not self.options.norhnsd:
            rhnreg.startRhnsd()

        RegisterKsCli.__runRhnCheck()

    @staticmethod
    def __generateProfileName(hardwareList):
        hostname = None
        ipaddr = None
        profileName = None
        for hw in hardwareList:
            if hw['class'] == 'NETINFO':
                hostname = hw.get('hostname')
                ipaddr = hw.get('ipaddr')
                
        if hostname:
            profileName = hostname
        else:
            if ipaddr:
                profileName = ipaddr
                
        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():
        os.system("/usr/sbin/rhn_check")

    @staticmethod
    def __activation_check(activation_key):
        """Check if activation is possible.

        Possible conditions:
            1. You can activate Solo edition with CLSOLO* key
            2. You can activate Shared edition with key for shared

        Arguments:
            activation_key: key to check

        Returns: Return or exit(-1)
        """

        is_solo_edition = bool(os.path.isfile("/etc/cloudlinux-edition-solo"))
        is_solo_key = bool(activation_key.startswith("CLSOLO"))

        if (is_solo_edition and is_solo_key) or activation_key.startswith("IPL") or \
            (not is_solo_edition and not is_solo_key):
            return

        print("Your license key does not match installed CloudLinux edition.")
        print("Please note that there are multiple CloudLinux OS editions: Shared (https://docs.cloudlinux.com/) and Solo (https://docs.solo.cloudlinux.com/)")
        print("Your current edition is %s, please, use the corresponding key." % ('Solo' if is_solo_edition else 'Shared'))
        print("Contact CloudLinux support in case you need help: https://cloudlinux.zendesk.com/")
        sys.exit(-1)

if __name__ == "__main__":
    cli = RegisterKsCli()
    cli.run()