o
    f3                     @   s   d dl Z d dl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mZmZ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 eeZdZddgZddiZ G dd de	Z!dd Z"dd Z#de!de$de%fddZ&G dd dZ'dS )    N)	signature)
ModuleType)DictList
NamedTupleOptional)configimporter	lifecycle
type_utilsutil)ALL_DISTROS)ConfigMerger)ReportEventStack)FREQUENCIES)Initcc_cc_migratorcc_rightscale_userdatacc_ubuntu_advantagecc_ubuntu_proc                   @   s2   e Zd ZU eed< eed< eed< ee ed< dS )ModuleDetailsmodulename	frequencyrun_argsN)__name__
__module____qualname__r   __annotations__strr    r!   r!   :/usr/lib/python3/dist-packages/cloudinit/config/modules.pyr   +   s
   
 r   c                 C   sX   |  dd}| dr|dt|d  }| }|sd S |ts*dt|f }|S )N-_z.pyr      z%s%s)replacelowerendswithlenstrip
startswith
MOD_PREFIX)r   
canon_namer!   r!   r"   form_module_name2   s   
r.   c                 C   s   t | drd| jvsd| jvrtd|  d| d| jd tvr2td|  d| d| jd  dt | d	rBtd|  d| d
d S )Nmetar   distroszModule 'z' with name 'z4' MUST have a 'meta' attribute of type 'MetaSchema'.z' has an invalid frequency .schemazX' has a JSON 'schema' attribute defined. Please define schema in cloud-init-schema,json.)hasattrr/   
ValueErrorr   )modr   r!   r!   r"   validate_module>   s$   


r6   module_detailscfgreturnc                 C   s2   t | jjdi }|sdS || sdS dS )Nactivate_by_schema_keysTF)	frozensetr   r/   getintersectionkeys)r7   r8   activate_by_schema_keys_keysr!   r!   r"   
_is_activeT   s   r@   c                   @   sx   e Zd ZddefddZedejfddZde	e
 fdd	Zde	e fd
dZde	e fddZdddZdd ZdS )ModulesNinitc                 C   s2   || _ || _d | _|d u rtdddd}|| _d S )Nzmodule-reporterzmodule-descF)r   descriptionreporting_enabled)rB   	cfg_files_cached_cfgr   reporter)selfrB   rE   rG   r!   r!   r"   __init__`   s   
zModules.__init__r9   c                 C   s<   | j d u rt| jj| jj| j| jjd}|j| _ t| j S )N)paths
datasourceadditional_fnsbase_cfg)	rF   r   rB   rJ   rK   rE   r8   copydeepcopy)rH   mergerr!   r!   r"   r8   m   s   
zModules.cfgc                 C   sP  g }|| j vr	|S | j |}|s|S |D ]}|sqt|tr)|d| i qt|trbi }t|dkr>|d  |d< t|dkrL|d  |d< t|dkrZ|dd |d< |ra|| qt|tri }d	}d
|v ry|d
  |d< d}d|v r|d  |d< d|v r|d pg |d< |r|r|| qt	d|t
|f |S )aU  Read the modules from the config file given the specified name.

        Returns a list of module definitions. E.g.,
        [
            {
                "mod": "bootcmd",
                "freq": "always",
                "args": "some_arg",
            }
        ]

        Note that in the default case, only "mod" will be set.
        r5      r      freqr%   NargsFr   Tr   z3Failed to read '%s' item in config, unknown type %s)r8   r<   
isinstancer    appendr*   listr)   dict	TypeErrorr   obj_name)rH   r   module_listcfg_modsitemcontentsvalidr!   r!   r"   _read_modules{   sX   





zModules._read_modulesc              	   C   s*  g }|D ]}|d }| d}| dpg }t|}|sq|r2|tvr2tjd| d| dd d}|tv rJtjd	| d
t|  ddd t| }t|dt	t
gdg\}}	|sr|tv rjtd|dd  ntd||	 qt|d }
t|
| |du r|
jd }|t|
|||d q|S )zConvert list of returned from _read_modules() into new format.

        Invalid modules and arguments are ignored.
        Also ensures that the module has the required meta fields.
        r5   rS   rT   zConfig specified module z has an unknown frequency z22.1
deprecateddeprecated_versionNzModule has been renamed from z to z/. Update any references in /etc/cloud/cloud.cfgz24.1 handlez\Module `%s` has been removed from cloud-init. It may be removed from `/etc/cloud/cloud.cfg`.r%   z,Could not find module named %s (searched %s)r   r   )r   r   r   r   )r<   r.   r   r
   	deprecateRENAMED_MODULESr	   find_moduler   rZ   r   REMOVED_MODULESLOGinfowarningimport_moduler6   r/   rV   r   )rH   raw_modsmostly_modsraw_modraw_namerS   r   mod_namemod_locslooked_locsr5   r!   r!   r"   _fixup_modules   sh   
	


zModules._fixup_modulesro   c                 C   sP  | j  }g }g }|D ]\}}}}zqtd||| || d| }	d|	|f }
t|	|
| jd}|| j||d}|= t|j	}|j
}t|dkrYtjddd	 |d
ti |j|	|j	||d\}}|rmd|	 |_nd|	 |_W d    n1 s|w   Y  W q ty } zttd|| |||f W Y d }~qd }~ww ||fS )Nz(Running module %s (%s) with frequency %szconfig-zrunning %s with frequency %s)r   rC   parent)r   r8   cloudrT      z%Config modules with a `log` parameterz23.2ra   log)rS   z%s ran successfullyz%s previously ranzRunning module %s (%s) failed)rB   cloudifyrj   debugrV   r   rG   r8   r   re   
parametersr)   r
   rf   updaterunmessage	Exceptionr   logexc)rH   ro   ccfailures	which_ranr5   r   rS   rT   run_namedescmyrep	func_argsfunc_signaturefunc_paramsran_rer!   r!   r"   _run_modules   sV   






zModules._run_modulesc                 C   s&   |||d}|g}|  |}| |S )N)r5   rT   rS   )ru   r   )rH   rr   rT   rS   	mod_to_bern   ro   r!   r!   r"   
run_single+  s   

zModules.run_singlec                 C   s  |  |}| |}| jjj}g }g }| jdg }g }g }	|D ]A}
|
\}}}}|du r-q |jd }t|
| js>|	| q |rX|t
gkrX||vrX||vrS|	| q |	| |		||||g q |rmtdd| |rytdd|| |rtdd| | |	S )	zRuns all modules in the given section.

        section_name - One of the modules lists as defined in
          /etc/cloud/cloud.cfg. One of:
         - cloud_init_modules
         - cloud_config_modules
         - cloud_final_modules
        unverified_modulesNr0   z?Skipping modules '%s' because no applicable config is provided.,zSkipping modules '%s' because they are not verified on distro '%s'.  To run anyway, add them to 'unverified_modules' in config.z running unverified_modules: '%s'z, )r`   ru   rB   distror   r8   r<   r/   r@   rV   r   rj   rk   joinr   )rH   section_namern   ro   distro_nameskippedforced
overriddeninapplicable_modsactive_modsr7   r5   r   _freq_argsworked_distrosr!   r!   r"   run_section7  sJ   
	






zModules.run_section)NN)r   r   r   r   rI   propertyr   Configr8   r   r   r`   r   ru   r   r   r   r!   r!   r!   r"   rA   _   s    ;C
2rA   )(rN   logginginspectr   typesr   typingr   r   r   r   	cloudinitr   r	   r
   r   r   cloudinit.distrosr   cloudinit.helpersr   cloudinit.reporting.eventsr   cloudinit.settingsr   cloudinit.stagesr   	getLoggerr   rj   r,   ri   rg   r   r.   r6   rX   boolr@   rA   r!   r!   r!   r"   <module>   s.   
