U
    w7e                  	   @   s   U d Z ddlZddlmZ ddl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 d
ZeZdddeegeedgg dZeed< eeZeeZ dZeee	eddddZdS )z@Final Message: Output final message when cloud-init has finished    N)dedent)	templaterutilversion)Cloud)Config)
MetaSchemaget_meta_doc)ALL_DISTROS)
PER_ALWAYSa  This module configures the final message that cloud-init writes. The message is
specified as a jinja template with the following variables set:

    - ``version``: cloud-init version
    - ``timestamp``: time at cloud-init finish
    - ``datasource``: cloud-init data source
    - ``uptime``: system uptime

This message is written to the cloud-init log (usually /var/log/cloud-init.log)
as well as stderr (which usually redirects to /var/log/cloud-init-output.log).

Upon exit, this module writes the system uptime, timestamp, and cloud-init
version to ``/var/lib/cloud/instance/boot-finished`` independent of any
user data specified for this module.
Zcc_final_messagezFinal Messagez1Output final message when cloud-init has finishedz            final_message: |
              cloud-init has finished
              version: $version
              timestamp: $timestamp
              datasource: $datasource
              uptime: $uptime
            )idnametitleZdescriptionZdistros	frequencyZexamplesZactivate_by_schema_keysmetazy## template: jinja
Cloud-init v. {{version}} finished at {{timestamp}}. Datasource {{datasource}}.  Up {{uptime}} seconds)r   cfgcloudargsreturnc                 C   s0  d}t |dkrt|d }nt|dd}| }|s<t}t }t }t	 }zR|||t|j
d}|tdd | D  tjdt|| dd	td
 W n  tk
r   ttd Y nX |jj}	z"d|||f }
tj|	|
dd W n$ tk
r   ttd|	 Y nX |j
jr,td d S )N r   Zfinal_message)uptimeZ	timestampr   
datasourcec                 S   s   g | ]\}}|  |fqS  )upper).0kvr   r   C/usr/lib/python3/dist-packages/cloudinit/config/cc_final_message.py
<listcomp>\   s     zhandle.<locals>.<listcomp>z%s
FT)Zconsolestderrlogz'Failed to render final message templatez%s - %s - v. %s
)Zensure_dir_existsz%Failed to write boot finished file %szUsed fallback datasource)lenstrr   Zget_cfg_option_strstripFINAL_MESSAGE_DEFr   Ztime_rfc2822r   Zversion_stringr   updatedictitemsZ	multi_logr   Zrender_stringLOG	ExceptionZlogexcpathsZboot_finishedZ
write_fileZis_disconnectedZwarning)r   r   r   r   Zmsg_inr   ZtsZcverZsubsZboot_fin_fncontentsr   r   r   handleF   sB    

r,   )__doc__Zloggingtextwrapr   Z	cloudinitr   r   r   Zcloudinit.cloudr   Zcloudinit.configr   Zcloudinit.config.schemar   r	   Zcloudinit.distrosr
   Zcloudinit.settingsr   ZMODULE_DESCRIPTIONr   r   __annotations__Z	getLogger__name__r(   r$   r"   listr,   r   r   r   r   <module>   s6   
