U
    b¥  ã                   @   sÜ   d Z 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 ddlm	Z	m
Z
 ddlmZ ddlmZmZmZmZ dd	gZd
ZdZddd„Zdd„ Zdd„ Zdd„ Zdd„ Zddd„Zdd„ Zdd„ ZedkrØe eƒ ¡ dS )zGDefine 'collect-logs' utility and handler to include in cloud-init cmd.é    N)Údatetime)ÚINSTANCE_JSON_SENSITIVE_FILE)ÚProcessExecutionErrorÚsubp)Útempdir)ÚchdirÚcopyÚ
ensure_dirÚ
write_filez/var/log/cloud-init.logz/var/log/cloud-init-output.logz/run/cloud-initz%/var/lib/cloud/instance/user-data.txtc              	   C   sZ   | st jddd�} | jdddddd	d
� | jddddd� | jdddddd t¡d� | S )a2  Build or extend and arg parser for collect-logs utility.

    @param parser: Optional existing ArgumentParser instance representing the
        collect-logs subcommand which will be extended to support the args of
        this utility.

    @returns: ArgumentParser with proper argument configuration.
    úcollect-logsz)Collect and tar all cloud-init debug info)ÚprogZdescriptionz	--verbosez-vÚcountr   Ú	verbosityzBe more verbose.)ÚactionÚdefaultÚdestÚhelpz	--tarfilez-tzcloud-init.tar.gzzOThe tarfile to create containing all collected logs. Default: cloud-init.tar.gz)r   r   z--include-userdataz-uFÚ
store_trueÚuserdatazPOptionally include user-data from {0} which could contain sensitive information.)r   r   r   r   )ÚargparseÚArgumentParserÚadd_argumentÚformatÚUSER_DATA_FILE©Úparser© r   ú:/usr/lib/python3/dist-packages/cloudinit/cmd/devel/logs.pyÚ
get_parser   s<    	þúû	ÿùr   c                 C   s    dg}t  ¡ dkr| t¡ |S )z>Return a list of files to ignore for /run/cloud-init directoryzhook-hotplug-cmdr   )ÚosÚgetuidÚappendr   )ÚcurdirÚfilesZignored_filesr   r   r   Ú_copytree_rundir_ignore_filesD   s
    ÿ
r$   c              
   C   st   zt | ƒ\}}W n@ tk
rP } z"t|t|ƒƒ td| d|ƒ W 5 d}~X Y n X t||ƒ td| d|ƒ |S dS )zCHelper which runs a command and writes output or error to filename.zcollecting %s failed.
é   Nzcollected %s
)r   r   r
   ÚstrÚ_debug)ÚcmdÚfilenameÚmsgr   ÚoutÚ_Úer   r   r   Ú_write_command_output_to_fileO   s    "
r.   c                 C   s   ||krt j | ¡ d S )N)ÚsysÚstderrÚwrite)r*   Úlevelr   r   r   r   r'   \   s    r'   c                 C   s<   t j | ¡r(t| |ƒ td|  d|ƒ ntd|  d|ƒ d S )Nzcollected file: %s
r%   zfile %s did not exist
é   )r   ÚpathÚisfiler   r'   )r4   Zout_dirr   r   r   r   Ú_collect_filea   s    
r6   c                 C   s  |r t  ¡ dkr tj d¡ dS t j | ¡} t ¡  	¡  
d¡}d |¡}tdd���®}t j ||¡}tdd	gt j |d
¡d|ƒ}tddddgt j |d¡d|ƒ}|s°|r¬|nd}td| d|ƒ tdgt j |d¡d|ƒ tddddgt j |d¡d|ƒ tD ]}t|||ƒ qþ|�r"tt||ƒ t j |d¡}	t|	ƒ t j t¡�r¾ztjtt j |	d¡td� W nF tjk
�rª }
 z$tj d¡ tj t|
ƒd ¡ W 5 d }
~
X Y nX td!t d|ƒ ntd"t d|ƒ t|ƒ�" td#d$| | |d% d&¡gƒ W 5 Q R X W 5 Q R X tj d'|  ¡ dS )(zÐCollect all cloud-init logs and tar them up into the provided tarfile.

    @param tarfile: The path of the tar-gzipped file to create.
    @param include_userdata: Boolean, true means include user-data.
    r   zMTo include userdata, root user is required. Try sudo cloud-init collect-logs
r%   z%Y-%m-%dzcloud-init-logs-{0}z/tmp)Údirz
cloud-initz	--versionÚversionzcloud-init --versionz
dpkg-queryz--showz-f=${Version}
zdpkg-versionzdpkg versionznot-availablez!collected cloud-init version: %s
Zdmesgz	dmesg.txtzdmesg outputZ
journalctlz--boot=0z-ozshort-precisezjournal.txtzsystemd journal of current bootÚrun)Úignorez(Failed collecting file(s) due to error:
Ú
Nzcollected dir %s
zdirectory '%s' did not exist
ZtarZczvfú/Ú z	Wrote %s
)r   r    r/   r0   r1   r4   Úabspathr   ZutcnowÚdateÚstrftimer   r   Újoinr.   r'   ÚCLOUDINIT_LOGSr6   r   r	   ÚexistsÚCLOUDINIT_RUN_DIRÚshutilZcopytreer$   ÚErrorr&   r   r   Úreplace)ÚtarfileZinclude_userdatar   r?   Zlog_dirZtmp_dirr8   Zdpkg_verÚlogZrun_dirr-   r   r   r   Úcollect_logsi   s~    ÿ
ü
üü
üý
&ý
0rJ   c                 C   s   t |j|j|jƒS )z:Handle calls to 'cloud-init collect-logs' as a subcommand.)rJ   rH   r   r   )ÚnameÚargsr   r   r   Úhandle_collect_logs_args³   s    rM   c                  C   s   t ƒ } td|  ¡ ƒS )z4Tool to collect and tar all cloud-init related logs.r   )r   rM   Ú
parse_argsr   r   r   r   Úmain¸   s    rO   Ú__main__)N)r   )Ú__doc__r   r   rE   r/   r   Zcloudinit.sourcesr   Zcloudinit.subpr   r   Zcloudinit.temp_utilsr   Zcloudinit.utilr   r   r	   r
   rB   rD   r   r   r$   r.   r'   r6   rJ   rM   rO   Ú__name__Úexitr   r   r   r   Ú<module>   s,   
-
J