Viewing File: /usr/lib/python2.7/site-packages/cloudinit/sources/DataSourceMAAS.pyo

ó
oBú]c@s¢ddlmZddlZddlZddlZddlmZddlmZddlm	Z	ddlm
Z
ejeƒZ
dZddeefd	d	eefd
d
eefddeefd
d
eefgZdejfd„ƒYZd„Zd„Zd„Zddeddd„Zd„Zdefd„ƒYZdefd„ƒYZeejejffgZd„Z edkržd„Z!e!ƒndS(iÿÿÿÿ(tprint_functionN(tlog(tsources(t
url_helper(tutils
2012-03-01smeta-data/instance-idsmeta-data/local-hostnamesmeta-data/public-keyssmeta-data/vendor-datasvendor-datas	user-datatDataSourceMAAScBsneZdZdZd
Zd
Zd„Zed„ƒZ	d„Z
d„Zd„Zd„Z
d„Zd	„ZRS(sç
    DataSourceMAAS reads instance information from MAAS.
    Given a config metadata_url, and oauth tokens, it expects to find
    files under the root named:
      instance-id
      user-data
      hostname
      vendor-data
    tMAAScCsStjj||||ƒd|_tjj|jdƒ|_t	|j
ƒ|_dS(Ntmaas(Rt
DataSourcet__init__tNonetbase_urltostpathtjointseed_dirtget_id_from_ds_cfgtds_cfgtid_hash(tselftsys_cfgtdistrotpaths((sD/usr/lib/python2.7/site-packages/cloudinit/sources/DataSourceMAAS.pyR	0s	cCs%|jst|jƒ|_n|jS(N(t
_oauth_helpertget_oauth_helperR(R((sD/usr/lib/python2.7/site-packages/cloudinit/sources/DataSourceMAAS.pytoauth_helper6s	cCs#tjj|ƒ}d||jfS(Ns%s [%s](RRt__str__R(Rtroot((sD/usr/lib/python2.7/site-packages/cloudinit/sources/DataSourceMAAS.pyR<sc
Cs|j}y$|j|jt|jƒƒtSWn<tk
r@n,tk
rk}tjd|j|ƒ‚nX|j	ddƒ}|sˆtSyL|j|ƒsžtS|j|t
|d|jjd|jddƒƒtSWn%tk
rûtjtd|ƒtSXdS(Ns%s was malformed: %stmetadata_urltread_file_or_urlRtretriesis$Failed fetching metadata from url %s(Rt	_set_dataRtread_maas_seed_dirtTruetMAASSeedDirNonetMAASSeedDirMalformedtLOGtwarningtgetR
tFalsetwait_for_metadata_servicetread_maas_seed_urlRtreadurlRt	ExceptionRtlogexc(Rtmcfgtexcturl((sD/usr/lib/python2.7/site-packages/cloudinit/sources/DataSourceMAAS.pyt	_get_data@s.	

cCs…||_|\}}}||_||_||_|rytj|ƒ|_Wqtk
r}}tj	d|ƒd|_qXndS(Ns"Invalid content in vendor-data: %s(Rtuserdata_rawtmetadatatvendordata_pureRtconvert_vendordatatvendordata_rawt
ValueErrorR$R%R
(RR/tdatatudtmdtvdte((sD/usr/lib/python2.7/site-packages/cloudinit/sources/DataSourceMAAS.pyRbs				cCsd|jS(s/Return the subplatform metadata source details.s
seed-dir (%s)(R(R((sD/usr/lib/python2.7/site-packages/cloudinit/sources/DataSourceMAAS.pyt_get_subplatformpsc	Cse|j}d}yt|jd|ƒƒ}Wn$tk
rQtjtd|ƒnX|dkrbtSd}y+||kr’t|jd|ƒƒ}nWn!tk
r¶tjd|ƒnXt	j	ƒ}|j
dƒrß|d	 }nd
|tf}|g}|jj
d|d|d|ƒ\}}|r8tjd|ƒn#tjd
|tt	j	ƒ|ƒƒt|ƒS(Nixtmax_waits Failed to get max wait. using %sii2ttimeoutsFailed to get timeout, using %st/iÿÿÿÿs%s/%s/meta-data/instance-idturlssUsing metadata source: '%s's(Giving up on md from %s after %i seconds(RtintR&R+RR,R$R'R%ttimetendswitht
MD_VERSIONRtwait_for_urltdebugtcriticaltbool(	RR/R-R=R>t	starttimet	check_urlR@t	_response((sD/usr/lib/python2.7/site-packages/cloudinit/sources/DataSourceMAAS.pyR(ts4	


		cCsD|jdkrtStj|d|jfiƒ}|jt|ƒkS(slocally check if the current system is the same instance.

        MAAS doesn't provide a real instance-id, and if it did, it is
        still only available over the network.  We need to check based
        only on local resources.  So compute a hash based on Oauth tokens.t
datasourceN(RR
R'Rtget_cfg_by_pathtdsnameR(RRtncfg((sD/usr/lib/python2.7/site-packages/cloudinit/sources/DataSourceMAAS.pytcheck_instance_id–sN(t__name__t
__module__t__doc__RNR
RRR	tpropertyRRR0RR<R(RP(((sD/usr/lib/python2.7/site-packages/cloudinit/sources/DataSourceMAAS.pyR!s				"			"cCsAd}tg|D]}||j|ƒf^qƒ}tj|S(s°Return an oauth helper instance for values in cfg.

       @raises ValueError from OauthUrlHelper if some required fields have
               true-ish values but others do not.tconsumer_keytconsumer_secrett	token_keyttoken_secret(RURVRWRX(tdictR&RtOauthUrlHelper(tcfgtkeystrtkwargs((sD/usr/lib/python2.7/site-packages/cloudinit/sources/DataSourceMAAS.pyR¢s.cCsTd}djg|D]}|j|dƒ^qƒ}dtj|jdƒƒjƒS(	s;Given a config, generate a unique identifier for this node.RURWRXttsv1:sutf-8(sconsumer_keys	token_keystoken_secret(RR&thashlibtsha256tencodet	hexdigest(Rtfieldstftidstr((sD/usr/lib/python2.7/site-packages/cloudinit/sources/DataSourceMAAS.pyR¬s.cCsm|jdƒr|d}ntjj|ƒsJttj|ƒƒdkrYtdƒ‚ntd|ddƒS(Nsfile://iis%s: not a directorys	file://%stversion(	t
startswithRR
tisdirtlentlistdirR"R)R
(tseed_d((sD/usr/lib/python2.7/site-packages/cloudinit/sources/DataSourceMAAS.pyR µs

.c	Csx|dkrtj}n|jdƒr4|d }ni}x.tD]&\}}}	}
|dkrrd||f}nd|||f}y‚tj|ƒ}||d|d|d|ƒ}
|
jƒrð|	r×|
j||<qtj	|
jƒ||<nt
jd||
jƒWqAtj
k
rf}|jd	krK|
rKtd
||fƒ‚qg|jd	krg|‚qgqAXqAWt||ƒS(s›
    Read the maas datasource at seed_url.
      read_file_or_url is a method that should provide an interface
      like util.read_file_or_url

    Expected format of seed_url is are the following files:
      * <seed_url>/<version>/meta-data/instance-id
      * <seed_url>/<version>/meta-data/local-hostname
      * <seed_url>/<version>/user-data
    If version is None, then <version>/ will not be used.
    R?iÿÿÿÿs%s/%ss%s/%s/%sRR>tssl_detailss4Fetching from %s resulted in an invalid http code %si”sMissing required %s: %sN(R
RRRCt	DS_FIELDSRtfetch_ssl_detailstoktcontentst
decode_binaryR$R%tcodetUrlErrorR#tcheck_seed_contents(tseed_urlRR>RhRRR9R
t	_dictnametbinarytoptionalR/RntrespR;((sD/usr/lib/python2.7/site-packages/cloudinit/sources/DataSourceMAAS.pyR)¿s4

		c
Csti}g}x¢tD]š\}}}}||krM|s|j|ƒqqnd|krŸ|jdƒ\}}	}
||krŠi||<n|||||
<q||||<qWt|ƒdkrÖtd|ƒ‚n|rõtd||fƒ‚nd}|jdƒrUtƒ}t	j
|jdƒd|dtƒ}||krUtdƒ‚qUn|jd	ƒ|jd
ƒ|fS(s½Validate if dictionary content valid as a return for a datasource.
       Either return a (userdata, metadata, vendordata) tuple or
       Raise MAASSeedDirMalformed or MAASSeedDirNone
    R?is%s: no data files founds%s: missing files %ssvendor-datatdefaulttalloweds%vendor-data was not loadable as yaml.s	user-datas	meta-dataN(Rotappendt	partitionRkR"R#R
R&tobjectRt	load_yaml(
tcontenttseedtrettmissingtspathtdpatht_binaryRzttopt_tptvd_dataterr((sD/usr/lib/python2.7/site-packages/cloudinit/sources/DataSourceMAAS.pyRvîs2
		R"cBseZRS((RQRR(((sD/usr/lib/python2.7/site-packages/cloudinit/sources/DataSourceMAAS.pyR"sR#cBseZRS((RQRR(((sD/usr/lib/python2.7/site-packages/cloudinit/sources/DataSourceMAAS.pyR#scCstj|tƒS(N(Rtlist_from_dependstdatasources(tdepends((sD/usr/lib/python2.7/site-packages/cloudinit/sources/DataSourceMAAS.pytget_datasource_list#st__main__c
s˜ddl}ddl}ddl}|jddƒ}|jddddd	d
dƒ|jddddd
d
dƒ|jdddddd
dƒ|jdddddd
dƒ|jdddddd
dƒ|jdddddd
tƒ|jddddƒ}xQdDdEdFfD]@\}}|j|d|ƒ}|jd#dd$d%d&d
dƒqW|j	ƒ}i|j
d'6|jd(6|jd)6|j
d*6}	|jdkrxfdGD][}
d-|
d.}tjj|ƒr¬tj|tjƒr¬|jjd/|ƒ||_q¬q¬Wn|jr¸tj|jƒ}d0|krF|d0d1}nxA|	jƒD]3}
|
|krS|	|
dkrS||
|	|
<qSqSW|jdkr¸d2|kr¸|d2|_q¸n|jdkrç|jjd3ƒ|jd4ƒnt|	ƒ‰‡fd5†‰‡fd6†‰‡‡‡fd7†‰|jd!kr1|jjd8|jƒˆj}|jd9d:ksz|jjd;ƒr•t|jƒ\}}}n-t |jd|j!d<|d=d>ƒ\}}}t"d?ƒt"|dkrád@n	|j#ƒƒt"dAƒ|j|ƒt"dBƒ|j|dkr'd@n|ƒnc|jdkrPˆ|jƒnD|jdkr”|jj$d:ƒs„dC|j|_nˆ|jƒndS(Hsä
        Call with single argument of directory or http or https url.
        If url is given additional arguments are allowed, which will be
        interpreted as consumer_key, token_key, token_secret, consumer_secret
        iÿÿÿÿNtdescriptionsInteract with MAAS DSs--configtmetavartfilethelpsspecify DS config fileR|s--ckeytkeysthe consumer key to auth withs--tkeysthe token key to auth withs--csectsecretsthe consumer secret (likely '')R`s--tsecsthe token secret to auth withs--apiverRhs the apiver to use ( can be used)ttitletsubcommandstdesttsubcmdtcrawlscrawl the datasourceR&sdo a single GET of provided urls
check-seedsread and verify seed at urlR/sthe datasource urltnargst?RURWRXRVt91_kernel_cmdline_urlt90_dpkg_maass/etc/cloud/cloud.cfg.d/s.cfgsUsed config in %s.
RLRRs)Must provide a url or a config with url.
icsˆj|ddƒjS(NRi(R*Rr(R/(R(sD/usr/lib/python2.7/site-packages/cloudinit/sources/DataSourceMAAS.pytgeturlescs$td|ˆ|ƒjƒfƒdS(Ns== %s ==
%s
(tprinttdecode(R/(R¢(sD/usr/lib/python2.7/site-packages/cloudinit/sources/DataSourceMAAS.pytprinturliscs |jdƒr’xŠˆ|ƒjƒjƒD]c}|jdƒrTˆd||fƒq(|dkrwˆd|dfƒq(ˆd||fƒq(Wn
ˆ|ƒdS(NR?s%s%ss	meta-datas
meta-data/(RCR¤t
splitlines(R/tline(RR¢R¥(sD/usr/lib/python2.7/site-packages/cloudinit/sources/DataSourceMAAS.pyRlssChecking seed at %s
iR?sfile://RRis=== user-data ===sN/As=== meta-data ===s=== vendor-data ===s%s/(scrawlscrawl the datasource(sgetsdo a single GET of provided url(s
check-seedsread and verify seed at url(R R¡(%targparsetpprinttsystArgumentParsertadd_argumentR
RDtadd_subparserst
add_parsert
parse_argstckeyttkeyttsectcsectconfigRR
texiststaccesstR_OKtstderrtwriteRt	read_confR\R/texitRRœR*RiR R)tapiverR£R¤RC(R¨R©RªtparsertsubcmdstnameR–R‹targstcredstfnametfpathR[R—R*tuserdataR2R:((RR¢RR¥sD/usr/lib/python2.7/site-packages/cloudinit/sources/DataSourceMAAS.pytmain(sŠ





'	
	%
"


"("t
__future__RRaRRBt	cloudinitRtloggingRRRt	getLoggerRQR$RDR'R!RoRRRRR R
R)RvR+R"R#tDEP_FILESYSTEMtDEP_NETWORKRR‘RÅ(((sD/usr/lib/python2.7/site-packages/cloudinit/sources/DataSourceMAAS.pyt<module>	s8	
			
.	&		i
Back to Directory File Manager