U
    lHJeeW                     @   s   d dl Z d dlZd dlZd dlZd dlmZ d dlmZmZm	Z	m
Z
mZmZ d dlmZmZmZmZmZmZmZmZ d dlmZ d dlmZmZmZ e Zeee Z!dZ"G dd	 d	ej#Z$dS )
    N)exists)AnyDictListOptionalTupleUnion)aptcontractevent_logger
exceptionshttpmessagessystemutil)base)ApplicationStatusCanDisableFailureCanDisableFailureReasonz<^linux-image-([\d]+[.-][\d]+[.-][\d]+-[\d]+-[A-Za-z0-9_-]+)$c                       sr  e Zd ZdZdZdZdZdZdZdZ	e
eeedf ddd	Ze
ee dd
dZedddZe
ejedddZd/eeeee f d fddZd0eedddZd1ddZdd Zdd Zdd Zdd Zee ee!j" f dd d!Z#d"d# Z$d2e%ee&f e%ee&f eed$ fd%d&Z'd3eee  eedd'd(d)Z(d4eddd*d+Z)d5eed,d-d.Z*  Z+S )6RepoEntitlementz*/etc/apt/sources.list.d/ubuntu-{name}.listz$/etc/apt/preferences.d/ubuntu-{name}z	{}/ubuntuNFT)returnc                 C   s   d S N selfr   r   </usr/lib/python3/dist-packages/uaclient/entitlements/repo.pyrepo_pin_priority4   s    z!RepoEntitlement.repo_pin_priorityc                 C   s<   g }| j di }|r8|di }t|dg }|}|S )zdebs to install on enablemententitlement
directivesadditionalPackages)entitlement_cfggetcopy)r   packagesr   r   Zadditional_packagesr   r   r   r#   8   s    
zRepoEntitlement.packagesc                 C   s    t jt| jd}t| |S )z%Check if system needs to be rebooted.)Zinstalled_pkgs)r   Zshould_rebootsetr#   eventZneeds_reboot)r   Zreboot_requiredr   r   r   _check_for_rebootI   s
    
z!RepoEntitlement._check_for_rebootc                 C   s   d S r   r   r   r   r   r   repo_key_fileQ   s    zRepoEntitlement.repo_key_file)ignore_dependent_servicesr   c                    sV   t  j|d\}}|dkr"||fS | jsN| jrNdttjtjj	| j
| j
dfS ||fS )N)r(   Fentitlement_nametitle)supercan_disableoriginpurger   r   ZNO_PURGE_WITHOUT_ORIGINr   ZREPO_PURGE_FAIL_NO_ORIGINformatr+   )r   r(   resultreason	__class__r   r   r-   V   s     
 
zRepoEntitlement.can_disable)silentr   c                 C   s   | j |d | jrZ| jrZt| jdkrBttjj	d
| jd ttjj	| jd n*|   ttjj	| jd | jdd dS )	zEnable specific entitlement.

        @return: True on success, False otherwise.
        @raises: UbuntuProError on failure to install suggested packages
        r5   r    r#   r+   install)Z	operationT)setup_apt_configZsupports_access_onlyZaccess_onlylenr#   r%   infor   ZSKIPPING_INSTALLING_PACKAGESr0   joinZACCESS_ENABLED_TMPLr+   install_packagesZENABLED_TMPLZ_check_for_reboot_msg)r   r5   r   r   r   _perform_enablel   s    
zRepoEntitlement._perform_enablec                 C   s   | j r| jrttj t  t| j}| |s6dS g }g }|D ]6}tj|| jd}|rn|	|t
|f qB|	| qB| ||sdS t| dr|   | j|d | j r| jr| | | | dS )NF)Zexclude_originremove_packagesr6   T)r/   r.   printr   ZPURGE_EXPERIMENTALr	   Z get_installed_packages_by_originpurge_kernel_checkZget_remote_versions_for_packageappendmaxprompt_for_purgehasattrrA   remove_apt_configexecute_reinstallexecute_removal)r   r5   Zrepo_origin_packagespackages_to_reinstallpackages_to_removepackageZalternativesr   r   r   _perform_disable   s@    

 
 


z RepoEntitlement._perform_disablec                    s   g  |D ]&}t t|j}|r |d q rttjj	| j
d td  t j}ttjj	|d t } fdd|D }|sttj dS ttjsdS dS )	a*  
        Checks if the purge operation involves a kernel.

        When package called 'linux-image-*' is in the package list, warn the
        user that a kernel is being removed. Then, show the user what the
        current kernel is.

        If the current kernel is to be removed, and there are no other valid
        Ubuntu Kernels installed in the system, return False to abort the
        operation.

        If there is another Ubuntu kernel - besides the one installed - then
        prompt the user for confirmation before proceeding.
           servicer7   )Zkernel_versionc                    s   g | ]}| kr|qS r   r   ).0versionZlinux_image_versionsr   r   
<listcomp>   s   z6RepoEntitlement.purge_kernel_check.<locals>.<listcomp>FT)researchRE_KERNEL_PKGnamerD   grouprB   r   ZPURGE_KERNEL_REMOVALr0   r+   r>   r   Zget_kernel_infoZuname_releaseZPURGE_CURRENT_KERNELZget_installed_ubuntu_kernelsZPURGE_NO_ALTERNATIVE_KERNELr   prompt_for_confirmationZPURGE_KERNEL_CONFIRMATION)r   package_listrM   mZcurrent_kernelZinstalled_kernelsZalternative_kernelsr   rT   r   rC      s4    


z"RepoEntitlement.purge_kernel_checkc                 C   sd   d}|r*t tj tdd |D  d}|rPt tj tdd |D  d}|r`ttjS dS )NFc                 S   s   g | ]
}|j qS r   rY   rR   rM   r   r   r   rU      s     z4RepoEntitlement.prompt_for_purge.<locals>.<listcomp>Tc                 S   s   g | ]\}}|j qS r   r^   )rR   rM   _r   r   r   rU      s     )rB   r   ZWARN_PACKAGES_REMOVALr   Zprint_package_listZWARN_PACKAGES_REINSTALLr[   ZPROCEED_YES_NO)r   rL   rK   promptr   r   r   rF      s     

z RepoEntitlement.prompt_for_purgec                    s8   t    fdd|D }|r4t |tjj|d d S )Nc                    s   g | ]}|j  kr|j qS r   r^   r_   Zinstalled_packagesr   r   rU      s   
z3RepoEntitlement.execute_removal.<locals>.<listcomp>r8   )r	   get_installed_packages_namesZpurge_packagesr   ZUNINSTALLING_PACKAGES_FAILEDr0   )r   rL   Z	to_remover   rb   r   rJ      s    
zRepoEntitlement.execute_removalc                    s,   t    fdd|D }|r(t | d S )Nc                    s*   g | ]"\}}|j  krd |j |jqS )z{}={})rY   r0   Zver_str)rR   rM   rS   rb   r   r   rU   	  s   
z5RepoEntitlement.execute_reinstall.<locals>.<listcomp>)r	   rc   Zreinstall_packages)r   rK   Zto_reinstallr   rb   r   rI     s    
z!RepoEntitlement.execute_reinstallc                 C   s   t jtjj| jdf}| j}|di di }|d}|sTt jtjj| jdfS t	j
tjd}t| j||}|rt jtjj| jdf}| jr| jD ]*}t	|st jtjj| j|df  S q|S )Nr9   r   r   aptURL)Z	error_msg)rQ   rM   )r   DISABLEDr   ZSERVICE_NOT_CONFIGUREDr0   r+   r    r!   ZNO_APT_URL_FOR_SERVICEr	   Zget_apt_cache_policyZAPT_POLICY_FAILEDrV   rW   repo_url_tmplZENABLEDZSERVICE_IS_ACTIVEcheck_packages_are_installedr#   Zis_installedZ SERVICE_DISABLED_MISSING_PACKAGErY   )r   Zcurrent_statusr    r   repo_urlZpolicymatchrM   r   r   r   application_status  s:     


 
z"RepoEntitlement.application_statusc                 C   sP   | j j| jd}tdd t| dD r6dS |s>dS t|t|kS )zCheck 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^   c                 s   s   | ]}| d V  qdS )#N)
startswith)rR   liner   r   r   	<genexpr>B  s   z<RepoEntitlement._check_apt_url_is_applied.<locals>.<genexpr>
FT)	repo_list_file_tmplr0   rY   allr   Z	load_filestripsplitbool)r   Zapt_urlZapt_filer   r   r   _check_apt_url_is_applied7  s    z)RepoEntitlement._check_apt_url_is_applied)orig_accessdeltasallow_enabler   c                    s2  t  |||rdS |di }|di }|d}|d}| jd}|r^|r^|  }	n|  \}	}
|	tjkrxdS | 	|st
d| j| ttjj| jd	 |di }|di d}|r| jj| jd
}t|| |   |   |r.t
d| ttjjd|d | j|d 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.
        Tr   r   rd   r   zstatus-cacheFz.New aptURL, updating %s apt sources list to %srP   r^   z%New additionalPackages, installing %r, r8   )r\   )r,   process_contract_deltasr!   cfgZ
read_cacheZ"_check_application_status_on_cacherj   r   re   ru   LOGr=   rY   r%   r   ZREPO_UPDATING_APT_SOURCESr0   rp   r	   remove_auth_apt_reporH   r;   Z REPO_REFRESH_INSTALLING_PACKAGESr>   r?   )r   rv   rw   rx   Zdelta_entitlementZdelta_directivesZdelta_apt_urlZdelta_packagesZstatus_cacherj   r`   Zorig_entitlementZold_urlrepo_filenamer3   r   r   rz   Q  sJ    




z'RepoEntitlement.process_contract_deltas)r\   cleanup_on_failureverboser   c                 C   s   |s
| j }|sdS | jdg }t|s.dS z|   W n$ tjk
r^   |rX|    Y nX |rzt	
tjj| jd | jrddi}dddg}nd}g }ztj|||d	 W n: tjk
r   t	
tjj| jd |r|    Y nX dS )
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
        NZpre_installr9   ZDEBIAN_FRONTENDZnoninteractivez--allow-downgradesz$-o Dpkg::Options::="--force-confdef"z$-o Dpkg::Options::="--force-confold")r#   apt_optionsoverride_env_vars)r#   Z	messagingr!   r   Zhandle_message_operationsZ_update_sources_listr   UbuntuProErrorrH   r%   r=   r   ZINSTALLING_SERVICE_PACKAGESr0   r+   apt_noninteractiver	   run_apt_install_commandZENABLE_FAILED)r   r\   r   r   Zmsg_opsr   r   r   r   r   r?     sH    

z RepoEntitlement.install_packagesc                 C   s  d}d}d}| j js| j jrNtd| j jtj}td| j jtj}tjj	}n@| j j
s^| j jrtd| j j
tj}td| j jtj}tjj}tj|||d | jj| jd}| j}|d di }|d di }|d	}	|	s>| j jd
 }
|ds&t| j }||
| j}|r&|d	}	|	s>|
}	td| j |d}|s\tj| jd|d}|sztj| jd|d}|stj| jd| jr| jstj | j| jd| j!j| jd}t"||| j| j g }t#tj$s|%d t#tj&s|%d |rh|s4t'(t)j*jd+|d ztj,|d W n" tj-k
rf   | .   Y nX t/|| j0||	|| j1 |st'(t)j2j| jd zt3| W n& tj-k
r   | j.dd  Y nX dS )zSetup apt config based on the resourceToken and directives.
        Also sets up apt proxy if necessary.

        :raise UbuntuProError: on failure to setup any aspect of this apt
           configuration
        Nr   Zhttps)
http_proxyhttps_proxyZproxy_scoper^   r   r   obligationsZresourceTokenZmachineTokenZenableByDefaultzWNo resourceToken present in contract for service %s. Using machine token as credentialsaptKeyr*   rd   Zsuitesr)   zapt-transport-httpszca-certificatesry   r8   F)run_apt_update)4r{   Zglobal_apt_http_proxyZglobal_apt_https_proxyr   Zvalidate_proxyZPROXY_VALIDATION_APT_HTTP_URLZPROXY_VALIDATION_APT_HTTPS_URLr	   ZAptProxyScopeZGLOBALZua_apt_http_proxyZua_apt_https_proxyZUACLIENTZsetup_apt_proxyrp   r0   rY   r    r!   machine_tokenr
   ZUAContractClientZget_resource_machine_accessr|   Zwarningr+   r   ZRepoNoAptKeyMissingAptURLDirectiveZRepoNoSuitesr   r.   ZRepoPinFailNoOriginrepo_pref_file_tmplZadd_ppa_pinningr   ZAPT_METHOD_HTTPS_FILErD   ZCA_CERTIFICATES_FILEr%   r=   r   ZINSTALLING_PACKAGESr>   r   r   rH   Zadd_auth_apt_reporf   r'   ZAPT_UPDATING_LISTZupdate_sources_list)r   r5   r   r   Zscoper~   Zresource_cfgr   r   tokenr   ZclientZmachine_accessr   rh   Zrepo_suitesrepo_pref_fileZprerequisite_pkgsr   r   r   r;     s    
  
 






z RepoEntitlement.setup_apt_config)r   r5   c           	      C   s   t  j}| jj| jd}| jjj| j 	di }|	di }|	d}|sZt
j| jdt||| j t|| | jr| jj| jd}t | |r|sttj t  dS )zRemove any repository apt configuration files.

        :param run_apt_update: If after removing the apt update
            command after removing the apt files.
        r^   r   r   rd   r   N)r   Zget_release_infoseriesrp   r0   rY   r{   Zmachine_token_fileZentitlementsr!   r   r   r	   r}   r'   Zremove_apt_list_filesr   r   Zensure_file_absentr%   r=   r   ZAPT_UPDATING_LISTSZrun_apt_update_command)	r   r   r5   r   r~   r   Zaccess_directivesrh   r   r   r   r   rH   H  s&    
 

z!RepoEntitlement.remove_apt_config)F)F)F)F)NTT)F)TF),__name__
__module____qualname__rp   r   rf   r.   r   rg   Zsupports_purgepropertyr   intstrr   r   r#   rt   r&   abcabstractmethodr'   r   r   r   r-   r@   rN   rC   rF   rJ   rI   r   r   ZNamedMessagerj   ru   r   r   rz   r?   r;   rH   __classcell__r   r   r3   r   r   !   sh    
)2& 

D   
;{    r   )%r   r"   ZloggingrV   os.pathr   typingr   r   r   r   r   r   Zuaclientr	   r
   r   r   r   r   r   r   Zuaclient.entitlementsr   Z(uaclient.entitlements.entitlement_statusr   r   r   Zget_event_loggerr%   Z	getLoggerZreplace_top_level_logger_namer   r|   rX   ZUAEntitlementr   r   r   r   r   <module>   s    (
