
­­­­­­­­­­­­­­­­­­
<!DOCTYPE html>
<html>
3
cjC              
   @   s   U d Z ddlZddlmZ ddlmZmZ ddlmZ ddl	m
Z
 ddlmZmZ ddlmZ ejeZd	Zd
ddedddgeedededgdgdZeeeZ ee
eeddddZG dd deZG dd dZdddZdS ) zDRed Hat Subscription: Register Red Hat Enterprise Linux based system    N)dedent)subputil)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``.
Zcc_rh_subscriptionzRed Hat Subscriptionz.Register Red Hat Enterprise Linux based systemZfedoraZrhelZ	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)idnametitledescriptionZdistrosZ	frequencyZexamplesZactivate_by_schema_keys)r   cfgcloudargsreturnc             C   sj  t |td}|j s$tjd|  d S |j s\y|j \}}|dk	rLt||j }|s`td|jr|j	d k	r|j
 stdq|jjd n$|jr|j stdn|jjd |jd k	 rt|jtsd	}t||j|j}	|	 std
j|j|j }	|	std|jd W n< tk
rX }
 z|jt|
 |jd W Y d d }
~
X nX n
|jd d S )N)logz%s: module not configured.Tz-Registration failed or did not run completelyzSetting of service-level failedz(Completed auto-attach with service levelzSetting auto-attach failedzCompleted auto-attachz"Pools must in the format of a listzUnable to attach pools {0}zUnable to add or remove reposz-rh_subscription plugin completed successfullyz4rh_subscription plugin did not complete successfullyzSystem is already registered)SubscriptionManagerLOGis_configureddebugis_registered_verify_keysSubscriptionErrorrhn_registerauto_attachservicelevel_set_service_levelr   _set_auto_attachpools
isinstancelistaddPoolformatupdate_reposlog_successlog_warnstr)r   r   r   r   ZsmZverifyZ
verify_msgZcontZ	pool_failZreturn_state r*   (/usr/lib/python3.6/cc_rh_subscription.pyhandleR   sJ    


r,   c               @   s   e Zd ZdS )r   N)__name__
__module____qualname__r*   r*   r*   r+   r      s   r   c               @   s   e Zd Zddddddddd	d
dgZd'ddZdd Zdd Zdd Zdd Zdd Z	dd Z
dd Zdd Zdd  Zd!d" Zd#d$ Zd%d& ZdS )(r   orgzactivation-keyusernamepasswordzdisable-repozenable-repozadd-poolzrhsm-baseurlzserver-hostnamezauto-attachzservice-levelNc             C   s   |d krt }|| _|| _| jjdi | _| jjd| _| jjd| _| jjd| _| jjd| _| jjd| _	| jjd| _
| jjd| _| jjd	| _| jjd
| _| jjd| _| jjd| _d S )Nr
   zrhsm-baseurlzserver-hostnamezadd-poolzactivation-keyr0   r1   r2   zauto-attachzenable-repozdisable-repozservice-level)r   r   r   getrhel_cfgrhsm_baseurlserver_hostnamer    activation_keyr0   useridr2   r   enable_repodisable_repor   )selfr   r   r*   r*   r+   __init__   s     zSubscriptionManager.__init__c             C   s   | j j| dS )z>Simple wrapper for logging info messages. Useful for unittestsN)r   info)r;   msgr*   r*   r+   r&      s    zSubscriptionManager.log_successc             C   s   | j j| dS )zASimple wrapper for logging warning messages. Useful for unittestsN)r   Zwarning)r;   r>   r*   r*   r+   r'      s    zSubscriptionManager.log_warnc             C   s   x4| j D ]*}|| jkrdj|dj| j}d|fS qW | jdk	rftj| jpVtj| j rfd}d|fS | jdk	r| j stjt	| jrd}d|fS dS )	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$   joinr   r   Zis_trueZis_falser   r(   )r;   kZbad_keyZnot_boolZno_autor*   r*   r+   r      s    


z SubscriptionManager._verify_keysc             C   s.   dg}yt | W n tjk
r(   dS X dS )zg
        Checks if the system is already registered and returns
        True if so, else False
        ZidentityFT)_sub_man_clir   ProcessExecutionError)r;   cmdr*   r*   r+   r      s    z!SubscriptionManager.is_registeredc             C   s  | j dk	r| jdk	rddj| j dj| jg}| jdk	rJ|jdj| j | jdk	rf|jdj| j yt|ddd	 }W n> tjk
r } z |j	d
kr| j
dj|j dS d}~X nX n| jdk	o| jdk	rddj| jdj| jg}| jdk	r
|jdj| j | jdk	r(|jdj| j yt|ddd	 }W nB tjk
r~ } z"|j	d
krp| j
dj|j dS d}~X nX n| j
d | j
d dS |jdd j }| jjd| dS )z
        Registers the system by userid and password or activation key
        and org.  Returns True when successful False when not.
        Nregisterz--activationkey={0}z	--org={0}z--baseurl={0}z--serverurl={0}T)logstring_valr    zRegistration 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 passwordzID:    z"Registered successfully with ID %s)r7   r0   r$   r5   appendr6   rB   r   rC   stdoutr'   stderrr8   r2   splitrstripr   r   )r;   rD   
return_outr)   Zreg_idr*   r*   r+   r      sL    




z SubscriptionManager.rhn_registerc             C   s   dddj | jg}yt|d }W np tjk
r } zR|jj dkrnx>|jjdD ]}|dkrR| j| qRW n| jdj |j	j
  dS d }~X nX x&|jdD ]}|dkr| jj| qW d	S )
Nattachz--autoz--servicelevel={0}r   rG   
z*Setting the service level failed with: {0}FT)r$   r   rB   r   rC   rJ   rM   rL   r'   rK   stripr   r   )r;   rD   rN   r)   liner*   r*   r+   r   .  s$    z&SubscriptionManager._set_service_levelc             C   sx   ddg}yt |d }W n2 tjk
rJ } z| jdj| dS d }~X nX x&|jdD ]}|dkrX| jj| qXW dS )	NrO   z--autor   zAuto-attach failed with: {0}FrP   rG   T)rB   r   rC   r'   r$   rL   r   r   )r;   rD   rN   r)   rR   r*   r*   r+   r   H  s    z$SubscriptionManager._set_auto_attachc             C   sX   g }g }dddg}t |d }|j jd}dddg}t |d }|j jd}||fS )zh
        Gets the list pools for the active subscription and returns them
        in list form.
        r"   z--availablez--pool-onlyr   rP   z
--consumed)rB   rM   rL   )r;   Z	availableconsumedrD   resultsr*   r*   r+   	_getPoolsT  s    

zSubscriptionManager._getPoolsc             C   s   ddg}t |d }g }x2|jdD ]$}d|kr$|j|jdd j  q$W ddg}t |d }g }x2|jdD ]$}d|krp|j|jdd j  qpW ||fS )	zl
        Obtains the current list of active yum repositories and returns
        them in list form.
        reposz--list-enabledr   rP   zRepo ID::rH   z--list-disabled)rB   rL   rI   rQ   )r;   rD   rN   active_reposZrepoinactive_reposr*   r*   r+   	_getReposh  s    zSubscriptionManager._getReposc             C   s   t |dkr| jjd dS | j \}}g }dg}x>|D ]6}||kr^||kr^|jdj| q8| jdj| q8W t |dkr|j| y(t| | jjddj	|j
d	d
 dS  tjk
r } z| jdj|| dS d}~X nX dS )zl
        Takes a list of subscription pools and "attaches" them to the
        current subscription
        r   zNo pools to attachTrO   z
--pool={0}zPool {0} is not availablez/Attached the following pools to your system: %sz, z--pool=rG   z$Unable to attach pool {0} due to {1}FN)lenr   r   rU   rI   r$   r'   extendrB   r@   replacer   rC   )r;   r    Zpool_availableZpool_consumedZ	pool_listrD   Zpoolr)   r*   r*   r+   r#   ~  s,    

zSubscriptionManager.addPoolc             C   s@  | j }| j}|dkrg }|dkr$g }t|ts<| jd dS t|tsT| jd dS t|dkr|t|dkr|| jjd dS | j \}}g }g }x0|D ](}||kr|j	dj
| q|j	| qW g }g }	x0|D ](}||kr|j	dj
| q|	j	| qW t|dkrDx8|D ]0}
|
|kr.| jjd	|
 n| jd
j
|
 qW t|	dkrpx|	D ]}
| jjd|
 qXW dg}t|dkr|j| t|dkr|j| yt| W n4 tjk
r } z| jdj
| dS d}~X nX t|dkr| jjddj|jdd t|dkr<| jjddj|jdd dS )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.Fr   z No repo IDs to enable or disableTz--enable={0}z--disable={0}zRepo %s is already enabledz!Repo {0} does not appear to existz.Repo %s not disabled because it is not enabledrV   z Unable to alter repos due to {0}zEnabled the following repos: %sz, z	--enable=rG   z Disabled the following repos: %sz
--disable=)r9   r:   r!   r"   r'   r[   r   r   rZ   rI   r$   r\   rB   r   rC   r@   r]   )r;   ZereposZdreposrX   rY   Zenable_listZenable_list_failZrepoidZdisable_listZdisable_list_failZfailrD   r)   r*   r*   r+   r%     sr    










z SubscriptionManager.update_reposc             C   s   t | jr| jp| jS )N)boolr8   r2   r7   )r;   r*   r*   r+   r     s    z!SubscriptionManager.is_configured)N)r-   r.   r/   r?   r<   r&   r'   r   r   r   r   r   rU   rZ   r#   r%   r   r*   r*   r*   r+   r      s0   
#H"Vr   Fc             C   s   t j dg|  |dS )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
    zsubscription-manager)Z	logstring)r   )rD   rF   r*   r*   r+   rB     s    rB   )F)__doc__Zloggingtextwrapr   Z	cloudinitr   r   Zcloudinit.cloudr   Zcloudinit.configr   Zcloudinit.config.schemar   r   Zcloudinit.settingsr	   Z	getLoggerr-   r   ZMODULE_DESCRIPTIONmetar(   r"   r,   	Exceptionr   r   rB   r*   r*   r*   r+   <module>   s:   
6  p