\`fڐddlZddlZddlZddlZddlZddlZddlZddlmZddl m Z ddl m Z ddl m Z ddl m Z ddl mZddl mZdd l mZdd l mZdd l mZdd l mZdd lmZddlmZddlmZddlmZddl mZddlmZddlm Z ddl!m"Z"m#Z#m$Z$m%Z%dZ&dZ'e"e#e$dZ(ejRe*Z+d2dZ,dZ-d3dZ.dZ/dZ0defdZ1dZ2d4d Z3d5d!ee4e5ffd"Z6d#Z7d$Z8d%Z9d&Z:d'Z;d(Z53G J Jr(ctjtjt j t j |S)N)r uptime timestampr5)WELCOME_MSG_TPLformatr version_stringr r8 time_rfc2822r5s r'r3r3Ys>  ! !&&({{}##% " r(cg}|jrI|jD]:}|jtjj |j <|SN)filesappendr+pathrealpathname)argsfn_cfgsfhs r' extract_fnsrIbsKG zz** 6B NN277++BGG4 5 6 Nr(modscNt|z}|j|\}}t|t|z}|dk(rBd|d|d}tjj d|zt j|gSt jdt|t||S)NrzNo 'z ' modules to run under section ''rzRan %s modules with %s failures)MOD_SECTION_TPL run_sectionlenr r!r"r-debug)rJ action_namesectionfull_section_name which_ranfailurestotal_attemptedr&s r'run_module_sectionrWos'73 ,,->?Y)ns8}4O!    3( #  -s9~s8} r(cp|jdr%tj|jdyy)Nr )getr update_configuration)cfgs r'apply_reporting_cfgr\s* ww{&&sww{';<r(cxtj|}|D]}||vs|||fcStd|d|d)Nz No keys (z) found in string 'rL)r keyval_str_to_dictKeyError)cmdlinenamesdatakeys r'parse_cmdline_urlrdsH  " "7 +D" $;S > !" '5I JJr(returnc B|tj} t|\}}|j d}|rmtjj|rN|r/d|d|d}tj}|rtj }||fSd|d|d}tj}||fS|dd d d }|s|rtj}d |d <ntj }d|d <d} d} tjd%i|} d} | jrd } t| tj r< | t#| j%t'| z } | j | s d} n| j*j | sd} | r7|dk(rt,j.t1tj2dddd fS|dk(rtj}ntj}|d|dt1| fS|d|d| j4dfS| | j*z} tj8|| d !tjd"|d#|d$|fS#t$rtj dfcYSwxYw#t($rY'wxYw#tj6$r}|d|d|fcYd}~Sd}~wwxYw)&aWrite data from url referenced in command line to path. path: a file to write content to if downloaded. network: should network access be assumed. cmdline: the cmdline to parse for cloud-config-url. This is used in MAAS datasource, in "ephemeral" (read-only root) environment where the instance netboots to iscsi ro root. and the entity that controls the pxe config has to configure the maas datasource. An attempt is made on network urls even in local datasource for case of network set up in initramfs. Return value is a tuple of a logger function (logging.DEBUG) and a message indicating what happened. Nz!No kernel command line url found.)zfile:///zfile 'zC' existed, possibly from local stage download of command line url 'z'. Not re-writing.zE' existed, possibly from previous boot download of command line url ' T)urltimeoutretriesstream sec_betweeng?s #cloud-configr() chunk_sizeFrjz!The kernel command line key `url`z22.3zD Please use `cloud-config-url` kernel command line parameter instead) deprecateddeprecated_version extra_message return_logcloud-config-urlz contents of 'z' did not start with zurl 'z' returned code z . Ignoring.zretrieving url 'z ' failed: i)modezwrote cloud-config data from z='z' to )r get_cmdlinerdr_loggingDEBUG startswithr+rCexistsINFOWARNrread_file_or_urlok isinstance UrlResponsenext iter_contentrO StopIterationcontentsr DEPRECATEDstr deprecatecodeUrlError write_file)rCnetworkr` cmdline_namerj path_is_localmlevelkwargsrbheaderrespsniffed_content is_cloud_cfges r'attempt_cmdline_urlrs&""$D-g6 cNN#34M- AEcK LLE qz AEcK LLEqzRA FF-  !}  #} D F/D**4V4 779L$ 6 67#t))S[)A(O '11&9#(L]]--f5$ 5( NN+N39/H+/    #55#LLE#LLE#C5(=c&k]K :=tyyI .  OOD$U+ d $ g D BCCDV%H   D3BCCDsgI!AI7%'I' A)I768I7/I7I7I$#I$' I40I73I44I77J JJJcdtjjtjjfz}tj j |jjdd}tj j|r\t|j}||k7r=tjd|jdtj ||yytj j|jj#drtj%dtj ||y) zPurge the cache if python version changed on us. There could be changes not represented in our cache (obj.pkl) after we upgrade to a new version of python, so at that point clear the cache z%d.%drbzpython-versionz-Python version change detected. Purging cacheTobj_pklzMWriting python-version file. Cache compatibility status is currently unknown.N)r version_infomajorminorr+rCjoinpaths get_cpathr|openreadr-rP purge_cacher r get_ipath_curr.)initcurrent_python_versionpython_version_pathcached_python_versions r'$purge_cache_on_python_version_changers %  ('',, V$&6 ww~~)* $%8 9 > > @ !$: : IIE F   T " OO/1G H ; 77>>$**229= > HHC  +-CDr(c^tj|jdry|j S)Ndisable_network_activationF)r get_cfg_option_boolr[local)rrFs r'_should_bring_up_interfacesrs& *FGzz>r(cltjtjg}|jrtjg}t t j jdtzd|j g}|js t|}ntd|z}tj||j}|jt|d}d} |jt j"dft%j&t%j(|j*|\}}|j6r)t0j7dt9j:t9j<|j*t?|j*tA|| |D]\} }t0j9| | |jC|jD} tG||jrtjHntjJ} | tjJk(r7d } tLjNjQd tSjTznd } t%jV|j*dd} | rt0j7dd } nH| jYd}t j j[|rt0j7d|d } |j]t_||} |ja| | tjJk(r>|jbjd| k7r%t0j7d| |jbdgfStm||jo}t0j7d| |||jq| tjHk(rts|dd|jk|| tjHk(ri|jbjd| k7r/t0j7d| |jb|jbgfSt0j7d | |jb|ju|jwts|d!d" |jyj{d#|j|t~gt~$\}}|s|j}t|jDjYd&}t j j[|rNt j|jd'k7r,ttt%j|ddd()nt0j7d*t?|j*t|t||j+} |}|}t%j|j*|\}}||k7s||k7r:t0jd,||t%j(|j*|\}}t9j<|j*|jt|jb|j*.|jbt|||fS#t,$rLd}t%j.t0|t3||jt j4|fYwxYw#t,$rt%j.t0d Y(wxYw#tjf$r| tjHk(rt0j7dnt%j.t0d|jhsH|jk|t0j7d| | tjHk(rdgfcYSddgfcYSt0j7d| Y]wxYw#t,$r,t%j.t0d%|jbd%gfcYSwxYw#t,$rt%j.t0d-YwxYw)/Nz%s.dz91_kernel_cmdline_url.cfg)rCrz%s-localds_depsreporterzClosing stdin.#Failed to setup output redirection!@Logging being reset, this logger may no longer be active shortlyr%z0Failed to initialize, likely bad things to come!trustrcheckmanual_cache_cleanFz"manual cache clean set from configmanual_clean_markerz(manual cache clean found from marker: %sexistingz)[%s] Exiting. datasource %s in local modezNo local datasource foundz8No instance datasource found! Likely bad things to come!)bring_upz[%s] Exiting without datasourcezNo instance datasource found.z2[%s] barreling on in force mode without datasourcez5[%s] %s will now be targeting instance id: %s. new=%srr)stage retry_stagez.[%s] Exiting. datasource %s not in local mode.z6[%s] %s is in local mode, will apply init modules now.zinit-netzmodules:config consume_data)rFfreqzConsuming user data failed! cloud_configrT)configstrict log_detailslog_deprecationsz2Skipping user-data validation. No user-data found.rz#Stdout, stderr changing to (%s, %s)z'Failed to re-adjust output redirection!) datasourcer[)LrDEP_FILESYSTEM DEP_NETWORKrrr+rCrrr3rInitrread_cfgrIrBryrzr close_stdin fixup_outputr[ Exceptionlogexcr-r$r~rPr reset_logging setup_loggingr\r6 initializerr DSMODE_LOCALDSMODE_NETWORKr r!r"r debug_inforrr|rrfetchrdsmodeDataSourceNotFoundExceptionforceapply_network_config_maybe_persist_instance_data instancifyis_new_instance_maybe_set_hostnamesetup_datasourceupdatecloudifyrunrrrstatst_sizerrload_text_filerget_output_cfgwarningactivate_datasourcedi_report_warnrW)rErFdeps early_logsw_msgroutfmterrfmtr&lvl path_helperrvrmcfgmfilebring_up_interfacesiidran_resultscloud_cfg_pathrJ outfmt_orig errfmt_origs r' main_initr%s  " "G$7$7 8D zz&&' %'B N  J, ::t$zT23 ;;tdmm  & K6$9 KK=vv N#004@ VVdhh doo488< OO/dDA BBi /3 C#7<<-. /4 M CKLMJ  . . 7'' ' II1 2 KKO zz  % %/B % C II7 >w+++bz!>?@@ IIDd %^ B C67"?!@AAB8 D CBCDsl"AZ [#A"\ A_7A+` A[ [ ##\  \  B!_0_7__2` `  #`32`3cd|vrtjdy|d}|tjdyt|tstj d|y|j d}|tj dyt|t stj d|y|jjdd }|jtjr|ttjd}ntj d ||||vrtjd |||ytjd ||t| y)N di_reportzno di_report found in config.zdi_report was None.z%di_report config not a dictionary: %sdatasource_listz(no 'datasource_list' found in di_report.z(di_report/datasource_list not a list: %s.riz1Datasource '%s' came from unexpected module '%s'.z:used datasource '%s' from '%s' was in di_report's list: %sdsid_missing_source)sourcedslist)r-rPrdictrrYlist __module__ rpartitionr{r DS_PREFIXrOr show_warningr)rr[dicfgrmodnames r'rrs7# 12  E } '( eT " ;UC YY( )F ~ >?  % >G##..s3A6G'++,#g//023 ?   & H      s73v;r(c|j}t|d|}tjg|j}|j t | |jdt|t!|t ||j} tj#dtj$tj&|j(||j"r)tj#d t-j.t-j0|j(t3|j(t5|| t7|||S#tj$r?d|z}tjt|t||js|gcYSYJwxYw#t*$rtjtdYwxYw) N:rrrzGCan not apply stage %s, no datasource found! Likely bad things to come!r Closing stdinrrr%)rvr3rrrrrIrrrr rr-r$rrrrPrrr[rrrrr\r6rW)rQrFrErrr&rJs r' main_modulesr 7s} 99D k48 9E ;;rDMM :DMM+d#$  G $!& 4T*T]] CD@ /"  $((D) zz N  dhh! De dD$ //C  . .   $ %  C#zz7N $ @ C>?@s&E$A F9$A F65F69#GGc|j}t|}tjg|j}|j t | |jdt|t!|t ||j}|j"}|rtj%d||j&}|r+tj%d|t(j+|} tj%d tj,tj.|j0d|j$r)tj%d t5j6t5j8|j0t;|j0t=|| |j?|||\}} | rtjAd |y|stjAd|yy#tj$r7tjtdtd|jsYyYwxYw#t2$rtjtd Y2wxYw)Nrrrz;Failed to fetch your datasource, likely bad things to come!rnrzUsing passed in arguments %szUsing passed in frequency %sr rrr%zRan %s but it failed!zDid not run %s, does it exist?r)!rEr3rrrrrIrrrr rr-r$rrr module_argsrP frequencyFREQ_SHORT_NAMESrYrrr[rrrrr\r6 run_singler) rErFmod_namerrrJmod_argsmod_freqrTrUs r' main_singlernsyyH 4 E ;;rDMM :DMM+d#$  G $!& 4T*T]] CDH 0(;~~H 0(;#''1@ /"  $((D) zz N  dhh! De!OOHhIY +X6  4h?g  . .  N   I zz 6 @ C>?@s&G80A I8AII#I,+I,cl|t}|jd}|tjj d}tjj |d}tjj |d}tjj |d}tjj |d}t j} tj||f|j\} } |dk(r|jrd} n&d} n#|dk(rd|jz} ntd |zd } | | vrtd j| d}| dk(r!||||fD]}tj|n) t!j"tj$|}gddd }| d ii}d|d d<| D] }||d vs |j)|d |<"|d }| |d<t+j*|| d<t-t/d| j0j3|| d<t5j6||tj8tjj;|||d | ||}| dvr|\}}|t=||d<n|}|Dcgc] }t=|c}|| d<t+j*|| d<d|d<t-t/d| j0j3|| d<t5j6||| dk(rg}| D]/}||ds |jE||jGdg1t5j6|d |d|ditj8tjj;|||dtI|| dS#t&$rY7wxYwcc}w#t&$rF}tj>t@d| tCd| zt=|g|| d<Yd}~hd}~wwxYw)Nrbz/run/cloud-initz status.jsonz result.jsonr init-localmodules modules-%szunknown name: %s)rrzmodules-config modules-finalz'Invalid cloud init mode specified '{0}')errorsstartfinishedv1rrrc6t|tjSr@rr LogExporterhs r'z status_wrapper..As7r(recoverable_errorsT)r)rrrzfailed stage %szfailed run of stage %src6t|tjSr@r r"s r'r$z status_wrapper..r%r(r)rr)%rrr+rCnormpathrry getLoggerr ensure_dirsr5rrv ValueErrorr;del_filejsonloadsrrcopytimerfilterhandlers export_logsr write_jsonsym_linkrelpathrrr-r$extendrYrO)rErFdata_dlink_dr status_path status_link result_path result_link root_logger_namefunctorrvmodesstatusf nullstatusrrretrrrs r'status_wrapperrFs ~ ( ~!!"34'',,v}5K'',,v}5K'',,v}5K'',,v}5K##%K   {{UG v~ ::DD  dii'+d233 E  5 5 < |j}tj|j|d\}}}|r$ t j d|j|dyy#tj $r+}tjd||t|Yd}~yd}~wwxYw)zCall set_hostname if metadata, vendordata or userdata provides it. @param stage: String representing current stage in which we are running. @param retry_stage: String represented logs upon error setting hostname. T) metadata_only set_hostnameNzGFailed setting hostname in %s stage. Will retry in %s stage. Error: %s.) rr get_hostname_fqdnr[rhandleSetHostnameErrorr-rPr)rrrcloudhostname_fqdn_rs r'rr8s MMOE11 %tXua   " ">488UD I//  II1A    s"AB1!BBctjjdjt t j dzy)Nr)r stdoutr"rsortedr FEATURES)rErFs r' main_featuresr\Os-JJTYYvg&6&6784?@r(c  tj|stj}t j |j d}|jddddtjzd|jd d d d d |jdd ddd |jd|jdd}d|_ |jdd}|jddd dd |jddd d!d"t jd#$|jdtf%|jd&d'}|jd(d)d*d+d,d-.|jddd d!d"t jd#$|jd&t f%|jd/d0}|jd1d2d*d3d4|jd5d*d6d t#t$j'7|jd8d d9d 4|jd:d;d|jddd d!d"t jd#$|jd/t(f%|jd?d@}|jdAdB}|jdAt*f%|jdCdD}|jdEdF} |jdGdH} |jdIdJ} |jdKdL} |jdMdN} |r t-dO|Dd}|dCk(rddPlm}||n|dEk(rddPlm}|| n|dGk(r%ddQlm}m}|| | jdG|f%n|dIk(r%ddRlm}m}|| | jdI|f%n}|d?k(r%ddSlm}m}|||jdT|f%nS|dMk(r%ddUl m}m!}|| | jdM|f%n)|dKk(r$ddVl"m}m#}|| | jdK|f%|jI|W}|jJ\}}|dXvr@tjL|jNrtPjRntPjTn |jNrtjLtWjXd|k(sd&|k(rd|jZk7rt\}d}d}|dk(r|j^rdY\}} ncdZ\}} n]|d&k(rd[|jZzd\|jZz} }n9|d/k(r+d]|j`zd^|j`z} }|jb}n |}d_|z} d }tejf|| |`|_4|jh5tkjltnjNda|zd|||fb}!dddtqjr!S#1swYxYw)cNr)progz --versionz-vr z %(prog)s z'Show program's version number and exit.)r5r helpz--debugz-d store_truez:Show additional pre-action logging (default: %(default)s).F)r5r_defaultz--forcezDForce running even if no datasource is found (use at your own risk).r)r5r_destrar Subcommands subcommand)titlerbTrz2Initialize cloud-init and perform initial modules.)r_z--localz-lz+Start in local mode (default: %(default)s).z--filez-frBrAz(Use additional yaml configuration files.rb)r5rbr_typer>rz1Activate modules using a given configuration key.z--modez-mstorez8Module configuration name to use (default: %(default)s).r)rrfinal)r5r_rachoicessinglezRun a single module.z--namez-nzModule name to run.)r5r_requiredz --frequencyzModule frequency for this run.)r5r_rlrjz--reportzEnable reporting.r*argumentz0Any additional arguments to pass to this module.)nargsmetavarr_queryz;Query standardized instance metadata from the command line.featureszList defined features.analyzez-Devel tool: Analyze cloud-init logs and data.develzRun development tools.z collect-logsz*Collect and tar all cloud-init debug info.cleanz3Remove logs and artifacts so cloud-init can re-run.rBz/Report cloud-init status or wait on completion.schemaz-Validate cloud-config files using jsonschema.c3DK|]}|jdr|yw)-N)r{).0posargs r' zmain..s J63D3DS3IV Js  ) get_parser)r|handle_collect_logs_args)r|handle_clean_args)r| handle_argsrender)r|handle_schema_args)r|handle_status_args)rF)rr)rzsearching for local datasources)z init-networkz!searching for network datasourcesrzrunning modules for %sz single/%szrunning single module %szrunning 'cloud-init %s')reporting_enabledzcloud-init mode '%s')logfuncr& get_uptimefuncrF):rconfigure_root_loggerr argvargparseArgumentParserpop add_argumentr r< set_defaultsadd_subparsersrl add_parserFileTyperr rrkeysrr\rcloudinit.analyzer|cloudinit.cmd.devel.parsercloudinit.cmd.devel.logsr}cloudinit.cmd.cleanr~cloudinit.cmd.queryrcloudinit.config.schemarcloudinit.cmd.statusr parse_argsr5setup_basic_loggingrPryrzWARNINGrattach_handlersrvrFrrEreportrReportEventStackrr log_timer-r flush_events)" sysv_argsparser subparsers parser_init parser_mod parser_single parser_queryparser_featuresparser_analyze parser_develparser_collect_logs parser_clean parser_status parser_schemardanalyze_parser devel_parser logs_parserr} clean_parserr~ query_parserhandle_query_args schema_parserr status_parserrrFrEr@rname report_onrdescretvals" r'mainrSs HH  $ $)--*: ;F  w5578 6    I   -  &&&]&NJJ''I(K  :    7   t $ VY$78&&K'J  G+    7   t $ I|#<=))-*M  "  -%**,-     ?     7   t $ x&=>(( J)L !++1,O  ](C D**G+N((.)L%//I0((K)L))H*M))F*M J) J    " F > * 7 " M  & > )  + ,  , ,&(@A - 7 "   &  % %g7H-I % J 7 "   &  % %h8I-J % K 8 #  - (  & &x9K.L & M 8 #  - (  & &x9K.L & M   )  ,DkkOT7 && !ZZGMMW__   !""$ ~)t+$))0C  EI v~ ::LLE5LE5   499 $ $tyy 0   $)) # & 2KK )D0 ++ u DM  II&-    M  s .WW__main__TZz:/etc/localtime)r@))rurj)TN)NN)Grr-r+r r0r#rytypingr cloudinitrrrrrr r r r r cloudinit.cmd.develrcloudinit.configrcloudinit.config.modulesrrrrcloudinit.reportingrcloudinit.safeyamlrcloudinit.settingsrrrrr:rMrr)__name__r-r$r0r6r3rIrWr\rdintrrrrrrr rrFrrrr\renvironr return_valueexitrwr(r'rst $ #.,,?&#OO( %   g! 7  W&= KoU38_odE< ]C@/d40nDNx#vEv{{E.AWt z 2::, 4>L r(