
    Vh&                         d dl mZmZmZ eZdZdZd dlZd dl	m
Z
  edd      Z ed	d
g      Zd Zd Zd Zd Zd ZddZd Zedk(  r e        yy)    )absolute_importdivisionprint_functionam	  
module: elasticsearch_plugin
short_description: Manage Elasticsearch plugins
description:
  - Manages Elasticsearch plugins.
author:
  - Mathew Davies (@ThePixelDeveloper)
  - Sam Doran (@samdoran)
extends_documentation_fragment:
  - community.general.attributes
attributes:
  check_mode:
    support: full
  diff_mode:
    support: none
options:
  name:
    description:
      - Name of the plugin to install.
    required: true
    type: str
  state:
    description:
      - Desired state of a plugin.
    choices: ["present", "absent"]
    default: present
    type: str
  src:
    description:
      - Optionally set the source location to retrieve the plugin from. This can be a C(file://) URL to install from a local
        file, or a remote URL. If this is not set, the plugin location is just based on the name.
      - The name parameter must match the descriptor in the plugin ZIP specified.
      - Is only used if the state would change, which is solely checked based on the name parameter. If, for example, the
        plugin is already installed, changing this has no effect.
      - For ES 1.x use O(url).
    required: false
    type: str
  url:
    description:
      - Set exact URL to download the plugin from (Only works for ES 1.x).
      - For ES 2.x and higher, use src.
    required: false
    type: str
  timeout:
    description:
      - 'Timeout setting: V(30s), V(1m), V(1h)...'
      - Only valid for Elasticsearch < 5.0. This option is ignored for Elasticsearch > 5.0.
    default: 1m
    type: str
  force:
    description:
      - Force batch mode when installing plugins. This is only necessary if a plugin requires additional permissions and console
        detection fails.
    default: false
    type: bool
  plugin_bin:
    description:
      - Location of the plugin binary. If this file is not found, the default plugin binaries will be used.
    type: path
  plugin_dir:
    description:
      - Your configured plugin directory specified in Elasticsearch.
    default: /usr/share/elasticsearch/plugins/
    type: path
  proxy_host:
    description:
      - Proxy host to use during plugin installation.
    type: str
  proxy_port:
    description:
      - Proxy port to use during plugin installation.
    type: str
  version:
    description:
      - Version of the plugin to be installed. If plugin exists with previous version, it will NOT be updated.
    type: str
a$  
- name: Install Elasticsearch Head plugin in Elasticsearch 2.x
  community.general.elasticsearch_plugin:
    name: mobz/elasticsearch-head
    state: present

- name: Install a specific version of Elasticsearch Head in Elasticsearch 2.x
  community.general.elasticsearch_plugin:
    name: mobz/elasticsearch-head
    version: 2.0.0

- name: Uninstall Elasticsearch head plugin in Elasticsearch 2.x
  community.general.elasticsearch_plugin:
    name: mobz/elasticsearch-head
    state: absent

- name: Install a specific plugin in Elasticsearch >= 5.0
  community.general.elasticsearch_plugin:
    name: analysis-icu
    state: present

- name: Install the ingest-geoip plugin with a forced installation
  community.general.elasticsearch_plugin:
    name: ingest-geoip
    state: present
    force: true
N)AnsibleModuleinstallremove)presentabsentz1/usr/share/elasticsearch/bin/elasticsearch-pluginz#/usr/share/elasticsearch/bin/pluginc                     | j                  d      }|d   }t        |      dkD  r|d   }dD ]#  } |j                  |       s|t        |       d  c S  |S )N/r      )zelasticsearch-zes-)splitlen
startswith)stringelementsrepos      z/home/dcms/DCMS/lib/python3.12/site-packages/ansible_collections/community/general/plugins/modules/elasticsearch_plugin.pyparse_plugin_repor      sg    ||C H A;D 8}q{ , &??6"F%%& K    c                 |    t         j                  j                  t         j                  j                  ||             S N)ospathisdirjoin)plugin_name
plugin_dirs     r   is_plugin_presentr      s$    77==j+>??r   c                     d}	 | | j                  |      t        |      z   d  j                         S # t        $ r | cY S w xY w)NzERROR: )indexr   strip
ValueError)r   reasons     r   parse_errorr%      sI    Ffll6*S[89:@@BB s   -2 A A c
                    |t         d   g}
t        j                  j                  |      dk(  }|r3|r"|
j	                  d       |
j	                  |       |r|dz   |z   }||
d<   |rD|rBd|z  d|z  d|z  d	|z  g}t        d
j                  |      d
j                  |            | _        |r"|
j	                  d       |
j	                  |       |	r|
j	                  d       |r|
j	                  |       n|
j	                  |       | j                  rd\  }}}n| j                  |
      \  }}}|dk7  r$t        |      }| j                  d|d||       d|
||fS )Nr	   pluginz	--timeoutr      z-Dhttp.proxyHost=%sz-Dhttp.proxyPort=%sz-Dhttps.proxyHost=%sz-Dhttps.proxyPort=%s )CLI_JAVA_OPTSES_JAVA_OPTSz--urlz--batchr   z
check mode r   zInstalling plugin '
' failed: msgerrT)PACKAGE_STATE_MAPr   r   basenameappenddictr   run_command_environ_update
check_moderun_commandr%   	fail_json)module
plugin_binr   versionsrcurl
proxy_host
proxy_porttimeoutforcecmdis_old_command	java_optsrcoutr1   r$   s                    r   install_pluginrH      sb   (3
4Cgg&&z2h>N JJ{#JJw%+g5K CFj*Z7*Z7+j8+j8:	 -1sxx	?R>Ahhy>Q-S) 

7

3

9


3

;*C))#.C	QwS!KQWX^abc3r   c                     |t         d   t        |      g}| j                  rd\  }}}n| j                  |      \  }}}|dk7  r$t	        |      }| j                  d|d||       d|||fS )Nr
   r,   r   zRemoving plugin 'r.   r/   T)r2   r   r7   r8   r%   r9   )r:   r;   r   rC   rF   rG   r1   r$   s           r   remove_pluginrJ      s}    (24Ek4R
SC*C))#.C	QwS!+vV\_`c3r   c           
         d }|r"t         j                  j                  |      r|}nt        t              }|r||vr|j                  d|       t        t        |D cg c]!  }t         j                  j                  |      # c}            }t        t        |D cg c]!  }t         j                  j                  |      # c}            }|D ]  }| j                  ||      }|s n |s| j                  d|z         |S c c}w c c}w )Nr   )opt_dirszh%s does not exist and no other valid plugin installers were found. Make sure Elasticsearch is installed.)r0   )r   r   isfilelistPLUGIN_BIN_PATHSinsertsetdirnamer3   get_bin_pathr9   )r:   r;   valid_plugin_bin	bin_pathsxplugin_dirsplugin_binsbin_files           r   get_plugin_binrZ      s    bggnnZ0%
 )*	*I5Q
+ 3IFq 2FGH3YG 0 0 3GHI $ 	H%228k2R	
   H  KU  U  	V  GGs   &D&Dc                     t        t        t        d      t        dt        t        j	                                     t        d       t        d       t        d      t        dd	      t        d
      t        dd
      t        d       t        d       t        d             dgd      } | j
                  d   }| j
                  d   }| j
                  d   }| j
                  d   }| j
                  d   }| j
                  d   }| j
                  d   }| j
                  d   }| j
                  d   }	| j
                  d   }
| j
                  d   }t        | |      }t        |      }t        ||      }|r|dk(  s|dk(  r|s| j                  d||       |dk(  rt        | ||||||	|
||
      \  }}}}n|dk(  rt        | ||      \  }}}}| j                  ||||       y )NT)requiredr	   )defaultchoices)r]   1mboolF)typer]   r   )ra   z!/usr/share/elasticsearch/plugins/)r]   ra   )namestater=   r>   rA   rB   r;   r   r?   r@   r<   )r=   r>   )argument_specmutually_exclusivesupports_check_moderb   rc   r>   r=   rA   rB   r;   r   r?   r@   r<   r
   )changedrb   rc   )rg   rC   rb   rc   r>   rA   stdoutstderr)r   r5   rN   r2   keysparamsrZ   r   r   	exit_jsonrH   rJ   )r:   rb   rc   r>   r=   rA   rB   r;   r   r?   r@   r<   r   r	   rg   rC   rG   r1   s                     r   mainrm     s   t$y$7H7M7M7O2PQT"T"&FE2($GfUD)D)&
 ++ F$ == DMM'"E
--
C
--
CmmI&GMM'"E|,J|,J|,J|,JmmI&G  
3JT"Dj1G 	EY&EX,=gT?	!/
D'SVX[]gisu|  D  "Ec3	(	!.vz4!Hc3
W#D3X_hktwxr   __main__r   )
__future__r   r   r   ra   __metaclass__DOCUMENTATIONEXAMPLESr   ansible.module_utils.basicr   r5   r2   tuplerO   r   r   r%   rH   rJ   rZ   rm   __name__ r   r   <module>rw      s    A @L\8 
 4  
 7)  &@+\>/yd zF r   