U
    Ld                     @   s\   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 e	e
ZG dd dejZdS )    N)handlers)log)subputil)
PER_ALWAYSc                   @   s*   e Zd ZdgZdd Zdd Zdd ZdS )	BootHookPartHandlerz#cloud-boothookc                 K   s2   t j| t |d| _d | _|r.| | _d S )NZ	boothooks)r   Handler__init__r   Z	get_ipathboothook_dirinstance_idZget_instance_id)selfpathsZ
datasourceZ_kwargs r   >/usr/lib/python3/dist-packages/cloudinit/handlers/boot_hook.pyr	      s
    zBootHookPartHandler.__init__c                 C   sJ   t |}tj| j|}t jt || jd d}t 	||
 d |S )Nr   )prefixi  )r   Zclean_filenameospathjoinr
   Zstrip_prefix_suffixZdos2unixprefixesZ
write_filelstrip)r   payloadfilenamefilepathcontentsr   r   r   _write_part!   s    
 zBootHookPartHandler._write_partc                 C   s   |t jkrd S | ||}z6tj }| jd k	r>t| j|d< tj|g|d W nD tj	k
rt   t
td| Y n" tk
r   t
td| Y nX d S )NZINSTANCE_ID)envz#Boothooks script %s execution errorz'Boothooks unknown error when running %s)r   ZCONTENT_SIGNALSr   r   environcopyr   strr   ZProcessExecutionErrorr   ZlogexcLOG	Exception)r   dataZctyper   r   Z	frequencyr   r   r   r   r   handle_part*   s     


  zBootHookPartHandler.handle_partN)__name__
__module____qualname__r   r	   r   r"   r   r   r   r   r      s   	r   )r   Z	cloudinitr   r   Zloggingr   r   Zcloudinit.settingsr   Z	getLoggerr#   r   r   r   r   r   r   r   <module>   s   
