Your IP : 216.73.217.13


Current Path : /usr/lib/python3/dist-packages/cloudinit/config/__pycache__/
Upload File :
Current File : //usr/lib/python3/dist-packages/cloudinit/config/__pycache__/cc_rh_subscription.cpython-38.pyc

U

�Ad0D�
@s�UdZddlmZddlmZddlmZddlmZmZddl	m
Z
ddlmZddl
mZmZdd	lmZe�e�Zd
Zddd
eddgeed�ed�ed�gdgd�Zeed<ee�Zeee
eedd�dd�ZGdd�de�ZGdd�d�Zd!dd �ZdS)"zDRed Hat Subscription: Register Red Hat Enterprise Linux based system�)�Logger)�dedent��log)�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``.
Zcc_rh_subscriptionzRed Hat Subscriptionz.Register Red Hat Enterprise Linux based systemZfedoraZrhelz�            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: 12345
            a�            rh_subscription:
                activation-key: foobar
                org: 12345
                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�titleZdescriptionZdistrosZ	frequencyZexamplesZactivate_by_schema_keys�metaN)r�cfg�cloudr�args�returnc
Csft||d�}|��s$|�d|�dS|���sXz�|��\}}|dk	rLt|��|��}|s`td��|jr�|jdk	r�|�	�s�td��q�|j
�d�n$|jr�|��s�td��n|j
�d�|jdk	r�t
|jt�s�d	}	t|	��|�|j�}
|
s�td
�|j���|��}
|
�std��|�d�Wn<tk
�rT}z|�t|��|�d
�W5d}~XYnXn
|�d�dS)Nrz%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)�SubscriptionManager�
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)rrrrrZsmZverifyZ
verify_msgZcontZ	pool_failZreturn_stat�e�r+�E/usr/lib/python3/dist-packages/cloudinit/config/cc_rh_subscription.py�handleSsN
�	



�r-c@seZdZdS)rN)�__name__�
__module__�__qualname__r+r+r+r,r�src@s�eZdZddddddddd	d
dgZd'd
d�Zdd�Zdd�Zdd�Zdd�Zdd�Z	dd�Z
dd�Zdd�Zdd �Z
d!d"�Zd#d$�Zd%d&�ZdS)(r�org�activation-key�username�password�disable-repo�enable-repo�add-pool�rhsm-baseurl�server-hostname�auto-attach�
service-levelNcCs�|dkrt}||_||_|j�di�|_|j�d�|_|j�d�|_|j�d�|_|j�d�|_|j�d�|_	|j�d�|_
|j�d�|_|j�d	�|_|j�d
�|_
|j�d�|_|j�d�|_dS)
Nr
r8r9r7r2r1r3r4r:r6r5r;)�LOGrr�get�rhel_cfg�rhsm_baseurl�server_hostnamer!�activation_keyr1�useridr4r�enable_repo�disable_repor)�selfrrr+r+r,�__init__�s zSubscriptionManager.__init__cCs|j�|�dS)z>Simple wrapper for logging info messages. Useful for unittestsN)r�info�rE�msgr+r+r,r'�szSubscriptionManager.log_successcCs|j�|�dS)zASimple wrapper for logging warning messages. Useful for unittestsN)rZwarningrHr+r+r,r(�szSubscriptionManager.log_warncCs�|jD].}||jkrd�|d�|j��}d|fSq|jdk	rdt�|j�sdt�|j�sdd}d|fS|jdk	r�|jr�t�t	|j��r�d}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}�, 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: True)TN)
r>�
valid_rh_keysr%�joinrrZis_trueZis_falserr))rE�kZbad_keyZnot_boolZno_autor+r+r,r�s2


��

�
��
���z SubscriptionManager._verify_keyscCs0dg}zt|�Wntjk
r*YdSXdS)zg
        Checks if the system is already registered and returns
        True if so, else False
        ZidentityFT)�_sub_man_clir�ProcessExecutionError)rE�cmdr+r+r,r�sz!SubscriptionManager.is_registeredc
Cs�|jdk	r�|jdk	r�dd�|j�d�|j�g}|jdk	rJ|�d�|j��|jdk	rf|�d�|j��zt|dd�d	}WnFtjk
r�}z&|j	d
kr�|�
d�|j��WY�dSd}~XYnXn�|jdk	�r�|j
dk	�r�dd
�|j�d�|j
�g}|jdk	�r|�d�|j��|jdk	�r2|�d�|j��zt|dd�d	}WnJtjk
�r�}z(|j	d
k�rz|�
d�|j��WY�dSd}~XYnXn|�
d�|�
d�dS|�d�d��}|j�d|�dS)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�zRegistration 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)rAr1r%r?�appendr@rNrrO�stdoutr(�stderrrBr4�split�rstriprr)rErP�
return_outr*Zreg_idr+r+r,r�sX

�	



�

�	
���z SubscriptionManager.rhn_registerc
Cs�ddd�|j�g}zt|�d}Wnttjk
r�}zT|j��dkrj|j�d�D]}|dkrP|�|�qPn|�d�|j	�
���WY�dSd}~XYnX|�d�D]}|dkr�|j�|�q�d	S)
N�attach�--autoz--servicelevel={0}rrS�
z*Setting the service level failed with: {0}FT)
r%rrNrrOrVrYrXr(rW�striprr�rErPrZr*�liner+r+r,r1s*
���z&SubscriptionManager._set_service_levelc
Cs|ddg}zt|�d}Wn:tjk
rR}z|�d�|��WY�dSd}~XYnX|�d�D]}|dkr^|j�|�q^dS)	Nr[r\rzAuto-attach failed with: {0}Fr]rST)rNrrOr(r%rXrrr_r+r+r,r Ksz$SubscriptionManager._set_auto_attachcCsXg}g}dddg}t|�d}|���d�}dddg}t|�d}|���d�}||fS)zh
        Gets the list pools for the active subscription and returns them
        in list form.
        r#z--availablez--pool-onlyrr]z
--consumed)rNrYrX)rEZ	available�consumedrPZresultsr+r+r,�	_getPoolsWs

zSubscriptionManager._getPoolscCs�ddg}t|�d}g}|�d�D]$}d|kr"|�|�d�d���q"ddg}t|�d}g}|�d�D]$}d|krj|�|�d�d���qj||fS)	zl
        Obtains the current list of active yum repositories and returns
        them in list form.
        �reposz--list-enabledrr]zRepo ID:�:rTz--list-disabled)rNrXrUr^)rErPrZ�active_reposZrepo�inactive_reposr+r+r,�	_getReposkszSubscriptionManager._getReposc
Cs�t|�dkr|j�d�dS|��\}}g}dg}|D]6}||kr\||kr\|�d�|��q6|�d�|��q6t|�dkr�|�|�z*t|�|j�dd�	|��
d	d
��WdStjk
r�}z|�d�||��WY�dSd
}~XYnXd
S)zl
        Takes a list of subscription pools and "attaches" them to the
        current subscription
        rzNo pools to attachTr[z
--pool={0}zPool {0} is not availablez/Attached the following pools to your system: %srJz--pool=rSz$Unable to attach pool {0} due to {1}FN)
�lenrrrbrUr%r(�extendrNrL�replacerrO)rEr!Zpool_availableZ
pool_consumedZ	pool_listrPZpoolr*r+r+r,r$�s0
�
�zSubscriptionManager.addPoolc

Cs8|j}|j}|dkrg}|dkr$g}t|t�s<|�d�dSt|t�sT|�d�dSt|�dkr|t|�dkr||j�d�dS|��\}}g}g}|D](}||kr�|�	d�
|��q�|�	|�q�g}g}	|D](}||kr�|�	d�
|��q�|	�	|�q�t|�dk�r8|D]0}
|
|k�r$|j�d	|
�n|�d
�
|
���qt|	�dk�r`|	D]}
|j�d|
��qJdg}t|�dk�r~|�|�t|�dk�r�|�|�zt|�Wn<t
jk
�r�}z|�d
�
|��WY�dSd}~XYnXt|�dk�r
|j�dd�|��dd��t|�dk�r4|j�dd�|��dd��dS)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 enabledrcz Unable to alter repos due to {0}zEnabled the following repos: %srJz	--enable=rSz Disabled the following repos: %sz
--disable=)rCrDr"r#r(rhrrrgrUr%rirNrrOrLrj)
rEZereposZdreposrerfZenable_listZenable_list_failZrepoidZdisable_listZdisable_list_failZfailrPr*r+r+r,r&�s|




��

��z SubscriptionManager.update_reposcCst|jr|jp|j�S)N)�boolrBr4rA)rEr+r+r,r�sz!SubscriptionManager.is_configured)N)r.r/r0rKrFr'r(rrrrr rbrgr$r&rr+r+r+r,r�s2�
#H"VrFcCstjdg||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
    zsubscription-manager)Z	logstring)r)rPrRr+r+r,rN�srN)F)�__doc__Zloggingr�textwraprZ	cloudinitrrrZcloudinit.cloudrZcloudinit.configr	Zcloudinit.config.schemar
rZcloudinit.settingsrZ	getLoggerr.r<ZMODULE_DESCRIPTIONr�__annotations__r)r#r-�	ExceptionrrrNr+r+r+r,�<module>sR
����'�1�8p