U
    M[^-                     @   s   d dl Z d dlZd dlZd dlZz$d dlmZmZmZmZm	Z	m
Z
 W n ek
rX   Y nX d dlmZ d dlmZ d dlmZ d dlmZ d dlmZ d dlmZ d	ZG d
d dejZdS )    N)AnyDictListOptionalTupleUnion)apt)
exceptions)base)status)util)ApplicationStatusz-32768c                       s   e Zd ZdZdZdZeddddZeeddd	Z	i Z
ed
dddZeejedddZddeedddZd#ddZddddZddddZd$ddeed fddZdddd Zd!d" Z  ZS )%RepoEntitlementz3/etc/apt/sources.list.d/ubuntu-{name}-{series}.listz-/etc/apt/preferences.d/ubuntu-{name}-{series}NzUnion[int, str, None])returnc                 C   s   d S N selfr   r   </usr/lib/python3/dist-packages/uaclient/entitlements/repo.pyrepo_pin_priority    s    z!RepoEntitlement.repo_pin_priorityc                 C   s   dS )NFr   r   r   r   r   disable_apt_auth_only%   s    z%RepoEntitlement.disable_apt_auth_onlyz	List[str]c                 C   s   g S )zdebs to install on enablementr   r   r   r   r   packages-   s    zRepoEntitlement.packagesc                 C   s   d S r   r   r   r   r   r   repo_key_file2   s    zRepoEntitlement.repo_key_fileF)silent_if_inapplicable)r   r   c                C   s   | j |dsdS |   | jrzVtdj| jd | jdg D ]}t| q@t	dddg| j t
jj| jd W n  tjk
r   |    Y nX tt
jj| jd | jd	g D ]}t| qd
S )aZ  Enable specific entitlement.

        :param silent_if_inapplicable:
            Don't emit any messages until after it has been determined that
            this entitlement is applicable to the current machine.

        @return: True on success, False otherwise.
        @raises: UserFacingError on failure to install suggested packages
        )silentFzInstalling {title} packagestitleZpre_installapt-getinstall--assume-yesZpost_enableT)Z
can_enablesetup_apt_configr   printformatr   	messaginggetr   run_apt_commandr   MESSAGE_ENABLED_FAILED_TMPLr	   UserFacingError_cleanupZMESSAGE_ENABLED_TMPL)r   r   msgr   r   r   enable7   s*    


zRepoEntitlement.enablec                 C   s   |  |sdS |   dS )NFT)Zcan_disabler(   )r   r   r   r   r   disableW   s    
zRepoEntitlement.disablec                 C   s>   |    ztdddg| j  W n tjk
r8   Y nX dS )z4Clean up the entitlement without checks or messagingr   remover   N)remove_apt_configr   Zsubpr   ZProcessExecutionErrorr   r   r   r   r(   ]   s
    zRepoEntitlement._cleanupzTuple[ApplicationStatus, str]c                 C   s   | j j| ji }|di di }|d}|sFtjd| jfS |d\}}t	
ddgtj}td||}|r|d	tkrtjd
| jfS tjd| jfS )Nentitlement
directivesaptURLz${} does not have an aptURL directivez://z	apt-cachepolicyz(?P<pin>(-)?\d+) {}/ubuntuZpinz{} is activez{} is not configured)cfgentitlementsr$   namer   DISABLEDr"   r   splitr   r%   r   ZMESSAGE_APT_POLICY_FAILEDresearchgroupAPT_DISABLED_PINZENABLED)r   Zentitlement_cfgr/   repo_urlZprotocolZ	repo_pathr1   matchr   r   r   application_statuse   s0     

  
z"RepoEntitlement.application_statuszDict[str, Any])orig_accessdeltasallow_enabler   c                    s   t  |||rdS |  \}}|tjjkr0dS td| j |	di }|	di 	dr|	di }|	di 	d}|rt
 d }	| jj| j|	d}
t|
| |   |   dS )a1  Process 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.
        Tz5Updating '%s' apt sources list on changed directives.r.   r/   r0   seriesr4   rA   )superprocess_contract_deltasr=   r   r   r5   logginginfor4   r$   r   get_platform_inforepo_list_file_tmplr"   r   remove_auth_apt_repor-   r    )r   r>   r?   r@   r=   _Zdelta_entitlementZorig_entitlementZold_urlrA   repo_filename	__class__r   r   rD   ~   s.      z'RepoEntitlement.process_contract_deltasc                 C   s.  t  d }| jj| j|d}| jj| j}|d di }|d}|sft	d| j
 | jjd }|d}|std	| j|d
}|st| j|d}|std| j| jj| jr@| jstd| jtjj| j
d| jj| j|d}	| jdkr(t|	|| j| j ntj|	r@t|	 g }
tjtjs^|
d tjtjsx|
d |
rtdd|
 zt dddg|
 tj! W n" tjk
r   | "   Y nX t#||||| j$ ttj% zt ddgtj& W n" tjk
r(   | "   Y nX dS )zSetup apt config based on the resourceToken and  directives.

        :raise UserFacingError: on failure to setup any aspect of this apt
           configuration
        rA   rB   r.   r/   ZresourceTokenzHNo specific resourceToken present. Using machine token as %s credentialsZmachineTokenaptKeyz<Ubuntu Advantage server provided no aptKey directive for {}.r0   Zsuitesz%Empty {} apt suites directive from {}z:Cannot setup apt pin. Empty apt repo origin value '{}'.
{}r   neverzapt-transport-httpszca-certificateszInstalling prerequisites: {}z, r   r   r   updateN)'r   rG   rH   r"   r4   r2   r3   r$   rE   debugr   Zmachine_tokenr	   r'   MissingAptURLDirectiveZcontract_urlr   originr   r&   repo_pref_file_tmplr   add_ppa_pinningospathexistsunlinkZAPT_METHOD_HTTPS_FILEappendZCA_CERTIFICATES_FILEr!   joinr%   ZMESSAGE_APT_INSTALL_FAILEDr-   Zadd_auth_apt_repor   MESSAGE_APT_UPDATING_LISTSMESSAGE_APT_UPDATE_FAILED)r   rA   rK   Zresource_cfgr/   tokenrN   r;   Zrepo_suitesrepo_pref_fileZprerequisite_pkgsr   r   r   r       s     



 	 


    
 z RepoEntitlement.setup_apt_configc                 C   s   t  d }| jj| j|d}| jj| j di }|di }|d}|sZt	| j| j
rvt| t| nt||| j t|| | jr| jj| j|d}| jdkrt||| j| j ntj|rt| ttj tddgtj d	S )
z.Remove any repository apt configuration files.rA   rB   r.   r/   r0   rO   r   rP   N)r   rG   rH   r"   r4   r2   r3   r$   r	   rR   r   r   Zremove_repo_from_apt_auth_fileZrestore_commented_apt_list_filerI   r   Zremove_apt_list_filesr   rT   rU   rS   rV   rW   rX   rY   r!   r   r\   r%   r]   )r   rA   rK   r.   Zaccess_directivesr;   r_   r   r   r   r-     sL     

   


 z!RepoEntitlement.remove_apt_config)F)F)__name__
__module____qualname__rH   rT   rS   propertyr   boolr   r#   r   abcabstractmethodstrr   r*   r+   r(   r=   rD   r    r-   __classcell__r   r   rL   r   r      s4    
 *`r   )re   rE   rV   r7   typingr   r   r   r   r   r   ImportErrorZuaclientr   r	   Zuaclient.entitlementsr
   r   r   Zuaclient.statusr   r:   ZUAEntitlementr   r   r   r   r   <module>   s   $