o
    ÙKÕiá  ã                   @   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mZ ddlmZ dd	lmZmZ d
ZdZe e
 e¡¡Zdefdd„Zdedef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)Ú_enabled_services)ÚContractExpiryStatusÚ_is_attached)ÚUAConfig)Úmachine_tokenÚnoticeszmotd-contract-statusz6/usr/lib/update-notifier/update-motd-updates-availableÚcfgc                 C   sž   t  | ¡}|j }| di ¡ di ¡ dd ¡}t | ¡}| | dd¡|¡}| di ¡ di ¡ dd ¡}|d urK||jkrM||d d d< | |¡ d S d S d S )NÚmachineTokenInfoÚcontractInfoÚidÚmachineTokenÚ ÚeffectiveTo)r   Úget_machine_token_fileÚgetr   ÚUAContractClientÚget_contract_machineÚcontract_expiry_datetimeÚwrite)r   Úmachine_token_fileÚ
orig_tokenÚcontract_idÚcontract_clientÚrespÚresp_expiry© r"   úA/usr/lib/python3/dist-packages/uaclient/timer/update_messaging.pyÚupdate_contract_expiry    s.   

ý
ÿ
ý

þÿúr$   Úreturnc                 C   s  t | ƒ}|js	dS t d¡ tj | jdt¡}|j	}|j
}t | ¡}|tjjtjjtjjfv r=t| ƒ t | ƒ}|j	}|j
}|tjjtjjfv rUt tjj¡ t |¡ dS |tjjkrtt tjj¡ t |tj |¡j|d�d ¡ dS |tjjkr¨t tjj¡ t j!| }|j"}|du r�d}n| #d¡}t |tj$ |¡j||d	�d ¡ dS |tjjk�rt %tjj¡ d
}	d}
dd„ t&| ƒj'D ƒ}t (¡ rÕd|v rÔd}	t)| ƒj*j+}
nt ,¡ råd|v råd}	t)| ƒj*j-}
|
dkrôt |tjd ¡ dS t |tj. |
¡j|
|	d�d ¡ dS )z¨Emit 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

NÚUnknownz%d %b %Y)Úexpired_dater&   zn/ar   c                 S   s   h | ]}|j ’qS r"   )Úname)Ú.0Úsr"   r"   r#   Ú	<setcomp>‚   s    ÿz'update_motd_messages.<locals>.<setcomp>z	esm-infrazesm-apps)Úpkg_numÚserviceT)/r   Úis_attachedÚLOGÚinfoÚosÚpathÚjoinÚdata_dirÚMOTD_CONTRACT_STATUS_FILE_NAMEÚcontract_statusÚcontract_remaining_daysr   r   r
   ÚACTIVE_EXPIRED_SOONÚvalueÚEXPIRED_GRACE_PERIODÚEXPIREDr$   ÚACTIVEÚNONEr   ÚremoveÚNoticeÚCONTRACT_EXPIREDr   Úensure_file_absentÚ
write_filer   ÚCONTRACT_EXPIRES_SOONÚ	pluralizeÚformatr   Ú!CONTRACT_EXPIRY_GRACE_PERIOD_DAYSr   ÚstrftimeÚCONTRACT_EXPIRED_GRACE_PERIODÚaddr	   Úenabled_servicesÚis_current_series_active_esmÚapi_u_pro_packages_updates_v1ÚsummaryÚnum_esm_infra_updatesÚis_current_series_ltsÚnum_esm_apps_updatesÚCONTRACT_EXPIRED_WITH_PKGS)r   Úis_attached_infoÚmotd_contract_status_msg_pathÚexpiry_statusr&   r   Úgrace_period_remainingÚexp_dtÚ
exp_dt_strr.   r-   Úenabled_services_namesr"   r"   r#   Úupdate_motd_messages;   sÄ   
ÿ
ýþ
C¾ÿýþ@Çÿ
ÿüúþ/Ûÿÿþ€ÿþþ÷þüþ	rZ   c               
   C   sR   t tƒr'z
t tdg¡ W d S  ty& }  zt | ¡ W Y d } ~ d S d } ~ ww d S )Nz--force)r   ÚUPDATE_NOTIFIER_MOTD_SCRIPTr   ÚsubpÚ	Exceptionr0   Ú	exception)Úexcr"   r"   r#   Úrefresh_motd¤   s   €ÿúr`   )"Ú__doc__Úloggingr2   Úos.pathr   Úuaclientr   r   r   r   r   Ú&uaclient.api.u.pro.packages.updates.v1r   rM   Ú-uaclient.api.u.pro.status.enabled_services.v1r	   Ú(uaclient.api.u.pro.status.is_attached.v1r
   r   Úuaclient.configr   Úuaclient.filesr   r   r6   r[   Ú	getLoggerÚreplace_top_level_logger_nameÚ__name__r0   r$   ÚboolrZ   r`   r"   r"   r"   r#   Ú<module>   s"    ÿi