Your IP : 216.73.216.52


Current Path : /usr/lib/python3/dist-packages/uaclient/entitlements/__pycache__/
Upload File :
Current File : //usr/lib/python3/dist-packages/uaclient/entitlements/__pycache__/repo.cpython-38.pyc

U

8�-dL<�@s�ddlZddlZddlZddlZddlmZddlmZmZm	Z	m
Z
mZmZddl
mZmZmZmZmZmZmZddlmZddlmZe��ZGdd�dej�ZdS)	�N)�exists)�Any�Dict�List�Optional�Tuple�Union)�apt�contract�event_logger�
exceptions�messages�system�util)�base)�ApplicationStatuscs$eZdZdZdZdZdZeee	e
dfd�dd��Zeee
d�dd	��Z
ed�d
d�Zeeje
d�dd
���Zd#eed�dd�Zd$dd�Zeeeejfd�dd�Zdd�Zd%ee
efee
efeed��fdd�
Zd&eee
eedd�dd�Zd'edd�dd�Z d(eed �d!d"�Z!�Z"S))�RepoEntitlementz*/etc/apt/sources.list.d/ubuntu-{name}.listz$/etc/apt/preferences.d/ubuntu-{name}NF)�returncCsdS�N���selfrr�</usr/lib/python3/dist-packages/uaclient/entitlements/repo.py�repo_pin_priority#sz!RepoEntitlement.repo_pin_prioritycCsJg}|jjj�|ji��di�}|rF|�di�}t�|�dg��}|}|S)zdebs to install on enablement�entitlement�
directives�additionalPackages)�cfg�machine_token_file�entitlements�get�name�copy)r�packagesrrZadditional_packagesrrrr#'s
��
�zRepoEntitlement.packagescCs tjt|j�d�}t�|�|S)z%Check if system needs to be rebooted.)Zinstalled_pkgs)rZ
should_reboot�setr#�eventZneeds_reboot)rZreboot_requiredrrr�_check_for_reboot:s
�
z!RepoEntitlement._check_for_rebootcCsdSrrrrrr�
repo_key_fileBszRepoEntitlement.repo_key_file)�silentrcCs�|j|d�|jrb|jrbt|j�dkr6dd�|j�nd}t�d�|��t�t	j
j|jd��n*|��t�t	j
j|jd��|jdd	�d
S)z�Enable specific entitlement.

        @return: True on success, False otherwise.
        @raises: UserFacingError on failure to install suggested packages
        �r(rz: � �zSkipping installing packages{}��title�install)Z	operationT)�setup_apt_configZsupports_access_onlyZaccess_only�lenr#�joinr%�info�formatr
ZACCESS_ENABLED_TMPLr-�install_packagesZENABLED_TMPLZ_check_for_reboot_msg)rr(Zpackages_strrrr�_perform_enableGs��zRepoEntitlement._perform_enablecCs"t|d�r|��|j|d�dS)N�remove_packagesr)T)�hasattrr6�remove_apt_config)rr(rrr�_perform_disable]s
z RepoEntitlement._perform_disablecCs�|jjj�|ji�}|�di��di�}|�d�}|sLtjtjj	|j
d�fStjtj
jd�}t�d�	|�|�}|r�tjtjj	|j
d�fStjtjj	|j
d�fS)Nrr�aptURLr,)�	error_msgz	{}/ubuntu)rrrr r!r�DISABLEDr
ZNO_APT_URL_FOR_SERVICEr3r-r	Zget_apt_cache_policyZAPT_POLICY_FAILED�msg�re�searchZENABLEDZSERVICE_IS_ACTIVEZSERVICE_NOT_CONFIGURED)rZentitlement_cfgr�repo_urlZpolicy�matchrrr�application_statuscs0
��
����z"RepoEntitlement.application_statuscCsP|jj|jd�}tdd�t�|����d�D��r6dS|s>dSt|t�|�k�S)z�Check if apt url delta should be applied.

        :param apt_url: string containing the apt url to be used.

        :return: False if apt url is already found on the source file.
                 True otherwise.
        �r!css|]}|�d�VqdS)�#N)�
startswith)�.0�linerrr�	<genexpr>�s�z<RepoEntitlement._check_apt_url_is_applied.<locals>.<genexpr>�
FT)	�repo_list_file_tmplr3r!�allrZ	load_file�strip�split�bool)rZapt_urlZapt_filerrr�_check_apt_url_is_applied�s�z)RepoEntitlement._check_apt_url_is_applied)�orig_access�deltas�allow_enablercs
t��|||�rdS|�di�}|�di�}|�d�}|�d�}|j�d�}|r^|r^|��}	n|��\}	}
|	tjkrxdS|�	|�s�t
�d|j�|�di�}|�di��d�}|r�|j
j|jd	�}
t�|
|�|��|��|�rt
�d
�d�|���|j|d�dS)
a1Process any contract access deltas for this entitlement.

        :param orig_access: Dictionary containing the original
            resourceEntitlement access details.
        :param deltas: Dictionary which contains only the changed access keys
        and values.
        :param allow_enable: Boolean set True if allowed to perform the enable
            operation. When False, a message will be logged to inform the user
            about the recommended enabled service.

        :return: True when delta operations are processed; False when noop.
        Trrr:rzstatus-cacheFz5Updating '%s' apt sources list on changed directives.rCz-Installing packages on changed directives: {}�, )�package_list)�super�process_contract_deltasr rZ
read_cacheZ"_check_application_status_on_cacherBrr<rO�loggingr2r!rJr3r	�remove_auth_apt_repor8r/r1r4)rrPrQrRZdelta_entitlementZdelta_directivesZ
delta_apt_urlZdelta_packagesZstatus_cacherB�_Zorig_entitlementZold_url�
repo_filename��	__class__rrrV�s@




���z'RepoEntitlement.process_contract_deltasT)rT�cleanup_on_failure�verbosercCs�|s
|j}|sdS|j�dg�}t�|�s.dS|rFt�dj|jd��|j	r`ddi}ddd	g}ni}g}z(t
jj|jd�}tj
|||j|d
�Wn$tjk
r�|r�|���YnXdS)a)Install contract recommended packages for the entitlement.

        :param package_list: Optional package list to use instead of
            self.packages.
        :param cleanup_on_failure: Cleanup apt files if apt install fails.
        :param verbose: If true, print messages to stdout
        NZpre_installzInstalling {title} packagesr,ZDEBIAN_FRONTENDZnoninteractivez--allow-downgradesz$-o Dpkg::Options::="--force-confdef"z$-o Dpkg::Options::="--force-confold")r#�apt_optionsr;�env)r#Z	messagingr rZhandle_message_operationsr%r2r3r-�apt_noninteractiver
�ENABLED_FAILEDr	�run_apt_install_commandr=r�UserFacingErrorr8)rrTr]r^Zmsg_opsr`r_r=rrrr4�s:
��
z RepoEntitlement.install_packagescCs�d}d}d}|jjs|jjrNt�d|jjtj�}t�d|jjtj�}tjj	}n@|jj
s^|jjr�t�d|jj
tj�}t�d|jjtj�}tjj}tj
|||d�|jj|jd�}|jjj�|j�}|d�di�}|d�di�}|�d	�}	|	�sJ|jjd
}
|�d��s2t�|j�}|�|
|j�}|�r2|�d	�}	|	�sJ|
}	t�d|j�|�d
�}
|
�slt�d�|j���|�d�}|�s�t�|j��|�d�}|�s�t�d�|j|jj���|j�r|j �s�t�d�|j t!j"j|jd�j#���|j$j|jd�}t�%|||j |j�g}t&tj'��s |�(d�t&tj)��s6|�(d�|�r�|�sXt*�+d�d�,|���ztj-|d�Wn"tjk
�r�|�.��YnXt�/|||	||j0�|�s�t*�+t!j1�zt�2�Wn&tjk
�r�|j.dd��YnXdS)z�Setup apt config based on the resourceToken and directives.
        Also sets up apt proxy if necessary.

        :raise UserFacingError: on failure to setup any aspect of this apt
           configuration
        NZhttpZhttps)�
http_proxy�https_proxyZproxy_scoperCrr�obligationsZ
resourceTokenZmachineTokenZenableByDefaultzWNo resourceToken present in contract for service %s. Using machine token as credentials�aptKeyz6Ubuntu Pro server provided no aptKey directive for {}.r:Zsuitesz%Empty {} apt suites directive from {}z:Cannot setup apt pin. Empty apt repo origin value '{}'.
{}r,zapt-transport-httpszca-certificateszInstalling prerequisites: {}rS)r#F)�run_apt_update)3rZglobal_apt_http_proxyZglobal_apt_https_proxyrZvalidate_proxyZPROXY_VALIDATION_APT_HTTP_URLZPROXY_VALIDATION_APT_HTTPS_URLr	Z
AptProxyScopeZGLOBALZua_apt_http_proxyZua_apt_https_proxyZUACLIENTZsetup_apt_proxyrJr3r!rrr �
machine_tokenr
ZUAContractClientZrequest_resource_machine_accessrWZwarningr-rrd�MissingAptURLDirectiveZcontract_urlr�originr
rbr=�repo_pref_file_tmplZadd_ppa_pinningrZAPT_METHOD_HTTPS_FILE�appendZCA_CERTIFICATES_FILEr%r2r1rcr8Zadd_auth_apt_repor'�APT_UPDATING_LISTS�run_apt_update_command)rr(rerfZscoperZZresource_cfgrrg�tokenrjZclientZmachine_accessrhr@Zrepo_suites�repo_pref_fileZprerequisite_pkgsrrrr/
s���
���
�
�
��

�����

���z RepoEntitlement.setup_apt_config)rir(c	Cs�t��d}|jj|jd�}|jjj|j�di�}|�di�}|�d�}|sZt	�
|j��t�|||j
�t�||�|jr�|jj|jd�}t�|�|r�|s�t�tj�t��dS)z�Remove any repository apt configuration files.

        :param run_apt_update: If after removing the apt update
            command after removing the apt files.
        �seriesrCrrr:N)rZget_platform_inforJr3r!rrrr rrkr	rXr'Zremove_apt_list_filesrrmZensure_file_absentr%r2r
rorp)	rrir(rsrZrZaccess_directivesr@rrrrrr8�s&�

z!RepoEntitlement.remove_apt_config)F)F)F)NTT)F)TF)#�__name__�
__module__�__qualname__rJrmrlra�propertyr�int�strrrr#rNr&�abc�abstractmethodr'r5r9rrrr
ZNamedMessagerBrOrrrVr4r/r8�
__classcell__rrr[rrsR
��

�?�
�3��r)rzr"rWr>�os.pathr�typingrrrrrrZuaclientr	r
rrr
rrZuaclient.entitlementsrZ(uaclient.entitlements.entitlement_statusrZget_event_loggerr%Z
UAEntitlementrrrrr�<module>s $