
    VhC                       d dl mZ d dlmZ d dlZd dlZd dlZd dlZd dlZd dl	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mZ d d	lmZ d d
lmZ d dlmZ  e       Z G d de      Z ddZ!e"dk(  r e!        yy)    )annotations)CLIN)	constants)context)option_helpers)AnsibleOptionsError)	to_nativeto_text)module_loader)run_cmd)Displayc                       e Zd ZdZdZdZdZdZdddZd	d
iZ	dZ
ed        Z fdZ fdZ fdZed        Zed        Z xZS )PullCLIaO   Used to pull a remote copy of ansible on each managed node,
        each set to run via cron and update playbook source via a source repository.
        This inverts the default *push* architecture of ansible into a *pull* architecture,
        which has near-limitless scaling potential.

        None of the CLI tools are designed to run concurrently with themselves,
        you should use an external scheduler and/or locking to ensure there are no clashing operations.

        The setup playbook can be tuned to change the cron frequency, logging locations, and parameters to ansible-pull.
        This is useful both for extreme scale-out as well as periodic remediation.
        Usage of the 'fetch' module to retrieve logs from ansible-pull runs would be an
        excellent way to gather and analyze remote logs from ansible-pull.
    zansible-pullgitz	local.yml)r   
subversionhgbzrzFile does not existzFile is not readable)      playbook.ymla  The name of one the YAML format files to run as an Ansible playbook. This can be a relative path within the checkout. By default, Ansible will look for a playbook based on the host's fully-qualified domain name, on the host hostname and finally a playbook named *local.yml*.Tc                 "   d} t         j                  j                  dd      rlt         j                  d   D ]V  }t        |t              r| ddj                  |      z  z  } +d|v s t        j                  j                  |      sO| d|z  z  } X | S )N 	inventoryFz	 -i '%s' ,z -i %s )	r   CLIARGSget
isinstancelistjoinospathexists)inv_optsinvs     @/home/dcms/DCMS/lib/python3.12/site-packages/ansible/cli/pull.py_get_inv_clizPullCLI._get_inv_cliB   s    ??{E2{3 0c4(chhsm ;;HCZ277>>##6	C/H	0     c                j   t         t        |   dd       t        j                  | j
                         t        j                  | j
                         t        j                  | j
                         t        j                  | j
                         t        j                  | j
                         t        j                  | j
                         t        j                  | j
                         | j
                  j                  dddd       | j
                  j                  d	d
dd       | j
                  j                  dddd
dd       | j
                  j                  ddddd       | j
                  j                  dddd
dd       | j
                  j                  ddddt        j                         d !       | j
                  j                  d"d#d$dd%       | j
                  j                  d&d'dd()       | j
                  j                  d*d+d,d-.       | j
                  j                  d/d
d0dd12       | j
                  j                  d3d4d5| j                  d6| j                  d7| j                  d8       | j
                  j                  d9d:d
dd;       | j
                  j                  d<d=d
dd>       | j
                  j                  d?d@d
ddA       | j
                  j                  dBd
dCddD2       | j
                  j                  dEt         j"                  dFddG2       y)Hz* create an options parser for bin/ansible z0%prog -U <repository> [options] [<playbook.yml>]z@pulls playbooks from a VCS repo and executes them on target host)usagedescargszPlaybook(s)r   *)helpmetavarnargsz--purgeF
store_truez!purge checkout after playbook run)defaultactionr-   z-oz--only-if-changed	ifchangedz8only run the playbook if the repository has been updated)destr1   r2   r-   z-sz--sleepsleepNz|sleep for random interval (between 0 and n number of seconds) before starting. This is a useful way to disperse git requests)r4   r1   r-   z-fz--forceforcez<run the playbook even if the repository could not be updatedz-dz--directoryr4   zDpath to the directory to which Ansible will checkout the repository.)r4   r1   typer-   z-Uz--urlurlzURL of the playbook repositoryz--full	fullclonez*Do a full clone, instead of a shallow one.)r4   r2   r-   z-Cz
--checkoutcheckoutzIbranch/tag/commit to checkout. Defaults to behavior of repository module.)r4   r-   z--accept-host-keyaccept_host_keyz6adds the hostkey for the repo url if not already added)r1   r4   r2   r-   z-mz--module-namemodule_namezRRepository module name, which ansible will use to check out the repo. Choices are z. Default is .z--verify-commitverifyzverify GPG signature of checked out commit, if it fails abort running the playbook. This needs the corresponding VCS module to support such an operationz--cleancleanz:modified files in the working repository will be discardedz--track-subs	tracksubsztsubmodules will track the latest changes. This is equivalent to specifying the --remote flag to git submodule updatez--checkcheckzRdon't make any changes; instead, try to predict some of the changes that may occurz--diffdiffzhwhen changing (small) files and templates, show the differences in those files; works great with --check)superr   init_parseropt_helpadd_connect_optionsparseradd_vault_optionsadd_runtask_optionsadd_subset_optionsadd_inventory_optionsadd_module_optionsadd_runas_prompt_optionsadd_argumentunfrack_pathDEFAULT_REPO_TYPEREPO_CHOICESCDIFF_ALWAYS)self	__class__s    r%   rD   zPullCLI.init_parserN   s    	gt(DS 	) 	U
 	$$T[[1""4;;/$$T[[1##DKK0&&t{{3##DKK0))$++6  m^[^ _ 	  E,Ux y  ':V[dp&` 	! 	b  yw'V 	! 	W 	  ywVb&d 	! 	f  }64V^VkVkVm&l 	! 	n  wUDOo p  L  XD   	E  |*&q 	! 	s  !4eJ[dp&^ 	! 	`  ]TXTjTj)-):):D<R<R'T 	! 	U 	  !25Ye'm 	! 	n 	  %P\&b 	! 	d  k5Ye '] 	! 	^ 	  EP\&z 	! 	|  1==vVb 'Q 	! 	Rr'   c                ~   t         t        |   |      }|j                  sHt	        j
                         }t        j                  j                  t        j                  d|      |_        t        j                  j                  |j                        rAt        j                  j                  |j                        st        d|j                  z        |j                  r0	 t        j                   t#        |j                              }||_        |j&                  st        d      |j(                  | j*                  vr4t        d|j(                  ddj                  | j*                              |j,                  t.        _        | j1                  |       |S # t$        $ r t        d|j                  z        w xY w)Npullz*%s is not a valid or accessible directory.z%s is not a number.z1URL for repository not specified, use -h for helpzUnsupported repo module z, choices are r   )rC   r   post_process_argsr4   socketgetfqdnr    r!   r   rR   ANSIBLE_HOMEr"   isdirr   r5   secrets	randbelowint
ValueErrorr8   r<   rQ   	verbositydisplayvalidate_conflicts)rT   optionshostnamesecsrU   s       r%   rX   zPullCLI.post_process_args   s`   8A||~~'H77<<IGL77>>',,'gll0K%&RU\UaUa&abb==Q((W]]);< $ {{%&YZZd&7&77%V]ViViknkskstx  uF  uF  lG  'H  I  I#--(  Q)*?'--*OPPQs    /F "F<c                F   t         t        |           t        j                  j	                         }t
        j                  |j                  d             t
        j                  dj                  t        j                               t        j                         }t        j                         }dj                  t        |||j                  d      d   |j                  d      d   g            }|rd|z  }nd}d}t         j"                  d	   dkD  r@|d
dj                  t%        dt         j"                  d	         D cg c]  }d c}      z  z  }| j'                         }|s.d}|dt)        j*                  dt        j,                  z        z  z  }t         j"                  d   dk(  rdt         j"                  d   dt         j"                  d   }	t         j"                  d   r|	dt         j"                  d   z  z  }	t         j"                  d   r|	dz  }	t         j"                  d   r|	dt         j"                  d   z  z  }	t         j"                  d   r|	dz  }	t         j"                  d   r|	dz  }	t         j"                  d    s|	d!z  }	nt         j"                  d   d"k(  rqd#t         j"                  d   dt         j"                  d   }	t         j"                  d   r|	d$t         j"                  d   z  z  }	t         j"                  d    s|	d%z  }	nt         j"                  d   d&k(  rWd#t         j"                  d   dt         j"                  d   }	t         j"                  d   r|	d$t         j"                  d   z  z  }	nt         j"                  d   d'k(  rWdt         j"                  d   dt         j"                  d   }	t         j"                  d   rU|	dt         j"                  d   z  z  }	n;t/        d(t         j"                  d   d)dj                  | j0                              t         j"                  d*   r|	d+z  }	t3        j4                  t         j"                  d         }
|
t/        d-t         j"                  d   z        t6        j8                  j;                  t6        j8                  j=                  t        j                  d               }|d.|d|d/t         j"                  d   d0|	d1|d2}t         j"                  d3   D ]  }|d4t)        j*                  |      z  z  } t         j"                  d5   rOt
        j                  d6t         j"                  d5   z         t?        j@                  t         j"                  d5          t
        jC                  d7       t
        jE                  d8|z         tG        |d9:      \  }}}|dk7  r+t         j"                  d;   rt
        jI                  d<       n/|S t         j"                  d=   rd>|vrt
        j                  d?       y| jK                  t         j"                  d         }|t/        d@      |dA|d|}t         j"                  dB   r t         j"                  dB   D ]
  }|dC|z  z  } t         j"                  dD   r t         j"                  dD   D ]
  }|dE|z  z  } t         j"                  dF   r|dGt         j"                  dF   z  z  }t         j"                  dH   r|dIt         j"                  dH   z  z  }t         j"                  d3   D ]  }|d4t)        j*                  |      z  z  } t         j"                  dJ   r|dKz  }t         j"                  dL   r1|dMtM        dj                  t         j"                  dL               z  z  }t         j"                  dN   r1|dOtM        dj                  t         j"                  dN               z  z  }t         j"                  dP   r|dQt         j"                  dP   z  z  }n|dQ|z  z  }t         j"                  dR   r|dSz  }t         j"                  dT   r|dUz  }t7        jN                  t         j"                  d          | j'                         }|r||z  }t
        jC                  dV       t
        jC                  d8|z         tG        |d9:      \  }}}t         j"                  dW   rgt7        jN                  dX       	 t
        jC                  dYt         j"                  d   z         tQ        jR                  t         j"                  d          |S |S c c}w # tT        $ r@}t
        jW                  dZt         j"                  d   d[tY        |             Y d,}~|S d,}~ww xY w)\z! use Runner lib to do SSH things zStarting Ansible Pull at %F %T r   r=   r   zlocalhost,%s,127.0.0.1zlocalhost,127.0.0.1z	-c local ra   z -%sr   vz -i localhost, z-e %s zansible_python_interpreter=%sr<   r   zname=r8   z dest=r4   r:   z version=%sr;   z accept_hostkey=yesprivate_key_filez key_file=%sr>   z verify_commit=yesr@   z track_submodules=yesr9   z depth=1r   zrepo=z revision=%sz export=yesr   r   zUnsupported (z$) SCM module for pull, choices are: r?   z
 force=yesNzmodule '%s' not found.
z	/ansible z -m z -a "z
" all -l ""
extra_varsz -e %sr5   zSleeping for %d seconds...z0running ansible with VCS module to checkout repozEXEC: %sT)liver6   zFUnable to update repository. Continuing with (forced) run of playbook.r3   s   "changed": truez%Repository has not changed, quitting.z!Could not find a playbook to run.z/ansible-playbook vault_password_filesz --vault-password-file=%s	vault_idsz --vault-id=%sbecome_password_filez --become-password-file=%sconnection_password_filez --connection-password-file=%sbecome_ask_passz --ask-become-pass	skip_tagsz --skip-tags "%s"tagsz -t "%s"subsetz -l "%s"rA   z -CrB   z -Dz*running ansible-playbook to do actual workpurge/zremoving: %szFailed to remove : )-rC   r   rundatetimenowrb   strftimer   sysargvplatformnoderY   rZ   setsplitr   r   ranger&   shlexquote
executabler   rQ   r   find_pluginr    r!   dirnameabspathtimer5   debugvvvvr   warningselect_playbookr	   chdirshutilrmtree	Exceptionerrorr
   )rT   r{   r   host	hostnames
limit_opts	base_optsxr#   	repo_optsr!   bin_pathcmdevrcb_outb_errplaybookvault_password_filevault_iderU   s                        r%   ry   zPullCLI.run   s    	gt " ##%%EFG*+ }}~~HHS$djjoa.@$**S/RSBT!UVW	1I=J.J	??;'!+"''aQ\A]8^*_13*_"```I $$&(H5;;/NQTQ_Q_/_#```H ??=)U2-4__U-CW__U[E\]Iz*]W__Z-HHH	0122	12^goo>P.QQQ	x(11	{+44	??;/Z'	__]+|;-4__U-CW__U[E\]Iz*^gooj.III	??;/]*	__]+t3-4__U-CW__U[E\]Iz*^gooj.III	__]+u4-4__U-CW__U[E\]Iz*]W__Z-HHH	%)0)G),$2C2C)D'F G G
 ??7#%I(()GH<%'AGOOTaDb'bdd77??277??388A;#?@>FR[>Eoom>\>GU //,/ 	.B8ekk"o--C	. ??7#OO87??7;SSTJJww/0 	HIZ#%&"3T2E57w' hi	__[).@.MOOCD''(?@%&IJJ .6y(K??12'.7M'N I#25HHHI??;'#OOK8 3'(223 ??12/'//BX2YYYC??563gooF`6aaaC//,/ 	.B8ekk"o--C	. ??,-''C??;'&499W__[=Y3Z)[[[C??6":	$))GOOF4K*L MMMC??8$: 999C:
**C??7#5LC??6"5LC
() $$&8OC 	BCj3&'"3T2E5??7#HHSMbnwv/FFGgoof56 	r	[ +`T  bGOOF<SU\]^U_`aa	bs    	e?Ae 	f  5ff c                    t         j                  j                  |       syt        j                  | t         j                        syy)Nr   r   r   )r    r!   r"   accessR_OK)r!   s    r%   try_playbookzPullCLI.try_playbookC  s-    ww~~d#yyrww'r'   c                x   d }g }t         j                  d   rt         j                  d   d   g }t         j                  d   D ]u  }t        j                  j	                  | |      }t
        j                  |      }|dk7  r(|j                  |dt
        j                  |          e|j                  |       w dt        |      k  r&t        j                  dj	                  |             |S t        |      t        t         j                  d         k(  rdj	                  |      }|S t        j                         }t        j                  j	                  | |dz         }t        j                  j	                  | |j                  d      d   dz         }	t        j                  j	                  | t
        j                        }
||	|
fD ]G  }t
        j                  |      }|dk(  r|} n)|j                  |dt
        j                  |          I |$t        j                  dj	                  |             |S )Nr+   r   rx   
rh   z.ymlr=   )r   r   r    r!   r   r   r   appendPLAYBOOK_ERRORSlenrb   r   rY   rZ   r   DEFAULT_PLAYBOOK)r!   r   errors	playbooksbook	book_pathr   fqdnhostpbshorthostpblocalpbpbs               r%   r   zPullCLI.select_playbookK  s   ??6"wv'>q'A'MI/ ,GGLLt4	)))47MMi9P9PQS9T"UV  +, 3v;		& 12 O Y3wv'>#??88I.O>>#DWW\\$v6F'',,tTZZ_Q-?&-HIKggll4)A)ABG{G4 P))"-7!HMMb'2I2I"2M"NOP 		& 12Or'   )__name__
__module____qualname____doc__namerP   r   rQ   r   	ARGUMENTSSKIP_INVENTORY_DEFAULTSstaticmethodr&   rD   rX   ry   r   r   __classcell__)rU   s   @r%   r   r   #   s     D"5L !O   "b eI
 #	 	2Rh:bH    r'   r   c                .    t         j                  |        y N)r   cli_executor)r+   s    r%   mainr   n  s    r'   __main__r   )#
__future__r   ansible.clir   rz   r    r   r]   r   r   rY   r}   r   ansibler   rR   r   ansible.cli.argumentsr   rE   ansible.errorsr   +ansible.module_utils.common.text.convertersr	   r
   ansible.plugins.loaderr   ansible.utils.cmd_functionsr   ansible.utils.displayr   rb   r   r   r    r'   r%   <module>r      ss    #   	      
  "  < . J 0 / ) )Hc HV
 zF r'   