HEX
Server: Apache/2.4.52 (Ubuntu)
System: Linux ip-10-0-8-47 6.8.0-1021-aws #23~22.04.1-Ubuntu SMP Tue Dec 10 16:31:58 UTC 2024 aarch64
User: ubuntu (1000)
PHP: 8.1.2-1ubuntu2.22
Disabled: NONE
Upload Files
File: //lib/python3/dist-packages/cloudinit/config/__pycache__/cc_rh_subscription.cpython-310.pyc
o

�Ad0D�@sUdZddlmZddlmZddlmZddlmZmZddl	m
Z
ddlmZddl
mZmZdd	lmZe�e�Zd
Zddd
eddgeed�ed�ed�gdgd�Zeed<ee�Zdedede
dededdfdd�ZGdd �d e�ZGd!d"�d"�Zd&d$d%�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``.
�cc_rh_subscriptionzRed Hat Subscriptionz.Register Red Hat Enterprise Linux based system�fedora�rhelz�            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�title�description�distros�	frequency�examples�activate_by_schema_keys�metar�cfg�cloudr�args�returnNc
Cs`t||d�}|��s|�d|�dS|��s�zs|��\}}|dur%t|��|��}|s/td��|jrF|jdurF|�	�s?td��|j
�d�n|jrW|��sQtd��|j
�d�|jdurxt
|jt�shd	}	t|	��|�|j�}
|
sxtd
�|j���|��}
|
s�td��|�d�WdSty�}z|�t|��|�d
�WYd}~dSd}~ww|�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)rrrrr�sm�verify�
verify_msg�cont�	pool_fail�return_stat�e�r9�E/usr/lib/python3/dist-packages/cloudinit/config/cc_rh_subscription.py�handleSsR�	

���r;c@seZdZdS)r#N)�__name__�
__module__�__qualname__r9r9r9r:r#�sr#c@s~eZdZgd�Zddd�Zdd�Zdd�Zd	d
�Zdd�Zd
d�Z	dd�Z
dd�Zdd�Zdd�Z
dd�Zdd�Zdd�ZdS)r)�org�activation-key�username�password�disable-repo�enable-repo�add-pool�rhsm-baseurl�server-hostname�auto-attach�
service-levelNcCs�|durt}||_||_|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)
NrrFrGrEr@r?rArBrHrDrCrI)�LOGrr�get�rhel_cfg�rhsm_baseurl�server_hostnamer)�activation_keyr?�useridrBr%�enable_repo�disable_repor&)�selfrrr9r9r:�__init__�s zSubscriptionManager.__init__cC�|j�|�dS)z>Simple wrapper for logging info messages. Useful for unittestsN)r�info�rS�msgr9r9r:r/��zSubscriptionManager.log_successcCrU)zASimple wrapper for logging warning messages. Useful for unittestsN)r�warningrWr9r9r:r0�rYzSubscriptionManager.log_warncCs�|jD]}||jvrd�|d�|j��}d|fSq|jdur2t�|j�s2t�|j�s2d}d|fS|jdurH|jrBt�t	|j��rHd}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)
rL�
valid_rh_keysr-�joinr%r�is_true�is_falser&r1)rS�k�bad_key�not_bool�no_autor9r9r:r"�s0

��
	
�
��
���z SubscriptionManager._verify_keyscCs,dg}zt|�WdStjyYdSw)zg
        Checks if the system is already registered and returns
        True if so, else False
        �identityFT)�_sub_man_clir�ProcessExecutionError)rS�cmdr9r9r:r!�s
��z!SubscriptionManager.is_registeredc
Cs�|jdur`|jdur`dd�|j�d�|j�g}|jdur%|�d�|j��|jdur3|�d�|j��z
t|dd�d	}Wn�tjy_}z|j	d
krT|�
d�|j��WYd}~dSd}~ww|jdur�|j
dur�dd
�|j�d�|j
�g}|jdur�|�d�|j��|jdur�|�d�|j��z
t|dd�d	}Wn.tjy�}z|j	d
kr�|�
d�|j��WYd}~dSd}~ww|�
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)rOr?r-rM�appendrNrerrf�stdoutr0�stderrrPrB�split�rstriprr )rSrg�
return_outr8�reg_idr9r9r:r$�s`

�
	


���

�
	


�����z SubscriptionManager.rhn_registerc
Cs�ddd�|j�g}zt|�d}Wn9tjyJ}z,|j��dkr4|j�d�D]}|dkr2|�|�q'n|�d�|j	�
���WYd}~dSd}~ww|�d�D]}|dkr\|j�|�qPd	S)
N�attach�--autoz--servicelevel={0}rrj�
z*Setting the service level failed with: {0}FT)
r-r&rerrfrmrpror0rn�striprr �rSrgrqr8�liner9r9r:r'1s4
�
�������z&SubscriptionManager._set_service_levelc
Cszddg}zt|�d}Wntjy(}z|�d�|��WYd}~dSd}~ww|�d�D]}|dkr:|j�|�q.dS)	NrsrtrzAuto-attach failed with: {0}FrurjT)rerrfr0r-rorr rwr9r9r:r(Ks���z$SubscriptionManager._set_auto_attachcCsTg}g}gd�}t|�d}|���d�}gd�}t|�d}|���d�}||fS)zh
        Gets the list pools for the active subscription and returns them
        in list form.
        )r+z--available�--pool-onlyrru)r+z
--consumedry)rerpro)rS�	available�consumedrg�resultsr9r9r:�	_getPoolsWszSubscriptionManager._getPoolscCs�ddg}t|�d}g}|�d�D]}d|vr#|�|�d�d���qddg}t|�d}g}|�d�D]}d|vrG|�|�d�d���q5||fS)	zl
        Obtains the current list of active yum repositories and returns
        them in list form.
        �reposz--list-enabledrruzRepo ID:�:rkz--list-disabled)rerorlrv)rSrgrq�active_repos�repo�inactive_reposr9r9r:�	_getReposks��zSubscriptionManager._getReposc
Cs�t|�dkr|j�d�dS|��\}}g}dg}|D]}||vr.||vr.|�d�|��q|�d�|��qt|�dkru|�|�zt|�|j�dd�	|��
d	d
��WdStjyt}z|�d�||��WYd}~d
Sd}~wwdS)zl
        Takes a list of subscription pools and "attaches" them to the
        current subscription
        rzNo pools to attachTrsz
--pool={0}zPool {0} is not availablez/Attached the following pools to your system: %sr[z--pool=rjz$Unable to attach pool {0} due to {1}NF)
�lenrr r}rlr-r0�extendrer]�replacerrf)rSr)�pool_available�
pool_consumed�	pool_listrg�poolr8r9r9r:r,�s6
�
����zSubscriptionManager.addPoolc

Cs$|j}|j}|durg}|durg}t|t�s|�d�dSt|t�s*|�d�dSt|�dkr>t|�dkr>|j�d�dS|��\}}g}g}|D]}||vrY|�	d�
|��qJ|�	|�qJg}g}	|D]}||vrt|�	d�
|��qe|	�	|�qet|�dkr�|D]}
|
|vr�|j�d	|
�q�|�d
�
|
��q�t|	�dkr�|	D]	}
|j�d|
�q�dg}t|�dkr�|�|�t|�dkr�|�|�zt|�Wnt
jy�}z|�d
�
|��WYd}~dSd}~wwt|�dkr�|j�dd�|��dd��t|�dk�r|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 enabledr~z Unable to alter repos due to {0}zEnabled the following repos: %sr[z	--enable=rjz Disabled the following repos: %sz
--disable=)rQrRr*r+r0r�rr r�rlr-r�rerrfr]r�)
rS�erepos�dreposr�r��enable_list�enable_list_fail�repoid�disable_list�disable_list_fail�failrgr8r9r9r:r.�s~



��

����z SubscriptionManager.update_reposcCst|jr|jp	|j�S�N)�boolrPrBrO)rSr9r9r:r�sz!SubscriptionManager.is_configuredr�)r<r=r>r\rTr/r0r"r!r$r'r(r}r�r,r.rr9r9r9r:r�s
#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)�	logstring)r)rgrir9r9r:re�sre)F)�__doc__�loggingr�textwrapr�	cloudinitrrr�cloudinit.cloudr�cloudinit.configr	�cloudinit.config.schemar
r�cloudinit.settingsr�	getLoggerr<rJ�MODULE_DESCRIPTIONr�__annotations__r1r+r;�	Exceptionr#rrer9r9r9r:�<module>s`
����'�1�����
�8p