
­­­­­­­­­­­­­­­­­­
<!DOCTYPE html>
<html>
3
\meJ                 @   s(  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	m
Z
mZmZ d dlZd dlmZ d dlmZ d dlmZ ejeZde
e eeed
ddZG dd deZG dd deZG dd deZG dd deZG dd deZG dd deZG dd deZ G dd deZ!dS )    N)datetime)StringIO)AnyDictListOptionalTuple)version)identity)UrlError|')data	delimiter	quotecharreturnc             C   s8   t  (}tj|||tjdj|  |j j S Q R X dS )zEncode report data with csv.)r   r   ZquotingN)r   csvwriterZQUOTE_MINIMALZwriterowgetvaluerstrip)r   r   r   io r   /usr/lib/python3.6/errors.pyencode_report   s    
r   c               @   sZ   e Zd Zddeeeeef  ddddZedddZe	dd	d
Z
edddZdS )ReportableErrorN)supporting_data)reasonr   r   c            C   sz   dt j  | _d| _|| _|r(|| _ni | _tj | _yt	j
 | _W n. tk
rt } zd|| _W Y d d }~X nX d S )NzCloud-Init/z%https://aka.ms/linuxprovisioningerrorzfailed to read vm id: )r	   Zversion_stringagentdocumentation_urlr   r   r   Zutcnow	timestampr
   Zquery_vm_idvm_id	Exception)selfr   r   Zid_errorr   r   r   __init__'   s    
zReportableError.__init__)r   c             C   sd   dd| j  d| j g}|dd | jj D 7 }|d| j d| jj  d| j g7 }t|S )	Nzresult=errorzreason=zagent=c             S   s   g | ]\}}| d | qS )=r   ).0kvr   r   r   
<listcomp>E   s    z5ReportableError.as_encoded_report.<locals>.<listcomp>zvm_id=z
timestamp=zdocumentation_url=)	r   r   r   itemsr    r   Z	isoformatr   r   )r"   r   r   r   r   as_encoded_report=   s    

z!ReportableError.as_encoded_reportc             C   s.   t |to,| j|jko,| j|jko,| j|jkS )N)
isinstancer   r   r   r   )r"   otherr   r   r   __eq__N   s    
zReportableError.__eq__c             C   s&   | j j d| j d| j d| j dS )Nz(reason=z, timestamp=z, supporting_data=))	__class____name__r   r   r   )r"   r   r   r   __repr__V   s    zReportableError.__repr__)r0   
__module____qualname__strr   r   r   r#   r*   boolr-   r1   r   r   r   r   r   &   s
   r   c                   s$   e Zd Zedd fddZ  ZS )$ReportableErrorDhcpInterfaceNotFoundN)durationr   c                s   t  jd || jd< d S )Nzfailure to find DHCP interfacer7   )superr#   r   )r"   r7   )r/   r   r   r#   `   s    z-ReportableErrorDhcpInterfaceNotFound.__init__)r0   r2   r3   floatr#   __classcell__r   r   )r/   r   r6   _   s   r6   c                   s*   e Zd Zeee dd fddZ  ZS )ReportableErrorDhcpLeaseN)r7   	interfacer   c                s$   t  jd || jd< || jd< d S )Nzfailure to obtain DHCP leaser7   r<   )r8   r#   r   )r"   r7   r<   )r/   r   r   r#   g   s    
z!ReportableErrorDhcpLease.__init__)r0   r2   r3   r9   r   r4   r#   r:   r   r   )r/   r   r;   f   s   r;   c                   sP   e Zd Zee ee ee eeeeef   eeef dd fddZ	  Z
S )(ReportableErrorDhcpOnNonPrimaryInterfaceN)r<   driverrouterstatic_routesleaser   c               sB   t  jd || jd< || jd< || jd< || jd< || jd< d S )Nz&failure to find primary DHCP interfacer<   r>   r?   r@   rA   )r8   r#   r   )r"   r<   r>   r?   r@   rA   )r/   r   r   r#   o   s    	



z1ReportableErrorDhcpOnNonPrimaryInterface.__init__)r0   r2   r3   r   r4   r   r   r   r   r#   r:   r   r   )r/   r   r=   n   s   
r=   c                   s&   e Zd Zeedd fddZ  ZS )ReportableErrorImdsUrlErrorN)	exceptionr7   r   c               s   t |jtjrd}nBt |jtjr(d}n.t |jtjr<d}n|jrRd|j d}nd}t j| |jrt|j| j	d< || j	d< t
|| j	d	< |j| j	d
< d S )Nz connection timeout querying IMDSzconnection error querying IMDSzread timeout querying IMDSzhttp error z querying IMDSzunexpected error querying IMDSZ	http_coder7   rC   url)r+   causerequestsZConnectTimeoutConnectionErrorZReadTimeoutcoder8   r#   r   reprrD   )r"   rC   r7   r   )r/   r   r   r#      s    
z$ReportableErrorImdsUrlError.__init__)r0   r2   r3   r   r9   r#   r:   r   r   )r/   r   rB      s   rB   c                   s$   e Zd Zedd fddZ  ZS )+ReportableErrorImdsMetadataParsingExceptionN)rC   r   c               s   t  jd t|| jd< d S )Nzerror parsing IMDS metadatarC   )r8   r#   rI   r   )r"   rC   )r/   r   r   r#      s    z4ReportableErrorImdsMetadataParsingException.__init__)r0   r2   r3   
ValueErrorr#   r:   r   r   )r/   r   rJ      s   rJ   c                   s$   e Zd Zedd fddZ  ZS )!ReportableErrorUnhandledExceptionN)rC   r   c                sX   t  jd djtjt|||j}tj|j	dj
d}t|| jd< || jd< d S )Nzunhandled exception zutf-8rC   Ztraceback_base64)r8   r#   join	tracebackformat_exceptiontype__traceback__base64Z	b64encodeencodedecoderI   r   )r"   rC   ZtraceZtrace_base64)r/   r   r   r#      s    z*ReportableErrorUnhandledException.__init__)r0   r2   r3   r!   r#   r:   r   r   )r/   r   rL      s   rL   c                   s&   e Zd Zeedd fddZ  ZS )"ReportableErrorImdsInvalidMetadataN)keyvaluer   c               s.   t  jd|  || jd< t|| jd< d S )Nzinvalid IMDS metadata for key=rW   rX   )r8   r#   r   rI   )r"   rW   rX   )r/   r   r   r#      s    
z+ReportableErrorImdsInvalidMetadata.__init__)r0   r2   r3   r4   r   r#   r:   r   r   )r/   r   rV      s   rV   )r   r   )"rS   r   ZloggingrO   r   r   r   Ztypingr   r   r   r   r   rF   Z	cloudinitr	   Zcloudinit.sources.azurer
   Zcloudinit.url_helperr   Z	getLoggerr0   ZLOGr4   r   r!   r   r6   r;   r=   rB   rJ   rL   rV   r   r   r   r   <module>   s*   
9