a 'Dg@sddlZddlZddlZddlZddlZddlZddlZddlmZm Z m Z m Z ddl m Z ddl mZddl mZddl mZddl mZddl mZdd l mZdd l mZdd l mZdd l mZdd l mZddl mZddl mZddl mZddlmZmZddlmZddl m!Z!ddl"m#Z#ddl$m%Z%ddl&m'Z'ddl(m)Z)ddl*m+Z+m,Z,m-Z-m.Z.e/Z0dZ1dZ2e+e,e-dZ3ddddd Z4e5e6Z7d\d"d#Z8d]d$d%Z9d&d'Z:e;d(e7je#d.d/d0Z?d1d2Z@d^d4d5ZAd_e eBe/fd7d8d9ZCd:d;ZDdd?d@ZHeejIe eGe0fdAdBdCZJdDdEZKdFdGZLdHdIZMdJdKZNdLdMZOejPdNdOdPZQdQdRZRdSdTZSd`dUdVZTdWdXZUdYdZZVe6d[kreWeTejXdS)aN)OptionalTupleCallableUnion)netinfo)signal_handler)sources)socket)stages) url_helper)util) performance)version)warnings) reporting) atomic_helper) lifecycle)handlers)log_utilloggers)read_cfg_paths)cc_set_hostname)Modules)validate_cloudconfig_schema)log_with_downgradable_level)events) PER_INSTANCE PER_ALWAYSPER_ONCE CLOUD_CONFIGzOCloud-init v. {version} running '{action}' at {timestamp}. Up {uptime} seconds.zcloud_%s_modules)instancealwaysoncez Local Stagez Network Stagez Config Stagez Final Stage) init-localinitmodules-config modules-finalcCsV|rtjd|tjdtjdtjtjdtjdtjddS)N%s z<------------------------------------------------------------ )file)sysstderrwrite traceback print_excmsgr26/usr/lib/python3.9/site-packages/cloudinit/cmd/main.pyr/Ts   r/cCs&|s t|}tjd|ddtd|S)Nr(FT)Zconsoler,log)welcome_formatrZ multi_logLOG)actionr1r2r2r3welcome^sr8cCstjttt|dS)N)ruptimeZ timestampr7)WELCOME_MSG_TPLformatrversion_stringr r9Z time_rfc2822r7r2r2r3r5es r5 Closing stdin)loggercCsfttjsZ|dttj&}t|tjWdqb1sN0Yn|ddS)zj reopen stdin as /dev/null to ensure no side effects logger: a function for logging messages r>Nz"Not closing stdin, stdin is a tty.)osisattyr+stdinfilenoopendevnulldup2)r?fpr2r2r3 close_stdinns  6rHcCs.g}|jr*|jD]}|tj|jq|SN)filesappendr@pathrealpathname)argsZfn_cfgsZfhr2r2r3 extract_fns}s  rP)modscCsvt|}||\}}t|t|}|dkrXd||f}tjd|t|gStdt|t||SdS)Nrz)No '%s' modules to run under section '%s'r(zRan %s modules with %s failures)MOD_SECTION_TPLZ run_sectionlenr+r,r-r6debug)rQ action_namesectionZfull_section_name which_ranfailuresZtotal_attemptedr1r2r2r3run_module_sections rYcCs|drt|ddS)Nr)getrZupdate_configuration)cfgr2r2r3apply_reporting_cfgs r\cloud-config-urlurlcCs@t|}|D]}||vr|||fSqtd||fdS)Nz!No keys (%s) found in string '%s')r Zkeyval_str_to_dictKeyError)cmdlinenamesdatakeyr2r2r3parse_cmdline_urls  reT)returnc Cs2|durt}zt|\}}Wnty<tjdfYS0|d}|rtj |r|rzd||f}tj }|rtj}nd||f}tj }||fS|dddd }|s|rtj }d |d <ntj}d |d <d} d } zt j fi|} d} | rd} t| t jrHz| t| jt| d7} Wnty4Yn0| | sZd} n| j| sZd} | r|dkrtjdddddWSn2|dkrtj }ntj }|d|dt| fWSn|d|| jffWS| | j} Wn8t jy }z|d||ffWYd}~Sd}~00tj|| ddtj d|||ffS)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:///z_file '%s' existed, possibly from local stage download of command line url '%s'. Not re-writing.zafile '%s' existed, possibly from previous boot download of command line url '%s'. Not re-writing. T)r_timeoutZretriesstreamZ sec_betweeng?s #cloud-config)Z chunk_sizeFr_z!The kernel command line key `url`z22.3zD Please use `cloud-config-url` kernel command line parameter instead deprecateddeprecated_version extra_messageZskip_logr^z contents of 'z' did not start with z$url '%s' returned code %s. Ignoring.zretrieving url '%s' failed: %si)modez*wrote cloud-config data from %s='%s' to %s)r Z get_cmdlinerer`loggingDEBUG startswithr@rLexistsINFOWARNr Zread_file_or_urlok isinstanceZ UrlResponsenextZ iter_contentrS StopIterationcontentsr deprecatestrcodeZUrlError write_file)rLnetworkraZ cmdline_namer_Z path_is_localmlevelkwargsrcheaderZrespZsniffed_contentZ is_cloud_cfger2r2r3attempt_cmdline_urls          &rcCsdtjjtjjf}tj|jdd}tj |rjt |}||krt d|dt ||n*tj |jdrt dt ||dS) 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.%drczpython-versionz-Python version change detected. Purging cacheTZobj_pklzMWriting python-version file. Cache compatibility status is currently unknown.N)r+ version_infomajorminorr@rLjoinpaths get_cpathrvr load_text_filer6rT purge_cacher get_ipath_curinfo)r$Zcurrent_python_versionZpython_version_pathZcached_python_versionr2r2r3$purge_cache_on_python_version_changes$     rcCst|jdrdS|j S)NZdisable_network_activationF)r get_cfg_option_boolr[local)r$rOr2r2r3_should_bring_up_interfaces5sr) raw_configrfc Cs|sdSt|dddkr&dSzt|}Wn:tyn}z"ttdtj d|dWYd}~d Sd}~00d |vr|d D]2}| d pi}| d d }|r| dsdSqdS| drdS| di drdS| drdSdS)aDetermine if our cloud-config requires us to wait User data requires us to wait during cloud-init network phase if: - We have user data that is anything other than cloud-config - This can likely be further optimized in the future to include other user data types - cloud-config contains: - bootcmd - random_seed command - mounts - write_files with source )Fzno configuration foundN ztext/cloud-config)Tz non-cloud-config user data foundz24.4z'Unexpected failure parsing userdata: %s)r?rZrequested_levelr1rO)Tz!failed to parse user data as yamlZ write_filessourceZurir')rgzfile:)Tz!write_files with source uri found)Fz$write_files without source uri foundZbootcmd)Tz bootcmd foundZ random_seedZcommand)Tzrandom_seed command foundZmounts)Tz mounts found)Fz8cloud-config does not contain network requiring elements) rZtype_from_starts_withstripyaml safe_load Exceptionrr6rsWARNINGrZru)rZ parsed_yamlritemZ source_dictZ source_urir2r2r3_should_wait_via_user_data;s@    r) datasourcerfcCs|sdSt|\}}|r*d|dfSt|\}}|rLd|dfSt|\}}|rnd|dfSdd|d|d |fS) zDetermine if we should wait on network connectivity for cloud-init. We need to wait during the cloud-init network phase if: - We have no datasource - We have user data that may require network access )Tzno datasource foundTz in user dataz in vendor dataz in vendor data2Fz user data: z, vendor data: z, vendor data2: )rZget_userdata_rawZget_vendordata_rawZget_vendordata2_raw)rZuser_should_waitZ user_reasonZvendor_should_waitZ vendor_reasonZvendor2_should_waitZvendor2_reasonr2r2r3_should_wait_on_networkps0 rcstjtjg}|jrtjg}ttjdtd|j dg|jrDdnd}t |}t j ||j d}| t|d}d}z@|jstfddt|j|\}}nt|j|\}}Wn:tyd }tt|t|tj|fYn0|jr td t|js*t|jt |jt!||d t"d t#|D]\} }t$| |qLz |%Wn tyttd Yn0|j&} t'||jrtj(ntj)} | tj)krtj*|j&+dstd|j,-d} t.j/0dt12nZd} t3|jdd} | r.tdd} n(| 4d}tj*|rVtd|d} |5t6||}zD|j7| d| tj)kr|j8j9| krtd| |j8dgfWSWntj:y:| tj(krtdn ttd|j;s*|j<|dtd| | tj(krdgfYSddgfYSn td| Yn0t=||>}td | |||?| tj(kr|t@|d!d"d#|j<|d| tj(krtA|j8\}}|rtd$|n td%|tB|j&+dd&|j8j9| krtd'| |j8|j8gfStd(| |j8|C|Dt@|d)d*d#z2|EjFd+|jGtHgtHd,\}}|s\|GtIWn,tyttd-|j8d-gfYS0|j&4d.}tj*|rtJ|jKd/krtLtMNtO|ddd0d1n td2t |jtP|t||j d3}zR|}|}t|j|\}}||ks.||krNtQd4||t|j|\}}Wn typttd5Yn0t|j|RtS|j8|jd6|j8tT|||fS)7Nz%s.dz91_kernel_cmdline_url.cfg)rLrr#r$Zds_depsreportercstj|fSrI)rKrsrtr0Z early_logsr2r3rmzmain_init..#Failed to setup output redirection!@Logging being reset, this logger may no longer be active shortlyr0!PID [%s] started cloud-init '%s'.z0Failed to initialize, likely bad things to come!z .skip-networkz4Will wait for network connectivity before continuingtrustr(checkZmanual_cache_cleanFz"manual cache clean set from configZmanual_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!)Zbring_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_stagezTNetwork connectivity determined necessary for cloud-init's network stage. Reason: %szVNetwork connectivity determined unnecessary for cloud-init's network stage. Reason: %sr'z.[%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)rOZfreqzConsuming user data failed!Z cloud_configrT)configstrictZ log_detailsZlog_deprecationsz2Skipping user-data validation. No user-data found.rz#Stdout, stderr changing to (%s, %s)z'Failed to re-adjust output redirection!)rr[)UrZDEP_FILESYSTEMZ DEP_NETWORKrrr@rLrrr5r Initrread_cfgrPskip_log_setuprHr fixup_outputr[Zget_output_cfgrlogexcr6r/rKrsrxrTr reset_logging setup_loggingr\r8rgetppidr4Z initializerrZ DSMODE_LOCALZDSMODE_NETWORKrv get_runpathZdistroZwait_for_networkr+r,r-rZ debug_inforrrrfetchrZdsmodeDataSourceNotFoundExceptionforceZapply_network_config_maybe_persist_instance_dataZ instancifyZis_new_instance_maybe_set_hostnamerrZsetup_datasourceupdatecloudifyrunrrrstatst_sizerrrrrwarningZactivate_datasourcedi_report_warnrY)rNrOZdepsbootstage_namew_msgr$ZoutfmtZerrfmtr1ZlvlZ path_helperrrrZmcfgZmfileZbring_up_interfacesZiidZ should_waitreasonZranZ_resultsZcloud_cfg_pathrQZ outfmt_origZ errfmt_origr2rr3 main_initsB                                  rcCsd|vrtddS|d}|dur4tddSt|tsNtd|dS|d}|durntddSt|tstd|dS|jdd }| t j r|t t j d}ntd ||||vrtd |||dSt jd ||t|d dS)NZ di_reportzno di_report found in config.zdi_report was None.z%di_report config not a dictionary: %sZdatasource_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: %sZdsid_missing_source)rdslist)r6rTrzdictrrZlist __module__ rpartitionrurZ DS_PREFIXrSrZ show_warningr)rr[Zdicfgrmodnamer2r2r3rsH          rcCs^|j}d||f}t|}tjg|jd}|t|z|jddWn@tj yd|}t t |t ||js|gYSYn0t|t|t||jd}z|jstt |j|Wntyt t dYn0|jrt dt|jst|jt|jt||d t d t||d krRtj d d ddt!|||S)Nz%s:%srrrzGCan not apply stage %s, no datasource found! Likely bad things to come!rrrr0rr$z `--mode init`24.1Use `cloud-init init` instead.)rorprq)"rrr5r rrrrPrrrr rr6r/rrrrrHrr[rrTrrrr\r8rr@rrr~rY)rUrOrNrrr$r1rQr2r2r3 main_modulessR       rc Cs~|j}t|}tjg|jd}|t|z|jddWn4tj ypt t dt d|jslYdSYn0t|t|t||jd}|j}|rt d||j}|rt d|t|}ztt |jdWntyt t d Yn0|jrt d tt|jt|jt||d ||||\}} | r`t d |dS|svt d |dSdSdS)Nrrrz;Failed to fetch your datasource, likely bad things to come!rlrzUsing passed in arguments %szUsing passed in frequency %srrr0zRan %s but it failed!zDid not run %s, does it exist?r)!rNr5r rrrrPrrrr rr6r/rrr module_argsrTZ frequencyFREQ_SHORT_NAMESrZrHrr[rrrrr\r8Z run_singler) rNrOmod_namerr$rQZmod_argsZmod_freqrWrXr2r2r3 main_singlesZ           rc Cst}|d}tj|j}tj|d}tj|d}tj|d}tj|d}t} t ||f|j \} } |dkr|j rd} qd} n |dkrd|j } n td|| tvrtd | giddd } d d| | | | d i}| dkr"||||fD]}t |qn*ztt |}WntyJYn0| |d vrj| |d | <|d }| |d <|| dr|| dstdt| tt || d<ttdd| j}|}t ||t j!tj"|||ddzzX| ||}| dvr6|\}}|dur:t#||d<n|}|| d$dd|DWnty}z:t%d| t&d| || d't#|WYd}~nnd}~0t(y}zL|j)rt%d| t&d| || d'dt#|j)dWYd}~n d}~00Wtt || d<d|d <|}|*|+D]J}||vr|t,t-|||||| d|<n|||| d|<qHt ||ntt || d<d|d <|}|*|+D]J}||vr t,t-|||||| d|<n|||| d|<qt ||0| dkrg}|+D]<}t.||t/rF||0drF|$||0dgqFt |d |d|d it j!tj"|||ddt1|| dS)!Nrcz status.jsonz result.jsonr$r#modules modules-%szunknown name: %sz'Invalid cloud init mode specified '{0}')errorsrecoverable_errorsstartfinishedv1)rr$r#r%r&rrrz=Unexpected start time found for %s. Was this stage restarted?cSs t|tjSrI)rzrZ LogExporter)hr2r2r3rrmz status_wrapper..T)r)r$r#rrcSsg|] }t|qSr2)r).0rr2r2r3 rmz"status_wrapper..zfailed stage %szfailed run of stage %sz sys.exit(z) calledrr&)rr)2rrr@rLnormpathZrun_dirrrs getLoggerr Z ensure_dirsr7rrr ValueError STAGE_NAMEr;copyZdel_filejsonloadsrrr6rfloatr9r{filterrZ export_logsrZ write_jsonZsym_linkrelpathrextend exceptionr/rK SystemExitrZ clean_logskeysrsetrzrrZrS)rNrOrZdata_dZlink_dZ status_pathZ status_link result_pathZ result_linkZ root_logger_namefunctorrrZ nullstatusstatusfrZhandlerZpreexisting_recoverable_errorsretrrrZnew_recoverable_errorsrdrr2r2r3status_wrapperCs             *  8       r)r$cCs6|jr2|jr2|jd}tj|s2|jjdddS)zCWrite instance-data.json file if absent and datasource is restored.Z instance_dataF)Z write_cacheN)rZ ds_restoredrrr@rLrvZpersist_instance_data)r$Zinstance_data_filer2r2r3rs   rc Csz|}tj|j|dd\}}}|rvztd|j|dWn:tjyt}z td||t |WYd}~n d}~00dS)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)Z metadata_onlyZ set_hostnameNzGFailed setting hostname in %s stage. Will retry in %s stage. Error: %s.) rr Zget_hostname_fqdnr[rZhandleZSetHostnameErrorr6rTr)r$rrZcloudhostnameZ_fqdn_rr2r2r3rs rcCs tjdttjddS)Nr))r+stdoutr-rsortedrZFEATURES)rNrOr2r2r3 main_featuressrc Cst|stj}tj|dd}|jddddt dd|jd d d d d d|jdd ddd d|jddd dd d|j dd|j ddd}|j ddd}|jddd d d d|jd!d"d#d$d%t d&d'|j dtfd(|j d)d*d}tjd+d,d-d.d/j}|jd0d1d2d3|d4d5d6|jd!d"d#d$d%t d&d'|j d)tfd(|j d7d8d}|jd9d:d2d;d.d<|jd=d2d>d ttd?|jd@d dAd d<|jdBdCdDdEdF|jd!d"d#d$d%t d&d'|j d7tfd(|j dGdHd}|j dIdJd}|j dItfd(|j dKdLd} |j dMdNd} |j dOdPd} |j dQdRd} |j dSdTd} |j dUdVd}|rtdWdX|Dd}|dKkrddYlm}|| q|dMkrddYlm}|| q|dOkrddZlm}m}|| d[| j dO|fd(n|dQkr$dd\lm}m}|| | j dQ|fd(n|dGkrXdd]l m}m!}|||j d^|fd(nf|dUkrdd_l"m}m#}|||j dU|fd(n2|dSkrdd`l$m}m%}|| | j dS|fd(n |&da|j'|db}t(|dcd |j)st*|St)|S)dNr)progz --versionz-vrz %(prog)s z'Show program's version number and exit.)r7rhelpz--debugz-d store_truez:Show additional pre-action logging (default: %(default)s).F)r7rdefaultz--forcezDForce running even if no datasource is found (use at your own risk).r)r7rdestrz --all-stages all_stageszsRun cloud-init's stages under a single process using a syncronization protocol. This is not intended for CLI usage.)rr7rrrZ Subcommands subcommand)titlerr$z2Initialize cloud-init and perform initial modules.)r--localz-lz+Start in local mode (default: %(default)s).z--filez-frKrJz(Use additional yaml configuration files.rb)r7rrtyper=rz1Activate modules using a given configuration key.z`init`rrTrnz--modez-mstorez9Module configuration name to use (default: %(default)s). r)r$rfinal)r7rrchoicessinglezRun a single module.z--namez-nzModule name to run.)r7rrequiredz --frequencyzModule frequency for this run.)r7rrr z--reportzEnable reporting.r*argumentz0Any additional arguments to pass to this module.)nargsmetavarrqueryz;Query standardized instance metadata from the command line.ZfeatureszList defined features.Zanalyzez-Devel tool: Analyze cloud-init logs and data.ZdevelzRun development tools.z collect-logsz*Collect and tar all cloud-init debug info.Zcleanz3Remove logs and artifacts so cloud-init can re-run.rz/Report cloud-init status or wait on completion.Zschemaz-Validate cloud-config files using jsonschema.css|]}|ds|VqdS)-N)ru)rZposargr2r2r3 rmzmain..) get_parser)rhandle_collect_logs_args)parser)rhandle_clean_args)r handle_argsZrender)rhandle_schema_args)rhandle_status_argsza subcommand is requiredrOr)+rZconfigure_root_loggerr+argvargparseArgumentParserpop add_argumentrr< set_defaultsadd_subparsers add_parserFileTyperrr~messagerrrrrrr{Zcloudinit.analyzerZcloudinit.cmd.devel.parserZcloudinit.cmd.devel.logsrZcloudinit.cmd.cleanrZcloudinit.cmd.queryrcloudinit.config.schemarZcloudinit.cmd.statusrerror parse_argssetattrrsub_main)Z sysv_argsrZ subparsersZ parser_initZ parser_modZ extra_helpZ parser_singleZ parser_queryZparser_featuresZparser_analyzeZ parser_develZparser_collect_logsZ parser_cleanZ parser_statusZ parser_schemarZanalyze_parserZ devel_parserZ logs_parserrZ clean_parserrZ query_parserZhandle_query_argsZ schema_parserrZ status_parserrrOr2r2r3mainsp                      r.cCstdtdddd}td|d0|jddgd }d |_t||_Wd n1sb0Y|d.|jdgd }d |_t||_Wd n1s0Y|d0|jd dgd }d |_t||_Wd n1s0Y|d0|jd dgd }d |_t||_Wd n1s:0Y|j rd}|j rdd|j }td|dtddStdtdd S)z>Run all stages in a single process using an ordering protocol.z*Running cloud-init in single process mode.rrrr zREADY=1r$rrFNTrz --mode=configz --mode=finalz%a stage of cloud-init exited non-zerozfirst exception received: zSTATUS=Completed with failure, z2. Run 'cloud-init status --long' for more details.z STOPPING=1rlzSTATUS=Completed) r6rr Z SocketSyncZ sd_notifyr+rr-Zsystemd_exit_codeZexperienced_any_errorZfirst_exception)rsyncrOr(r2r2r3rs<   ( ( ( *    rc Cs|j\}}|dvr,t|jr"tjntjn|jr:ttd|ks\d|kr`d|j kr`t }d}d}|dkr|j rd\}}qd\}}nV|dkrd|j d|j }}n6|d krd |j d |j }}|j }n|}d |}d }tj|||d|_|jNtd|d|||}Wdn1s.0YWdn1sN0Ytd|kr~d|j kr~t|}|S)N)r$rr$rT)r#zsearching for local datasources)z init-networkz!searching for network datasourcesrzrunning modules for %srz single/%szrunning single module %szrunning 'cloud-init %s'F)Zreporting_enabledzcloud-init stage: '')r7rZsetup_basic_loggingrTrsrtrrZattach_handlersrrrrrNreportrZReportEventStackrr ZTimedrZ flush_eventsrS)rOrNrZrnameZ report_onZrdescZretvalr2r2r3r-.sL   Jr-__main__)r')N)r])TN)N)Yr rr@r+r.rsrtypingrrrrZ cloudinitrrrr r r r r rrrrrrZ cloudinit.logrrZcloudinit.cmd.develrZcloudinit.configrZcloudinit.config.modulesrr)rZcloudinit.lifecyclerZcloudinit.reportingrZcloudinit.settingsrrrrrZReasonr:rRrrr__name__r6r/r8r5ZtimedrTrHrPrYr\reintrrrbytesboolrZ DataSourcerrrrrrrrrrr.rr-exitrr2r2r2r3s                          m 6 %v2AF s;E