o
    w7e                     @   sL  d dl Z d dlZd dlZd dlZd dlZd dlZd dlmZ d dlm	Z	 d dl
mZ e	  d dlmZ d dlmZ d dlmZ d dlmZ d d	lmZ d d
lmZ d dlmZ d dlmZ d dlmZ d dlmZ d dlmZ d dlmZ d dlmZmZmZm Z  d dlm!Z! d dl"m#Z# d dl$m%Z% dZ&dZ'eeedZ(e)e*Z+dGddZ,dHddZ-dd Z.dd  Z/d!efd"d#Z0d$d% Z1dId'd(Z2dJd*ee3e4f fd+d,Z5d-d. Z6d/d0 Z7d1d2 Z8d3d4 Z9d5d6 Z:d7d8 Z;dKd9d:Z<d;ej=fd<d=Z>d>d? Z?d@dA Z@dHdBdCZAe*dDkr"dEejBvrdFejBdE< eAejCZDeDr$eEeD dS dS dS )L    N)Tuple)patcher)Modules)validate_cloudconfig_schema)log)netinfo)signal_handler)sources)stages)
url_helper)util)version)warnings)	reporting)events)PER_INSTANCE
PER_ALWAYSPER_ONCECLOUD_CONFIG)atomic_helper)cc_set_hostname)read_cfg_pathszOCloud-init v. {version} running '{action}' at {timestamp}. Up {uptime} seconds.zcloud_%s_modules)instancealwaysonce c                 C   sV   | r
t jd|   t jd t jd tjt jd t jd t jd d S )N%s
z<------------------------------------------------------------
)file)sysstderrwrite	traceback	print_excmsg r&   4/usr/lib/python3/dist-packages/cloudinit/cmd/main.pyr#   K   s   r#   c                 C   s&   |st | }tjd| ddtd |S )Nr   FT)consoler    r   )welcome_formatr   	multi_logLOG)actionr%   r&   r&   r'   welcomeU   s   r-   c                 C   s   t jt t t | dS )N)r   uptime	timestampr,   )WELCOME_MSG_TPLformatr   version_stringr   r.   time_rfc2822r,   r&   r&   r'   r)   \   s   r)   c                 C   s.   g }| j r| j D ]}|tj|j q|S N)filesappendospathrealpathname)argsfn_cfgsfhr&   r&   r'   extract_fnse   s
   
r?   modsc                 C   sr   t | }| |\}}t|t| }|dkr,d||f }tjd|  t| g S tdt|t| |S )Nr   z)No '%s' modules to run under section '%s'r   zRan %s modules with %s failures)MOD_SECTION_TPLrun_sectionlenr   r    r!   r+   debug)r@   action_namesectionfull_section_name	which_ranfailurestotal_attemptedr%   r&   r&   r'   run_module_sectionr   s   
rK   c                 C   s"   |  drt|  d d S d S )Nr   )getr   update_configuration)cfgr&   r&   r'   apply_reporting_cfg   s   
rO   cloud-config-urlurlc                 C   s<   t | }|D ]}||v r||| f  S qtd| |f )Nz!No keys (%s) found in string '%s')r   keyval_str_to_dictKeyError)cmdlinenamesdatakeyr&   r&   r'   parse_cmdline_url   s   
rY   Treturnc              
   C   s  |du rt  }zt|\}}W n ty   tjdf Y S w |d}|rMtj	| rM|r@d| |f }tj
}|r<tj}||fS d| |f }tj}||fS |dddd	}|sX|r`tj}d
|d< ntj}d|d< d}	d}
zvtjdi |}d}| rd}t|tjrz|t|jt|
d7 }W n	 ty   Y nw ||
sd}n|j|
sd}|r|dkrt jdddd n"|dkrtj}ntj
}|d| dt|
 fW S |d||jf fW S ||j }	W n tjy } z|d||f fW  Y d}~S d}~ww t j| |	dd tj
d||| f fS )a  Write 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.)z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)rR   timeoutretriesstream   sec_betweeng?s   #cloud-config    )
chunk_sizeFrR   z!The kernel command line key `url`z22.3zD Please use `cloud-config-url` kernel command line parameter instead)
deprecateddeprecated_versionextra_messagerQ   zcontents of 'z' did not start with z$url '%s' returned code %s. Ignoring.zretrieving url '%s' failed: %si  )modez*wrote cloud-config data from %s='%s' to %sr&   )r   get_cmdlinerY   rT   loggingDEBUG
startswithr8   r9   existsINFOWARNr   read_file_or_urlok
isinstanceUrlResponsenextiter_contentrC   StopIterationcontents	deprecatestrcodeUrlError
write_file)r9   networkrU   cmdline_namerR   path_is_localmlevelkwargsrW   headerrespsniffed_contentis_cloud_cfger&   r&   r'   attempt_cmdline_url   s   



r   c                 C   s   dt jjt jjf }tj| jdd}tj	|r9t
| }||kr7td | d t|| dS dS tj	| jdrHtd t|| dS )	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.%drW   zpython-versionz-Python version change detected. Purging cacheTobj_pklzMWriting python-version file. Cache compatibility status is currently unknown.N)r   version_infomajorminorr8   r9   joinpaths	get_cpathrm   openreadr+   rD   purge_cacher   r|   get_ipath_curinfo)initcurrent_python_versionpython_version_pathcached_python_versionr&   r&   r'   $purge_cache_on_python_version_change   s&   

r   c                 C   s   t | jdr	dS |j S )Ndisable_network_activationF)r   get_cfg_option_boolrN   local)r   r<   r&   r&   r'   _should_bring_up_interfaces  s   r   c                 C   s  t jt jg}|jrt jg}ttjdt d|j dg}|js%t	| }nt	d|  }t
j||jd}|t| d }d }z|tjdf t  t|j| \}}W n tyr   d}tt| t| |tj|f Y nw |jrtd t  t|j t|j t| |d	 |D ]
\}	}t |	| qz|!  W n ty   ttd
 Y nw |j"}
t#| |jrt j$nt j%}|t j%krd}t&j'(dt)*   n+d}t+|jdd}|rtd d}n|
,d}tj-|rtd| d}|.  t/||}z"|j0|d |t j%kr&|j1j2|kr&td||j1 d g fW S W nF t j3ym   |t j$kr<td nttd |j4se|j5|d td| |t j$kr^d g f Y S d dgf Y S td| Y nw t6| |7 }td|| ||8  |t j$krt9|ddd |j5|d |t j$kr|j1j2|krtd||j1 |j1g fS td ||j1 |:  |;  t9|d!d"d z|< j=d#|j>t?gt?d$\}}|s|>t@ W n ty   ttd% |j1d%gf Y S w tj-|j",d&r
tA|jddd'd( ntd) t|j tB|t||jd*}z)|}|}tC|j| \}}||ks6||krFtDd+|| t|j| \}}W n tyW   ttd, Y nw t|j |E  tF|j1|jd- |j1tG|| | fS ).Nz%s.dz91_kernel_cmdline_url.cfg)r9   r}   z%s-localds_depsreporterzClosing 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!trustr   checkmanual_cache_cleanFz"manual cache clean set from configmanual_clean_markerz(manual cache clean found from marker: %sexistingz)[%s] Exiting. datasource %s in local modezNo local datasource foundz8No instance datasource found! Likely bad things to come!)bring_upz[%s] Exiting without datasourcezNo instance datasource found.z2[%s] barreling on in force mode without datasourcez5[%s] %s will now be targeting instance id: %s. new=%sr   r}   )stageretry_stagez.[%s] Exiting. datasource %s not in local mode.z6[%s] %s is in local mode, will apply init modules now.zinit-netzmodules:configconsume_data)r<   freqzConsuming user data failed!userdata_rawT)configstrictlog_detailslog_deprecationsz2Skipping user-data validation. No user-data found.r   z#Stdout, stderr changing to (%s, %s)z'Failed to re-adjust output redirection!)
datasourcerN   )Hr	   DEP_FILESYSTEMDEP_NETWORKr   r   r8   r9   r   r   r)   r
   Initr   read_cfgr?   r7   rj   rk   r   close_stdinfixup_outputrN   	Exceptionlogexcr+   r#   ro   rD   resetLoggingsetupLoggingrO   r-   r   
initializer   r   DSMODE_LOCALDSMODE_NETWORKr   r    r!   r   
debug_infor   r   rm   r   r   fetchr   dsmodeDataSourceNotFoundExceptionforceapply_network_config_maybe_persist_instance_data
instancifyis_new_instance_maybe_set_hostnamesetup_datasourceupdatecloudifyrunr   r   r   r   r   get_output_cfgwarningactivate_datasourcedi_report_warnrK   )r;   r<   deps
early_logsw_msgr   outfmterrfmtr%   lvlpath_helperrh   r   mcfgmfilebring_up_interfacesiidran_resultsr@   outfmt_origerrfmt_origr&   r&   r'   	main_init"  s0  











r   c                 C   s   d|vrt d d S |d }|d u rt d d S t|ts't d| d S |d}|d u r7t d d S t|tsDt d| d S | jdd	 }|	t
jr\|tt
jd  }nt d
| | ||v rqt d| || d S tjd||t|d d S )N	di_reportzno di_report found in config.zdi_report was None.z%di_report config not a dictionary: %sdatasource_listz(no 'datasource_list' found in di_report.z(di_report/datasource_list not a list: %s.r]   z1Datasource '%s' came from unexpected module '%s'.z:used datasource '%s' from '%s' was in di_report's list: %sdsid_missing_source)sourcedslist)r+   rD   rr   dictr   rL   list
__module__
rpartitionrl   r	   	DS_PREFIXrC   r   show_warningry   )r   rN   dicfgr   modnamer&   r&   r'   r     sH   






r   c                 C   s,  |j }td| |f }tjg |jd}|t| z|jdd W n  tj	yB   d| }t
t| t| |js@|g Y S Y nw t| t|t||jd}ztd t
  t
|j| W n tyr   t
td Y nw |jrtd	 t  t|j t|j t||d
 t|||S )Nz%s:%sr   r   r   zGCan not apply stage %s, no datasource found! Likely bad things to come!r   Closing stdinr   r   r$   )rh   r)   r
   r   r   r   r?   r   r	   r   r   r   r+   r#   r   r   r   rD   r   r   rN   r   rj   r   r   rO   r-   rK   )rE   r<   r;   r   r   r%   r@   r&   r&   r'   main_modules3  sF   



r   c           
      C   s  |j }t| }tjg |jd}|t| z|jdd W n tj	y8   t
td td |js6Y dS Y nw t| t|t||jd}|j}|rRtd| |j}|rbtd| t|}ztd	 t
  t
|jd  W n ty   t
td
 Y nw |jrtd t  t|j t|j t| |d ||||\}}	|	rt d| dS |st d| dS dS )Nr   r   r   z;Failed to fetch your datasource, likely bad things to come!ra   r   zUsing passed in arguments %szUsing passed in frequency %sr   r   r   r$   zRan %s but it failed!zDid not run %s, does it exist?r   )!r;   r)   r
   r   r   r   r?   r   r	   r   r   r   r+   r#   r   r   r   module_argsrD   	frequencyFREQ_SHORT_NAMESrL   r   r   rN   r   rj   r   r   rO   r-   
run_singler   )
r;   r<   mod_namer   r   r@   mod_argsmod_freqrH   rI   r&   r&   r'   main_singlej  sb   	


r   c              
   C   s  |d u rt  }|d}|d u rtjd}tj|d}tj|d}tj|d}tj|d}t||f |j\}	}
| dkrK|j	rHd}nd}n| dkrUd|j
 }ntd	|  d
}||vrhtd|d }|dkr}||||fD ]}t| qtnz
tt|}W n	 ty   Y nw g d d d}|d u rdi i}d |d d< |D ]}||d vr| |d |< q|d }||d< t || d< t|| tjtj|||dd z'|
| |}|dv r|\}}|d urt||d< n|}dd |D || d< W n) ty+ } zttd| td|  t|g|| d< W Y d }~nd }~ww t || d< d |d< t|| |dkrwg }|D ]}|| d r[||| dg  qGt|d|d |di tjtj|||dd t|| d S )NrW   z/run/cloud-initzstatus.jsonzresult.jsonr   
init-localmodules
modules-%szunknown name: %s)r   r   zmodules-initzmodules-configmodules-finalz'Invalid cloud init mode specified '{0}')errorsstartfinishedv1r   r   r  T)r   )r   r   c                 S   s   g | ]}t |qS r&   )ry   ).0r   r&   r&   r'   
<listcomp>  s    z"status_wrapper.<locals>.<listcomp>r   zfailed stage %szfailed run of stage %sr  r   )r   r   )r   r   r8   r9   normpathr   r   ensure_dirsr,   r   rh   
ValueErrorr1   del_filejsonloads	load_filer   copytimer   
write_jsonsym_linkrelpathry   r   r+   r#   extendrL   rC   )r;   r<   data_dlink_dr   status_pathstatus_linkresult_pathresult_link_namefunctorrh   modesstatusf
nullstatusr   r  retr   r   r   r&   r&   r'   status_wrapper  s   



r   r   c                 C   sB   | j r| jr| jd}tj|s| j jdd dS dS dS dS )zCWrite instance-data.json file if absent and datasource is restored.instance_dataF)write_cacheN)r   ds_restoredr   get_runpathr8   r9   rm   persist_instance_data)r   instance_data_filer&   r&   r'   r   $  s   r   c              
   C   s~   |   }tj| j|dd\}}}|r=ztd| j|d W dS  tjy< } ztd||t	| W Y d}~dS d}~ww dS )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.)
r   r   get_hostname_fqdnrN   r   handleSetHostnameErrorr+   rD   ry   )r   r   r   cloudhostname_fqdn_r   r&   r&   r'   r   ,  s"   r   c                 C   s    t jdttjd  d S )Nr   )r   stdoutr!   r   sortedr   FEATURES)r;   r<   r&   r&   r'   main_featuresC  s    r3  c           "   	   C   sl  | st j} tj| dd}|jddddt  dd |jd	d
dddtdd |jdddddd |jdddddd |j	d d |j
ddd}d|_|jdd d!}|jd"d#dd$dd |j	dtfd% |jd&d'd!}|jd(d)d*d+d,d-d. |j	d&tfd% |jd/d0d!}|jd1d2d*d3dd4 |jd5d*d6dtt d7 |jd8dd9dd4 |jd:d;d<d=d> |j	d/tfd% |jd?d@d!}|jdAdBd!}|j	dAtfd% |jdCdDd!}|jdEdFd!}	|jdGdHd!}
|jdIdJd!}|jdKdLd!}|jdMdNd!}| r| d }|dCkrddOlm} || n|dEkr!ddOlm} ||	 n|dGkr;ddPlm}m} ||
 |
j	dG|fd% ng|dIkrUddQlm}m} || |j	dI|fd% nM|d?kroddRlm}m} || |j	dS|fd% n3|dMkrddTlm}m} || |j	dM|fd% n|dKkrddUlm}m } || |j	dK|fd% |j!| dV}|j"\}}|j#rt$%  t&'  |dWv rt(}d }d}|dkr|j)rdX\}} n2dY\}} n-|d&krdZ|j* d[|j* }} n|d/krd\|j+ d]|j+ }} |j,}n|}d^| } d}t-j.|| |d_|_/|j/ t0j1t2j#d`| d|||fda}!W d    n	1 s+w   Y  t34  |!S )bNr   )progz	--versionz-vr   z	%(prog)s z'Show program's version number and exit.)r,   r   helpz--filez-fr7   r6   z(Use additional yaml configuration files.rb)r,   destr5  typez--debugz-d
store_truez:Show additional pre-action logging (default: %(default)s).F)r,   r5  defaultz--forcezDForce running even if no datasource is found (use at your own risk).r   )r,   r5  r7  r:  r   Subcommands
subcommand)titler7  Tr   z2Initialize cloud-init and perform initial modules.)r5  z--localz-lz+Start in local mode (default: %(default)s).r4   r   z1Activate modules using a given configuration key.z--modez-mstorez8Module configuration name to use (default: %(default)s).r   )r   r   final)r,   r5  r:  choicessinglezRun a single module.z--namez-nzModule name to run.)r,   r5  requiredz--frequencyzModule frequency for this run.)r,   r5  rB  r@  z--reportzEnable reporting.r   *argumentz0Any additional arguments to pass to this module.)nargsmetavarr5  queryz;Query standardized instance metadata from the command line.featureszList defined features.analyzez-Devel tool: Analyze cloud-init logs and data.develzRun development tools.zcollect-logsz*Collect and tar all cloud-init debug info.cleanz3Remove logs and artifacts so cloud-init can re-run.r  z/Report cloud-init status or wait on completion.schemaz-Validate cloud-config files using jsonschema.)
get_parser)rM  handle_collect_logs_args)rM  handle_clean_args)rM  handle_argsrender)rM  handle_schema_args)rM  handle_status_args)r<   )r   r   )r   zsearching for local datasources)zinit-networkz!searching for network datasourcesr   zrunning modules for %sz	single/%szrunning single module %szrunning 'cloud-init %s')reporting_enabledzcloud-init mode '%s')logfuncr%   
get_uptimefuncr<   )5r   argvargparseArgumentParserpopadd_argumentr   r2   FileTypeset_defaultsadd_subparsersrB  
add_parserr   r   r   r   keysr   r3  cloudinit.analyze.__main__rM  cloudinit.cmd.devel.parsercloudinit.cmd.devel.logsrN  cloudinit.cmd.cleanrO  cloudinit.cmd.queryrP  cloudinit.config.schemarR  cloudinit.cmd.statusrS  
parse_argsr,   rD   rj   setupBasicLoggingr   attach_handlersr   r   rh   r;   reportr   ReportEventStackr   r   log_timer+   r   flush_events)"	sysv_argsparser
subparsersparser_init
parser_modparser_singleparser_queryparser_featuresparser_analyzeparser_develparser_collect_logsparser_cleanparser_statusparser_schemar<  analyze_parserdevel_parserlogs_parserrN  clean_parserrO  query_parserhandle_query_argsschema_parserrR  status_parserrS  r<   r;   r  rname	report_onrdescretvalr&   r&   r'   mainG  sn  
	
















r  __main__TZz:/etc/localtime)r   r5   )rP   )TN)NN)FrY  r
  r8   r   r  r"   typingr   	cloudinitr   cloudinit.config.modulesr   patch_loggingrg  r   r   rj   r   r   r	   r
   r   r   r   r   r   cloudinit.reportingr   cloudinit.settingsr   r   r   r   r   cloudinit.configr   cloudinit.cmd.develr   r0   rA   r   	getLogger__name__r+   r#   r-   r)   r?   rK   rO   rY   intry   r   r   r   r   r   r   r   r   r   r   r   r3  r  environrX  return_valueexitr&   r&   r&   r'   <module>   s~   



	
l `27
Gs
 
}

