3 cjC @sUdZddlZddlmZddlmZmZddlmZddl m Z ddl m Z m Z ddlmZejeZd Zd d d ed ddgeedededgdgdZe e eZee eeddddZGdddeZGdddZdddZdS) zDRed Hat Subscription: Register Red Hat Enterprise Linux based systemN)dedent)subputil)Cloud)Config) MetaSchema get_meta_doc) PER_INSTANCEa[Register a Red Hat system either by username and password *or* activation and org. Following a successful registration, you can: - auto-attach subscriptions - set the service level - add subscriptions based on pool id - enable/disable yum repositories based on repo id - alter the rhsm_baseurl and server-hostname in ``/etc/rhsm/rhs.conf``. Zcc_rh_subscriptionzRed Hat Subscriptionz.Register Red Hat Enterprise Linux based systemZfedoraZrhelZ openeulerz rh_subscription: username: joe@foo.bar ## Quote your password if it has symbols to be safe password: '1234abcd' zk rh_subscription: activation-key: foobar org: "ABC" a rh_subscription: activation-key: foobar org: "ABC" auto-attach: true service-level: self-support add-pool: - 1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a - 2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b enable-repo: - repo-id-to-enable - other-repo-id-to-enable disable-repo: - repo-id-to-disable - other-repo-id-to-disable # Alter the baseurl in /etc/rhsm/rhsm.conf rhsm-baseurl: http://url # Alter the server hostname in /etc/rhsm/rhsm.conf server-hostname: foo.bar.com rh_subscription)idnametitle descriptionZdistrosZ frequencyZexamplesZactivate_by_schema_keys)r cfgcloudargsreturnc Csjt|td}|js$tjd|dS|js\y|j\}}|dk rLt||j}|s`td|jr|j dk r|j stdq|j jdn$|jr|j stdn |j jd|j dk rt|j tsd }t||j|j } | std j|j |j} | std |jd Wn<tk rX} z|jt| |jd WYdd} ~ XnXn |jddS)N)logz%s: module not configured.Tz-Registration failed or did not run completelyzSetting of service-level failedz(Completed auto-attach with service levelzSetting auto-attach failedzCompleted auto-attachz"Pools must in the format of a listzUnable to attach pools {0}zUnable to add or remove reposz-rh_subscription plugin completed successfullyz4rh_subscription plugin did not complete successfullyzSystem is already registered)SubscriptionManagerLOG is_configureddebug is_registered _verify_keysSubscriptionError rhn_register auto_attach servicelevel_set_service_levelr_set_auto_attachpools isinstancelistaddPoolformat update_repos log_successlog_warnstr) r rrrZsmZverifyZ verify_msgZcontZ pool_failZ return_stater*(/usr/lib/python3.6/cc_rh_subscription.pyhandleRsJ          r,c@s eZdZdS)rN)__name__ __module__ __qualname__r*r*r*r+rsrc @seZdZddddddddd d d g Zd'd dZddZddZddZddZddZ ddZ ddZ ddZ dd Z d!d"Zd#d$Zd%d&Zd S)(rorgzactivation-keyusernamepasswordz disable-repoz enable-repozadd-poolz rhsm-baseurlzserver-hostnamez auto-attachz service-levelNcCs|dkr t}||_||_|jjdi|_|jjd|_|jjd|_|jjd|_|jjd|_|jjd|_ |jjd|_ |jjd|_ |jjd |_ |jjd |_ |jjd |_|jjd |_dS) Nr z rhsm-baseurlzserver-hostnamezadd-poolzactivation-keyr0r1r2z auto-attachz enable-repoz disable-repoz service-level)rrrgetrhel_cfg rhsm_baseurlserver_hostnamer activation_keyr0useridr2r enable_repo disable_repor)selfrrr*r*r+__init__s zSubscriptionManager.__init__cCs|jj|dS)z>Simple wrapper for logging info messages. Useful for unittestsN)rinfo)r;msgr*r*r+r&szSubscriptionManager.log_successcCs|jj|dS)zASimple wrapper for logging warning messages. Useful for unittestsN)rZwarning)r;r>r*r*r+r'szSubscriptionManager.log_warncCsx4|jD]*}||jkrdj|dj|j}d|fSqW|jdk rftj|jpVtj|j rfd}d|fS|jdk r|j stjt |jrd}d|fSdS) zq Checks that the keys in the rh_subscription dict from the user-data are what we expect. z?{0} is not a valid key for rh_subscription. Valid keys are: {1}z, FNz8The key auto-attach must be a boolean value (True/False zqThe service-level key must be used in conjunction with the auto-attach key. Please re-run with auto-attach: TrueT)TN) r4 valid_rh_keysr$joinrrZis_trueZis_falserr()r;kZbad_keyZnot_boolZno_autor*r*r+rs     z SubscriptionManager._verify_keysc Cs.dg}y t|Wntjk r(dSXdS)zg Checks if the system is already registered and returns True if so, else False ZidentityFT) _sub_man_clirProcessExecutionError)r;cmdr*r*r+rs  z!SubscriptionManager.is_registeredcCs|jdk r|jdk rddj|jdj|jg}|jdk rJ|jdj|j|jdk rf|jdj|jyt|ddd }Wn>tjk r}z |j d kr|j d j|j d Sd}~XnXn|j dk o|j dk rdd j|j dj|j g}|jdk r |jdj|j|jdk r(|jdj|jyt|ddd }WnBtjk r~}z"|j d krp|j d j|j d Sd}~XnXn|j d|j dd S|jddj}|jjd|dS)z Registers the system by userid and password or activation key and org. Returns True when successful False when not. Nregisterz--activationkey={0}z --org={0}z --baseurl={0}z--serverurl={0}T) logstring_valrzRegistration failed due to: {0}Fz--username={0}z--password={0}z8Unable to register system due to incomplete information.z9Use either activationkey and org *or* userid and passwordzID: z"Registered successfully with ID %s)r7r0r$r5appendr6rBrrCstdoutr'stderrr8r2splitrstriprr)r;rD return_outr)Zreg_idr*r*r+rsL        z SubscriptionManager.rhn_registercCsdddj|jg}yt|d}Wnptjk r}zR|jjdkrnx>|jjdD]}|dkrR|j|qRWn|jdj|j j dSd}~XnXx&|jdD]}|dkr|j j |qWd S) Nattachz--autoz--servicelevel={0}rrG z*Setting the service level failed with: {0}FT) r$rrBrrCrJrMrLr'rKstriprr)r;rDrNr)liner*r*r+r.s$z&SubscriptionManager._set_service_levelcCsxddg}yt|d}Wn2tjk rJ}z|jdj|dSd}~XnXx&|jdD]}|dkrX|jj|qXWdS) NrOz--autorzAuto-attach failed with: {0}FrPrGT)rBrrCr'r$rLrr)r;rDrNr)rRr*r*r+rHsz$SubscriptionManager._set_auto_attachcCsXg}g}dddg}t|d}|jjd}dddg}t|d}|jjd}||fS)zh Gets the list pools for the active subscription and returns them in list form. r"z --availablez --pool-onlyrrPz --consumed)rBrMrL)r;Z availableconsumedrDresultsr*r*r+ _getPoolsTs    zSubscriptionManager._getPoolscCsddg}t|d}g}x2|jdD]$}d|kr$|j|jddjq$Wddg}t|d}g}x2|jdD]$}d|krp|j|jddjqpW||fS) zl Obtains the current list of active yum repositories and returns them in list form. reposz--list-enabledrrPzRepo ID::rHz--list-disabled)rBrLrIrQ)r;rDrN active_reposZrepoinactive_reposr*r*r+ _getReposhs  zSubscriptionManager._getReposcCst|dkr|jjddS|j\}}g}dg}x>|D]6}||kr^||kr^|jdj|q8|jdj|q8Wt|dkr|j|y(t||jjddj |j d d dSt j k r}z|jd j||d Sd }~XnXd S)zl Takes a list of subscription pools and "attaches" them to the current subscription rzNo pools to attachTrOz --pool={0}zPool {0} is not availablez/Attached the following pools to your system: %sz, z--pool=rGz$Unable to attach pool {0} due to {1}FN) lenrrrUrIr$r'extendrBr@replacerrC)r;r Zpool_availableZ pool_consumedZ pool_listrDZpoolr)r*r*r+r#~s,      zSubscriptionManager.addPoolc Cs@|j}|j}|dkrg}|dkr$g}t|ts<|jddSt|tsT|jddSt|dkr|t|dkr||jjddS|j\}}g}g}x0|D](}||kr|j dj |q|j |qWg}g} x0|D](}||kr|j dj |q| j |qWt|dkrDx8|D]0} | |kr.|jjd | n|jd j | qWt| dkrpx| D]} |jjd | qXWd g} t|dkr| j |t|dkr| j |y t | Wn4t jk r} z|jd j | dSd} ~ XnXt|dkr|jjddj|jddt|dkr<|jjddj|jdddS)z Takes a list of yum repo ids that need to be disabled or enabled; then it verifies if they are already enabled or disabled and finally executes the action to disable or enable Nz&Repo IDs must in the format of a list.Frz No repo IDs to enable or disableTz --enable={0}z --disable={0}zRepo %s is already enabledz!Repo {0} does not appear to existz.Repo %s not disabled because it is not enabledrVz Unable to alter repos due to {0}zEnabled the following repos: %sz, z --enable=rGz Disabled the following repos: %sz --disable=)r9r:r!r"r'r[rrrZrIr$r\rBrrCr@r]) r;ZereposZdreposrXrYZ enable_listZenable_list_failZrepoidZ disable_listZdisable_list_failZfailrDr)r*r*r+r%sr              z SubscriptionManager.update_reposcCst|jr|jp|jS)N)boolr8r2r7)r;r*r*r+rsz!SubscriptionManager.is_configured)N)r-r.r/r?r<r&r'rrrrrrUrZr#r%rr*r*r*r+rs0 #H "VrFcCstjdg||dS)z Uses the prefered cloud-init subprocess def of subp.subp and runs subscription-manager. Breaking this to a separate function for later use in mocking and unittests zsubscription-manager)Z logstring)r)rDrFr*r*r+rBsrB)F)__doc__ZloggingtextwraprZ cloudinitrrZcloudinit.cloudrZcloudinit.configrZcloudinit.config.schemarrZcloudinit.settingsr Z getLoggerr-rZMODULE_DESCRIPTIONmetar(r"r, ExceptionrrrBr*r*r*r+s:     6p