HEX
Server: Apache/2.4.52 (Ubuntu)
System: Linux ip-10-0-8-47 6.8.0-1021-aws #23~22.04.1-Ubuntu SMP Tue Dec 10 16:31:58 UTC 2024 aarch64
User: ubuntu (1000)
PHP: 8.1.2-1ubuntu2.22
Disabled: NONE
Upload Files
File: //lib/python3/dist-packages/cloudinit/__pycache__/helpers.cpython-310.pyc
o

�Ad�A�@sddlZddlZddlmZmZmZddlmZddlmZddl	m
Zddl	mZm
Z
mZddlmZmZmZmZe�e�ZGdd	�d	e�ZGd
d�d�ZGdd
�d
�ZGdd�d�Zdd�ZGdd�d�ZGdd�d�ZGdd�d�ZGdd�d�Z Gdd�dej!�Z"Gdd�de�Z#dS)�N)�
NoOptionError�NoSectionError�RawConfigParser)�StringIO)�time)�log)�persistence�
type_utils�util)�CFG_ENV_NAME�
PER_ALWAYS�PER_INSTANCE�PER_ONCEc@�eZdZdS)�LockFailureN��__name__�
__module__�__qualname__�rr�3/usr/lib/python3/dist-packages/cloudinit/helpers.pyr�rc@r)�	DummyLockNrrrrrrrrc@s<eZdZdd�Zejd
dd��Zdd�Zdd	�Zd
d�Z	dS)�DummySemaphorescC�dS�Nr��selfrrr�__init__!�zDummySemaphores.__init__Fccs�t�VdSr)r)r�_name�_freq�_clear_on_failrrr�lock$s�zDummySemaphores.lockcC�dS�NFr�rr r!rrr�has_run(rzDummySemaphores.has_runcCr$)NTrr&rrr�clear+rzDummySemaphores.clearcCrrrrrrr�	clear_all.rzDummySemaphores.clear_allN�F)
rrrr�
contextlib�contextmanagerr#r'r(r)rrrrr src@seZdZdd�Zdd�ZdS)�FileLockcC�
||_dSr)�fn)rr/rrrr3�
zFileLock.__init__cCsdt�|�|jfS)Nz<%s using file %r>)r	�obj_namer/rrrr�__str__6szFileLock.__str__N)rrrrr2rrrrr-2sr-cCs|�dd�S)N�-�_)�replace)�namerrr�canon_sem_name:sr7c@sLeZdZdd�Zejddd��Zdd�Zdd	�Zd
d�Z	dd
�Z
dd�ZdS)�FileSemaphorescCr.r)�sem_path)rr9rrrr?r0zFileSemaphores.__init__Fccs@�t|�}z
|�||�VWdSty|r|�||��wr)r7�_acquire�	Exceptionr()rr6�freq�
clear_on_failrrrr#Bs��zFileSemaphores.lockc	CsLt|�}|�||�}zt�|�WdSttfy%t�td|�YdSw)NzFailed deleting semaphore %sFT)r7�	_get_pathr
�del_file�IOError�OSError�logexc�LOG)rr6r<�sem_filerrrr(Ls��zFileSemaphores.clearc	Cs<z	t�|j�WdSttfyt�td|j�YdSw)Nz&Failed deleting semaphore directory %s)r
�del_dirr9r@rArBrCrrrrr)Vs
��zFileSemaphores.clear_allc	Csl|�||�rdS|�||�}dt��t�f}zt�||�Wt|�Sttfy5t�	t
d|�YdSw)Nz%s: %s
z Failed writing semaphore file %s)r'r>�os�getpidrr
�
write_filer@rArBrCr-)rr6r<rD�contentsrrrr:^s��zFileSemaphores._acquirecCsf|r|tkrdSt|�}|�||�}tj�|�rdS||kr1tj�|�||��r1t�d||�dSdS)NFTz�%s has run without canonicalized name [%s].
likely the migrator has not yet run. It will run next boot.
run manually with: cloud-init single --name=migrator)rr7r>rF�path�existsrC�warning)rr6r<�cnamerDrrrr'ns�zFileSemaphores.has_runcCs6|j}|r	|tkrtj�||�Stj�|d||f�S)Nz%s.%s)r9r
rFrJ�join)rr6r<r9rrrr>�szFileSemaphores._get_pathNr*)rrrrr+r,r#r(r)r:r'r>rrrrr8>s	
r8c@s&eZdZdd�Zdd�Zd	dd�ZdS)
�RunnerscCs||_i|_dSr)�paths�sems)rrPrrrr��
zRunners.__init__cCsh|tks|sdSd}|tkr|j�d�}n
|tkr|j�d�}|s#dS||jvr/t|�|j|<|j|S)N�sem)rr
rP�	get_ipathr�	get_cpathrQr8)rr<r9rrr�_get_sem�s

zRunners._get_semNFc	Cs�|�|�}|s
t�}|sg}|�||�rt�d||�dS|�|||��,}|s-td|��t�d||�t|t�rA|di|��}n||�}d|fWd�S1sSwYdS)Nz%s already ran (freq=%s))FNzFailed to acquire lock for %szRunning %s using lock (%s)Tr)	rVrr'rC�debugr#r�
isinstance�dict)	rr6�functor�argsr<r=rS�lk�resultsrrr�run�s"

$�zRunners.runr%)rrrrrVr^rrrrrO�srOc@sReZdZ					ddd�Zdd�Zdd�Zd	d
�Zdefdd
�Ze	dd��Z
dS)�ConfigMergerNTcCs(||_||_||_||_||_d|_dSr)�_paths�_ds�_fns�	_base_cfg�_include_vendor�_cfg)rrP�
datasource�additional_fns�base_cfg�include_vendorrrrr�s
zConfigMerger.__init__cCshg}|jr2z|j��}|rt|t�r|�|�W|SW|SW|Sty1t�td|j�Y|Sw|S)Nz2Failed loading of datasource config object from %s)	ra�get_config_objrXrY�appendr;r
rBrC)r�d_cfgs�ds_cfgrrr�_get_datasource_configs�s(
�����z$ConfigMerger._get_datasource_configscCsVg}ttjvr)tjt}z|�t�|��W|Sty(t�td|�Y|Sw|S)Nz%Failed loading of env. config from %s)	rrF�environrkr
�	read_confr;rBrC)r�e_cfgs�e_fnrrr�_get_env_configs�s

��zConfigMerger._get_env_configsc	Cs�g}|js|Sdg}|jr|�d�|�d�|D]9}|j�|�}|rRtj�|�rRz
|�t�|��Wqt	yBt
�d|�YqtyQt�
t
d|�Yqwq|S)N�cloud_config�vendor2_cloud_config�vendor_cloud_configz5Skipped loading cloud-config from %s due to non-root.z&Failed loading of cloud-config from %s)r`rdrk�
get_ipath_currFrJ�isfiler
rp�PermissionErrorrCrWr;rB)r�i_cfgs�cc_paths�cc_p�cc_fnrrr�_get_instance_configs�s0

����z"ConfigMerger._get_instance_configs�returnc	Cs�g}|jr%|jD]}z
|�t�|��Wqty$t�td|�Yqw|�|���|�|�	��|�|�
��|jrC|�|j�t�|�S)Nz'Failed loading of configuration from %s)
rbrkr
rpr;rBrC�extendrsr~rnrc�
mergemanydict)r�cfgs�c_fnrrr�	_read_cfgs 
��
zConfigMerger._read_cfgcCs|jdur
|��|_|jSr)rer�rrrr�cfgs

zConfigMerger.cfg)NNNNT)rrrrrnrsr~rYr��propertyr�rrrrr_�s
�
 r_c@sFeZdZdd�Zdd�Zdd�Zdd�Zddd�Zd
d�Zdd�Z	dS)�ContentHandlerscCsi|_g|_dSr)�
registered�initializedrrrrr(rRzContentHandlers.__init__cC�
|�|�Sr)�
is_registered)r�itemrrr�__contains__,r0zContentHandlers.__contains__cCr�r)�_get_handler)r�keyrrr�__getitem__/r0zContentHandlers.__getitem__cCs
||jvSr�r��r�content_typerrrr�2r0zContentHandlers.is_registeredFTcCsjt�}|��D]}|r|�|�q|�|�s|�|�q|D]}||j|<q|r3||jvr3|j�|�|Sr)�set�
list_types�addr�r�r�rk)r�modr��	overwrite�types�trrr�register5s

�zContentHandlers.registercCs
|j|Srr�r�rrrr�Cr0zContentHandlers._get_handlercCst|j���Sr)�listr��itemsrrrrr�F�zContentHandlers.itemsN)FT)
rrrrr�r�r�r�r�r�rrrrr�'s
r�c@steZdZdZddefdd�Zdeddfdd	�Zdd
d�Zddd
�Z	ddd�Z
ddd�Zddefdd�Z
ddd�ZdS)�Paths�N�	path_cfgscCs�||_|�dd�|_|�dd�|_tj�|jd�|_tj�|jd�|_tj�|jd�|_	|�dd	�}tj�|d
�|_
idd�dd
�dd�dd�dd�dd�dd�dd�dd�dd�dd�dd�dd�d d!�d"d#�d$d%�d&d'�d(d)d*d+d,��|_||_dS)-N�	cloud_dirz/var/lib/cloud�run_dirz/run/cloud-init�instancez
boot-finished�seed�
templates_dirz/etc/cloud/templates/z%s.tmpl�	boothooksrtzcloud-config.txt�data�handlers�
instance_data�instance-data.json�instance_data_sensitive�instance-data-sensitive.json�instance_idz.instance-id�manual_clean_markerzmanual-clean�obj_pklzobj.pkl�scriptsrS�userdatazuser-data.txt.i�userdata_rawz
user-data.txt�
vendordatazvendor-data.txt.i�vendordata2zvendor-data2.txt.i�vendordata2_rawzvendor-data2.txt�vendordata_rawzvendor-data.txtzvendor2-cloud-config.txtzvendor-cloud-config.txtzscripts/vendor�warnings)rurv�vendor_scriptsr�)
r��getr�r�rFrJrN�
instance_link�
boot_finished�seed_dir�template_tpl�lookupsrf)rr��ds�template_dirrrrrMsj���������
����������
�
zPaths.__init__�ci_pkl_versionrcCsNt|d�st|j|jd�j|_d|jvrd|jd<d|jvr%d|jd<dSdS)z(Perform deserialization fixes for Paths.r�)r�r�r�r�r�r�N)�hasattrr�r�rfr�r�)rr�rrr�	_unpickle{s
��


���zPaths._unpicklecC�|�|j|�Sr)r>r��rr6rrrrw�r�zPaths.get_ipath_curcCr�r)r>r�r�rrrrU�r�zPaths.get_cpathcCsf|jsdS|j��}|durdSt|��tjd�}tj�|jd|�}|j	�
|�}|r1tj�||�}|S)Nr4�	instances)rf�get_instance_id�strr5rF�seprJrNr�r�r�)rr6�iid�
path_safe_iid�ipath�add_onrrr�
_get_ipath�s
zPaths._get_ipathcCs |�|�}|st�d�dS|S)Nz?No per instance data available, is there an datasource/iid set?)r�rCrL)rr6r�rrrrT�s
�zPaths.get_ipath�basecCs |dur|Stj�||j|�Sr)rFrJrNr�)rr�r6rrrr>�szPaths._get_pathcCr�r)r>r�r�rrr�get_runpath�r�zPaths.get_runpathr)rrr�_ci_pkl_versionrYr�intr�rwrUr�rTr�r>r�rrrrr�Js.



r�c@sXeZdZdZdZdZdZdd�Zddd�Zd	d
�Z	dd�Z
d
d�Zdd�Zddd�Z
dS)�DefaultingConfigParserrgFNcCsB|j}z
t�|||�}W|StyY|Sty Y|Swr)�DEF_BASErr�rr�r�section�option�valuerrrr��s���zDefaultingConfigParser.getcCs4|�|�s|��dkr|�|�t�||||�dS)N�default)�has_section�lower�add_sectionrr�r�rrrr��s
zDefaultingConfigParser.setcCs"|�||�rt�|||�dSdSr)�
has_optionr�
remove_option�rr�r�rrrr��s�z$DefaultingConfigParser.remove_optioncC� |�||�s	|jSt�|||�Sr)r��DEF_BOOLEANr�
getbooleanr�rrrr���z!DefaultingConfigParser.getbooleancCr�r)r��	DEF_FLOATr�getfloatr�rrrr��r�zDefaultingConfigParser.getfloatcCr�r)r��DEF_INTr�getintr�rrrr��r�zDefaultingConfigParser.getintcCs<d}t�}|�|�|��|��}|rd�||dg�}|S)N��
)r�write�flush�getvaluerN)r�headerrI�outputstreamrrr�	stringify�s
z DefaultingConfigParser.stringifyr)rrrr�r�r�r�r�r�r�r�r�r�r�rrrrr��s

r�)$r+rF�configparserrrr�iorr�	cloudinitr�loggingrr	r
�cloudinit.settingsrrr
r�	getLoggerrrCr;rrrr-r7r8rOr_r��CloudInitPickleMixinr�r�rrrr�<module>s(

R-j#|