B
    `+                 @   s   d dl mZmZmZ eZd dl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 d dlmZ d d	lmZ e ZG d
d deZdS )    )absolute_importdivisionprint_functionN)AnsibleError)to_text)boolean)AnsibleUnicode)
ActionBase)become_loader)Displayc                   sL   e Zd ZdZdd Zdd ZdddZd	d
 Zdd Zd fdd	Z	  Z
S )ActionModulei  c             C   s*   t d | jj}| jd||||d}|S )Nz'win_updates: running win_updates modulezansible.windows.win_updates)module_namemodule_args	task_vars
wrap_asyncuse_task)displayvvv_task	async_val_execute_module_with_become)selfr   r   r   r   result r   m/home/dcms/DCMS/lib/python3.7/site-packages/ansible_collections/ansible/windows/plugins/action/win_updates.py_run_win_updates   s    
zActionModule._run_win_updatesc          
   C   s   t d d|i}| jd||d}|ddr:t|d |rJt d nxt d	 d
}d|i}y,| jd||d|d}t dt|  W n4 tk
r }	 zt 	dt
|	  W d d }	~	X Y nX t d | d|}
|
ddrt|
d d S )Nz7win_updates: rebooting remote host after update installreboot_timeoutzansible.windows.win_reboot)r   failedFmsgzMwin_updates: skipping WUA is not busy check as use_scheduled_task=True is setz<win_updates: checking WUA is not busy with win_shell commandzO(New-Object -ComObject Microsoft.Update.Session).CreateUpdateInstaller().IsBusyZ_raw_paramszansible.windows.win_shell)r   r   r   r   r   z#win_updates: shell wait results: %szNwin_updates: Fatal error when running shell command, attempting to recover: %sz4win_updates: ensure the connection is up and runningZwait_for_connection)r   r   _run_action_plugingetr   r   jsondumps	Exceptiondebugr   )r   r   r   r   Zreboot_argsZreboot_resultcommandZshell_module_argsZshell_resultexcZwait_for_resultr   r   r   _reboot_server   s2    


zActionModule._reboot_serverNc          	   C   sj   | j  }i |_|d k	r8x| D ]\}}||j|< q"W | jjj||| j| j| j	| j
| jd}|j|dS )N)Ztask
connectionZplay_contextloaderZtemplarZshared_loader_obj)r   )r   copyargsitemsZ_shared_loader_objZaction_loaderr    _connection_play_contextZ_loaderZ_templarrun)r   Zplugin_namer   r   Znew_taskkeyvalueactionr   r   r   r   O   s    

zActionModule._run_action_pluginc             C   s   |  }|| |S )N)r*   update)r   originalnewZdict_varr   r   r   _merge_dictd   s    
zActionModule._merge_dictc       	   	   C   sf   | j j}zJ|s>|d kr>td}|jdd dd | j | | j||||d}W d | j | X |S )NZrunasZSYSTEM)Zbecome_userZbecome_pass)direct)r   r   r   r   )r-   becomer
   r    Zset_optionsZset_become_pluginZ_execute_module)	r   r   r   r   r   r   Zorig_becomer8   Z
module_resr   r   r   r   i   s    
z(ActionModule._execute_module_with_becomec          
      s  d| _ d| _tt| ||}~| jjdd}| jjdd}| jjd| j}t	| jjdddd}|d	krd|d
< d|d< |S yt	|}W n: t
k
r } zd|d
< dt| |d< |S d }~X Y nX t|tsd|d
< d|d< |S |r| jjdkrd|d
< d|d< |S | jj }	|	dd  |	dd  | |	||}|d
d}
d| krn| jjdkst|
rd|d
< |S |dd}|dt }|dt }|dd}|dd}|rd|dkrd| jjsdd}x|d dks|d dks|d dkrbtdt|  |d
dr<|r6P d}nd}d }|dddkrZd}|ddr|d krvd}yd}| ||| W nB tk
r } z"d|d
< d|t|f |d< P W d d }~X Y nX |dd  | |	||}|d
dr|S |dt }|dt }| ||}| ||}||dd7 }||dd7 }|d rd}qW | jjdkr||d< ||d< ||d< ||d< ||d< |S )NTstate	installedrebootFr   Zuse_scheduled_task)strict)r:   Zsearched
downloadedr   z6state must be either installed, searched or downloadedr   z$cannot parse reboot as a boolean: %sz!reboot_timeout must be an integerr   z4async is not supported for this task when reboot=yesupdateschangedfiltered_updatesfound_update_countinstalled_update_countZreboot_requiredz?win_updates: check win_updates results for automatic reboot: %s z9A reboot is required before more updates can be installedz8reboot was required before more updates can be installedz.reboot was required to finalise update installz(Failed to reboot remote host when %s: %s)Z_supports_check_modeZ_supports_asyncsuperr   r/   r   r+   r    DEFAULT_REBOOT_TIMEOUTr   	TypeErrorr   
isinstanceintr   r*   popr   keysdictr.   Z
check_moder   r   r!   r"   r'   r   r6   )r   tmpr   r   r9   r;   r   r   r&   Znew_module_argsr   r?   r>   r@   rA   rB   Zpreviously_erroredZreboot_errorZresult_updatesZresult_filtered_updates)	__class__r   r   r/   {   s    

"




zActionModule.run)N)NN)__name__
__module____qualname__rE   r   r'   r   r6   r   r/   __classcell__r   r   )rM   r   r      s   
0
r   )
__future__r   r   r   type__metaclass__r!   Zansible.errorsr   Zansible.module_utils._textr   Z)ansible.module_utils.parsing.convert_boolr   Zansible.parsing.yaml.objectsr   Zansible.plugins.actionr	   Zansible.plugins.loaderr
   Zansible.utils.displayr   r   r   r   r   r   r   <module>   s   