
"T`                 @   s  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 e j   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  d e d e d e i Z! e
 j"   Z# d d d  Z$ d d d  Z% d d   Z& d d   Z' d  d!   Z( d" d#   Z) d@ d& d'  Z* d( d d) d*  Z+ d+ d,   Z, d- d.   Z- d/ d0   Z. d1 d2   Z/ d d d3 d4  Z0 d5 d6   Z1 d7 d8   Z2 d9 d:   Z3 d d; d<  Z4 e5 d= k rd> e j6 k rd? e j6 d> <e4 e j7  Z8 e8 re j9 e8  d S)A    N)patcher)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)dhclient_hookzOCloud-init v. {version} running '{action}' at {timestamp}. Up {uptime} seconds.zcloud_%s_modulesinstancealwaysonce c             C   sy   |  r t  j j d |   t  j j d d  t  j j d  t j d t  j  t  j j d d  t  j j d  d  S)Nz%s
-<   
file)sysstderrwrite	traceback	print_exc)msg r#   4/usr/lib/python3/dist-packages/cloudinit/cmd/main.pyr!   B   s    r!   c             C   s9   | s t  |   } t j d | d d d d d t | S)Nz%s
ZconsoleFr   Tr   )welcome_formatr	   Z	multi_logLOG)actionr"   r#   r#   r$   welcomeL   s
    r(   c          	   C   s4   t  j d t j   d t j   d t j   d |   S)Nr
   uptimeZ	timestampr'   )WELCOME_MSG_TPLformatr
   version_stringr	   r)   Ztime_rfc2822)r'   r#   r#   r$   r%   T   s
    	r%   c             C   sC   g  } |  j  r? x- |  j  D]" } | j t j j | j   q W| S)N)filesappendospathrealpathname)argsZfn_cfgsZfhr#   r#   r$   extract_fns\   s
    	 r4   c             C   s   t  | } |  j |  \ } } t |  t |  } | d k rv d | | f } t j j d |  t j |  g  St j d t |  t |   | Sd  S)Nr   z)No '%s' modules to run under section '%s'z%s
zRan %s modules with %s failures)MOD_SECTION_TPLZrun_sectionlenr   r   r   r&   debug)modsaction_nameZsectionZfull_section_name	which_ranfailuresZtotal_attemptedr"   r#   r#   r$   run_module_sectioni   s    
	r<   c             C   s)   |  j  d  r% t j |  j  d   d  S)Nr   )getr   Zupdate_configuration)cfgr#   r#   r$   apply_reporting_cfgy   s    r?   cloud-config-urlurlc             C   sT   t  j |   } x( | D]  } | | k r | | | f Sq Wt d |  | f   d  S)Nz!No keys (%s) found in string '%s')r	   Zkeyval_str_to_dictKeyError)cmdlinenamesdatakeyr#   r#   r$   parse_cmdline_url~   s    rG   Tc             C   s+  | d k r t  j   } y t |  \ } } Wn t k
 rO t j d f SYn X| j d  pk | j d  } | r t j j	 |   r | r d |  | f } t j
 } | r t j } n d |  | f } t j } | | f Sd | d d	 d
 d i } | s | rt j } d | d <n t j } d | d <d }	 d }
 y t j |   } | j   r| j }	 | j j |
  s| d k rt j } n	 t j
 } | d | |
 f f Sn | d | | j f f SWn< t j k
 r} z | d | | f f SWYd d } ~ Xn Xt  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.rA   Ztimeout
   Zretries      Zsec_betweeng?s   #cloud-configzcloud-config-urlz&contents of '%s' did not start with %sz$url '%s' returned code %s. Ignoring.zretrieving url '%s' failed: %smodei  z*wrote cloud-config data from %s='%s' to %s)r	   Zget_cmdlinerG   rB   loggingDEBUG
startswithr/   r0   existsINFOWARNr   Zread_file_or_urlokcontentscodeZUrlErrorZ
write_file)r0   networkrC   Zcmdline_namerA   Zpath_is_localmlevelkwargsrE   headerZresper#   r#   r$   attempt_cmdline_url   sX    		
		
		'r\   c          -   C   s  t  j t  j g } | j r' t  j g } t d t j j d t d  d | j  g } | j sn t	 |   } n t	 d |   } t
 j d | d | j  } | j t |   d  } d  } y? | j t j d f  t j   t j | j |   \ } } WnH t k
 rAd	 } t j t |  t |  | j t j | f  Yn X| j rbt j d
  t j   t j | j  t | j  t |  d | x$ | D] \ }	 } t j  |	 |  qWy | j!   Wn" t k
 rt j t d  Yn X| j" }
 | j rt  j# n t  j$ } | t  j$ k rd } t% j& j' d t( j)    t j d  t j j |
 j* d  d  g } g  } x- | D]% } t j j+ |  rw| j |  qwW| rt j d | |  d  g  f St j d  n d } t j, | j d d  } | rt j d  d } n7 |
 j- d  } t j j. |  rDt j d |  d } | j/   t j0 t j j |
 j* d  d   yU | j1 d |  | t  j$ k r| j2 j3 | k rt j d | | j2  d  g  f SWn t  j4 k
 rt| t  j# k rt j d  n t j t d  | j5 s`| j6 d | j  t j d |  | t  j# k rPd  g  f Sd  d  g f Sn t j d! |  Yn Xt7 |  | j8   } t j d" | |  | | j9    | t  j# k rt: | d# d$ d% d | j6 d t; | t  j# k   | t  j# k rH| j2 j3 | k r2t j d& | | j2  | j2 g  f St j d' | | j2  | j<   | j=   t: | d# d( d% d) yG | j>   j? d* | j@ d+ tA g d, tA \ } } | s| j@ tB  Wn2 t k
 rt j t d-  | j2 d- g f SYn Xt | j  t
 jC | t |  d | j } yq | } | } t jD | j |   \ } } | | k s^| | k rt jE d. | |  t j | j |   \ } } Wn" t k
 rt j t d/  Yn Xt j | j  | jF   tG d0 | j2 d1 | j  | j2 tH | |  |   f S)2Nr0   z%s.dz91_kernel_cmdline_url.cfgrV   z%s-localds_depsreporterzClosing stdin.z#Failed to setup output redirection!z@Logging being reset, this logger may no longer be active shortlyr"   z0Failed to initialize, likely bad things to come!trustz%s
zjChecking to see if files that we need already exist from a previous run that would allow us to stop early.rE   zno-netz"[%s] Exiting. stop file %s existedzQExecution continuing, no previous run detected that would allow us to stop early.ZcheckZmanual_cache_cleanFz"manual cache clean set from configZ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!Z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=%sstagelocal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_datar3   ZfreqzConsuming user data failed!z#Stdout, stderr changing to (%s, %s)z'Failed to re-adjust output redirection!
datasourcer>   )Ir   ZDEP_FILESYSTEMZDEP_NETWORKrb   r\   r/   r0   joinr   r%   r   Initr^   read_cfgr4   r.   rM   rN   r	   close_stdinfixup_outputr>   	Exceptionlogexcr&   r!   rR   r7   resetLoggingsetupLoggingr?   r(   r   Z
initializepathsZDSMODE_LOCALZDSMODE_NETWORKr   r   r   r   Z
debug_infoZ	get_cpathisfileZget_cfg_option_boolZget_ipath_currP   Zpurge_cachedel_filefetchre   ZdsmodeDataSourceNotFoundExceptionforceZapply_network_config_maybe_persist_instance_dataZ
instancifyZis_new_instance_maybe_set_hostnameboolZsetup_datasourceupdatecloudifyZrunrd   r   r   ModulesZget_output_cfgwarningZactivate_datasourcedi_report_warnr<   )r2   r3   ZdepsZ
early_logsw_msginitZoutfmtZerrfmtr"   ZlvlZpath_helperrL   r`   Z
stop_filesZexisting_filesfnZmcfgZmfileZiidZranZ_resultsr8   Zoutfmt_origZerrfmt_origr#   r#   r$   	main_init   s    			

	
	!	

	
%!		
	
			

		!	

r   c             C   sY  d | k r t  j d  d  S| d } | d  k rD t  j d  d  St | t  sg t  j d |  d  S| j d  } | d  k r t  j d  d  St | t  s t  j d |  d  S|  j j d  d	 } | j	 t
 j  r | t t
 j  d   } n t  j d
 |  |  | | k r3t  j d |  | |  d  St j d | d | d t |  d  S)NZ	di_reportzno di_report found in config.zdi_report was None.z%di_report config not a dictionary: %sZdatasource_listz(no 'datasource_list' found in di_report.z(di_report/datasource_list not a list: %s.rJ   z1Datasource '%s' came from unexpected module '%s'.z:used datasource '%s' from '%s' was in di_report's list: %sZdsid_missing_sourcesourcedslist)r&   r7   
isinstancedictr{   r=   list
__module__
rpartitionrO   r   Z	DS_PREFIXr6   r   Zshow_warningstr)re   r>   Zdicfgr   modnamer#   r#   r$   r|     s8    
	
	r|   c             C   s  | j  } t d |  | f  } t j d g  d | j  } | j t |   y | j d d  WnI t j	 k
 r d | } t
 j t |  t |  | j s | g SYn Xt |  t j | t |  d | j } y. t j d  t
 j   t
 j | j |  Wn" t k
 r*t
 j t d  Yn X| j rKt j d	  t j   t j | j  t | j  t | d
 | t | | |  S)Nz%s:%sr]   r^   r`   r_   zGCan not apply stage %s, no datasource found! Likely bad things to come!zClosing stdinz#Failed to setup output redirection!z@Logging being reset, this logger may no longer be active shortlyr"   )rL   r%   r   rg   r^   rh   r4   rr   r   rs   r	   rl   r&   r!   rt   ru   rz   r7   ri   rj   r>   rk   rM   rm   rn   r?   r(   r<   )r9   r3   r2   r}   r~   r"   r8   r#   r#   r$   main_modules  s8    	
	
!
	
r   c       
      C   s  | j  } t |   } t j d g  d | j  } | j t |   y | j d d  Wn< t j	 k
 r t
 j t d  t d  | j s d SYn Xt |  t j | t |  d | j } | j } | r t j d |  | j } | rt j d |  t j |  } y. t j d	  t
 j   t
 j | j d   Wn" t k
 r`t
 j t d
  Yn X| j rt j d  t j   t j | j  t | j  t |  d | | j | | |  \ } }	 |	 rt j  d |  d S| st j  d |  d Sd Sd  S)Nr]   r^   r`   r_   z;Failed to fetch your datasource, likely bad things to come!rK   zUsing passed in arguments %szUsing passed in frequency %szClosing stdinz#Failed to setup output redirection!z@Logging being reset, this logger may no longer be active shortlyr"   zRan %s but it failed!zDid not run %s, does it exist?r   )!r2   r%   r   rg   r^   rh   r4   rr   r   rs   r	   rl   r&   r!   rt   ru   rz   module_argsr7   Z	frequencyFREQ_SHORT_NAMESr=   ri   rj   r>   rk   rM   rm   rn   r?   r(   Z
run_singler{   )
r2   r3   mod_namer}   r~   r8   Zmod_argsZmod_freqr:   r;   r#   r#   r$   main_single  sT    		
		
!		
	
	r   c             C   s  | d  k r t  j j d  } | d  k r< t  j j d  } t  j j | d  } t  j j | d  } t  j j | d  } t  j j | d  } t j | | f  | j \ } }	 |  d k r | j r d }
 qd }
 n, |  d k r d | j }
 n t	 d	 |    d } |
 | k r,t	 d j
 |
    d  } |
 d k rkx[ | | | | f D] } t j |  qQWn1 y t j t j |   } Wn t k
 rYn Xd g  d d  d d  i } | d  k rd i  i } d  | d d <x2 | D]* } | | d k r| j   | d | <qW| d } |
 | d <t j   | |
 d <t j | |  t j t  j j | |  | d d yk |	 |  |  } |
 d k r| \ } } | d  k	 rt |  | d <n | } d d   | D | |
 d <Wn] t k
 r6} z= t j t d |
  t d |
  t |  g | |
 d <WYd  d  } ~ Xn Xt j   | |
 d <d  | d <t j | |  |
 d k rg  } x9 | D]1 } | | d r~| j | | j d g    q~Wt j | d d | d d | i i  t j t  j j | |  | d d t | |
 d  S)Nz/var/lib/cloud/dataz/run/cloud-initzstatus.jsonzresult.jsonr~   
init-localmodulesz
modules-%szunknown name: %smodules-initmodules-configmodules-finalz'Invalid cloud init mode specified '{0}'errorsstartZfinishedv1re   ra   rt   Tc             S   s   g  |  ] } t  |   q Sr#   )r   ).0r[   r#   r#   r$   
<listcomp>  s   	 z"status_wrapper.<locals>.<listcomp>zfailed stage %szfailed run of stage %s)initr   r   r   r   )r   r   )r/   r0   normpathrf   r	   Zensure_dirsr'   rb   rL   
ValueErrorr+   rq   jsonloadsZ	load_filerk   copytimer   Z
write_jsonZsym_linkrelpathr   rl   r&   r!   extendr=   r6   )r2   r3   Zdata_dZlink_dZstatus_pathZstatus_linkZresult_pathZresult_link_namefunctorrL   ZmodesstatusfZ
nullstatusrW   r   Zretre   r   r[   r#   r#   r$   status_wrapperL  s    			 

*
!r   c             C   sJ   |  j  rF t j j |  j j t j  } t j j |  sF |  j	 j
   d S)zCWrite instance-data.json file if absent and datasource is restored.N)Zds_restoredr/   r0   rf   ro   Zrun_dirr   ZINSTANCE_JSON_FILErP   re   Zpersist_instance_data)r~   Zinstance_data_filer#   r#   r$   ru     s
    		ru   c             C   s   |  j    } t j |  j | d d \ } } | r y  t j d |  j | t d  WnD t j k
 r } z! t j d | | t	 |   WYd d } ~ Xn Xd 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.
    Zmetadata_onlyTzset-hostnameNzGFailed setting hostname in %s stage. Will retry in %s stage. Error: %s.)
ry   r	   Zget_hostname_fqdnr>   r   Zhandler&   ZSetHostnameErrorr7   r   )r~   ra   rc   ZcloudZhostnameZ_fqdnr[   r#   r#   r$   rv     s     rv   c             C   s*   t  j j d j t t j   d  d  S)Nr   )r   stdoutr   rf   sortedr
   ZFEATURES)r2   r3   r#   r#   r$   main_features  s    r   c             C   s)  |  s t  j }  t j d |  d  } |  d d   }  | j d d d d d d t j   | j d	 d
 d d d d d d d t j d  | j d d d d d d d d | j d d d d d d d d d | j d d   | j	 d d d d  } d | _
 | j d  d d! } | j d" d# d d d d$ d d | j d d  t f  | j d% d d& } | j d' d( d d) d d* d d+ d, db | j d d% t f  | j d. d d/ } | j d0 d1 d d) d d2 d3 d | j d4 d d) d d5 d3 d d, t t j    | j d6 d d d d7 d3 d | j d8 d9 d: d; d< d d= | j d d. t f  | j d> d d? } | j t j d t j } t j |  | j d@ d dA } | j d d@ t f  | j dB d dC }	 | j dD d dE }
 | j dF d dG } | j dH d dI } | j dJ d dK } |  r|  d dB k rdd dL l m } | |	  nN|  d dD k rd dL l m } | |
  n!|  d dF k rd dM l m } m } | |  | j d dF | f  n |  d dH k r#d dN l m } m } | |  | j d dH | f  n |  d d> k rld dO l m } m } | |  | j d dP | f  nF |  d dJ k rd dQ l m } m  } | |  | j d dJ | f  | j! dR |   } | j" \ } } | j# rt$ j%   t& j'   | dc k rt( } d  } d } | d  k rA| j) r2dd \ } } qde \ } } ns | d% k rkdW | j* dX | j* } } nI | d. k rdY | j+ dZ | j+ } } | j, } n | } d[ | } d } t- j. | | d\ | | _/ | j/ J t0 j1 d] t2 j# d^ d_ | d` d da | dR | | f  } t3 j4   | SWd  QRXd  S)fNprogr   rK   z	--versionz-vr'   r
   z	%(prog)s z--filez-fr.   destr-   helpz*additional yaml configuration files to usetyperbz--debugz-d
store_truez9show additional pre-action logging (default: %(default)s)defaultFz--forcezCforce running even if no datasource is found (use at your own risk)rt   r^   titleZSubcommandsZ
subcommandTr~   z3initializes cloud-init and performs initial modulesz--localz-lz*start in local mode (default: %(default)s)r   z1activates modules using a given configuration keyz--modez-mZstorez7module configuration name to use (default: %(default)s)configchoicesfinalZsinglezrun a single module z--namez-nzmodule name to runrequiredz--frequencyzfrequency of the modulez--reportzenable reportingr   nargs*metavarZargumentz/any additional arguments to pass to this moduleZqueryz;Query standardized instance metadata from the command line.Zfeatureszlist defined featuresZanalyzez,Devel tool: Analyze cloud-init logs and dataZdevelzRun development toolszcollect-logsz)Collect and tar all cloud-init debug infoZcleanz3Remove logs and artifacts so cloud-init can re-run.r   z/Report cloud-init status or wait on completion.)
get_parser)r   handle_collect_logs_args)r   handle_clean_args)r   handle_argsZrender)r   handle_status_argsr3   
init-localsearching for local datasourcesinit-network!searching for network datasourcesz
modules-%szrunning modules for %sz	single/%szrunning single module %szrunning 'cloud-init %s'Zreporting_enabledZlogfuncr"   zcloud-init mode '%s'Z
get_uptimefunc)initzconfigzfinal)zmodulesr   )r   r   )r   r   )5r   argvargparseArgumentParseradd_argumentr
   r,   ZFileTypeZset_defaultsZadd_subparsersr   Z
add_parserr   r   r   r   keysr   r   NAME__doc__r   r   Zcloudinit.analyze.__main__Zcloudinit.cmd.devel.parserZcloudinit.cmd.devel.logsr   Zcloudinit.cmd.cleanr   Zcloudinit.cmd.queryr   Zcloudinit.cmd.statusr   
parse_argsr'   r7   rM   ZsetupBasicLoggingr   Zattach_handlersr   rb   rL   r2   reportr   ZReportEventStackr^   r	   Zlog_timer&   r   Zflush_events)Z	sysv_argsparserZ
subparsersZparser_initZ
parser_modZparser_singleZparser_queryZparser_dhclientZparser_featuresZparser_analyzeZparser_develZparser_collect_logsZparser_cleanZparser_statusZanalyze_parserZdevel_parserZlogs_parserr   Zclean_parserr   Zquery_parserZhandle_query_argsZstatus_parserr   r3   r2   r   ZrnameZ	report_onZrdescZretvalr#   r#   r$   main  s    							
	
	
	
		

	 



	
r   __main__ZTZz:/etc/localtime)r@   zurl):r   r   r/   r   r   r    Z	cloudinitr   Zpatchr   rM   r   r   r   r   r   r	   r
   r   r   Zcloudinit.reportingr   Zcloudinit.settingsr   r   r   r   r   Zcloudinit.configr   r   r*   r5   r   Z	getLoggerr&   r!   r(   r%   r4   r<   r?   rG   r\   r   r|   r   r   r   ru   rv   r   r   __name__environr   Zreturn_valueexitr#   r#   r#   r$   <module>   sf   
"
	N*4Fd	