U
    Ld/                     @   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!ee"e#d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)InitZ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sTdt|f }|S )N-_z.pyr      z%s%s)replacelowerendswithlenstrip
startswith
MOD_PREFIX)r   Z
canon_namer   r   r   form_module_name'   s    
r(   c                 C   s   t | drd| jksd| jkr4td|  d| d| jd tkrdtd|  d| d| jd  dt | d	rt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 .ZschemazX' has a JSON 'schema' attribute defined. Please define schema in cloud-init-schema,json.)hasattrr)   
ValueErrorr   )modr   r   r   r   validate_module3   s"    
r/   )module_detailscfgreturnc                 C   s2   t | jjdi }|sdS || s.dS dS )NZactivate_by_schema_keysTF)	frozensetr   r)   getintersectionkeys)r0   r1   Zactivate_by_schema_keys_keysr   r   r   
_is_activeI   s    r7   c                   @   sx   e Zd ZdedddZeejdddZe	e
 ddd	Ze	e dd
dZe	e dddZdddZdd ZdS )ModulesN)initc                 C   s2   || _ || _d | _|d kr(tdddd}|| _d S )Nzmodule-reporterzmodule-descF)r   descriptionZreporting_enabled)r9   	cfg_files_cached_cfgr   reporter)selfr9   r;   r=   r   r   r   __init__U   s    zModules.__init__)r2   c                 C   s<   | j d kr0t| jj| jj| j| jjd}|j| _ t| j S )N)paths
datasourceZadditional_fnsZbase_cfg)	r<   r   r9   r@   rA   r;   r1   copydeepcopy)r>   Zmergerr   r   r   r1   b   s    
zModules.cfgc                 C   s`  g }|| j kr|S | j |}|s&|S |D ].}|s6q*t|trT|d| i q*t|tri }t|dkr~|d  |d< t|dkr|d  |d< t|dkr|dd |d< |r|| q*t|trDi }d	}d
|kr|d
  |d< d}d|kr|d  |d< d|kr,|d p&g |d< |rZ|rZ|| q*t	d|t
|f q*|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.
        r.      r      freqr    NargsFr   Tr   z3Failed to read '%s' item in config, unknown type %s)r1   r4   
isinstancer   appendr%   listr$   dict	TypeErrorr   obj_name)r>   r   Zmodule_listZcfg_modsitemcontentsZvalidr   r   r   _read_modulesp   sV    


 


zModules._read_modulesc              	   C   s   g }|D ]}|d }| d}| dp*g }t|}|s:q|rX|tkrXtd|| d}t|dtt	gdg\}}	|std||	 qt
|d	 }
t|
| |dk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.
        r.   rF   rG   z6Config specified module %s has an unknown frequency %sN handlez,Could not find module named %s (searched %s)r   r   )r   r   r   r   )r4   r(   r   LOGZwarningr	   find_moduler   rM   r   import_moduler/   r)   rI   r   )r>   raw_modsmostly_modsZraw_modZraw_namerF   r   mod_nameZmod_locsZlooked_locsr.   r   r   r   _fixup_modules   sP    
  

zModules._fixup_modules)rW   c                 C   s@  | j  }g }g }|D ]\}}}}ztd||| || d| }	d|	|f }
t|	|
| jd}|| j||d}|p t|j	}|j
}t|dkrtjddd	 |d
ti |j|	|j	||d\}}|rd|	 |_n
d|	 |_W 5 Q R X W q tk
r4 } z"ttd|| |||f W 5 d }~X Y qX q||fS )Nz(Running module %s (%s) with frequency %szconfig-zrunning %s with frequency %s)r   r:   parent)r   r1   ZcloudrG      z%Config modules with a `log` parameterz23.2)Z
deprecatedZdeprecated_versionr
   )rF   z%s ran successfullyz%s previously ranzRunning module %s (%s) failed)r9   ZcloudifyrS   debugrI   r   r=   r1   r   rR   Z
parametersr$   r   Z	deprecateupdaterunmessage	ExceptionZlogexc)r>   rW   ZccZfailuresZ	which_ranr.   r   rF   rG   run_nameZdescZmyrepZ	func_argsZfunc_signatureZfunc_paramsZranZ_rer   r   r   _run_modules   s\    
   

  
   
"zModules._run_modulesc                 C   s&   |||d}|g}|  |}| |S )N)r.   rG   rF   )rY   rc   )r>   rX   rG   rF   Z	mod_to_berV   rW   r   r   r   
run_single  s    
zModules.run_singlec                 C   s  |  |}| |}| jjj}g }g }| jdg }g }g }	|D ]}
|
\}}}}|dkrZq@|jd }t|
| js||	| q@|r|t
gkr||kr||kr|	| q@|	| |		||||g q@|rtdd| |rtdd|| |r
t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
        Z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, )rP   rY   r9   Zdistror   r1   r4   r)   r7   rI   r   rS   infojoinrc   )r>   Zsection_namerV   rW   Zdistro_nameZskippedZforcedZ
overriddenZinapplicable_modsZactive_modsr0   r.   r   Z_freqZ_argsZworked_distrosr   r   r   run_section  sJ    	






zModules.run_section)NN)NN)r   r   r   r   r?   propertyr   ZConfigr1   r   r   rP   r   rY   rc   rd   rh   r   r   r   r   r8   T   s   ;02
r8   )&rB   inspectr   typesr   typingr   r   r   r   Z	cloudinitr   r	   r
   Zloggingr   r   Zcloudinit.distrosr   Zcloudinit.helpersr   Zcloudinit.reporting.eventsr   Zcloudinit.settingsr   Zcloudinit.stagesr   Z	getLoggerr   rS   r'   r   r(   r/   rK   boolr7   r8   r   r   r   r   <module>	   s$   
