o
    w7e/                     @   s   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 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 eeZd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)log)
type_utilsutil)ALL_DISTROS)ConfigMerger)ReportEventStack)FREQUENCIES)Initcc_c                   @   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_name'   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_module3   s$   


r2   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)r3   r4   activate_by_schema_keys_keysr   r   r   
_is_activeI   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)r>   	cfg_files_cached_cfgr   reporter)selfr>   rA   rC   r   r   r   __init__U   s   
zModules.__init__r5   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)	rB   r   r>   rF   rG   rA   r4   copydeepcopy)rD   mergerr   r   r   r4   b   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.
        r1      r      freqr!   NargsFr   Tr   z3Failed to read '%s' item in config, unknown type %s)r4   r8   
isinstancer   appendr&   listr%   dict	TypeErrorr   obj_name)rD   r   module_listcfg_modsitemcontentsvalidr   r   r   _read_modulesp   sX   





zModules._read_modulesc              	   C   s   g }|D ]a}|d }| d}| dpg }t|}|sq|r,|tvr,td|| d}t|dtt	gdg\}}	|sEtd||	 qt
|d	 }
t|
| |du rZ|
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.
        r1   rO   rP   z6Config specified module %s has an unknown frequency %sN handlez,Could not find module named %s (searched %s)r   r   )r   r   r   r   )r8   r*   r   LOGwarningr	   find_moduler   rV   r   import_moduler2   r+   rR   r   )rD   raw_modsmostly_modsraw_modraw_namerO   r   mod_namemod_locslooked_locsr1   r   r   r   _fixup_modules   sL   


zModules._fixup_modulesrd   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   r?   parent)r   r4   cloudrP      z%Config modules with a `log` parameterz23.2)
deprecateddeprecated_versionr
   )rO   z%s ran successfullyz%s previously ranzRunning module %s (%s) failed)r>   cloudifyr_   debugrR   r   rC   r4   r   r^   
parametersr%   r   	deprecateupdaterunmessage	Exceptionlogexc)rD   rd   ccfailures	which_ranr1   r   rO   rP   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)r1   rP   rO   )rj   r   )rD   rg   rP   rO   	mod_to_berc   rd   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_modulesNr,   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\   rj   r>   distror   r4   r8   r+   r<   rR   r   r_   infojoinr   )rD   section_namerc   rd   distro_nameskippedforced
overriddeninapplicable_modsactive_modsr3   r1   r   _freq_argsworked_distrosr   r   r   run_section  sJ   
	






zModules.run_section)NN)r   r   r   r   rE   propertyr   Configr4   r   r   r\   r   rj   r   r   r   r   r   r   r   r=   T   s    ;0
2r=   )&rJ   inspectr   typesr   typingr   r   r   r   	cloudinitr   r	   r
   loggingr   r   cloudinit.distrosr   cloudinit.helpersr   cloudinit.reporting.eventsr   cloudinit.settingsr   cloudinit.stagesr   	getLoggerr   r_   r(   r   r*   r2   rT   boolr<   r=   r   r   r   r   <module>   s&   
