o
    m[hº  ã                   @   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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 œZ5e 6e7¡Z8d\d"d#„Z9d]d$d%„Z:d&d'„ Z;e <d(¡e8j=fd)e
e0gdf fd*d+„ƒZ>d,d-„ Z?d.e$fd/d0„Z@d1d2„ ZAd^d4d5„ZBd_d7e	eCe0f fd8d9„ZDd:d;„ ZEd<d=„ ZFd>eee0eGf  d7e	eHe1f fd?d@„ZIdAeejJ d7e	eHe1f fdBdC„ZKdDdE„ ZLdFdG„ ZMdHdI„ ZNdJdK„ ZOdLdM„ ZPdNejQfdOdP„ZRdQdR„ ZSdSdT„ ZTd]dUdV„ZUdWdX„ ZVdYdZ„ ZWe7d[k�r{e XeUejYƒ¡ dS dS )`é    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-finalÚ 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© r3   ú4/usr/lib/python3/dist-packages/cloudinit/cmd/main.pyr0   T   s   r0   c                 C   s&   |st | ƒ}tjd| ddtd� |S )Nr)   FT)Úconsoler-   Úlog)Úwelcome_formatr   Ú	multi_logÚLOG)Úactionr2   r3   r3   r4   Úwelcome^   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:   r3   r3   r4   r7   e   s   ür7   úClosing stdinÚloggerc                 C   sj   t  tj ¡ ¡s/| dƒ tt jƒ�}t  | ¡ tj ¡ ¡ W d  ƒ dS 1 s(w   Y  dS | dƒ dS )zj
    reopen stdin as /dev/null to ensure no side effects

    logger: a function for logging messages
    rC   Nz"Not closing stdin, stdin is a tty.)ÚosÚisattyr,   ÚstdinÚfilenoÚopenÚdevnullÚdup2)rD   Úfpr3   r3   r4   Úclose_stdinn   s   "ÿrM   c                 C   sT   g }| j r| j D ]}| tj |j¡¡ q| jr(| jD ]}| tj |j¡¡ q|S ©N)Ú
main_filesÚappendrE   ÚpathÚrealpathÚnameÚfiles)ÚargsÚfn_cfgsÚfhr3   r3   r4   Úextract_fns}   s   

rX   Ú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.   r9   Údebug)rY   Úaction_nameÚsectionÚfull_section_nameÚ	which_ranÚfailuresÚtotal_attemptedr2   r3   r3   r4   Úrun_module_section�   s   þ
ÿrd   c                 C   s"   |   d¡rt |   d¡¡ d S d S )Nr   )Úgetr   Úupdate_configuration)Úcfgr3   r3   r4   Úapply_reporting_cfg¢   s   
ÿrh   ©ú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Úkeyr3   r3   r4   Úparse_cmdline_url§   s   
ÿrr   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}
zxtjdi |¤Ž}d}| ¡ rÓd}t|tjƒr¢z|t|jt|
ƒd�ƒ7 }W n	 ty™   Y nw | |
¡s¡d}n|j |
¡sªd}|r»|dkrºtjddddd�W S n"|dkrÃt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)rk   ÚtimeoutÚretriesÚstreamé   Úsec_betweengš™™™™™¹?s   #cloud-configó    )Ú
chunk_sizeFrk   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_logrj   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 %sr3   )r   Úget_cmdlinerr   rm   ÚloggingÚDEBUGÚ
startswithrE   rQ   ÚexistsÚINFOÚWARNr   Úread_file_or_urlÚokÚ
isinstanceÚUrlResponseÚnextÚiter_contentr\   ÚStopIterationÚcontentsr   Ú	deprecateÚstrÚcodeÚUrlErrorÚ
write_file)rQ   Únetworkrn   Úcmdline_namerk   Úpath_is_localÚmÚlevelÚkwargsrp   ÚheaderÚrespÚsniffed_contentÚis_cloud_cfgÚer3   r3   r4   Úattempt_cmdline_url¯   sž   ÿ
ÿÿûÿÿ

ÿÿ
€ûÿ	þþ€ÿÿþr£   c                 C   sž   dt jjt jjf }tj | j d¡d¡}tj 	|¡r8t
 |¡}||kr6t d¡ |  d¡ t
 ||¡ dS dS tj 	| j d¡¡rGt d¡ t
 ||¡ dS )	zÊPurge 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.%drp   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ÚminorrE   rQ   ÚjoinÚpathsÚ	get_cpathrˆ   r   Úload_text_filer9   r]   Úpurge_cacher—   Úget_ipath_curÚinfo)r%   Úcurrent_python_versionÚpython_version_pathÚcached_python_versionr3   r3   r4   Ú$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_boolrg   Úlocal)r%   rU   r3   r3   r4   Ú_should_bring_up_interfaces:  s   r¶   Ú
raw_configc              
   C   sú   | sdS t  |  ¡ dd… ¡dkrdS zt | ¡}W n ty8 } zttdtj	d|fd� W Y d}~d	S d}~ww t
|tƒs@d
S d|v rb|d D ]}| d¡pPi }| dd¡}|ra| d¡sa dS qH| d¡ridS | di ¡ d¡rtdS | d¡r{dS dS )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)rD   r   Úrequested_levelr2   rU   )Tz!failed to parse user data as yaml)Tz(parsed config not in cloud-config formatÚwrite_filesÚsourceÚurir(   )rt   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   r9   r…   ÚWARNINGr�   Údictre   r‡   )r·   Úparsed_yamlr¢   ÚitemÚsource_dictÚ
source_urir3   r3   r4   Ú_should_wait_via_user_data@  sF   ÿû€ø

€

rÌ   Ú
datasourcec                 C   sˆ   | sdS t |  ¡ ƒ\}}|rd|› d�fS t |  ¡ ƒ\}}|r&d|› d�fS t |  ¡ ƒ\}}|r7d|› d�fS dd|› d|› d	|› �fS )
zÙDetermine 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_reasonr3   r3   r4   Ú_should_wait_on_network{  s0   	ÿÿÿÿþýr×   c                    s¦  t jt jg}|jrt jg}ttj dt d¡|j d�g‰ |jr"dnd}t	|ƒ}t
j||jd�}| t|ƒ¡ d }d }z |jsQt‡ fdd„ƒ t |j| ¡\}}n	t |j| ¡\}}W n tyx   d	}t t|¡ t|ƒ ˆ  tj|f¡ Y nw |jr…t d
¡ t ¡  |js“t |j¡ t |jƒ t!| |d� t "dt #¡ |¡ ˆ D ]
\}	}t $|	|¡ q¤z| %¡  W n tyÄ   t td¡ Y nw |j&}
t'|ƒ |jrÒt j(nt j)}|t j)krþt*j+rñtj ,|j& -d¡¡sñt d¡ |j. /¡  d}t0j1 2dt3 4¡  ¡ n-d}t 5|jdd¡}|�rt d¡ d}n|
 6d¡}tj ,|¡�r't d|¡ d}| 7¡  t8||ƒ}z"|j9|d� |t j)k�rQ|j:j;|k�rQt d||j:¡ d g fW S W nF t j<�y˜   |t j(k�rgt d¡ nt td¡ |j=�s�|j>|d� t d|¡ |t j(k�r‰d g f Y S d dgf Y S t d|¡ Y nw t?|ƒ | @¡ }t d || || A¡ ¡ |t j(k�r¹tB|d!d"d#� |j>|d� |t j(k�rt*j+�rêtC|j:ƒ\}}|�rÚt d$|¡ nt d%|¡ t D|j& -d¡d&¡ |j:j;|k�rþt d'||j:¡ |j:g fS t d(||j:¡ | E¡  | F¡  tB|d)d*d#� z| G¡ jHd+|jItJgtJd,�\}}|�s-| ItK¡ W n t�yD   t td-¡ |j:d-gf Y S w |j& 6d.¡}tj ,|¡�rjt L|¡jMd/k�rjtNtO Pt Q|¡¡ddd0d1� nt d2¡ t |jƒ tR|t|ƒ|jd3�}z)|}|}t |j| ¡\}}||k�s–||k�r¦t Sd4||¡ t |j| ¡\}}W n t�y·   t td5¡ Y nw t |j¡ | T¡  tU|j:|jd6� |j:tV|| | ƒfS )7Nz%s.dz91_kernel_cmdline_url.cfg)rQ   r˜   r$   r%   ©Úds_depsÚreporterc                    s   ˆ   tj| f¡S rN   )rP   r…   r†   r1   ©Ú
early_logsr3   r4   Ú<lambda>Å  s    zmain_init.<locals>.<lambda>ú#Failed to setup output redirection!ú@Logging being reset, this logger may no longer be active shortlyr1   ú!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)rU   Ú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Í   rg   )Wr	   ÚDEP_FILESYSTEMÚDEP_NETWORKrµ   r£   rE   rQ   r¨   r    r7   r   ÚInitrÚ   Úread_cfgrX   Úskip_log_setuprM   r   Úfixup_outputrg   Úget_output_cfgrÅ   Úlogexcr9   r0   rP   r…   rŠ   r]   r   Úreset_loggingÚsetup_loggingrh   r;   r®   Úgetppidr6   Ú
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_warnrd   )rS   rU   ÚdepsÚbootstage_nameÚw_msgr%   ÚoutfmtÚerrfmtr2   ÚlvlÚpath_helperrƒ   ræ   ÚmcfgÚmfileÚbring_up_interfacesÚiidÚshould_waitÚreasonÚranÚ_resultsÚcloud_cfg_pathrY   Úoutfmt_origÚerrfmt_origr3   rÛ   r4   Ú	main_init   sZ  ÿüÿ€üÿ
ÿ

ÿ




ý
€ÿÿîûýýý
ý
ü
€þ ü

€ÿ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Ú.rv   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)r9   r]   r�   rÇ   r  re   ÚlistÚ
__module__Ú
rpartitionr‡   r	   Ú	DS_PREFIXr\   r   Úshow_warningr”   )rÍ   rg   Údicfgr.  Úmodnamer3   r3   r4   r  ˜  sH   





ýü
ÿr  c                 C   sZ  |j }d| |f }t|ƒ}tjg |jd�}| t|ƒ¡ z|jdd� W n  tj	yD   d| }t
 t|¡ t|ƒ |jsB|g Y S Y nw t|ƒ t|t|ƒ|jd�}z|jsatƒ  t
 |j|¡ W n tyq   t
 td¡ Y nw |jr~t d¡ t ¡  |jsŒt |j¡ t|jƒ t||d	� t d
t ¡ |¡ |dkr§tj dddd� t!|||ƒS )Nz%s:%srØ   rá   rå   zGCan not apply stage %s, no datasource found! Likely bad things to come!rñ   rÞ   rß   r1   rà   r%   z`--mode init`ú24.1úUse `cloud-init init` instead.)r   r€   r�   )"rƒ   r7   r   rô   rÚ   rõ   rX   r  r	   r  r   rù   r9   r0   r  r
  r   rö   rM   r÷   rg   rÅ   r]   r   rú   rû   rh   r;   r®   rE   rü   r   r“   rd   )r^   rU   rS   r  r  r%   r2   rY   r3   r3   r4   Úmain_modulesÊ  sZ   ÿÿ
ÿø
€ÿÿ
ýr8  c           
      C   st  |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ƒ  t
 |jd ¡ W n ty}   t
 td	¡ Y nw |jrŠt d
¡ t ¡  t |j¡ t|jƒ t| |d� | |||¡\}}	|	r®t  d|¡ dS |s¸t  d|¡ dS dS )NrØ   rá   rå   z;Failed to fetch your datasource, likely bad things to come!rz   rñ   zUsing passed in arguments %szUsing passed in frequency %srÞ   rß   r1   zRan %s but it failed!zDid not run %s, does it exist?r   )!rS   r7   r   rô   rÚ   rõ   rX   r  r	   r  r   rù   r9   r0   r  r
  r   Úmodule_argsr]   Ú	frequencyÚFREQ_SHORT_NAMESre   rM   r÷   rg   rÅ   r   rú   rû   rh   r;   Ú
run_singler  )
rS   rU   Úmod_namer  r%   rY   Úmod_argsÚmod_freqra   rb   r3   r3   r4   Úmain_single  s`   	ÿÿÿö
ÿÿ
r@  c                 C   s¾  t ƒ }| d¡}tj |j¡}tj |d¡}tj |d¡}tj |d¡}tj |d¡}t ¡ }	t	 
||f¡ |j\}
}| dkrH|jrEd}nd}n| dkrRd|j }ntd|  ƒ‚|tvrctd	 |¡ƒ‚g i d d d
œ}dd | ¡ | ¡ | ¡ | ¡ dœi}|dkr�||||fD ]}t	 |¡ q†nz
t t	 |¡¡}W n	 ty¢   Y nw ||d vr±| ¡ |d |< |d }||d< || d rÍ|| d sÍt dt| ¡ tt	 ¡ ƒ|| d< ttdd„ |	jƒƒ}| ¡ }t  ||¡ t	j!tj "||¡|dd� zÒz,|| |ƒ}|dv �r|\}}|d u�rt#|ƒ|d< n|}|| d  $dd„ |D ƒ¡ W n\ t�yO } zt %d|¡ t&d| ƒ || d  't#|ƒ¡ W Y d }~n7d }~w t(�y� } z&|j)�rwt %d|¡ t&d| ƒ || d  'dt#|j)ƒ› d�¡ W Y d }~nd }~ww W tt	 ¡ ƒ|| d< d |d< | ¡ }| *¡  | +¡ D ]&}||v �r¸t,t-|| ||  ƒƒ|| d |< �q�|| || d |< �q�t  ||¡ nHtt	 ¡ ƒ|| d< d |d< | ¡ }| *¡  | +¡ D ]&}||v �r t,t-|| ||  ƒƒ|| d |< �qå|| || d |< �qåt  ||¡ w |dk�rWg }| +¡ D ]}t.|| t/ƒ�r;||  0d¡�r;| $||  0dg ¡¡ �qt  |d|d |d œi¡ t	j!tj "||¡|dd� t1|| d ƒS )!Nrp   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è   rE  rF  z=Unexpected start time found for %s. Was this stage restarted?c                 S   s   t | tjƒS rN   )r�   r   ÚLogExporter)Úhr3   r3   r4   rÝ   œ  s    z status_wrapper.<locals>.<lambda>T)r  )r%   r$   rÍ   rC  c                 S   s   g | ]}t |ƒ‘qS r3   )r”   )Ú.0r¢   r3   r3   r4   Ú
<listcomp>°  s    z"status_wrapper.<locals>.<listcomp>zfailed stage %szfailed run of stage %sz	sys.exit(z) calledrD  r'   )rÍ   rC  )2r   rª   rE   rQ   ÚnormpathÚrun_dirr¨   r…   Ú	getLoggerr   Úensure_dirsr:   rµ   rƒ   Ú
ValueErrorÚ
STAGE_NAMEr?   ÚcopyÚdel_fileÚjsonÚloadsr«   rÅ   r9   r  Úfloatr<   r�   Úfilterr   Úexport_logsr   Ú
write_jsonÚsym_linkÚrelpathr”   ÚextendÚ	exceptionr0   rP   Ú
SystemExitr•   Ú
clean_logsÚkeysr/  Úsetr�   rÇ   re   r\   )rS   rU   r©   Údata_dÚlink_dÚstatus_pathÚstatus_linkÚresult_pathÚresult_linkÚroot_loggerÚ_nameÚfunctorrƒ   Ú
nullstatusÚstatusÚfrG  ÚhandlerÚpreexisting_recoverable_errorsÚretrÍ   rC  r¢   Únew_recoverable_errorsrq   r›   r3   r3   r4   Ústatus_wrapperQ  s  
þÿ
ÿüûÿ	ÿÿþ
ÿÿÿ


€ "€ €÷€
ÿÿÿÿì
ÿÿÿÿ
 €þÿrr  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©   r  rE   rQ   rˆ   Úpersist_instance_data)r%   Úinstance_data_filer3   r3   r4   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 )zâCall 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_fqdnrg   r   ÚhandleÚSetHostnameErrorr9   r]   r”   )r%   rè   ré   ÚcloudÚhostnameÚ_fqdnÚ_r¢   r3   r3   r4   r  ô  s"   ÿû€ÿýr  c                 C   s    t j d ttjƒ¡d ¡ d S )Nr*   )r,   Ústdoutr.   r¨   Úsortedr   ÚFEATURES)rS   rU   r3   r3   r4   Úmain_features  s    r„  c              	   C   s  t  ¡  | s	tj} tj|  d¡d�}|jddddt 	¡  dd� |jd	d
dd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!�}|jd"d#d$�}|jd%d&dd'dd� |jdddd(d)t 
d¡d� |jd"tfd*� |jd+d,d$�}tjd-d.d/d0d1�j}|jd2d3d4d5|› �d6d7d8� |jdddd(d)t 
d¡d� |jd+tfd*� |jd9d:d$�}|jd;d<d4d=d0d>� |jd?d4d@dtt ¡ ƒdA� |jdBddCdd>� |jdDdEdFdGdH� |jdddd(d)t 
d¡d� |jd9tfd*� |jdIdJd$�}|jdKdLd$�}|jdKtfd*� |jdMdNd$�}	|jdOdPd$�}
|jdQdRd$�}|jdSdTd$�}|jdUdVd$�}|jdWdXd$�}| �rìtdYdZ„ | D ƒd ƒ}|dMk�rYdd[lm} ||	ƒ n˜|dOk�ridd[lm} ||
ƒ nˆ|dQk�r„dd\lm}m} ||d]� |jdQ|fd*� nm|dSk�rždd^lm}m} ||ƒ |jdS|fd*� nS|dIk�r¸dd_l m}m!} ||ƒ |jd`|fd*� n9|dWk�rÒddal"m}m#} ||ƒ |jdW|fd*� n|dUk�rëddbl$m}m%} ||ƒ |jdU|fd*� n| &dc¡ |j'| dd�}t(|dedƒ |j)�st*|ƒS t)|ƒS )fNr   )Ú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--filez-frP   rO   zPUse additional yaml configuration files for subcommands: init, modules or singleÚrb)r:   Údestr†  Útypez--forcezDForce running even if no datasource is found (use at your own risk).r  )r:   r†  rŠ  rˆ  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).rT   z(Use additional yaml configuration files.rB   rA  z1Activate modules using a given configuration key.z`init`r6  r7  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.r9  Ú*Ú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.rl  z/Report cloud-init status or wait on completion.Úschemaz-Validate cloud-config files using jsonschema.c                 s   s   � | ]
}|  d ¡s|V  qdS )ú-N)r‡   )rJ  Úposargr3   r3   r4   Ú	<genexpr>Ë  s   € zmain.<locals>.<genexpr>)Ú
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©rU   rö   )+r   Úconfigure_root_loggerr,   ÚargvÚargparseÚArgumentParserÚpopÚadd_argumentr   r@   ÚFileTypeÚset_defaultsÚadd_subparsersÚ
add_parserr)  r   r“   Úmessager8  r/  r;  r`  r@  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©  rU   r3   r3   r4   Úmain  s†  
ûû÷øøÿûú
ÿüûÿ÷úÿû
ûüüúþÿÿÿÿÿÿÿþ





ÿ



€
r×  c                 C   s–  t  d¡ t dddd¡}t d¡ |dƒ� | jddgd	�}d
|_t|ƒ|_W d  ƒ n1 s1w   Y  |dƒ� | jdgd	�}d|_t|ƒ|_W d  ƒ n1 sTw   Y  |dƒ� | jddgd	�}d|_t|ƒ|_W d  ƒ n1 sxw   Y  |dƒ� | jddgd	�}d|_t|ƒ|_W d  ƒ n1 sœw   Y  |j	r¿d}|j
r¯d|j
› �}t d|› d�¡ t d¡ dS t d¡ t d¡ dS )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ª  FNTrA  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=1rz   zSTATUS=Completed)r9   r®   r
   Ú
SocketSyncÚ	sd_notifyr¾  rö   rÀ  Úsystemd_exit_codeÚexperienced_any_errorÚfirst_exception)r¤  ÚsyncrU   rµ  r3   r3   r4   rŒ    sD   


û
û
û
û
ÿ

rŒ  c              	   C   sz  | j \}}|dvrt | jrtjntj¡ n| jrt ¡  t ¡  d|ks.d|kr0d| j	kr0t
}d }d}|dkrE| jr@d\}}n0d\}}n+|dkrUd| j	 d| j	 }}n|d	krhd
| j d| j }}| j}n|}d| }d}tj|||d�| _| j�& t d|› d�¡� ||| ƒ}W d   ƒ n1 s–w   Y  W d   ƒ n1 s¥w   Y  t ¡  d|kr»d| j	kr»t|ƒ}|S )N)r%   rA  r%   rA  T)r$   zsearching for local datasources)zinit-networkz!searching for network datasourcesrB  zrunning modules for %sr”  z	single/%szrunning single module %szrunning 'cloud-init %s'F)Úreporting_enabledzcloud-init stage: 'ú')r:   r   Úsetup_basic_loggingr]   r…   r†   rÆ   r   Úattach_handlersrƒ   rr  rµ   rS   Úreportr   ÚReportEventStackrÚ   r   ÚTimedr   Úflush_eventsr\   )rU   rS   rj  ÚrnameÚ	report_onÚrdescÚretvalr3   r3   r4   rÀ  G  sR   
ÿ

þþÿÿ€ÿrÀ  Ú__main__)r(   rN   )ri   )TN)Zr­  rT  rE   r,   r/   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”   ÚReasonr>   rZ   r;  rQ  rN  Ú__name__r9   r0   r;   r7   Útimedr]   rM   rX   rd   rh   rr   Úintr£   r²   r¶   ÚbytesÚboolrÌ   Ú
DataSourcer×   r)  r  r8  r@  rr  rô   r
  r  r„  r×  rŒ  rÀ  Úexitr¬  r3   r3   r3   r4   Ú<module>   s¦   ÿýü



	 
mÿ

þ;ÿ

þ% y2AF 
 ~;
Eÿ