
­­­­­­­­­­­­­­­­­­
<!DOCTYPE html>
<html>
3
\me.                 @   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 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j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
type_utilsutil)ALL_DISTROS)ConfigMerger)ReportEventStack)FREQUENCIES)InitZcc_c               @   s"   e Zd ZU eeeee 	dS )ModuleDetailsN)
__name__
__module____qualname__r   modulestrname	frequencyr   run_args r   r   /usr/lib/python3.6/modules.pyr      s   
r   c             C   sX   | j dd}|j jdr.|dt|d  }|j }|s>d S |j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 s d| jks d| jkr6td|  d| d| jd tkrft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jdi }|sdS |j|j s.dS dS )NZactivate_by_schema_keysTF)	frozensetr   r'   getintersectionkeys)r.   r/   Zactivate_by_schema_keys_keysr   r   r   
_is_activeH   s    r5   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)r7   	cfg_files_cached_cfgr   reporter)selfr7   r9   r;   r   r   r   __init__T   s    zModules.__init__)r0   c             C   s<   | j d kr0t| jj| jj| j| jjd}|j| _ tj| j S )N)paths
datasourceZadditional_fnsZbase_cfg)	r:   r   r7   r>   r?   r9   r/   copydeepcopy)r<   Zmergerr   r   r   r/   a   s    
zModules.cfgc             C   sh  g }|| j kr|S | j j|}|s&|S x:|D ]0}|s:q.t|trX|jd|j i q.t|tri }t|dkr|d j |d< t|dkr|d j |d< t|dkr|dd |d< |r|j| q.t|trJi }d	}d
|k r|d
 j |d< d}d|kr|d j |d< d|kr2|d p,g |d< |r`|r`|j| q.t	d|t
j|f q.W |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)r/   r2   
isinstancer   appendr#   listr"   dict	TypeErrorr
   obj_name)r<   r   Zmodule_listZcfg_modsitemcontentsZvalidr   r   r   _read_moduleso   sL    





zModules._read_modulesc             C   s   g }x|D ]}|d }|j d}|j dp,g }t|}|s<q
|rZ|tkrZtjd|| d}tj|dtjt	gdg\}}	|stjd||	 q
tj
|d	 }
t|
| |dkr|
jd
 }|jt|
|||d q
W |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,   rD   rE   z6Config specified module %s has an unknown frequency %sN handlez,Could not find module named %s (searched %s)r   r   )r   r   r   r   )r2   r&   r   LOGZwarningr	   find_moduler
   rK   r   import_moduler-   r'   rG   r   )r<   raw_modsmostly_modsZraw_modZraw_namerD   r   mod_nameZmod_locsZlooked_locsr,   r   r   r   _fixup_modules   sB    



zModules._fixup_modules)rU   c             C   sF  | j j }g }g }x(|D ]\}}}}ytjd||| |j| d| }	d|	|f }
t|	|
| jd}|| j||d}|p t|j	}|j
}t|dkrtjddd	 |jd
ti |j|	|j	||d\}}|rd|	 |_n
d|	 |_W d Q R X W q tk
r8 } z$tjtd|| |j||f W Y d d }~X qX qW ||fS )Nz(Running module %s (%s) with frequency %szconfig-zrunning %s with frequency %s)r   r8   parent)r   r/   ZcloudrE      z%Config modules with a `log` parameterz23.2)Z
deprecatedZdeprecated_versionlog)rD   z%s ran successfullyz%s previously ranzRunning module %s (%s) failed)r7   ZcloudifyrQ   debugrG   r   r;   r/   r   rP   Z
parametersr"   r   Z	deprecateupdateZrunmessage	ExceptionZlogexc)r<   rU   ZccZfailuresZ	which_ranr,   r   rD   rE   Zrun_nameZdescZmyrepZ	func_argsZfunc_signatureZfunc_paramsZranZ_rer   r   r   _run_modules   sB    



$zModules._run_modulesc             C   s&   |||d}|g}| j |}| j|S )N)r,   rE   rD   )rW   r`   )r<   rV   rE   rD   Z	mod_to_berT   rU   r   r   r   
run_single  s    
zModules.run_singlec             C   s  | j |}| j|}| jjj}g }g }| jjdg }g }g }	x|D ]}
|
\}}}}|dkr\qB|jd }t|
| js~|j	| qB|r|t
gkr||kr||kr|j	| qB|j	| |	j	||||g qBW |rtjddj| |rtjddj|| |rtjddj| | j|	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, )rN   rW   r7   Zdistror   r/   r2   r'   r5   rG   r   rQ   infojoinr`   )r<   Zsection_namerT   rU   Zdistro_nameZskippedZforcedZ
overriddenZinapplicable_modsZactive_modsr.   r,   r   Z_freqZ_argsZworked_distrosr   r   r   run_section  sF    	







zModules.run_section)NN)NN)r   r   r   r   r=   propertyr   ZConfigr/   r   r   rN   r   rW   r`   ra   re   r   r   r   r   r6   S   s   ;02
r6   )%r@   Zlogginginspectr   typesr   Ztypingr   r   r   r   Z	cloudinitr   r	   r
   r   Zcloudinit.distrosr   Zcloudinit.helpersr   Zcloudinit.reporting.eventsr   Zcloudinit.settingsr   Zcloudinit.stagesr   Z	getLoggerr   rQ   r%   r   r&   r-   rI   boolr5   r6   r   r   r   r   <module>	   s"   
