Current File : //usr/lib/python3/dist-packages/cloudinit/config/__pycache__/cc_rh_subscription.cpython-312.pyc
�

Ӈg�<�
���UdZddlZddlmZmZddlmZddlmZddl	m
Z
ddlmZeje�Zdgd	�ed
gd�Ze
ed<d
ededededdf
d�ZGd�de�ZGd�d�Zdd�Zy)zDRed Hat Subscription: Register Red Hat Enterprise Linux based system�N)�subp�util)�Cloud)�Config)�
MetaSchema)�PER_INSTANCE�cc_rh_subscription)�fedora�rhel�	openeuler�rh_subscription)�id�distros�	frequency�activate_by_schema_keys�meta�name�cfg�cloud�args�returnc�"�t|t��}|j�stjd|�y|j	��st	|j�\}}|durt
|��|j�}|st
d��|jrC|j�7|j�st
d��|jjd�nB|jr6|j�st
d��|jjd�|j�ht|jt�s
d	}t
|��|j!|j�}	|	s$t
d
j#|j���|j%�}	|	st
d��|j'd�y|j'd�y#t$r5}
|j)t+|
��|j)d
�Yd}
~
yd}
~
wwxYw)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)�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)rrrr�sm�verify�
verify_msg�cont�	pool_fail�return_stat�es           �E/usr/lib/python3/dist-packages/cloudinit/config/cc_rh_subscription.py�handler7s���	�S�c�	*�B�
�����	�	�.��5��
����*	P�!#���!2��F�J��T�!�'�
�3�3��?�?�$�D��'�C����~�~�"�/�/�"=��,�,�.�+�,M�N�N��F�F�L�L�!K�L�����*�*�,�+�,H�I�I��F�F�L�L�!8�9��x�x�#�!�"�(�(�D�1� D�I�+�I�6�6� �j�j����2��"�+�4�;�;�B�H�H�E����/�/�+�K��'�(G�H�H��N�N�J�K�
	���5�6��	!�	P��K�K��A����K�K�N�O�O��	P�s�E2G�	H�+H	�	Hc��eZdZy)r N)�__name__�
__module__�__qualname__��r6r r Ps��r=r c�d�eZdZgd�Zdd�Zd�Zd�Zd�Zd�Zd�Z	d	�Z
d
�Zd�Zd�Z
d
�Zd�Zd�Zy)r)�org�activation-key�username�password�disable-repo�enable-repo�add-pool�rhsm-baseurl�server-hostname�auto-attach�
service-levelNc�2�|�t}||_||_|jjdi�|_|jjd�|_|jjd�|_|jjd�|_|jjd�|_|jjd�|_	|jjd�|_
|jjd�|_|jjd	�|_|jjd
�|_
|jjd�|_|jjd�|_y)
Nr
rFrGrEr@r?rArBrHrDrCrI)rrr�get�rhel_cfg�rhsm_baseurl�server_hostnamer&�activation_keyr?�useridrBr"�enable_repo�disable_repor#)�selfrrs   r6�__init__zSubscriptionManager.__init__cs,���;��C������������%6��;��
� �M�M�-�-�n�=���#�}�}�0�0�1B�C����]�]�&�&�z�2��
�"�m�m�/�/�0@�A����=�=�$�$�U�+����m�m�'�'�
�3����
�
�)�)�*�5��
��=�=�,�,�]�;����=�=�,�,�]�;��� �M�M�-�-�n�=��� �M�M�-�-�o�>��r=c�:�|jj|�y)z>Simple wrapper for logging info messages. Useful for unittestsN)r�info�rS�msgs  r6r,zSubscriptionManager.log_successus�����
�
�c�r=c�:�|jj|�y)zASimple wrapper for logging warning messages. Useful for unittestsN)r�warningrWs  r6r-zSubscriptionManager.log_warnys��������r=c���|jD]A}||jvs�dj|dj|j��}d|fcS|j�Dtj|j�s%tj|j�sd}d|fS|j�:|jr(tjt|j��rd}d|fSy)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}�, Fz8The 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#r.)rS�k�bad_key�not_bool�no_autos     r6rz SubscriptionManager._verify_keys}s������	&�A���*�*�*�� �&��D�I�I�d�.@�.@�$A�B��
�g�~�%�	&�
���(��L�L��)�)�*�d�m�m�D�<L�<L�.M�K�
��(�?�"����)��!�!�t�}�}�S��9I�9I�5J�'K�$�
�
�'�>�!�r=c�T�dg}	t|�y#tj$rYywxYw)zg
        Checks if the system is already registered and returns
        True if so, else False
        �identityFT)�_sub_man_clir�ProcessExecutionError)rS�cmds  r6rz!SubscriptionManager.is_registered�s7��
�l��	�������)�)�	��	�s��'�'c�^�|j��|j��ddj|j�dj|j�g}|j�*|j	dj|j��|j
�*|j	dj|j
��	t
|d��d	}n�|j��|j��dd
j|j�dj|j�g}|j�*|j	dj|j��|j
�*|j	dj|j
��	t
|d��d	}n#|jd�|jd�y|jd�dj�}|j j#d|�y#tj$rC}|jd
k(r*|jdj|j��Yd}~yd}~wwxYw#tj$rC}|jd
k(r*|jdj|j��Yd}~yd}~wwxYw)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�appendrNrgrrh�stdoutr-�stderrrPrB�split�rstriprr)rSri�
return_outr5�reg_ids     r6r!z SubscriptionManager.rhn_register�sM��
���+�$�(�(�2F��%�,�,�T�-@�-@�A��"�"�4�8�8�,��C�� � �,��
�
�?�1�1�$�2C�2C�D�E��#�#�/��
�
�,�3�3�D�4H�4H�I�J�
�)�#�T�B�1�E�
��k�k�%�D�M�M�,E�� �'�'����4� �'�'��
�
�6��C�� � �,��
�
�?�1�1�$�2C�2C�D�E��#�#�/��
�
�,�3�3�D�4H�4H�I�J�
�)�#�T�B�1�E�
�
�M�M�J�
�
�M�M�K�
���!�!�&�)�!�,�3�3�5�������;�V�D���[�-�-�
��8�8�r�>��M�M�9�@�@����J����
��6�-�-�
��8�8�r�>��M�M�9�@�@����J����
�s0�=G=�
I�=I�9I�I�J,�)9J'�'J,c�&�dddj|j�g}	t|�d}|jd�D]#}|dk7s�	|jj|��%y	#tj$r�}|j
j
�dk7r8|j
jd�D]}|dk7s�	|j|��n8|jdj|jj���Yd}~yd}~wwxYw)
N�attach�--autoz--servicelevel={0}rrm�
z*Setting the service level failed with: {0}FT)
r*r#rgrrhrprsrrr-rq�striprr�rSrirtr5�lines     r6r$z&SubscriptionManager._set_service_level�s����� �'�'��(9�(9�:�
��
	�%�c�*�1�-�J��$�$�T�*�	%�D��r�z������t�$�	%����)�)�	��x�x��� �B�&��H�H�N�N�4�0�,�D��r�z��
�
�d�+�,��
�
�@�G�G������(���
��	�s�A&�&D�9AD�:AD�Dc��ddg}	t|�d}|jd�D]#}|dk7s�	|jj|��%y#tj$r*}|jdj	|��Yd}~yd}~wwxYw)	NrwrxrzAuto-attach failed with: {0}FryrmT)rgrrhr-r*rrrrr{s     r6r%z$SubscriptionManager._set_auto_attachs�����"��	�%�c�*�1�-�J��$�$�T�*�	%�D��r�z������t�$�	%���
�)�)�	��M�M�8�?�?��B�C���	�s�A�B	� B�B	c���g}g}gd�}t|�d}|j�jd�}gd�}t|�d}|j�jd�}||fS)zh
        Gets the list pools for the active subscription and returns them
        in list form.
        )r(z--available�--pool-onlyrry)r(z
--consumedr)rgrsrr)rS�	available�consumedri�resultss     r6�	_getPoolszSubscriptionManager._getPoolsss��
�	���5���s�#�A�&���^�^�%�,�,�T�2�	�4���s�#�A�&���N�N�$�+�+�D�1���(�"�"r=c��ddg}t|�d}g}|jd�D]8}d|vs�|j|jd�dj���:ddg}t|�d}g}|jd�D]8}d|vs�|j|jd�dj���:||fS)	zl
        Obtains the current list of active yum repositories and returns
        them in list form.
        �reposz--list-enabledrryzRepo ID:�:rnz--list-disabled)rgrrrorz)rSrirt�active_repos�repo�inactive_reposs      r6�	_getReposzSubscriptionManager._getRepos0s����(�)��!�#�&�q�)�
����$�$�T�*�	B�D��T�!��#�#�T�Z�Z��_�Q�%7�$>�$>�$@�A�	B��)�*��!�#�&�q�)�
����$�$�T�*�	D�D��T�!��%�%�t�z�z�#��q�'9�&@�&@�&B�C�	D��^�+�+r=c�x�t|�dk(r|jjd�y|j�\}}g}dg}|D]K}||vr%||vr!|j	dj|���,|j
dj|���Mt|�dkDrY|j|�	t|�|jjddj|�jd	d
��yy#tj$r+}|j
dj|��Yd}~y
d}~wwxYw)zl
        Takes a list of subscription pools and "attaches" them to the
        current subscription
        rzNo pools to attachTrwz
--pool={0}zPool {0} is not availablez/Attached the following pools to your system: %sr\z--pool=rmz$Unable to attach pool {0} due to {1}NF)
�lenrrr�ror*r-�extendrgr^�replacerrh)rSr&�pool_available�
pool_consumed�	pool_listri�poolr5s        r6r)zSubscriptionManager.addPoolFs'���u�:��?��H�H�N�N�/�0��(,���(8�%��
��	��j���	H�D��M�)���0F�� � ��!4�!4�T�!:�;��
�
�9�@�@��F�G�		H�
�y�>�A���J�J�y�!�
��S�!������E��Y�Y�y�)�2�2�9�b�A������-�-�
��
�
�:�A�A�$��J����	
�s�3AC;�;D9�!D4�4D9c��|j}|j}|�g}|�g}t|t�s|j	d�yt|t�s|j	d�yt|�dk(r*t|�dk(r|jjd�y|j�\}}g}g}|D]8}||vr!|jdj|���(|j|��:g}g}	|D]8}||vr!|jdj|���(|	j|��:t|�dkDrH|D]C}
|
|vr|jjd	|
��$|j	d
j|
���Et|	�dkDr#|	D]}
|jjd|
�� dg}t|�dkDr|j|�t|�dkDr|j|�	t|�t|�dkDr;|jjddj|�j!dd��t|�dkDr;|jjddj|�j!dd��y#tj$r*}|j	d
j|��Yd}~yd}~wwxYw)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=rmz Disabled the following repos: %sz
--disable=)rQrRr'r(r-r�rrr�ror*r�rgrrhr^r�)
rS�erepos�dreposr�r��enable_list�enable_list_fail�repoid�disable_list�disable_list_fail�failrir5s
             r6r+z SubscriptionManager.update_reposhs����!�!���"�"���>��F��>��F��&�$�'��M�M�B�C���&�$�'��M�M�B�C��
��K�1��3�v�;�!�#3��H�H�N�N�=�>��'+�~�~�'7�$��n������	0�F���'��"�"�>�#8�#8��#@�A� �'�'��/�		0������	1�F���%��#�#�O�$:�$:�6�$B�C�!�(�(��0�		1��� �1�$�(�
���<�'��H�H�N�N�#?��F��M�M�;�B�B�4�H��
�� �!�A�%�)�
�������D�d��
�
�i���|��q� ��J�J�|�$��{��a���J�J�{�#�	����
�{��a���H�H�N�N�1����;�'�0�0��b�A�
��|��q� ��H�H�N�N�2����<�(�1�1�,��C�
����)�)�	��M�M�<�C�C�A�F�G���	�s�;J�K�, K�Kc�d�t|jxr|jxs|j�S�N)�boolrPrBrO)rSs r6rz!SubscriptionManager.is_configured�s%���T�[�[�2�T�]�]�J�t�7J�7J�K�Kr=r�)r9r:r;r]rTr,r-rrr!r$r%r�r�r)r+rr<r=r6rrTsQ���M�?�$��!�F�F�P�4
�#�(,�, �DT�lLr=rc�8�tjdg|z|��S)z�
    Uses the preferred 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)rirls  r6rgrg�s���9�9�,�-��3�}�M�Mr=)F)�__doc__�logging�	cloudinitrr�cloudinit.cloudr�cloudinit.configr�cloudinit.config.schemar�cloudinit.settingsr�	getLoggerr9rr�__annotations__r.r(r7�	Exceptionr rrgr<r=r6�<module>r�s���K�� �!�#�.�+��g����!���.�� 1�2�	��j��37��37�6�37�%�37�t�37��37�l	�	�	�kL�kL�\Nr=