Current File : //lib/python2.7/site-packages/cloudinit/url_helper.pyo
�
�jbc@s�ddlZddlZddlZddlZddlZddlZddlmZddlm	Z	ddl
mZddlm
Z
ddlmZddlmZmZmZddlmZdd	lmZeje�ZejrddlZejZnddlZej jZe!Z"e!Z#dZ%d
Z&y�ddl'm(Z(ddl)Z)e)j*d�Z+e(e+j�Z%e%e(d
�kr�e,Z"ne(d�e%ko�e(d�knr�e,Z#nWne-k
r�nXd�Z.d�Z/d�Z0de1fd��YZ2de2fd��YZ3de1fd��YZ4de5fd��YZ6d�Z7dddddddde,e,dde!e,dd�Z8dddddddddd�	Z9d e1fd!��YZ:dd"�Z;d#�Z<dS($i����N(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<d|d<nt|�S(Ntschemethttpiit(tlistRR(turlt
parsed_url((s8/usr/lib/python2.7/site-packages/cloudinit/url_helper.pyt	_cleanurl;s

cGs3d�}|}x|D]}|||�}qW|S(NcSsott|��}|d}|r?|jd�r?|d7}n|tt|�dd�7}||d<t|�S(Nit/tsafes/:(RRtendswithturlquotetstrR(Rtadd_ont
url_parsedtpath((s8/usr/lib/python2.7/site-packages/cloudinit/url_helper.pytcombine_singleGs


((tbasetadd_onsRRR((s8/usr/lib/python2.7/site-packages/cloudinit/url_helper.pytcombine_urlEs
		
c	Ks|j�}|jd�r(d|}n|j�jd�r�|jd�r_tjd|�n|td�}y(t|d��}|j�}WdQXWnUt	k
r�}|j
}|j
tkr�t}nt
d|d	|d
dd|��nXt|d|�St||�SdS(
s0Wrapper function around readurl to allow passing a file path as url.

    When url is not a local file path, passthrough any kwargs to readurl.

    In the case of parameter passthrough to readurl, default values for some
    parameters. See: call-signature of readurl in this module for param docs.
    Rs	file://%ssfile://tdatas'Unable to post data to file resource %strbNtcausetcodetheadersRtcontents(tlstript
startswithtlowertgettLOGtwarningtlentopentreadtIOErrorterrnoRt	NOT_FOUNDtUrlErrortNonetFileResponsetreadurl(Rtkwargst	file_pathtfpR%teR#((s8/usr/lib/python2.7/site-packages/cloudinit/url_helper.pytread_file_or_urlVs"
		%tStringResponsecBs&eZdd�Zd�Zd�ZRS(i�cCs(||_i|_||_d|_dS(N(R#R$R%R3R(tselfR%R#((s8/usr/lib/python2.7/site-packages/cloudinit/url_helper.pyt__init__vs			cOs|jdkrtStS(Ni�(R#tFalsetTrue(R<targsR6((s8/usr/lib/python2.7/site-packages/cloudinit/url_helper.pytok|scCs|jjd�S(Nsutf-8(R%tdecode(R<((s8/usr/lib/python2.7/site-packages/cloudinit/url_helper.pyt__str__�s(t__name__t
__module__R=RARC(((s8/usr/lib/python2.7/site-packages/cloudinit/url_helper.pyR;us	R4cBseZdd�ZRS(i�cCs#tj||d|�||_dS(NR#(R;R=R(R<RR%R#((s8/usr/lib/python2.7/site-packages/cloudinit/url_helper.pyR=�s(RDRER=(((s8/usr/lib/python2.7/site-packages/cloudinit/url_helper.pyR4�stUrlResponsecBsbeZd�Zed��Zed��Zed�Zed��Zed��Z	d�Z
RS(cCs
||_dS(N(t	_response(R<tresponse((s8/usr/lib/python2.7/site-packages/cloudinit/url_helper.pyR=�scCs
|jjS(N(RGtcontent(R<((s8/usr/lib/python2.7/site-packages/cloudinit/url_helper.pyR%�scCs
|jjS(N(RGR(R<((s8/usr/lib/python2.7/site-packages/cloudinit/url_helper.pyR�scCs@d}|rd}nd|jko/|knr8tStSdS(Ni,i�i�(R#R?R>(R<tredirects_oktupper((s8/usr/lib/python2.7/site-packages/cloudinit/url_helper.pyRA�s	cCs
|jjS(N(RGR$(R<((s8/usr/lib/python2.7/site-packages/cloudinit/url_helper.pyR$�scCs
|jjS(N(RGtstatus_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.pyRC�s(RDRER=tpropertyR%RR>RAR$R#RC(((s8/usr/lib/python2.7/site-packages/cloudinit/url_helper.pyRF�s		R2cBseZdddd�ZRS(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.pyR2�scCs�i}t|�j}|dkr�|r�ts@tjdt�q�d|krg|drg|d|d<n
t|d<d|kr�d|kr�|d|dg|d<q�d|kr�t|d�|d<q�n|S(NthttpssGSSL is not supported in requests v%s, cert. verification can not occur!tca_certstverifyt	cert_filetkey_filetcert(RR
tSSL_ENABLEDR*R+t_REQ_VERR?R(Rtssl_detailstssl_argsR
((s8/usr/lib/python2.7/site-packages/cloudinit/url_helper.pyt
_get_ssl_args�s	

iicsJt|�}i|d6}|jt||��|
|d<|sT|rKdnd}n||d<|dk	r�tt|�d�|d<n|dkr�g}ntr�itd6}||d	<nd
}|r�tt|�d
d
�}nidt	j
�d6}�r|j��n|�|s4�fd
�}|}n|rG||d<n|dkr\d}ng}x�|
rtt�ntd|�D]�}||�|d<i}x�|j
�D]�\}}|dkr�q�n|dkr@|r@g|D]}|j|�r�|^q�}|rJtj|�||<x|D]}t|||<q"WqJq�|||<q�Wy�|rtjd||
rodn|||�n|dkr�tj�}n|�}|j|�}WdQX|	r�|j�ntjd||jt|j�|d
�t|�SWq�tjk
r.}t|tj�r{t |d�r{t |j!d�r{|j"t#|d|j!jd|j!j$d|��n5|j"t#|d|��t%r�t|tj&�r�Pn|r�||j�|d�r�Pn|
r�|dks|d
|kr/|dkr/|rtjd|�nt'j(|�q/q�Xq�W|rF|d�ndS(s�Wrapper around requests.Session to read the url and retry if necessary

    :param url: Mandatory url to request.
    :param data: Optional form data to post the URL. Will set request_method
        to 'POST' if present.
    :param timeout: Timeout in seconds to wait for a response
    :param retries: Number of times to retry on exception if exception_cb is
        None or exception_cb returns True for the exception caught. Default is
        to fail with 0 retries on exception.
    :param sec_between: Default 1: amount of seconds passed to time.sleep
        between retries. None or -1 means don't sleep.
    :param headers: Optional dict of headers to send during request
    :param headers_cb: Optional callable returning a dict of values to send as
        headers during request
    :param headers_redact: Optional list of header names to redact from the log
    :param ssl_details: Optional dict providing key_file, ca_certs, and
        cert_file keys for use on in ssl connections.
    :param check_status: Optional boolean set True to raise when HTTPError
        occurs. Default: True.
    :param allow_redirects: Optional boolean passed straight to Session.request
        as 'allow_redirects'. Default: True.
    :param exception_cb: Optional callable which accepts the params
        msg and exception and returns a boolean True if retries are permitted.
    :param session: Optional exiting requests.Session instance to reuse.
    :param infinite: Bool, set True to retry indefinitely. Default: False.
    :param log_req_resp: Set False to turn off verbose debug messages.
    :param request_method: String passed as 'method' to Session.request.
        Typically GET, or POST. Default: POST if data is provided, GET
        otherwise.
    Rtallow_redirectstPOSTtGETtmethodittimeoutt
store_cookiestconfigis
Cloud-Init/%ss
User-Agentcs�S(N((R(R$(s8/usr/lib/python2.7/site-packages/cloudinit/url_helper.pyt_cbsR i����R$s'[%s/%s] open '%s' with %s configurationtinfiniteNs(Read from %s (%s, %sb) after %s attemptsRHRLR#s1Please wait %s seconds while we wait to try again()RtupdateRYR3tmaxtfloattCONFIG_ENABLEDR>tintR	tversion_stringRtrangetitemsR)tcopytdeepcopyR
R*tdebugRtSessiontrequesttraise_for_statusRLR,RIRFRtRequestExceptiont
isinstancet	HTTPErrorthasattrRHtappendR2R$RUtSSLErrorttimetsleep(RR R^tretriestsec_betweenR$t
headers_cbtheaders_redactRWtcheck_statusRZtexception_cbtsessionRbtlog_req_resptrequest_methodtreq_argst
req_configtmanual_triestdef_headersRatexcpstitfiltered_req_argstktvtmatched_headerstkeytsesstrR9((R$s8/usr/lib/python2.7/site-packages/cloudinit/url_helper.pyR5�s�#


	

	
	%(

	

 

c
Cs�tj�}
dd�}|dkr-|}nd�}d}
d}x�tr�|dk	ri|||
�}nt|
d�d}x|D]}tj�}|
dkr�|||
�r�Pn|dk	r�|r�|||
|kr�t|
||�}q�nd}d}y�|dk	r||�}ni}t|d|d|d	|d
td|	�}|js�d|j}tt	|�d
|jd|j
d|�}nS|j�s�d|j}tt	|�d
|jd|j
d|�}n
||jfSWnEtk
r}d|}|}n#tk
r-}d|}|}nXttj�|
�}|rTd|nd}d||||f}||�|r�|d|d|�q�q�W|||
�r�Pn|
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_cb�scSs0|dkrtS|dkp/tj�||kS(Ni(R3R>Rw(tmax_waitt
start_time((s8/usr/lib/python2.7/site-packages/cloudinit/url_helper.pyttimeup�siiiRR$R|R^R}R�sempty response [%s]R#Rsbad status code [%s]srequest error [%s]sunexpected error [%s]s%sst	unlimitedsCalling '%s' failed [%s/%s]: %sR�t	exceptions1Please wait %s seconds while we wait to try againN(RwR3R?RgR5R>R%R#R2t
ValueErrorR$RAt	ExceptionR*RmRx(turlsR�R^t	status_cbR{R|t
sleep_timeR~t
sleep_time_cbR�R�R�R�tloop_nRHRtnowtreasonturl_excR$R9t
time_takentmax_wait_strt
status_msg((s8/usr/lib/python2.7/site-packages/cloudinit/url_helper.pytwait_for_urlbsx&			
		


	




	tOauthUrlHelpercBsqeZdddddd�Zd�Zd�Zd�Zd�Zd�Zd�Z	d�Z
d	�Zd
�ZRS(s/run/oauth_skew.jsoncCs�||_|pd|_||_||_||_t|_d|_|j|j|jf}t|�sut	|_nt
|�s�td��n|j�}|p�i|_
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>tallR�tread_skew_filet	skew_data(R<R�R�R�R�R�trequiredtold((s8/usr/lib/python2.7/site-packages/cloudinit/url_helper.pyR=�s						cCsP|jrLtjj|j�rLt|jdd��}tj|�SWdQXndS(NtmodeR�(R�tosRtisfileR-tjsontloadR3(R<R8((s8/usr/lib/python2.7/site-packages/cloudinit/url_helper.pyR��scCsp|js
dS|j�}|dkr.i}n|||<t|jdd��}|jtj|��WdQXdS(NR�tw(R�R�R3R-twriteR�tdumps(R<thosttvaluetcurR8((s8/usr/lib/python2.7/site-packages/cloudinit/url_helper.pytupdate_skew_file�s		
c	Cs4t|t�o*|jdkp*|jdks1dSd|jkrWtjd|j�dS|jd}ytjt|��}Wn't	k
r�}tjd||�dSXt
|tj��}t|j�j
}|jj|d�}t||�|jkr#|j||�tjd||�n||j|<dS(Ni�i�tdates$Missing header 'date' in %s responses#Failed to convert datetime '%s': %sis$Setting oauth clockskew for %s to %d(RrR2R#R$R*R+RwtmktimeRR�RgRRtnetlocR�R)tabsR�R�(	R<R�R�R�tremote_timeR9tskewR�told_skew((s8/usr/lib/python2.7/site-packages/cloudinit/url_helper.pyR~s*	


cCs�|js
iSd}t|�j}|jrZ||jkrZttj��|j|}ntd|d|jd|j	d|j
d|jd|�S(NRR�R�R�R�t	timestamp(R�R3RR�R�RgRwt
oauth_headersR�R�R�R�(R<RR�R�((s8/usr/lib/python2.7/site-packages/cloudinit/url_helper.pyR{s	 cCsKt|j|jd��|d<t|j|jd��|d<|||�S(NR{R~(Rt_headers_cbR)t
_exception_cb(R<twrapped_funcR@R6((s8/usr/lib/python2.7/site-packages/cloudinit/url_helper.pyt_wrapped*s
cOs|jt||�S(N(R�R�(R<R@R6((s8/usr/lib/python2.7/site-packages/cloudinit/url_helper.pyR�1scOs|jt||�S(N(R�R5(R<R@R6((s8/usr/lib/python2.7/site-packages/cloudinit/url_helper.pyR54scCs:d}z|r!|||�}nWd|j||�X|S(N(R3R~(R<textra_exception_cbR�R�tret((s8/usr/lib/python2.7/site-packages/cloudinit/url_helper.pyR�7scCs5i}|r||�}n|j|j|��|S(N(RcR{(R<textra_headers_cbRR$((s8/usr/lib/python2.7/site-packages/cloudinit/url_helper.pyR�@s
N(
RDRER3R=R�R�R~R{R�R�R5R�R�(((s8/usr/lib/python2.7/site-packages/cloudinit/url_helper.pyR��s									cCs�yddlj}Wntk
r2td��nX|rHt|�}nd}|j|d|d|d|d|jd|�}|j|�\}}	}
|	S(Ni����soauth support is not availablet
client_secrettresource_owner_keytresource_owner_secrettsignature_methodR�(	toauthlib.oauth1toauth1tImportErrortNotImplementedErrorRR3tClienttSIGNATURE_PLAINTEXTtsign(RR�R�R�R�R�R�tclientt_uritsigned_headerst_body((s8/usr/lib/python2.7/site-packages/cloudinit/url_helper.pyR�Hs 
		cCsLt|t�stS|jtkr&tS|jrHt|jtj�rHtStS(s�readurl exception_cb that will retry on NOT_FOUND and Timeout.

    Returns False to raise the exception from readurl, True to retry.
    (	RrR2R>R#R1R?R"RtTimeout(R�R�((s8/usr/lib/python2.7/site-packages/cloudinit/url_helper.pytretry_on_url_exc_s(=RkR�R�RtsixRwtemail.utilsRR0Rt	functoolsRt	itertoolsRRtsix.moves.urllib.parseRRRRt	cloudinitRtloggingR	t	getLoggerRDR*tPY2thttplibR1thttp.clientRR�R>RURfR3RVR
tdistutils.versionRt
pkg_resourcestget_distributiont_REQR?R�RRR:tobjectR;R4RFR/R2RYR5R�R�R�R�(((s8/usr/lib/python2.7/site-packages/cloudinit/url_helper.pyt<module>sj		(

	
		!			�		wn