U
    Ad-                     @   s   d dl 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)
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_module2   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)r/   r0   Zactivate_by_schema_keys_keysr   r   r   
_is_activeH   s    r6   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)r8   	cfg_files_cached_cfgr   reporter)selfr8   r:   r<   r   r   r   __init__T   s    zModules.__init__)r1   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   r8   r?   r@   r:   r0   copydeepcopy)r=   Zmergerr   r   r   r0   a   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)r0   r3   
isinstancer   appendr$   listr#   dict	TypeErrorr
   obj_name)r=   r   Zmodule_listZcfg_modsitemcontentsZvalidr   r   r   _read_moduleso   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-   rE   rF   z6Config specified module %s has an unknown frequency %sN handlez,Could not find module named %s (searched %s)r   r   )r   r   r   r   )r3   r'   r   LOGZwarningr   find_moduler
   rL   r   import_moduler.   r(   rH   r   )r=   raw_modsmostly_modsZraw_modZraw_namerE   r   mod_nameZmod_locsZlooked_locsr-   r   r   r   _fixup_modules   sP    
  

zModules._fixup_modules)rV   c                 C   s  | j  }g }g }|D ]\}}}}ztd||| || j|t|g}	|| d| }
d|
|f }t|
|| jd}|8 |j|
|j	|	|d\}}|rd|
 |_
n
d|
 |_
W 5 Q R X W q tk
r } 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   r9   parent)rE   z%s ran successfullyz%s previously ranzRunning module %s (%s) failed)r8   ZcloudifyrR   debugr0   rH   r   r<   runrQ   message	Exceptionr   Zlogexc)r=   rV   ZccZfailuresZ	which_ranr-   r   rE   rF   Z	func_argsrun_nameZdescZmyrepZranZ_rer   r   r   _run_modules   sD    
   

     
"zModules._run_modulesc                 C   s&   |||d}|g}|  |}| |S )N)r-   rF   rE   )rX   r`   )r=   rW   rF   rE   Z	mod_to_berU   rV   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, )rO   rX   r8   Zdistror   r0   r3   r(   r6   rH   r   rR   infojoinr`   )r=   Zsection_namerU   rV   Zdistro_nameZskippedZforcedZ
overriddenZinapplicable_modsZactive_modsr/   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Configr0   r   r   rO   r   rX   r`   ra   re   r   r   r   r   r7   S   s   ;0)
r7   )$rA   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   rR   r&   r   r'   r.   rJ   boolr6   r7   r   r   r   r   <module>	   s"   
