
    x[h                     z   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Zd dlmZm	Z	m
Z
mZ d dlmZ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 d dlmZ d dlmZ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/ e0Z1dZ2dZ3e,e-e.dZ4ddddd Z5 ejl                  e7      Z8dAd!Z9dBd"Z:d# Z; ejx                  d$      e8jz                  fd%e
e0gdf   fd&       Z>d' Z?d(e$fd)Z@d* ZAdCd+ZBdDd,e	eCe0f   fd-ZDd. ZEd/ ZFd0eee0eGf      d,e	eHe1f   fd1ZId2eej                     d,e	eHe1f   fd3ZKd4 ZLd5 ZMd6 ZNd7 ZOd8 ZPd9ej                  fd:ZRd; ZSd< ZTdBd=ZUd> ZVd? ZWe7d@k(  r# ej                   eUej                               yy)E    N)OptionalTupleCallableUnion)featuresnetinfo)signal_handler)sources)socket)stages)
url_helper)util)performance)version)warnings)	reporting)atomic_helper)	lifecycle)handlers)log_utilloggers)read_cfg_paths)cc_set_hostname)Modules)validate_cloudconfig_schema)log_with_downgradable_level)events)PER_INSTANCE
PER_ALWAYSPER_ONCECLOUD_CONFIGzOCloud-init v. {version} running '{action}' at {timestamp}. Up {uptime} seconds.zcloud_%s_modules)instancealwaysoncezLocal StagezNetwork StagezConfig StagezFinal Stage)
init-localinitmodules-configmodules-finalc                    | r"t         j                  j                  d| z         t         j                  j                  d       t         j                  j                  d       t        j                  t         j                         t         j                  j                  d       t         j                  j                  d       y )N%s
z<------------------------------------------------------------
)file)sysstderrwrite	traceback	print_excmsgs    4/usr/lib/python3/dist-packages/cloudinit/cmd/main.pyr1   r1   T   su    


3(JJXJJTSZZ(JJXJJT    c                 `    |st        |       }t        j                  d|z  ddt               |S )Nr*   FT)consoler.   log)welcome_formatr   	multi_logLOG)actionr3   s     r4   welcomer=   ^   s,    V$v~uTsKJr5   c                     t         j                  t        j                         t	        j
                         t	        j                         |       S )N)r   uptime	timestampr<   )WELCOME_MSG_TPLformatr   version_stringr   r?   time_rfc2822r<   s    r4   r9   r9   e   s>    !!&&({{}##%	 "  r5   Closing stdinloggerc                 d   t        j                  t        j                  j	                               sk | d       t        t         j                        5 }t        j                  |j	                         t        j                  j	                                ddd       y | d       y# 1 sw Y   yxY w)zj
    reopen stdin as /dev/null to ensure no side effects

    logger: a function for logging messages
    rF   Nz"Not closing stdin, stdin is a tty.)osisattyr-   stdinfilenoopendevnulldup2)rG   fps     r4   close_stdinrQ   n   sy     99SYY%%'("** 	5GGBIIK!1!1!34	5 	5 	34	5 	5s   AB&&B/c                     g }| j                   rI| j                   D ]:  }|j                  t        j                  j	                  |j
                               < |S N)filesappendrI   pathrealpathname)argsfn_cfgsfhs      r4   extract_fnsr\   }   sK     Gzz** 	6B NN277++BGG45	6 Nr5   modsc                 N   t         |z  }| j                  |      \  }}t        |      t        |      z   }|dk(  rBd|d|d}t        j                  j                  d|z         t        j                  |       g S t        j                  dt        |      t        |             |S )Nr   zNo 'z ' modules to run under section ''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_attemptedr3   s           r4   run_module_sectionrj      s    '73 ,,->?Y)ns8}4O!
 	

3(		#			-s9~s8}	
 r5   c                 p    | j                  d      r%t        j                  | j                  d             y y )Nr   )getr   update_configuration)cfgs    r4   apply_reporting_cfgro      s*    
ww{&&sww{';< r5   c                 x    t        j                  |       }|D ]  }||v s|||   fc S  t        d| d|d      )Nz	No keys (z) found in string 'r_   )r   keyval_str_to_dictKeyError)cmdlinenamesdatakeys       r4   parse_cmdline_urlrw      sH    ""7+D "$;S	>!" '5I
JJr5   returnc                    |t        j                         }	 t        |      \  }}|j                  d      }|rmt        j                  j                  |       rN|r/d| d|d}t        j                  }|rt        j
                  }||fS d| d|d}t        j                  }||fS |dd	d
d}|s|rt        j                  }d|d<   nt        j
                  }d|d<   d}	d}
	 t        j                  d%i |}d}|j                         rd
}t        |t        j                         r<	 |t#        |j%                  t'        |
                  z  }|j                  |
      s d}n|j*                  j                  |
      sd}|r|dk(  rgt-        j.                  dddd
      S |dk(  rt        j                  }nt        j                  }|d| dt1        |
       fS |d|d|j2                  dfS ||j*                  z   }	t        j6                  | |	d !       t        j                  d"|d#|d$| fS # t        $ r t        j
                  dfcY S w xY w# t(        $ r Y w xY w# t        j4                  $ r}|d|d|fcY d}~S d}~ww xY w)&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 '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)urltimeoutretriesstream   sec_betweeng?s   #cloud-configr5   )
chunk_sizeFr}   z!The kernel command line key `url`z22.3zD Please use `cloud-config-url` kernel command line parameter instead
deprecateddeprecated_versionextra_messageskip_logcloud-config-urlzcontents of 'z' did not start with zurl 'z' returned code z. Ignoring.zretrieving url 'z
' failed: i  )modezwrote cloud-config data from z='z' to  )r   get_cmdlinerw   rr   loggingDEBUG
startswithrI   rV   existsINFOWARNr   read_file_or_urlok
isinstanceUrlResponsenextiter_contentrb   StopIterationcontentsr   	deprecatestrcodeUrlError
write_file)rV   networkrs   cmdline_namer}   path_is_localmlevelkwargsru   headerrespsniffed_contentis_cloud_cfges                  r4   attempt_cmdline_urlr      s   & ""$D-g6c NN#34M- AEcK  LLE qz  AEcK  LLEqzRAFF- !} #}DF*D**4V4779L$
 6 67#t))S[)A( O
 '11&9#(L]]--f5$5($..#F+1'@!%   #55#LLE#LLE#C5(=c&k]K  :=tyyI  .
 	OOD$U+d	$ ]  DBCCDV % >  D3BCCDsg   H- !AI %'I AI 8I I +I -I
I	II II J1	J :J Jc                    dt         j                  j                  t         j                  j                  fz  }t        j
                  j                  | j                  j                  d      d      }t        j
                  j                  |      rXt        j                  |      }||k7  r=t        j                  d       | j                  d       t        j                  ||       yyt        j
                  j                  | j                  j!                  d            rt        j#                  d       t        j                  ||       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.%dru   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minorrI   rV   joinpaths	get_cpathr   r   load_text_filer;   rc   purge_cacher   get_ipath_curinfo)r&   current_python_versionpython_version_pathcached_python_versions       r4   $purge_cache_on_python_version_changer     s    %(  '',,

V$&6 
ww~~)* $ 3 34G H !$::IIEFT"OO/1GH ;
 77>>$**229=>HHC 	+-CDr5   c                 ^    t        j                  | j                  d      ry|j                   S )Ndisable_network_activationF)r   get_cfg_option_boolrn   local)r&   rY   s     r4   _should_bring_up_interfacesr   5  s&    *FGzz>r5   
raw_configc                 @   | syt        j                  | j                         dd       dk7  ry	 t        j                  |       }t        |t              sy
d|v rG|d   D ]?  }|j                  d      xs i }|j                  dd      }|s-|j                  d      r? y |j                  d      ry|j                  di       j                  d      ry|j                  d      ryy# t
        $ r-}t        t        dt        j                  d|f       Y d}~y	d}~ww xY w)a  Determine 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
    )Fzno configuration foundN*   ztext/cloud-config)Tz non-cloud-config user data foundz24.4z'Unexpected failure parsing userdata: %s)rG   r   requested_levelr3   rY   )Tz!failed to parse user data as yaml)Tz(parsed config not in cloud-config formatwrite_filessourceuri )rz   zfile:)Tz!write_files with source uri foundbootcmd)Tzbootcmd foundrandom_seedcommand)Tzrandom_seed command foundmounts)Tzmounts found)Fz8cloud-config does not contain network requiring elements)r   type_from_starts_withstripyaml	safe_load	Exceptionr   r;   r   WARNINGr   dictrl   r   )r   parsed_yamlr   itemsource_dict
source_uris         r4   _should_wait_via_user_datar   ;  s"    . 	&&z'7'7'9#2'>?	 8
9nnZ0 k4(? #. 	AD((8,2K$3J:#8#8#H@		A
 y!$}b)--i80x #L5  9##OO9	
 99s   C' '	D0#DD
datasourcec                     | syt        | j                               \  }}|rd| dfS t        | j                               \  }}|rd| dfS t        | j                               \  }}|rd| dfS dd| d| d	| fS )
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
    )Tzno datasource foundTz in user dataz in vendor dataz in vendor data2Fzuser data: z, vendor data: z, vendor data2: )r   get_userdata_rawget_vendordata_rawget_vendordata2_raw)r   user_should_waituser_reasonvendor_should_waitvendor_reasonvendor2_should_waitvendor2_reasons          r4   _should_wait_on_networkr   v  s     *$>##%%!k }M222(B%%')% o666*D&&(+' ''7888 	+ ')? ++,. r5   c                    t         j                  t         j                  g}|j                  rt         j                  g}t	        t
        j                  j                  dt        z  d      |j                         g|j                  rdnd}t        |      }t        j                  ||j                        }|j                  t        |             d }d }	 |j                  s2t!        fd       t#        j$                  |j&                  |       \  }}n#t#        j(                  |j&                  |       \  }}|j8                  r)t.        j9                  d	       t;        j<                          |j                  s4t;        j>                  |j&                         tA        |j&                         tC        | |
       t.        jE                  dt        jF                         |       D ]  \  }	}t.        jI                  |	|        	 |jK                          |jL                  }
tO        |       |j                  rt         jP                  nt         jR                  }|t         jR                  k(  rtT        jV                  rgt
        j                  jY                  |jL                  j[                  d            s/t.        j9                  d       |j\                  j_                          d}t`        jb                  je                  dtg        jh                         z         nd}t#        jj                  |j&                  dd      }|rt.        j9                  d       d}nH|
jm                  d      }t
        j                  jY                  |      rt.        j9                  d|       d}|jo                          tq        ||      }	 |js                  |       |t         jR                  k(  r>|jt                  jv                  |k7  r%t.        j9                  d||jt                         d g fS t        |       |j                         }t.        j9                  d|| ||j                                |t         jP                  k(  rt        |d d!"       |j}                  |       |t         jP                  k(  rtT        jV                  rvt        |jt                        \  }}|rt.        j9                  d#|       nEt.        j9                  d$|       t#        j                  |jL                  j[                  d      d%       |jt                  jv                  |k7  r/t.        j9                  d&||jt                         |jt                  g fS t.        j9                  d'||jt                         |j                          |j                          t        |d(d)"       	 |j                         j                  d*|j                  t        gt        +      \  }}|s|j                  t               |jL                  jm                  d-      }t
        j                  jY                  |      rXt        j                  |      j                  d.k7  r6t        t        j                  t#        j                  |            ddd/0       nt.        j9                  d1       tA        |j&                         t        |t        |      |j                  2      }	 |}|}t#        j(                  |j&                  |       \  }}||k7  s||k7  r:t.        j                  d3||       t#        j$                  |j&                  |       \  }}t;        j>                  |j&                         |j                          t        |jt                  |j&                  5       |jt                  t        || |       fS # t*        $ rL d}t#        j,                  t.        |       t1        |       j3                  t4        j6                  |f       Y w xY w# t*        $ r t#        j,                  t.        d       Y /w xY w# t         jx                  $ r |t         jP                  k(  rt.        j9                  d       nt#        j,                  t.        d       |jz                  sH|j}                  |       t.        j9                  d|       |t         jP                  k(  rd g fcY S d dgfcY S t.        j9                  d|       Y w xY w# t*        $ r, t#        j,                  t.        d,       |jt                  d,gfcY S w xY w# t*        $ r t#        j,                  t.        d4       Y w xY w)6Nz%s.dz91_kernel_cmdline_url.cfg)rV   r   r%   r&   ds_depsreporterc                 F    j                  t        j                  | f      S rS   )rU   r   r   )r3   
early_logss    r4   <lambda>zmain_init.<locals>.<lambda>  s    J$5$5w}}c6J$K r5   #Failed to setup output redirection!@Logging being reset, this logger may no longer be active shortlyr2   !PID [%s] started cloud-init '%s'.z0Failed to initialize, likely bad things to come!z.skip-networkz4Will wait for network connectivity before continuing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_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.zinit-netzmodules:configconsume_data)rY   freqzConsuming user data failed!cloud_configr   T)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!)r   rn   )Wr
   DEP_FILESYSTEMDEP_NETWORKr   r   rI   rV   r   r!   r9   r   Initr   read_cfgr\   skip_log_setuprQ   r   fixup_outputrn   get_output_cfgr   logexcr;   r1   rU   r   r   rc   r   reset_loggingsetup_loggingro   r=   r   getppidr8   
initializer   r   DSMODE_LOCALDSMODE_NETWORKr   MANUAL_NETWORK_WAITr   get_runpathdistrowait_for_networkr-   r.   r/   r   
debug_infor   r   r   r   fetchr   dsmodeDataSourceNotFoundExceptionforceapply_network_config_maybe_persist_instance_data
instancifyis_new_instance_maybe_set_hostnamer   r   setup_datasourceupdatecloudifyrunr   r   r   statst_sizer   r   r   r   r   warningactivate_datasourcedi_report_warnrj   )rX   rY   depsbootstage_namew_msgr&   outfmterrfmtr3   lvlpath_helperr   r   mcfgmfilebring_up_interfacesiidshould_waitreasonran_resultscloud_cfg_pathr]   outfmt_origerrfmt_origr   s                            @r4   	main_initr7    sw   ""G$7$78Dzz&&' 	%'B 

N		
J, &*ZZ\VN>*E;;tdmm<DMM+d#$FF
/""KL!..txx>NFF!004@NFF zz		N	
 	dhh'DHH%
 DeHH0"**,O  SSM **K(.#'::773I3IDw%%%''JJ""?31
 IILMKK((*

7#5#5#789''2FNII:;H--.CDEww~~e$		DeL" 6dDA

H
% 7)))doo.D.D.LII;
 ":. !&
//
CII? w### 	DYG':;w###''"9$//"JK		= 		=
 

 6 6 GL??!!T)II@
 OOR((IIH 	KKMJ<LMB --/--	 . 
h  j) ZZ--n=N	ww~~n%"''.*A*I*IQ*N#>>$"5"5n"EF!		
 			FG! 4T*T]]CDD..txx>& K6$9KK=vvN#004@VV $((# 	doo488< OO/dDABBY  /3C#7<<-.	/8  MCKLMT ..  7'''II12KKO zz%%/B%CII7>w+++bz!>?@@IIDd%@  BC67"?!@AAB8  DCBCDsl   A!_ ` ;A"a Ad 2A+e A``#aaB!d+d2dd2ee#e.-e.c                    d|vrt         j                  d       y |d   }|t         j                  d       y t        |t              st         j	                  d|       y |j                  d      }|t         j	                  d       y t        |t              st         j	                  d|       y | j                  j                  d      d	   }|j                  t        j                        r|t        t        j                        d  }nt         j	                  d
| |       ||v rt         j                  d| ||       y t        j                  d||t        |             y )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)r   dslist)r;   rc   r   r   r"  rl   list
__module__
rpartitionr   r
   	DS_PREFIXrb   r   show_warningr   )r   rn   dicfgr=  modnames        r4   r$  r$    s7   #		12E} 			'(eT";UCYY()F~>?%>G ##..s3A6G'++,#g//023?	
 &		H		
 	s73v;r5   c                 H   |j                   }| d|}t        |      }t        j                  g |j                        }|j                  t        |             	 |j                  d       t        |       t!        |t        |      |j                        }	 |j"                  s*t%                t        j&                  |j(                  |       |j,                  r)t        j-                  d       t/        j0                          |j"                  s4t/        j2                  |j(                         t5        |j(                         t7        ||	       t        j9                  d
t;        j<                         |       |dk(  rt?        j@                  ddd       tC        |||      S # t        j                  $ r? d|z  }t        j                  t        |       t        |       |j                  s|gcY S Y w xY w# t*        $ r t        j                  t        d       Y Pw xY w)N:r   r   r   zGCan not apply stage %s, no datasource found! Likely bad things to come!r   r   r   r2   r   r&   z`--mode init`24.1Use `cloud-init init` instead.)r   r   r   )"r   r9   r   r  r   r  r\   r  r
   r  r   r  r;   r1   r  r  r   r  rQ   r  rn   r   rc   r   r  r	  ro   r=   r   rI   r
  r   r   rj   )rd   rY   rX   r&  r'  r&   r3   r]   s           r4   main_modulesrI    s   99D !,T2N>*E;;rDMM:DMM+d#$

G
$ !&4T*T]]CD@""Mdhh- zz		N	
 	dhh'DHH% DeHH0"**,Ov~&%:	
 dD$//U .. 	 $% 	 	C#zz7N 	$  @C>?@s%   F% 6G: %AG76G7:#H! H!c                    |j                   }t        |       }t        j                  g |j                        }|j                  t        |             	 |j                  d       t        |       t!        |t        |      |j                        }|j"                  }|rt        j%                  d|       |j&                  }|r+t        j%                  d|       t(        j+                  |      }	 t-                t        j.                  |j0                  d        |j$                  r)t        j%                  d
       t5        j6                          t5        j8                  |j0                         t;        |j0                         t=        | |       |j?                  |||      \  }}	|	rt        jA                  d|       y|st        jA                  d|       yy# t        j                  $ r7 t        j                  t        d       t        d       |j                  sY yY w xY w# t2        $ r t        j                  t        d	       Y 2w xY w)Nr   r   r   z;Failed to fetch your datasource, likely bad things to come!r   r   zUsing passed in arguments %szUsing passed in frequency %sr   r   r2   zRan %s but it failed!zDid not run %s, does it exist?r   )!rX   r9   r   r  r   r  r\   r  r
   r  r   r  r;   r1   r  r  r   module_argsrc   	frequencyFREQ_SHORT_NAMESrl   rQ   r  rn   r   r   r  r	  ro   r=   
run_singler"  )
rX   rY   mod_namer'  r&   r]   mod_argsmod_freqrg   rh   s
             r4   main_singlerR    s    yyH4 E;;rDMM:DMM+d#$

G
$ !&4T*T]]CDH		0(;~~H		0(;#''1@$((D) zz		N	
 	$((#! De !OOHhIY+X64h? e ..  	N	
 	I	
 zz 4  @C>?@s%   G 0*H& AH#"H#&#IIc           
         t               }|j                  d      }t        j                  j	                  |j
                        }t        j                  j                  |d      }t        j                  j                  |d      }t        j                  j                  |d      }t        j                  j                  |d      }t        j                         }	t        j                  ||f       |j                  \  }
}| dk(  r|j                  rd}n&d}n#| dk(  rd|j                  z  }nt        d| z        |t        vrt        d	j!                  |            g i d d d
}dd |j#                         |j#                         |j#                         |j#                         di}|dk(  r!||||fD ]  }t        j$                  |        n)	 t'        j(                  t        j*                  |            }||d   vr|j#                         |d   |<   |d   }||d<   ||   d   r%||   d   st.        j1                  dt        |          t3        t        j4                               ||   d<   t7        t9        d |	j:                              }|j=                         }t?        j@                  ||       t        jB                  t        j                  jE                  ||      |d       	  || |      }|dv r|\  }}|tG        |      |d<   n|}||   d   jI                  |D cg c]  }tG        |       c}       t3        t        j4                               ||   d<   d |d<   |j=                         }|jU                          |jW                         D ];  }||v r'tY        t[        ||   ||   z               ||   d   |<   .||   ||   d   |<   = t?        j@                  ||       |dk(  rg }|jW                         D ]O  }t]        ||   t^              s||   ja                  d      s,|jI                  ||   ja                  dg              Q t?        j@                  |d|d   |di       t        jB                  t        j                  jE                  ||      |d       tc        ||   d         S # t,        $ r Y w xY wc c}w # t,        $ rO}t.        jK                  d|       tM        d|z         ||   d   jO                  tG        |             Y d }~d }~wtP        $ ri}|jR                  rRt.        jK                  d|       tM        d|z         ||   d   jO                  dtG        |jR                         d       Y d }~Rd }~ww xY w# t3        t        j4                               ||   d<   d |d<   |j=                         }|jU                          |jW                         D ];  }||v r'tY        t[        ||   ||   z               ||   d   |<   .||   ||   d   |<   = t?        j@                  ||       w xY w)Nru   zstatus.jsonzresult.jsonr&   r%   modules
modules-%szunknown name: %sz'Invalid cloud init mode specified '{0}')errorsrecoverable_errorsstartfinishedv1)r   r&   r%   r'   r(   r   rX  rY  z=Unexpected start time found for %s. Was this stage restarted?c                 6    t        | t        j                        S rS   )r   r   LogExporter)hs    r4   r   z status_wrapper.<locals>.<lambda>  s    jG$7$78 r5   T)r  )r&   r%   r   rV  zfailed stage %szfailed run of stage %sz	sys.exit(z) calledrW  r(   )r   rV  )2r   r   rI   rV   normpathrun_dirr   r   	getLoggerr   ensure_dirsr<   r   r   
ValueError
STAGE_NAMErB   copydel_filejsonloadsr   r   r;   r"  floatr?   r   filterr   export_logsr   
write_jsonsym_linkrelpathr   extend	exceptionr1   rU   
SystemExitr   
clean_logskeysr>  setr   r   rl   rb   )rX   rY   r   data_dlink_dstatus_pathstatus_linkresult_pathresult_linkroot_logger_namefunctorr   
nullstatusstatusfrZ  handlerpreexisting_recoverable_errorsretr   rV  r   new_recoverable_errorsrv   r   s                             r4   status_wrapperr  L  s   E__V$FWWemm,F'',,v}5K'',,v}5K'',,v}5K'',,v}5K##%K	
 {{UGv~::DD		dii'+d233:5<<TB
 	

  	J 	OO%$//+(oo/'__.
F |{KE 	AMM!		ZZ 3 3K @AF 6$<'__.tT	BBwK	$xD*!5Kt	

 dkkm,BtHW8+:N:N	
G
 &-%8%8%:" [&1MM
V,k16dD!))#& Z%#&z?< F
4!!6":a3q6":;(  %T[[]347 ")!4!4!6)..0 	C446:6s;05674-.s3 7M74-.s3	 	  f5 	7A"Q%&2a599X+>beii"56	7 	  "\"2fEF	
 	GGOOK0+T	
 r$x!""A  		N #; *'.*T12
4!!#a&)) 	I 66 MM+T2.56tHX%%	#aff+h&GH	I  %T[[]347 ")!4!4!6)..0 	C446:6s;05674-.s3 7M74-.s3	 	  f5sc   (R4 ':S	 !S3S	 4	S SS	 		VATV V(AVV VV B.Yr&   c                     | j                   re| j                  rX| j                  j                  d      }t        j
                  j                  |      s| j                   j                  d       yyyy)zCWrite instance-data.json file if absent and datasource is restored.instance_dataF)write_cacheN)r   ds_restoredr   r  rI   rV   r   persist_instance_data)r&   instance_data_files     r4   r  r    sX    4++!ZZ33ODww~~01OO11e1D 2 ,r5   c           	      >   | j                         }t        j                  | j                  |d      \  }}}|r$	 t	        j
                  d| j                  |d       yy# t        j                  $ r+}t        j                  d||t        |             Y d}~yd}~ww xY w)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;   rc   r   )r&   r   r   cloudhostname_fqdn_r   s           r4   r  r    s     MMOE11%tXua 		"">488UDI  // 	II1A 	s   "A B1!BBc                     t         j                  j                  dj                  t	        t
        j                              dz          y )Nr+   )r-   stdoutr/   r   sortedr   FEATURES)rX   rY   s     r4   main_featuresr    s-    JJTYYvg&6&6784?@r5   c           
      h	   t        j                          | st        j                  } t	        j
                  | j                  d            }|j                  ddddt        j                         z   d       |j                  d	d
ddd       |j                  ddddd       |j                  ddddd       |j                  d        |j                  dd      }|j                  dd      }|j                  dddd d       |j                  d!d"d#d$d%t	        j                  d&      '       |j                  dt        f(       |j                  d)d*      }t        j                   d+d,d-d./      j"                  }|j                  d0d1d2d3| d4d56       |j                  d!d"d#d$d%t	        j                  d&      '       |j                  d)t$        f(       |j                  d7d8      }|j                  d9d:d2d;d.<       |j                  d=d2d>dt'        t(        j+                               ?       |j                  d@ddAd<       |j                  dBdCdDdEF       |j                  d!d"d#d$d%t	        j                  d&      '       |j                  d7t,        f(       |j                  dGdH      }|j                  dIdJ      }|j                  dIt.        f(       |j                  dKdL      }	|j                  dMdN      }
|j                  dOdP      }|j                  dQdR      }|j                  dSdT      }|j                  dUdV      }| rt1        dW | D        d       }|dKk(  rddXlm}  ||	       n|dMk(  rddXlm}  ||
       n|dOk(  r&ddYlm}m}  ||Z       |j                  dO|f(       n|dQk(  r%dd[lm}m}  ||       |j                  dQ|f(       n|dGk(  r%dd\l m}m!}  ||       |j                  d]|f(       ne|dUk(  r%dd^l"m}m#}  ||       |j                  dU|f(       n;|dSk(  r6dd_l$m}m%}  ||       |j                  dS|f(       n|jM                  d`       |jO                  | a      }tQ        |dbd       |jR                  stU        |      S tS        |      S )cNr   )progz	--versionz-vr   z	%(prog)s z'Show program's version number and exit.)r<   r   helpz--debugz-d
store_truez:Show additional pre-action logging (default: %(default)s).F)r<   r  defaultz--forcezDForce running even if no datasource is found (use at your own risk).r  )r<   r  destr  z--all-stages
all_stageszsRun cloud-init's stages under a single process using a syncronization protocol. This is not intended for CLI usage.)r  r<   r  r  r   Subcommands
subcommand)titler  r&   z2Initialize cloud-init and perform initial modules.)r  --localz-lz+Start in local mode (default: %(default)s).z--filez-frU   rT   z(Use additional yaml configuration files.rb)r<   r  r  typerE   rT  z1Activate modules using a given configuration key.z`init`rG  rH  Tr   z--modez-mstorez9Module configuration name to use (default: %(default)s). r   )r&   r   final)r<   r  r  choicessinglezRun a single module.z--namez-nzModule name to run.)r<   r  requiredz--frequencyzModule frequency for this run.)r<   r  r  r  z--reportzEnable reporting.rK  *argumentz0Any additional arguments to pass to this module.)nargsmetavarr  queryz;Query standardized instance metadata from the command line.r   z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.c              3   D   K   | ]  }|j                  d       r|  yw)-N)r   ).0posargs     r4   	<genexpr>zmain.<locals>.<genexpr>  s     J63D3DS3IVJs     )
get_parser)r  handle_collect_logs_args)parser)r  handle_clean_args)r  handle_argsrender)r  handle_schema_args)r  handle_status_argsza subcommand is requiredrY   r  )+r   configure_root_loggerr-   argvargparseArgumentParserpopadd_argumentr   rC   set_defaultsadd_subparsers
add_parserFileTyper7  r   r   messagerI  r>  rM  rr  rR  r  r   cloudinit.analyzer  cloudinit.cmd.devel.parsercloudinit.cmd.devel.logsr  cloudinit.cmd.cleanr  cloudinit.cmd.queryr  cloudinit.config.schemar  cloudinit.cmd.statusr  error
parse_argssetattrr  sub_main)	sysv_argsr  
subparsersparser_init
parser_mod
extra_helpparser_singleparser_queryparser_featuresparser_analyzeparser_develparser_collect_logsparser_cleanparser_statusparser_schemar  analyze_parserdevel_parserlogs_parserr  clean_parserr  query_parserhandle_query_argsschema_parserr  status_parserr  rY   s                                r4   mainr  
  s   !!#HH	$$)--*:;F w55786   I   -   	 K   	 &&&]&NJ ''I ( K :   7t$   VY$78 &&K ' J $$!6	
 g  | +  
 7t$   I|#<= ))- * M "   -%**,-    	   ?	   7t$   x&=>((J ) L
 !++1 , O   ](C D**G + N ((. ) L %//I 0  ((K ) L ))H * M ))F * M J)J

 "F >*7"M &>)
 23,,&(@A -  7"
 &%%g7H-I%J7"
 &%%h8I-J%K8#
 -(&&x9K.L&M8#
 -(&&x9K.L&M/0),DD"E*??~fr5   c                    t         j                  d       t        j                  dddd      }t        j                  d        |d      5  | j                  ddg	      }d
|_        t        |      |_        ddd        |d      5  | j                  dg	      }d|_        t        |      |_        ddd        |d      5  | j                  ddg	      }d|_        t        |      |_        ddd        |d      5  | j                  ddg	      }d|_        t        |      |_        ddd       |j                  rLd}|j                  rd|j                   }t        j                  d| d       t        j                  d       yt        j                  d       t        j                  d       y# 1 sw Y   @xY w# 1 sw Y   xY w# 1 sw Y   xY w# 1 sw Y   xY w)z>Run all stages in a single process using an ordering protocol.z*Running cloud-init in single process mode.r   r   r   r  zREADY=1r&   r  r  FNTrT  z--mode=configz--mode=finalz%a stage of cloud-init exited non-zerozfirst exception received: zSTATUS=Completed with failure, z2. Run 'cloud-init status --long' for more details.z
STOPPING=1r   zSTATUS=Completed)r;   r   r   
SocketSync	sd_notifyr  r  r  systemd_exit_codeexperienced_any_errorfirst_exception)r  syncrY   r  s       r4   r  r    s   HH9: Wi7CD Y	g 0  vy&9 :#!)$0 
i 0  vh /"!)$0 
h 0  y/&B C"!)$0 
g 0  y.&A B"!)$0 !!9243G3G2HIG-gY 7) )	
 	&+,&[0 00 00 00 0s0   ,F5+G,G>,G5F?GGG$c                    | j                   \  }}|dvr@t        j                  | j                  rt        j
                  nt        j                         n | j                  rt        j                          t        j                          d|k(  sd|k(  rd| j                  k7  rt        }d }d}|dk(  r| j                  rd\  }}ncd\  }}n]|dk(  rd| j                  z  d| j                  z  }}n9|d	k(  r+d
| j                  z  d| j                  z  }}| j                  }n	|}d|z  }d}t        j                  |||      | _        | j                   5  t#        j$                  d| d      5   |||       }d d d        d d d        t'        j(                          d|k(  rd| j                  k(  rt+              }S # 1 sw Y   FxY w# 1 sw Y   JxY w)N)r&   rT  r&   rT  T)r%   zsearching for local datasources)zinit-networkz!searching for network datasourcesrU  zrunning modules for %sr  z	single/%szrunning single module %szrunning 'cloud-init %s'F)reporting_enabledzcloud-init stage: 'r_   )r<   r   setup_basic_loggingrc   r   r   r   r	   attach_handlersr   r  r   rX   reportr   ReportEventStackr   r   Timedr   flush_eventsrb   )rY   rX   r|  rname	report_onrdescretvals          r4   r  r  7  s    kkOT7 &&##!ZZGMMW__	
 
##% ""$
 ~)t+$))0C EIv~::LLE5LE5 
	499$$tyy0  
	$))#&2  KK	)D0	++u	DM 
 )!4UG1=> 	)T4(F	))  DVtyy0VM	) 	)) )s$   G(
F72G7G 	<GG__main__)r   rS   ))r   r}   )TN)Zr  rf  rI   r-   r0   r   r   typingr   r   r   r   	cloudinitr   r   r	   r
   r   r   r   r   r   r   r   r   r   r   r   cloudinit.logr   r   cloudinit.cmd.develr   cloudinit.configr   cloudinit.config.modulesr   r  r   cloudinit.lifecycler   cloudinit.reportingr   cloudinit.settingsr   r   r    r!   r   ReasonrA   r`   rM  rc  r`  __name__r;   r1   r=   r9   timedrc   rQ   r\   rj   ro   rw   intr   r   r   bytesboolr   
DataSourcer   r7  r$  rI  rR  r  r  r  r  r  r  r  r  exitr  r   r5   r4   <module>r     s     	 
    3 3 ' $       !    #   + . , , ? ; &  
(  %
    $"	
 g!
 ?#03		 5#- 5 $5
W &=
KjU38_ jZE<8MsEz*+8M
4<8Mv"++,"
4<"JuCp/d>0BCLX#vEv{{ E.Aod8'vBJ zCHHT#((^ r5   