jbc@sddlZddlZddlZddlZddlZddlZddlmZddlm Z ddl m Z ddl m Z ddlmZddlmZmZmZddlmZdd lmZejeZejrddlZejZnddlZej jZe!Z"e!Z#dZ%d Z&ydd l'm(Z(ddl)Z)e)j*d Z+e(e+jZ%e%e(d kre,Z"ne(de%koe(dknre,Z#nWne-k rnXdZ.dZ/dZ0de1fdYZ2de2fdYZ3de1fdYZ4de5fdYZ6dZ7dddddddde,e,dde!e,ddZ8dddddddddd Z9d e1fd!YZ:dd"Z;d#Z<dS($iN(t parsedate(tENOENT(tpartial(tcount(t exceptions(turlparset urlunparsetquote(tlog(tversiontREDACTED(t LooseVersiontrequestss0.8.8s0.7.0s1.0.0cCsRtt|dd}|d rH|drH|d|d(R<t redirects_oktupper((s8/usr/lib/python2.7/site-packages/cloudinit/url_helper.pyRAs  cCs |jjS(N(RGR$(R<((s8/usr/lib/python2.7/site-packages/cloudinit/url_helper.pyR$scCs |jjS(N(RGt status_code(R<((s8/usr/lib/python2.7/site-packages/cloudinit/url_helper.pyR#scCs |jjS(N(RGttext(R<((s8/usr/lib/python2.7/site-packages/cloudinit/url_helper.pyRCs( RDRER=tpropertyR%RR>RAR$R#RC(((s8/usr/lib/python2.7/site-packages/cloudinit/url_helper.pyRFs  R2cBseZddddZRS(cCsYtj|t|||_||_||_|jdkrLi|_n||_dS(N(R/R=RR"R#R$R3R(R<R"R#R$R((s8/usr/lib/python2.7/site-packages/cloudinit/url_helper.pyR=s    N(RDRER3R=(((s8/usr/lib/python2.7/site-packages/cloudinit/url_helper.pyR2scCsi}t|j}|dkr|rts@tjdtqd|krg|drg|d|dtintR tversion_stringRtrangetitemsR)tcopytdeepcopyR R*tdebugR tSessiontrequesttraise_for_statusRLR,RIRFRtRequestExceptiont isinstancet HTTPErrorthasattrRHtappendR2R$RUtSSLErrorttimetsleep(RR R^tretriest sec_betweenR$t headers_cbtheaders_redactRWt check_statusRZt exception_cbtsessionRbt log_req_resptrequest_methodtreq_argst req_configt manual_triest def_headersRatexcpstitfiltered_req_argstktvtmatched_headerstkeytsesstrR9((R$s8/usr/lib/python2.7/site-packages/cloudinit/url_helper.pyR5s#             % (            c Cstj} dd} |dkr-| }nd} d} d}xtr|dk ri||| }nt| dd}x|D]}tj}| dkr| || rPn|dk r|r||| |krt| ||}qnd}d}y|dk r||}ni}t|d|d|d |d td | }|jsd |j}tt |d |jd|j d|}nS|j sd|j}tt |d |jd|j d|}n ||jfSWnEtk r }d|}|}n#t k r-}d|}|}nXttj| }|rTd|nd}d||||f}|||r|d|d|qqW| || rPn| d} t jd|tj|qEWtdfS(sI urls: a list of urls to try max_wait: roughly the maximum time to wait before giving up The max time is *actually* len(urls)*timeout as each url will be tried once and given the timeout provided. a number <= 0 will always result in only one try timeout: the timeout provided to urlopen status_cb: call method with string message when a url is not available headers_cb: call method with single argument of url to get headers for request. headers_redact: a list of header names to redact from the log exception_cb: call method with 2 arguments 'msg' (per status_cb) and 'exception', the exception that occurred. sleep_time_cb: call method with 2 arguments (response, loop_n) that generates the next sleep time. request_method: indicate the type of HTTP request, GET, PUT, or POST returns: tuple of (url, response contents), on failure, (False, None) the idea of this routine is to wait for the EC2 metadata service to come up. On both Eucalyptus and EC2 we have seen the case where the instance hit the MD before the MD service was up. EC2 seems to have permanently fixed this, though. In openstack, the metadata service might be painfully slow, and unable to avoid hitting a timeout of even up to 10 seconds or more (LP: #894279) for a simple GET. Offset those needs with the need to not hang forever (and block boot) on a system where cloud-init is configured to look for EC2 Metadata service but is not going to find one. It is possible that the instance data host (169.254.169.254) may be firewalled off Entirely for a system, meaning that the connection will block forever unless a timeout is set. A value of None for max_wait will retry indefinitely. cSstj|dS(N(R*Rm(tmsgtexc((s8/usr/lib/python2.7/site-packages/cloudinit/url_helper.pyt log_status_cbscSs0|dkrtS|dkp/tj||kS(Ni(R3R>Rw(tmax_waitt start_time((s8/usr/lib/python2.7/site-packages/cloudinit/url_helper.pyttimeups iiiRR$R|R^R}Rsempty response [%s]R#Rsbad status code [%s]srequest error [%s]sunexpected error [%s]s%sst unlimitedsCalling '%s' failed [%s/%s]: %sRt exceptions1Please wait %s seconds while we wait to try againN(RwR3R?RgR5R>R%R#R2t ValueErrorR$RAt ExceptionR*RmRx(turlsRR^t status_cbR{R|t sleep_timeR~t sleep_time_cbRRRRtloop_nRHRtnowtreasonturl_excR$R9t time_takent max_wait_strt status_msg((s8/usr/lib/python2.7/site-packages/cloudinit/url_helper.pyt wait_for_urlbsx&                          tOauthUrlHelpercBsqeZd d d d ddZdZdZdZdZdZdZ dZ d Z d Z RS( s/run/oauth_skew.jsoncCs||_|pd|_||_||_||_t|_d|_|j|j|jf}t|sut |_nt |st dn|j }|pi|_ dS(NRisBall or none of token_key, token_secret, or consumer_key can be set(t consumer_keytconsumer_secrett token_keyt token_secrettskew_data_fileR?t _do_oauthtskew_change_limittanyR>tallRtread_skew_filet skew_data(R<RRRRRtrequiredtold((s8/usr/lib/python2.7/site-packages/cloudinit/url_helper.pyR=s          cCsP|jrLtjj|jrLt|jdd}tj|SWdQXndS(NtmodeR(RtosRtisfileR-tjsontloadR3(R<R8((s8/usr/lib/python2.7/site-packages/cloudinit/url_helper.pyRscCsp|js dS|j}|dkr.i}n|||R#R1R?R"R tTimeout(RR((s8/usr/lib/python2.7/site-packages/cloudinit/url_helper.pytretry_on_url_exc_s(=RkRRR tsixRwt email.utilsRR0Rt functoolsRt itertoolsRRtsix.moves.urllib.parseRRRRt cloudinitRtloggingR t getLoggerRDR*tPY2thttplibR1t http.clientRRR>RURfR3RVR tdistutils.versionR t pkg_resourcestget_distributiont_REQR?RRRR:tobjectR;R4RFR/R2RYR5RRRR(((s8/usr/lib/python2.7/site-packages/cloudinit/url_helper.pyt sj             (    !       wn