
    Vhx                         d dl mZmZmZ eZdZdZdZd dl	Z	d dl
mZ d dlmZ  edd	
      Zd Zd Zd ZddZddZd Zd Zedk(  r e        yy)    )absolute_importdivisionprint_functionak  
module: kibana_plugin
short_description: Manage Kibana plugins
description:
  - This module can be used to manage Kibana plugins.
author: Thierno IB. BARRY (@barryib)
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
  url:
    description:
      - Set exact URL to download the plugin from.
      - For local file, prefix its absolute path with C(file://).
    type: str
  timeout:
    description:
      - 'Timeout setting: V(30s), V(1m), V(1h) and so on.'
    default: 1m
    type: str
  plugin_bin:
    description:
      - Location of the Kibana binary.
    default: /opt/kibana/bin/kibana
    type: path
  plugin_dir:
    description:
      - Your configured plugin directory specified in Kibana.
    default: /opt/kibana/installedPlugins/
    type: path
  version:
    description:
      - Version of the plugin to be installed.
      - If plugin exists with previous version, plugin will B(not) be updated unless O(force) is set to V(true).
    type: str
  force:
    description:
      - Delete and re-install the plugin. Can be useful for plugins update.
    type: bool
    default: false
  allow_root:
    description:
      - Whether to allow C(kibana) and C(kibana-plugin) to be run as root. Passes the C(--allow-root) flag to these commands.
    type: bool
    default: false
    version_added: 2.3.0
a  
- name: Install Elasticsearch head plugin
  community.general.kibana_plugin:
    state: present
    name: elasticsearch/marvel

- name: Install specific version of a plugin
  community.general.kibana_plugin:
    state: present
    name: elasticsearch/marvel
    version: '2.3.3'

- name: Uninstall Elasticsearch head plugin
  community.general.kibana_plugin:
    state: absent
    name: elasticsearch/marvel
a~  
cmd:
  description: The launched command during plugin management (install / remove).
  returned: success
  type: str
name:
  description: The plugin name to install or remove.
  returned: success
  type: str
url:
  description: The URL from where the plugin is installed from.
  returned: success
  type: str
timeout:
  description: The timeout for plugin download.
  returned: success
  type: str
stdout:
  description: The command stdout.
  returned: success
  type: str
stderr:
  description: The command stderr.
  returned: success
  type: str
state:
  description: The state for the managed plugin.
  returned: success
  type: str
N)AnsibleModule)LooseVersionz	--installz--remove)presentabsentc                     | 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      s/home/dcms/DCMS/lib/python3.12/site-packages/ansible_collections/community/general/plugins/modules/kibana_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_dirworking_dirs     r   is_plugin_presentr      s$    77==k:>??r   c                     d}	 | | j                  |      t        |      z   d  j                         S # t        $ r | cY S w xY w)Nzreason: )indexr   strip
ValueError)r   reasons     r   parse_errorr#      sI    Ffll6*S[89:@@BB s   -2 A A c                 l   t        |      t        d      kD  rgt        j                  j                  t        j                  j	                  |      d      }|dg}|r|j                  |       n4|j                  |       n"|dt        d   |g}|r|j                  d|g       |r|j                  d|g       |r|j                  d       | j                  rd	d
j                  |      ddfS | j                  |      \  }	}
}|	dk7  rt        |
      }| j                  |       d	d
j                  |      |
|fS )N4.6kibana-plugininstallpluginr   z--urlz	--timeout--allow-rootT 
check mode r   msg)r   r   r   r   dirnameappendPACKAGE_STATE_MAPextend
check_moderun_commandr#   	fail_json)module
plugin_binplugin_nameurltimeout
allow_rootkibana_versionkibana_plugin_bincmd_argsrcouterrr"   s                r   install_pluginrB      s   N#l5&99GGLL)DoV%y1OOC OOK(*;I*FTOOWcN+g./'SXXh'r99%%h/LBS	QwS!V$(#S#--r   c                    t        |      t        d      kD  rCt        j                  j                  t        j                  j	                  |      d      }|d|g}n|dt
        d   |g}|r|j                  d       | j                  rddj                  |      d	d
fS | j                  |      \  }}}	|dk7  rt        |      }
| j                  |
       ddj                  |      ||	fS )Nr%   r&   remover(   r	   r)   Tr*   r+   r,   r   r-   )r   r   r   r   r/   r1   r0   r3   r4   r#   r5   )r6   r7   r8   r;   r<   r=   r>   r?   r@   rA   r"   s              r   remove_pluginrE      s    N#l5&99GGLL)DoV%x=*;H*E{S'SXXh'r99%%h/LBS	QwS!V$(#S#--r   c                     |dg}|r|j                  d       | j                  |      \  }}}|dk7  r| j                  d|z         |j                         S )Nz	--versionr)   r   z!Failed to get Kibana version : %sr-   )r0   r4   r5   r    )r6   r7   r;   r>   r?   r@   rA   s          r   get_kibana_versionrG      s[    K(H'%%h/LBS	Qw@3FG99;r   c                     t        t        t        d      t        dt        t        j	                                     t        d       t        d      t        dd	      t        d
d	      t        d       t        dd	      t        dd	      	      d      } | j
                  d   }| j
                  d   }| j
                  d   }| j
                  d   }| j
                  d   }| j
                  d   }| j
                  d   }| j
                  d   }| j
                  d   }	d\  }
}}}t        | ||	      }t        t        |      |      }|r|dk(  r|r	|dk(  r|s|s| j                  d||       |r|dz   |z   }|dk(  r(|rt        | |||	|       t        | |||||	|      \  }
}}}n|dk(  rt        | |||	|      \  }
}}}| j                  |
|||||||       y )NT)requiredr   )defaultchoices)rJ   1mz/opt/kibana/bin/kibanar   )rJ   typez/opt/kibana/installedPlugins/Fbool)	namestater9   r:   r7   r   versionforcer;   )argument_specsupports_check_moderO   rP   r9   r:   r7   r   rQ   rR   r;   )Fr,   r,   r,   r	   )changedrO   rP   r   )rU   cmdrO   rP   r9   r:   stdoutstderr)r   dictlistr1   keysparamsrG   r   r   	exit_jsonrE   rB   )r6   rO   rP   r9   r:   r7   r   rQ   rR   r;   rU   rV   r@   rA   r<   r   s                   r   mainr^      s   t$y$7H7M7M7O2PQT"&$<6J$C&Q&u62E7

 !F == DMM'"E
--
CmmI&G|,J|,JmmI&GMM'"E|,J.GS#s'
JGN 1$ 7DG 	EY&u%8:KT[diT?czG#	&*dJO!/
D#wXbdr!sc3	(	!.vz4Uc!dc3
W#D3X_hktwxr   __main__)r%   )
__future__r   r   r   rM   __metaclass__DOCUMENTATIONEXAMPLESRETURNr   ansible.module_utils.basicr   Bansible_collections.community.general.plugins.module_utils.versionr   rY   r1   r   r   r#   rB   rE   rG   r^   __name__ r   r   <module>ri      s    A @=~$
> 
 4 [  &@.>.*
/yd zF r   