3 \men@sddlZddlmZddlmZmZmZmZddlZddlm Z ddl m Z ddl m Z mZejeZdZGdd d Zd d d eeeeed ddZeeedddZdeeeedddZedddZdS)N)time)DictOptionalTypeUnion)util)report_diagnostic_event)UrlErrorreadurlzhttp://169.254.169.254/metadatac@sFeZdZdZdddddeeeeedd d d Zed d dZ dS)ReadUrlRetryHandleraFManager for readurl retry behavior using exception_callback(). :param logging_backoff: Backoff to limit logging. :param max_connection_errors: Number of connection errors to retry on. :param retry_codes: Set of http codes to retry on. :param retry_deadline: Optional time()-based deadline to retry until. g?N)logging_backoffmax_connection_errors retry_codesretry_deadline)rrrreturncCs.||_||_||_||_d|_d|_d|_dS)Ng?r)rrrr_logging_threshold_request_count _last_error)selfrrrrr/usr/lib/python3.6/imds.py__init__s zReadUrlRetryHandler.__init__)rcCsP|jd7_t|ts.td|tjddSd}|jdk rNt|jkrNd}nd}|jdk rt|j t j r|jd8_|jdkrd}n|j dk r|j |j krd}|j|jkr|j|j9_nd}|j dk r|j|j krd}|j |_n>|jdkst|jt st|j |j r&d}t|j |_|s4| rLtd|j|ftjd|S)Nz1Polling IMDS failed with unexpected exception: %r) logger_funcFTrz1Polling IMDS failed attempt %d with exception: %r)r isinstancer rLOGwarningrrrcauserequestsConnectionErrorcoderrrrtypeinfo)rZreq_argsZ exceptionlogZretryrrrexception_callback2sJ               z&ReadUrlRetryHandler.exception_callback)r r rr) __name__ __module__ __qualname____doc__floatrintrboolr(rrrrr s r T) log_responsetimeout)url retry_handlerr1r2rcCs\yt||jddid||d}Wn6tk rT}ztd|tjdWYdd}~XnX|jS)a'Fetch URL from IMDS. :param url: url to fetch. :param log_response: log responses in readurl(). :param retry_deadline: time()-based deadline to retry until. :param timeout: Read/connection timeout in seconds for readurl(). :raises UrlError: on error fetching metadata. MetadatatrueT) exception_cbheadersinfinite log_req_respr2z&Failed to fetch metadata from IMDS: %s)rN)r r(r rrr contents)r3r4r1r2responseerrorrrr _fetch_urlps  r>)r3r4rcCsRt||d}y tj|Stk rL}ztd|tjdWYdd}~XnXdS)zFetch IMDS metadata. :param url: url to fetch. :param retry_deadline: time()-based deadline to retry until. :raises UrlError: on error fetching metadata. :raises ValueError: on error parsing metadata. )r4z&Failed to parse metadata from IMDS: %s)rN)r>rZ load_json ValueErrorrrr )r3r4Zmetadatar=rrr_fetch_metadatas   r@)rrrcCst||d}ytd}t||dStk r|}z@|jdkrjtdtjdt||d}td}t||dSWYdd}~XnXdS) zFetch extended metadata, falling back to non-extended as required. :param retry_deadline: time()-based deadline to retry until. :raises UrlError: on error fetching metadata. :raises ValueError: on error parsing metadata. )rrz./instance?api-version=2021-08-01&extended=true)r4iz,Falling back to IMDS api-version: 2019-06-01)rz /instance?api-version=2019-06-01N)r IMDS_URLr@r r$rrr )rrr4r3r=rrr fetch_metadata_with_api_fallbacks"     rB)rcCsTtd}tddddd}t||jd d id d d d}td|jddtjd|jS)zhFetch extended metadata, falling back to non-extended as required. :raises UrlError: on error. z'/reprovisiondata?api-version=2019-06-01g@rN)rrrrr5r6TFr0)r7r8r9r:r2z Polled IMDS z time(s))r)rCrDrE) rAr r r(rrrdebugr;)r3Zhandlerr<rrrfetch_reprovision_datas& rG)N)ZloggingrZtypingrrrrr"Z cloudinitrZcloudinit.sources.helpers.azurerZcloudinit.url_helperr r Z getLoggerr)rrAr strr/r.bytesr>r@r-rBrGrrrrs$    `