
    Vh4                         d dl mZmZmZ eZd dlZd dl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mZ  e       Zd
Z G d de      Zy)    )absolute_importdivisionprint_functionN)AnsibleError)to_text)PY3)urlsplit)ActionModule)Display)checksum
checksum_szANSIBLE_NETWORK_IMPORT_MODULES:c                   X     e Zd Zd fd	Zd Zd ZddZd Zd Zd Z	d Z
d	 Zd
 Z xZS )r
   c                    t        | d      xr | j                  }|r6| j                  j                  j	                  d      r	 | j                          |d   }| j                  |      }|rY| j                         \  }}t        j                  dj                  t        | j                  j                  |      |       t!        |dd       r| j#                  ||       t        j                  d	j                  t        | j                  j                  
      |       | j%                  |      }	t        j                  dj                  t              |       t        j'                  dj                  t        |	      |       nAd}t        j                  dj                  t        | j                  j                  
      |       |st(        t*        | [  |      }	|rl| j                  j                  j	                  d      rG	j	                  d      s6| j/                  |	|| j                  j                  j	                  d             	S # t        $ r }t        dt        |            cY d }~S d }~ww xY w)N_config_modulesrcT)failedmsgansible_hostz#{prefix} found {action}  at {fname})prefixactionfnameAnsibleModulez{prefix} running {module})r   modulez{prefix} completer   z{prefix} Result: {result})r   resultFz<{prefix} {module} doesn't support direct execution, disabled)	task_varsbackupr   backup_options)hasattrr   _taskargsget_handle_src_optionr   dictr   _check_dexec_eligibility_find_load_moduledisplayvvvvformatDEXEC_PREFIXr   getattr_patch_update_module_exec_modulevvvvvsuperr
   run_handle_backup_option)selftmpr   config_moduleexchostdexec_eligiblefilenamer   r   	__class__s             l/home/dcms/DCMS/lib/python3.12/site-packages/ansible_collections/ansible/netcommon/plugins/action/network.pyr0   zActionModule.run   s   &67OD<O<OTZZ__007;'') (66t< #557HfLL5<<'::,," = 
  v5))&)</66+DJJ4E4E 7  	 **62077|7LdS/66lSY6Z "'RYY+DJJ4E4E Z  	 <2Y2GFTZZ__00:6::hCW&&

##$45 o   ;4WS\::;s   I 	I5I0*I50I5c                 $   d }d }	 | j                   j                  j                  d|      }	 | j                  |j                  d      |      }|r"|j                  d      }|j                  d      }t        j                  dt        j                  t        j                                     }|s0| j                         }	t        j                  j                  |	d      }|s
|d	   d
|}t        j                  j                  ||      }
t        j                  j!                  |      st        j"                  |       d}t        j                  j!                  |
      rt%        |
      t'        |      k7  r)	 t)        |
d      5 }|j+                  |       d d d        d}|
|d<   ||d<   |j1                  d      \  |d<   |d<   |r|j                  d      sNt        j                  j3                  |d         |d<   t        j                  j5                  |d         d   |d<   y y # t        t        f$ r g }Y w xY w# t        $ r t        d      w xY w# 1 sw Y   xY w# t,        $ r#}d|d<   d|
dt/        |      |d<   Y d }~y d }~ww xY w)Nnon_config_lines
__backup__)contentsfiltersz)Failed while reading configuration backupr8   dir_pathz%Y-%m-%d@%H:%M:%Sr   inventory_hostnamez_config.FwTr   z$Could not write to destination file z: r   backup_pathchanged@datetimer   	shortname)_connectioncliconf
get_optionAttributeErrorKeyError_sanitize_contentspopr   r"   rG   strftime	localtime_get_working_pathospathjoinexistsmakedirsr   r   openwrite	Exceptionr   splitbasenamesplitext)r2   r   r   r   r8   rC   non_config_regexescontenttstampcwddestrD   output_filer5   s                 r:   r1   z"ActionModule._handle_backup_option\   sa   	$!%!1!1!9!9!D!DEWYb!c	L--L1;M . G %))*5H(,,Z8K2DNN499;4OP((*C'',,sH5K./H
 ww||K2ww~~k*KK$ww~~d#x~G9L'L	$_ /%%g./ G $}#y)/c):&vv>#5#5j#A!#!1!1&2G!HF:"$''"2"26-3H"I!"LF; $B] ) 	$!#	$  	LJKK	L2/ / #'x CL!u sL   &H' "H? 9I# II# 'H<;H<?II I# #	J,J

Jc                     | j                   j                         }| j                  j                   | j                  j                  j                  }|S N)_loaderget_basedirr    _role
_role_path)r2   ra   s     r:   rR   zActionModule._get_working_path   s>    ll&&(::'**""--C
    c                    | j                   j                  j                  d      }| j                         }t        j
                  j                  |      st        d      j                  r|}n;| j                  j                  |d|      }|s| j                  j                  ||      }t        j
                  j                  |      st        d      	 t        |d      5 }t        |j                               }d d d        |g}| j                   j(                  |j+                  | j                   j(                  j,                         t/        | j                   d      rH| j                   j0                  j3                         }	|	"|	D ]  }
|j+                  |
j,                          |j+                  t        j
                  j5                  |             || j6                  j8                  j:                  _        | j6                  j?                        | j                   j                  d<   y # 1 sw Y   6xY w# t         $ r5}t        dj#                  ||j$                  |j&                              d }~ww xY w)Nr   	templateszpath specified in src not foundrz0unable to load src file {0}, I/O error({1}): {2}z_block:) r    r!   r"   rR   rS   rT   isabsr	   schemerf   path_dwim_relativerV   r   rX   r   readIOErrorr)   errnostrerrorrh   appendri   r   _blockget_dep_chaindirname_templarenvironmentloader
searchpathtemplate)r2   convert_datar   working_pathsourceftemplate_dataer|   	dep_chainroles              r:   r#   zActionModule._handle_src_option   s   jjoo!!%(--/77==%!7!7F\\44\;PSTF88sKww~~f%@AA	fc" 2a ' 12 #^
::'djj..99:tzz9- JJ--;;=	( ) ;"))$//:;"''//&126@!!((3!%!7!7!F

+2 2 	BIIAGGQZZ 	s0   H: H-9H: -H72H: :	I80I33I8c                    d| j                   j                  v rI| j                   j                  d   r0t        j                  d       | j                   j                  d   }|S | j                  j
                  r-t        j                  d       | j                  j
                  }|S d|j                  di       v r'|d   d   rt        j                  d       |d   d   }|S t        d      )N
network_osz%Getting network OS from task argumentz!Getting network OS from inventoryansible_factszGetting network OS from factz1ansible_network_os must be specified on this host)r    r!   r'   r(   _play_contextr   r"   r   )r2   r   r   s      r:   _get_network_oszActionModule._get_network_os   s    4::??*tzz|/LLL@A6J  **LL<=++66J  IMM/2>>/*<8LL78"?3LAJ  RSSrj   c                    | j                  d      }|rt        j                  dj                  t              |       t
        s-d}t        j                  dj                  t              |       | j                  j                  r-d}t        j                  dj                  t              |       |S t        j                  dj                  t              |       t        j                  d	j                  t              |       |S )
z,Check if current python and task are eligbleimport_modulesz{prefix} enabledr   Fz'{prefix} disabled for when not Python 3)r6   z({prefix} disabled for a task using asyncz{prefix} disabledz.{prefix} module execution time may be extended)get_connection_optionr'   r(   r)   r*   r   r    	async_val)r2   r6   dexecs      r:   r%   z%ActionModule._check_dexec_eligibility   s    **+;< LL+22,2GN =DDLDY   zz##>EE\EZ    LL,33<3H$OLL@GG|G\
 rj   c                    ddl }| j                  j                  }	 |j                  | j                  j
                  | j                  j                        }|j                  }|j                  |j                        }||fS # t        $ rU |j                  | j                  j
                  | j                  j                        \  }}|j                  |      }Y ||fS w xY w)zUse the task action to find a module
        and import it.

        :return filename: The module's filename
        :rtype filename: str
        :return module: The loaded module file
        :rtype module: module
        r   N)collection_list)	importlib_shared_loader_objmodule_loaderfind_plugin_with_contextr    r   collectionsplugin_resolved_pathimport_moduleplugin_resolved_namerL   find_plugin_with_name)r2   r   mloadrcontextr8   r   fullnames          r:   r&   zActionModule._find_load_module   s     	((66	755

!!4::3I3I 6 G 33H,,W-I-IJF   	7!'!=!=

!!4::3I3I "> "Hh ,,X6F	7s   A"B AC C c                    ddl }ddlm}  G d d|      }| j                  | j                  j
                  | j                  j                  |       |j                  | j                  j                        |_        ||_        y)a  Update a module instance, replacing it's AnsibleModule
        with one that doesn't load params

        :param module: An loaded module
        :type module: A module file that was loaded
        :param task_vars: The vars provided to the task
        :type task_vars: dict
        r   N)r   c                       e Zd Zd Zy)?ActionModule._patch_update_module.<locals>.PatchedAnsibleModulec                      y re    )r2   s    r:   _load_paramszLActionModule._patch_update_module.<locals>.PatchedAnsibleModule._load_params  s    rj   N)__name__
__module____qualname__r   r   rj   r:   PatchedAnsibleModuler     s    rj   r   )	copyansible.module_utils.basicr   _update_module_argsr    r   r!   deepcopyparams)r2   r   r   r   _AnsibleModuler   s         r:   r,   z!ActionModule._patch_update_module  sb     	N	> 	
 	  !2!2DJJOOYO '+mmDJJOO&D#  4rj   c                 h   ddl }ddl}ddlm} ddlm} |j                  }|j                         |_        d}d}	 |j                          ||_        ||j                         ||j                         d}
| j                  |
      } ||       d|v r-d|vr)|j                  dd      xs d}|j                         |d<   d	|v r-d
|vr)|j                  d	d      xs d}|j                         |d
<   |S # t        $ r |j                  j                         }Y t        $ r}	 ||	      }Y d}	~	d}	~	ww xY w)a  exec the module's main() since modules
        print their result, we need to replace stdout
        with a buffer. If main() fails, we assume that as stderr
        Once we collect stdout/stderr, use our super to json load
        it or handle a traceback

        :param module: An loaded module
        :type module: A module file that was loaded
        :return module_result: The result of the module
        :rtype module_result: dict
        r   N)	to_native)remove_internal_keys )stdoutstdout_linesstderrstderr_linesr   r   r   r   )iosysansible.module_utils._textr   ansible.vars.cleanr   r   StringIOmain
SystemExitgetvaluerZ   
splitlines_parse_returned_datar"   )r2   r   r   r   r   r   
sys_stdoutr   r   r5   dict_outdatatxts                r:   r-   zActionModule._exec_module)  s0    	8; ZZ
[[]
	$KKM  
 "--/"--/	
 ((2T" td :((8T*0bC#&>>#3D td :((8T*0bC#&>>#3D ?  	+ZZ((*F 	$s^F	$s   C2 2#D1D1D,,D1c                 ^    |D ]  }t        j                  |d|      } |j                         S )z^remove lines from contents that match
        regexes specified in the `filters` list
        r   )resubstrip)r2   r>   r?   xs       r:   rN   zActionModule._sanitize_contentse  s2      	/AvvaX.H	/~~rj   )NN)T)r   r   r   r0   r1   rR   r#   r   r%   r&   r,   r-   rN   __classcell__)r9   s   @r:   r
   r
      s<    <|5Mn$GL$@ 848:x rj   r
   )
__future__r   r   r   type__metaclass__rS   r   rG   ansible.errorsr   r   r   ansible.module_utils.sixr   +ansible.module_utils.six.moves.urllib.parser	   ansible.plugins.action.normalr
   _ActionModuleansible.utils.displayr   ansible.utils.hashingr   r   r'   r*   r   rj   r:   <module>r      sO    A @  	 	  ' . ( @ G ) 6 )0N = N rj   