o
    ϴf                     @   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rttjj t j!| }|j"}|du rd}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( rd|v rd}	t)| j*j+}
nt, rd|v rd}	t)| j*j-}
|
dkrt|tjd  dS 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

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