
Â�Â­Â�Â­Â�Â­Â�Â­Â�Â­Â�Â­Â�Â­Â�Â­Â�Â­Â�Â­Â�Â­Â�Â­Â�Â­Â�Â­Â�Â­Â�Â­Â�Â­Â�Â­
<!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	ƒr†t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ƒrÊi }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ƒ\}}	|sŒtjd||	ƒ q
tj
|d	 ƒ}
t|
|ƒ |dkr¶|
jd
 }|jt|
|||d�ƒ q
W |S )z¾Convert 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 �]\}}}}yÎtjd|||ƒ |j|ƒ d|› �}	d|	|f }
t|	|
| jd�}|| j||dœ}|�p t|j	ƒ}|j
}t|ƒdkr¸tjddd	� |jd
tiƒ |j|	|j	||d�\}}|ràd|	 |_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 |rÞtjddj|ƒƒ |rötjddj|ƒ|ƒ |�rtjddj|ƒƒ | j|	ƒS )	zóRuns 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"   
