U
    lHJeD                     @   s   d 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
 ddlmZ ddlmZ ddlmZ ddlmZmZ dd	lmZ d
ZdZee
eZedddZeedddZdd Z dS )a%  
Update messaging text for use in MOTD and APT custom Ubuntu Pro messages.

Messaging files will be emitted to /var/lib/ubuntu-advantage/message-* which
will be sourced by apt-hook/hook.cc and various /etc/update-motd.d/ hooks to
present updated text about Ubuntu Pro service and token state.
    N)exists)contractdefaultsmessagessystemutil)_updates)_is_attached)UAConfig)ESMAppsEntitlementESMInfraEntitlement)ApplicationStatuszmotd-contract-statusz6/usr/lib/update-notifier/update-motd-updates-available)cfgc                 C   s   | j }|dd}|di di dd }t| }|||}|di di dd }|d k	r|| jjkr||d d d< | j| d S )NZmachineToken ZmachineTokenInfoZcontractInfoidZeffectiveTo)machine_tokengetr   ZUAContractClientZget_contract_machinemachine_token_filecontract_expiry_datetimewrite)r   Z
orig_tokenr   Zcontract_idZcontract_clientZrespZresp_expiry r   A/usr/lib/python3/dist-packages/uaclient/timer/update_messaging.pyupdate_contract_expiry   s:      
  

r   )r   returnc                 C   s  t | jsdS td tj| jdt}t	
| \}}|t	jjt	jjt	jjfkrht|  t	
| \}}|t	jjt	jjfkrt| nP|t	jjkrt|tj|j|dd  n |t	jjkrtj| }| jj}|dkrd}n
|d}t|tj|j||d	d  n|t	jjkrd
}d}t rdt | ! \}	}
|	t"j#krd}t$| j%j&}n6t' rt(| ! \}}
|t"j#krd}t$| j%j)}|dkrt|tj*d  n"t|tj+|j||dd  dS )zEmit human-readable status message used by motd.

    Used by /etc/update.motd.d/91-contract-ua-esm-status

    :param cfg: UAConfig instance for this environment.
    Fz&Updating Ubuntu Pro messages for MOTD.r   )remaining_daysz

NZUnknownz%d %b %Y)Zexpired_dater   zn/ar   z	esm-infrazesm-apps)pkg_numserviceT),r	   Zis_attachedLOGdebugospathjoinZdata_dirMOTD_CONTRACT_STATUS_FILE_NAMEr   Zget_contract_expiry_statusZContractExpiryStatusZACTIVE_EXPIRED_SOONZEXPIRED_GRACE_PERIODZEXPIREDr   ZACTIVEZNONEr   Zensure_file_absentZ
write_filer   ZCONTRACT_EXPIRES_SOONZ	pluralizeformatr   Z!CONTRACT_EXPIRY_GRACE_PERIOD_DAYSr   r   strftimeZCONTRACT_EXPIRED_GRACE_PERIODZis_current_series_active_esmr   Zapplication_statusr   ZENABLEDapi_u_pro_packages_updates_v1ZsummaryZnum_esm_infra_updatesZis_current_series_ltsr   Znum_esm_apps_updatesZCONTRACT_EXPIREDZCONTRACT_EXPIRED_WITH_PKGS)r   Zmotd_contract_status_msg_pathZexpiry_statusr   Zgrace_period_remainingZexp_dtZ
exp_dt_strr   r   Zesm_infra_status_Zesm_apps_statusr   r   r   update_motd_messages6   s    

  





	r'   c               
   C   sL   t trHzttdg W n, tk
rF }  zt|  W 5 d } ~ X Y nX d S )Nz--force)r   UPDATE_NOTIFIER_MOTD_SCRIPTr   Zsubp	Exceptionr   Z	exception)excr   r   r   refresh_motd   s
    r+   )!__doc__Zloggingr   os.pathr   Zuaclientr   r   r   r   r   Z&uaclient.api.u.pro.packages.updates.v1r   r%   Z(uaclient.api.u.pro.status.is_attached.v1r	   Zuaclient.configr
   Zuaclient.entitlementsr   r   Z(uaclient.entitlements.entitlement_statusr   r"   r(   Z	getLoggerZreplace_top_level_logger_name__name__r   r   boolr'   r+   r   r   r   r   <module>   s    _