f<ddlZddlZddlZddlZddlmZmZmZddlm Z ddl m Z eje ZdZgdZGddej"ZGd d eZd Zd Zd ZddeddfdZdZGddeZGddeZeej8ffeej8ej:ffgZdZe dk(r dZ e yy)N)sources url_helperutil)KlibcNetworkConfigSource)NetworkConfigSourcez 2012-03-01))meta-data/instance-idrFF)meta-data/local-hostnamer FF)meta-data/public-keysr FT)zmeta-data/vendor-data vendor-dataTT) user-datar TTcVeZdZdZdZdZdZdZedZ dZ dZ dZ d Z d Zd Zy) DataSourceMAASz 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 MAASNctjj||||d|_tj j |jd|_t|j|_ y)Nmaas) r DataSource__init__base_urlospathjoinseed_dirget_id_from_ds_cfgds_cfgid_hash)selfsys_cfgdistropathss B/usr/lib/python3/dist-packages/cloudinit/sources/DataSourceMAAS.pyrzDataSourceMAAS.__init__.sM##D'65A  U^^V< )$++6 cf|jst|j|_|jSN) _oauth_helperget_oauth_helperrrs r oauth_helperzDataSourceMAAS.oauth_helper4s(!!!1$++!>D !!!r!cdtjj|}|d|jdS)Nz [])rr__str__r)rroots r r*zDataSourceMAAS.__str__:s'!!))$/ $--00r!c |j} |j|jt|jy#t$rYn2t $r'}t jd|j|d}~wwxYw|jdd}|sy |j|sy|j|t||jj|jdy#t$rtj t d|YywxYw)NTz%s was malformed: %s metadata_urlF)read_file_or_urlrretriesz$Failed fetching metadata from url %s)r _set_datarread_maas_seed_dirMAASSeedDirNoneMAASSeedDirMalformedLOGwarninggetwait_for_metadata_serviceread_maas_seed_urlr'readurlr Exceptionrlogexc)rmcfgexcurls r _get_datazDataSourceMAAS._get_data>s{{  NN4==*%>**    KKCS I s3/> A8 A8"A33A8C!#=C!!$DDc||_|\}}}||_||_||_|r t j ||_yy#t$r'}tjd|d|_Yd}~yd}~wwxYw)Nz"Invalid content in vendor-data: %s) r userdata_rawmetadatavendordata_purerconvert_vendordatavendordata_raw ValueErrorr5r6)rr?dataudmdvdes r r1zDataSourceMAAS._set_dataesw  B !  +&-&@&@&D#  + @!D&*## +sA A2 A--A2c d|jzS)z/Return the subplatform metadata source details.z seed-dir (%s))rr&s r _get_subplatformzDataSourceMAAS._get_subplatformss..r!c|j}d} t|jd|}|dk(ryd} ||vrt|jd|}tj}|jd r|dd }|d td }|g}|jj||| \}}|r!t jd |t|St jd|ttj|z t|S#t$rt j t d|Y wxYw#t$rt jd|Y wxYw)Nxmax_waitz Failed to get max wait. using %srF2timeoutzFailed to get timeout, using %s/z/meta-data/instance-id)urlsrQrSzUsing metadata source: '%s'z(Giving up on md from %s after %i seconds)rintr7r;rr<r5r6timeendswith MD_VERSIONr' wait_for_urldebugcriticalbool) rr?r=rQrS starttime check_urlrV _responses r r8z(DataSourceMAAS.wait_for_metadata_servicewsQ{{ K488J9:H q= D$dhhy':;IIK << cr(C58*E {**77'8 Y  II3S 9Cy LL:DIIK)+,  Cy? K KK? J K D KK97 C Ds"DD;$D87D8;EEc|jytj|d|jfi}|jt |k(S)alocally 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.F datasource)rrget_cfg_by_pathdsnamer)rrncfgs r check_instance_idz DataSourceMAAS.check_instance_idsC << ##GlDKK-H"M||1$777r!)__name__ __module__ __qualname____doc__rerr$rpropertyr'r*r@r1rNr8rgr!r rrsQFGM7 "" 1%N +/$L 8r!rczeZdZejej ej ejfZfdZ xZ S)DataSourceMAASLocalctjstjdytjdt|S)NzNo initramfs applicable configFz!Found initramfs applicable config)r is_applicabler5r\superr@)r __class__s r r@zDataSourceMAASLocal._get_datas?')779 II6 7 56w ""r!) rhrirjrCMD_LINE SYSTEM_CFGDS INITRAMFSnetwork_config_sourcesr@ __classcell__)rss@r roros9$$&&%% ##r!roc d}t|Dcgc]}||j|fc}}tjdi|Scc}w)zReturn an oauth helper instance for values in cfg. @raises ValueError from OauthUrlHelper if some required fields have true-ish values but others do not.) consumer_keyconsumer_secret token_key token_secretrm)dictr7rOauthUrlHelper)cfgkeysrkwargss r r%r%sD LD D1qAswwqz?1 2F  $ $ .v ..2sAc d}dj|Dcgc]}|j|dc}}dtj|j dj zScc}w)z;Given a config, generate a unique identifier for this node.)r{r}r~zv1:zutf-8)rr7hashlibsha256encode hexdigest)rfieldsfidstrs r rrsX :F II&9Qvzz!R(9 :E 7>>%,,w"78BBD DD:sA'c|jdr|dd}tjj|r!t tj |dk(r t dtd|zdS)Nfile://rz%s: not a directoryz file://%s)version) startswithrrisdirlenlistdirr3r9)seed_ds r r2r2s` # 77== C 6(:$;q$@344 kF2D AAr!cn|tj}|jdr|dd}i}tD]\}}} } ||d|} n |d|d|} t j |} || ||| } | j r5| r| j||<nDt j| j||<n!tjd| | jt||S#tj$r>}|jdk(r| std|d|||jdk7r|Yd}~d}~wwxYw) a 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: * //meta-data/instance-id * //meta-data/local-hostname * //user-data If version is None, then / will not be used. NrTrU)r0rS ssl_detailsz4Fetching from %s resulted in an invalid http code %sizMissing required z: )rr/rY DS_FIELDSrfetch_ssl_detailsokcontents decode_binaryr5r6codeUrlErrorr4check_seed_contents)seed_urlr/rSrrr0rJr _dictnamebinaryoptionalr?rresprLs r r9r9s?&%66CR= B-6)i ?%t,C ('48C 007K#Wg;Dwwy#}}BtH#11$--@BtH JII!: r8 ,,"" vv}X*15q93  sBC##D464D//D4ci}g}tD]U\}}}}||vr|s|j| d|vr*|jd\}} } ||vri||<||||| <N||||<Wt|dk(rt d|z|rt |d|d} |j drDt} tj|j d| t} | | ur t d|j d |j d | fS) zValidate if dictionary content valid as a return for a datasource. Either return a (userdata, metadata, vendordata) tuple or Raise MAASSeedDirMalformed or MAASSeedDirNone rTrz%s: no data files foundz: missing files Nr )defaultallowedz%vendor-data was not loadable as yaml.r meta-data) rappend partitionrr3r4r7objectr load_yaml) contentseedretmissingspathdpath_binaryrtop_pvd_dataerrs r rrs$ CG+4 ('ugx  u%  %<,ICA#~C!%.CHQK CJ ( 3x1}7$>??"T7#KLLG ww}h.. GGM "C&  c>&'NO O 77; !5w >>r!c eZdZy)r3Nrhrirjrmr!r r3r39r!r3c eZdZy)r4Nrrmr!r r4r4=rr!r4c6tj|tSr#)rlist_from_depends datasources)dependss r get_datasource_listrIs  $ $Wk ::r!__main__cddl}ddl}ddl}|jd}|j dddd|j d d d d|j d d d d|j dddd|j dddd|j dddt |j dd}dD]-\}}|j||}|j dddd/|j}|j|j|j|jd } |jyd!D]t} d"| zd#z} tjj!| s+tj"| tj$sP|j&j)d$| z| |_ v|jrmt+j,|j} d%| vr| d%d&} | j/D]} | | vs| | | | | | <|j0d'| vr | d'|_|j0,|j&j)d(|j3d)t5| fd*fd+fd,|j6d-k(r |j&j)d.|j0zj8}|j0dd/k(s|j0j;d0rt=|j0\}}}n't?|j0|j@|d12\}}}tCd3tC|d4n|jEtCd5|j|tCd6|j|d4y|y|j6d7k(r|j0y|j6d8k(rB|j0jGd/sd9|j0z|_|j0yy):z 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 rNzInteract with MAAS DS) descriptionz--configfilezspecify DS config file)metavarhelprz--ckeykeyzthe consumer key to auth withz--tkeyzthe token key to auth withz--csecsecretzthe consumer secret (likely '')rz--tseczthe token secret to auth withz--apiverrz the apiver to use ( can be used) subcommandssubcmd)titledest))crawlzcrawl the datasource)r7zdo a single GET of provided url) check-seedzread and verify seed at url)rr?zthe datasource url?)rnargsr)r{r}r~r|)91_kernel_cmdline_url 90_dpkg_maasz/etc/cloud/cloud.cfg.d/z.cfgzUsed config in %s. rcrr-z)Must provide a url or a config with url. r.c>j|djS)Nr.)r0)r:r)r?r's r geturlzmain..geturls''Q'7@@ @r!cRtd|d|jdy)Nz== z ==  )printdecode)r?rs r printurlzmain..printurls c6#;+=+=+?@ Ar!c|jdrf|jjD]>}|jdr ||!|dk(r |d3||@y|y)NrTrz meta-data/)rYr splitlines)r?linerrrs r rzmain..crawlsx||C "3K..0;;=7D}}S)T23,\:; 3!567 r!rzChecking seed at %s rTr)rr/r0z=== user-data ===zN/Az=== meta-data ===z=== vendor-data ===r7rz%s/)$argparsepprintsysArgumentParser add_argumentrZadd_subparsers add_parser parse_argsckeytkeytseccsecconfigrrexistsaccessR_OKstderrwriter read_confrr?exitr%rr:rr2r9apiverrrrY)rrrparsersubcmdsnamerrargscredsfnamefpathrrr:userdatarCrKrrr'rs @@@@r mainrOs ((5L(M )   0   -   2   0   3  ''m('K  LT4 ""4d"3A NN0T      "!II II#yy   ;; B (1E9FB77>>%(RYYubgg-FJJ$$%;e%CD"'DK  ( ;;..-Cs",'/zz| *#:%*"4!$SE#J *xxNc$9~. 88  JJ  I J HHQK'.  A B  ;;, & JJ  4txx? @"**Gxx{c!TXX%8%8%C+=dhh+G(8R+=HH KK%, ,(8R % & 8+%1B C % & MM( # ' ( MM2:% 62 6 [[E ! TXX  [[G #88$$S) 488+ $((O$r!)!rloggingrrX cloudinitrrrcloudinit.net.cmdlinercloudinit.sourcesr getLoggerrhr5rZrrrror%rr2r9rr;r3r4DEP_FILESYSTEM DEP_NETWORKrrrrmr!r rs //:1g!    G8W''G8T#.#$/EB     7-t$?N i  9 71134g,,g.A.ABC ; zHT FYr!