3 \me@s`ddlZddlZddlZddlZddlZddlZddlZddlmZddl m Z ddl m Z ddl m Z ddl m Z ddl mZddl mZdd l mZdd l mZdd l mZdd l mZdd lmZddlmZddlmZddlmZddlmZmZmZm Z m!Z!m"Z"ddl#m$Z$ddl%m&Z&ddl'm(Z(m)Z)m*Z*m+Z+dZ,dZ-e(e)e*dZ.ej/e0Z1dJddZ2ddZ3dKddZ4dd Z5d!d"Z6ed#d$d%Z7d&d'Z8dMd*d+Z9dNee:e;fd-d.d/Zd4d5Z?d6d7Z@d8d9ZAd:d;ZBdOdd?d@ZEdAdBZFdCdDZGdPdEdFZHe0dGkr\dHejIkrBdIejIdH<eHejJZKeKr\ejLeKdS)QN)Tuple)netinfo)signal_handler)sources)stages) url_helper)util)version)warnings) reporting) atomic_helper)read_cfg_paths)cc_set_hostname)Modules)validate_cloudconfig_schema) LogExportersetup_basic_logging setup_logging reset_loggingconfigure_root_logger DEPRECATED)events)load) PER_INSTANCE PER_ALWAYSPER_ONCE CLOUD_CONFIGzOCloud-init v. {version} running '{action}' at {timestamp}. Up {uptime} seconds.zcloud_%s_modules)instancealwaysoncecCs^|rtjjd|tjjddtjjdtjtjdtjjddtjjddS)Nz%s -< )file)sysstderrwrite traceback print_exc)msgr+/usr/lib/python3.6/main.pyr)Hs r)cCs tjrtj}tjd|dS)NzPID [%s] started cloud-init.)rZis_LinuxosgetppidLOGinfo)Zppidr+r+r,log_ppidRsr1cCs,|s t|}tjd|ddtdt|S)Nz%s FT)Zconsoler&log)welcome_formatrZ multi_logr/r1)actionr*r+r+r,welcomeXs r5cCstjtjtjtj|dS)N)r uptimeZ timestampr4)WELCOME_MSG_TPLformatr version_stringrr6Z time_rfc2822)r4r+r+r,r3`s r3cCs2g}|jr.x"|jD]}|jtjj|jqW|S)N)filesappendr-pathrealpathname)argsZfn_cfgsZfhr+r+r, extract_fnsis  r@)modscCsvt|}|j|\}}t|t|}|dkrXd||f}tjjd|tj|gStjdt|t||SdS)Nrz)No '%s' modules to run under section '%s'z%s zRan %s modules with %s failures)MOD_SECTION_TPLZ run_sectionlenr%r&r'r/debug)rA action_nameZsectionZfull_section_name which_ranfailuresZtotal_attemptedr*r+r+r,run_module_sectionvs rHcCs|jdrtj|jddS)Nr )getr Zupdate_configuration)cfgr+r+r,apply_reporting_cfgs rKcloud-config-urlurlcCs@tj|}x |D]}||kr|||fSqWtd||fdS)Nz!No keys (%s) found in string '%s')rZkeyval_str_to_dictKeyError)cmdlinenamesdatakeyr+r+r,parse_cmdline_urls   rST)returncCs(|dkrtj}yt|\}}Wntk r:tjdfSX|jd}|rtjj |r|rxd||f}tj }|rtj}nd||f}tj }||fS|ddd d }|s|rtj }d |d <ntj}d |d <d} d} yt j f|} d} | jrd } t| t jrDy| t| jt| d7} Wntk r0YnX| j| sVd} n| jj| sVd} | r|dkrtttjdddd dfSn0|dkrtj }ntj }|d|dt| fSn|d|| jffS| | j} Wn0t jk r}z|d||ffSd}~XnXtj|| 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.file:///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)rMZtimeoutZretriesstreamZ sec_betweeng?s #cloud-config)Z chunk_sizeFrMz!The kernel command line key `url`z22.3zD Please use `cloud-config-url` kernel command line parameter instead)Z deprecatedZdeprecated_versionZ extra_messageZ return_logzcloud-config-urlz 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)rUrV)rZ get_cmdlinerSrNloggingDEBUG startswithr-r<existsINFOWARNrZread_file_or_urlok isinstanceZ UrlResponsenextZ iter_contentrC StopIterationcontentsrstrZ deprecatecodeZUrlError write_file)r<networkrOZ cmdline_namerMZ path_is_localmlevelkwargsrQheaderZrespZsniffed_contentZ is_cloud_cfger+r+r,attempt_cmdline_urls          rqcCsdtjjtjjf}tjj|jjdd}tjj |rlt |j }||krt j d|jdtj||n*tjj |jjdrt jdtj||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.%drQzpython-versionz-Python version change detected. Purging cacheTZobj_pklzMWriting python-version file. Cache compatibility status is currently unknown.N)r% version_infomajorminorr-r<joinpaths get_cpathr`openreadr/rD purge_cacherrj get_ipath_curr0)initZcurrent_python_versionZpython_version_pathZcached_python_versionr+r+r,$purge_cache_on_python_version_changes     r}cCstj|jdrdS|j S)NZdisable_network_activationF)rget_cfg_option_boolrJlocal)r|r?r+r+r,_should_bring_up_interfaces&src-Cstjtjg}|jrtjg}ttjjdtd|j dg}|jsJt |}n t d|}t j ||j d}|j t|d}d}y.|jtjdftjtj|j|\}}Wn<tk rd}tjt|t||jtj|fYnX|jrtjdtt|jt|jt||d x|D]\} }tj | |q&Wy |j!Wn"tk rntjtd YnX|j"} t#||jrtj$ntj%} | tj%krd } t&j'j(d t)j*nZd } tj+|jdd} | rtjdd } n(| j,d}tjj-|r tjd|d } |j.t/||}yB|j0| d| tj%kr\|j1j2| kr\tjd| |j1dgfSWntj3k r| tj$krtjdn tjtd|j4s|j5|dtjd| | tj$krdgfSddgfSn tjd| YnXt6||j7}tjd| |||j8| tj$kr(t9|ddd|j5|d| tj$krx|j1j2| krhtjd| |j1|j1gfStjd | |j1|j:|j;t9|d!d"dy2|j<j=d#|j>t?gt?d$\}}|s|j>t@Wn*tk rtjtd%|j1d%gfSX|j"j,d&}tjj-|rr?ZdepsZ early_logsw_msgr|ZoutfmtZerrfmtr*ZlvlZ path_helperr\rZmcfgZmfileZbring_up_interfacesZiidZranZ_resultsZcloud_cfg_pathrAZ outfmt_origZ errfmt_origr+r+r, main_init,s                                     rcCsd|krtjddS|d}|dkr4tjddSt|tsNtjd|dS|jd}|dkrntjddSt|tstjd|dS|jjdd }|j t j r|t t j d}ntjd ||||krtjd |||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.rXz1Datasource '%s' came from unexpected module '%s'.z:used datasource '%s' from '%s' was in di_report's list: %sZdsid_missing_source)sourcedslist)r/rDrddictrrIlist __module__ rpartitionr_rZ DS_PREFIXrCr Z show_warningrh)rrJZdicfgrmodnamer+r+r,r sB         rcCs(|j}td||f}tjg|jd}|jt|y|jddWn>tj k rd|}t j t |t ||js~|gSYnXt|t|t||jd}y$t jdt jt j|j|Wn tk rt j t dYnX|jrt jd tt|jt|jt||d t|||S) Nz%s:%s)rrr)rzGCan not apply stage %s, no datasource found! Likely bad things to come!)rz Closing stdinz#Failed to setup output redirection!z@Logging being reset, this logger may no longer be active shortly)r*)r\r3rrrrr@rrrrrr/r)rrrrDrrrJrrrrKr5rH)rEr?r>rr|r*rAr+r+r, main_modules>s:       rc Cs|j}t|}tjg|jd}|jt|y|jddWn4tj k rpt j t dt d|jsldSYnXt|t|t||jd}|j}|rt jd||j}|rt jd|tj|}y$t jd t jt j|jdWn"tk r t j t d YnX|jr$t jd tt|jt|jt||d |j|||\}} | rlt jd |dS|st jd|dSdSdS)N)rrr)rz;Failed to fetch your datasource, likely bad things to come!rZ)rzUsing passed in arguments %szUsing passed in frequency %sz Closing stdinz#Failed to setup output redirection!z@Logging being reset, this logger may no longer be active shortly)r*zRan %s but it failed!zDid not run %s, does it exist?r) r>r3rrrrr@rrrrrr/r)rrr module_argsrDZ frequencyFREQ_SHORT_NAMESrIrrrJrrrrKr5Z run_singler) r>r?Zmod_namerr|rAZmod_argsZmod_freqrFrGr+r+r, main_singleusV           rcCs`|dkrt}|jd}|dkr,tjjd}tjj|d}tjj|d}tjj|d}tjj|d}tj} tj ||f|j \} } |dkr|j rd} qd} n |dkrd|j } n t d |d} | | krt d j| d}| dkrxL||||fD]}tj|qWn,ytjtj|}Wntk r2YnXgddd }|dkr^dii}d|dd<x,| D]$}||dkrd|j|d|<qdW|d}| |d<tj|| d<ttdd| jj|| d<tj||tjtjj|||ddyR| ||}| d kr(|\}}|dk r,t||d<n|}dd|D|| d<WnPtk r}z2tjt d| t!d| t|g|| d<WYdd}~XnXtj|| d<d|d<ttdd| jj|| d<tj||| d krPg}x2| D]*}||dr|j"||j#dgqWtj|d|d|ditjtjj|||ddt$|| dS)!NrQz/run/cloud-initz status.jsonz result.jsonr| init-localmodulesz modules-%szunknown name: %smodules-config modules-finalz'Invalid cloud init mode specified '{0}')errorsstartfinishedv1rrrcSs t|tS)N)rdr)hr+r+r,sz status_wrapper..Zrecoverable_errorsT)rcSsg|] }t|qSr+)rh).0rpr+r+r, sz"status_wrapper..rzfailed stage %szfailed run of stage %srcSs t|tS)N)rdr)rr+r+r,r!s)rr)r|rrr)r|r)%r rwr-r<normpathrur] getLoggerrZ ensure_dirsr4rr\ ValueErrorr8Zdel_filejsonloadsrrcopytimerefilterZhandlersZ export_logsr Z write_jsonZsym_linkrelpathrhrr/r)extendrIrC)r>r?Zdata_dZlink_drvZ status_pathZ status_linkZ result_pathZ result_linkZ root_logger_namefunctorr\ZmodesstatusfZ nullstatusrlrretrrrpr+r+r,status_wrappers               $   r)r|cCs6|jr2|jr2|jjd}tjj|s2|jjdddS)zCWrite instance-data.json file if absent and datasource is restored.Z instance_dataF)Z write_cacheN)rZ ds_restoredrvZ get_runpathr-r<r`Zpersist_instance_data)r|Zinstance_data_filer+r+r,r7s   rcCsx|j}tj|j|dd\}}}|rtytjd|j|dWn8tjk rr}ztjd||t |WYdd}~XnXdS)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.) rrZget_hostname_fqdnrJrZhandleZSetHostnameErrorr/rDrh)r|rrZcloudZhostnameZ_fqdn_rpr+r+r,r?srcCs tjjdjttjddS)Nr#)r%stdoutr'rusortedr ZFEATURES)r>r?r+r+r, main_featuresVsrc"Cst|stj}tj|jdd}|jddddtjdd|jd d d d d tj dd|jdddddd|jdddddd|j dd|j ddd}d|_ |j dd d!}|jd"d#dd$dd|j dtfd%|j d&d'd!}|jd(d)d*d+d,dcd.|j d&tfd%|j d/d0d!}|jd1d2d*d3dd4|jd5d*d6dttjd7|jd8dd9dd4|jd:d;d|j d/tfd%|j d?d@d!}|j dAdBd!}|j dAtfd%|j dCdDd!}|j dEdFd!} |j dGdHd!} |j dIdJd!} |j dKdLd!} |j dMdNd!} |rN|d}|dCkr*ddOlm}||n$|dEkrLddOlm}|| n|dGkrddPlm}m}|| | j dG|fd%n|dIkrddQlm}m}|| | j dI|fd%n|d?krddRlm}m}|||j dS|fd%nf|dMkrddTlm}m}|| | j dM|fd%n2|dKkrNddUl m}m!}|| | j dK|fd%|j"|dV}|j#\}}|ddkrt$|j%r~t&j'nt&j(n|j%rt$t)j*d|ksd&|krd|j+krt,}d}d}|dkr|j-rde\}} ndf\}} nZ|d&krd[|j+d\|j+}} n8|d/kr:d]|j.d^|j.}} |j/}n|}d_|} d}t0j1|| |d`|_2|j2$t3j4t5j%da|d|||fdb}!WdQRXt6j7|!S)gNr)progz --versionz-vr z %(prog)s z'Show program's version number and exit.)r4r helpz--filez-fr;r:z(Use additional yaml configuration files.rb)r4destrtypez--debugz-d store_truez:Show additional pre-action logging (default: %(default)s).F)r4rdefaultz--forcezDForce running even if no datasource is found (use at your own risk).r)r4rrr)rZ Subcommands subcommand)titlerTr|z2Initialize cloud-init and perform initial modules.)rz--localz-lz+Start in local mode (default: %(default)s).)r4rz1Activate modules using a given configuration key.z--modez-mZstorez8Module configuration name to use (default: %(default)s).rfinal)r4rrchoicesZsinglezRun a single module.z--namez-nzModule name to run.)r4rrequiredz --frequencyzModule frequency for this run.)r4rrrz--reportzEnable reporting.r*Zargumentz0Any additional arguments to pass to this module.)nargsmetavarrZqueryz;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.) get_parser)rhandle_collect_logs_args)rhandle_clean_args)r handle_argsZrender)rhandle_schema_args)rhandle_status_args)r? init-localsearching for local datasources init-network!searching for network datasourcesz modules-%szrunning modules for %sz single/%szrunning single module %szrunning 'cloud-init %s')Zreporting_enabledzcloud-init mode '%s')Zlogfuncr*Z get_uptimefuncr?)r|rr)r|r)rr)rr)8rr%argvargparseArgumentParserpop add_argumentr r9ZFileTypeZ set_defaultsZadd_subparsersrZ add_parserrrrrkeysrrZcloudinit.analyzerZcloudinit.cmd.devel.parserZcloudinit.cmd.devel.logsrZcloudinit.cmd.cleanrZcloudinit.cmd.queryrcloudinit.config.schemarZcloudinit.cmd.statusr parse_argsr4rrDr]r^ZWARNINGrZattach_handlersr\rrr>ZreportrZReportEventStackrrZlog_timer/r Z flush_events)"Z sysv_argsparserZ subparsersZ parser_initZ parser_modZ 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_parserrr?r>rZrnameZ report_onZrdescZretvalr+r+r,mainZs@                                r__main__ZTZz:/etc/localtime)r )NrLrM)r)TN)NN)N)Mrrr-r%rr(r]ZtypingrZ cloudinitrrrrrrr r r r Zcloudinit.cmd.develr Zcloudinit.configrZcloudinit.config.modulesrrrZ cloudinit.logrrrrrrZcloudinit.reportingrZcloudinit.safeyamlrZcloudinit.settingsrrrrr7rBrr__name__r/r)r1r5r3r@rHrKrSintrhrqr}rrrrrrrrrrrenvironrZ return_valueexitr+r+r+r,sx                        ra27G {