
    Vhs                         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 d dlmZ d d	lmZ d d
lmZ d Zd Zd Zedk(  r e        yy)    )absolute_importdivisionprint_functiona  
author: James Hogarth (@hogarthj)
module: jenkins_script
short_description: Executes a groovy script in the jenkins instance
description:
  - The C(jenkins_script) module takes a script plus a dict of values to use within the script and returns the result of the
    script being run.
extends_documentation_fragment:
  - community.general.attributes

attributes:
  check_mode:
    support: none
  diff_mode:
    support: none

options:
  script:
    type: str
    description:
      - The groovy script to be executed. This gets passed as a string Template if args is defined.
    required: true
  url:
    type: str
    description:
      - The jenkins server to execute the script against. The default is a local jenkins instance that is not being proxied
        through a webserver.
    default: http://localhost:8080
  validate_certs:
    description:
      - If set to V(false), the SSL certificates will not be validated. This should only set to V(false) used on personally
        controlled sites using self-signed certificates as it avoids verifying the source site.
    type: bool
    default: true
  user:
    type: str
    description:
      - The username to connect to the jenkins server with.
  password:
    type: str
    description:
      - The password to connect to the jenkins server with.
  timeout:
    type: int
    description:
      - The request timeout in seconds.
    default: 10
  args:
    type: dict
    description:
      - A dict of key-value pairs used in formatting the script using string.Template (see https://docs.python.org/2/library/string.html#template-strings).
notes:
  - Since the script can do anything this does not report on changes. Knowing the script is being run it is important to set
    C(changed_when) for the ansible output to be clear on any alterations made.
ah  
- name: Obtaining a list of plugins
  community.general.jenkins_script:
    script: 'println(Jenkins.instance.pluginManager.plugins)'
    user: admin
    password: admin

- name: Setting master using a variable to hold a more complicate script
  ansible.builtin.set_fact:
    setmaster_mode: |
      import jenkins.model.*
      instance = Jenkins.getInstance()
      instance.setMode(${jenkins_mode})
      instance.save()

- name: Use the variable as the script
  community.general.jenkins_script:
    script: "{{ setmaster_mode }}"
    args:
      jenkins_mode: Node.Mode.EXCLUSIVE

- name: Interacting with an untrusted HTTPS connection
  community.general.jenkins_script:
    script: "println(Jenkins.instance.pluginManager.plugins)"
    user: admin
    password: admin
    url: https://localhost
    validate_certs: false # only do this when you trust the network!
zc
output:
  description: Result of script.
  returned: success
  type: str
  sample: 'Result: true'
N)AnsibleModule)http_cookiejar)	urlencode)	fetch_url)	to_nativec                 B   t        | | j                  d   dz   | j                  d   d      \  }}|d   dk7  r+| j                  dt        |d         z   d	z   |d
   z   d       t	        |j                               }t        j                  |      j                  dd      S )Nurlz	/api/jsontimeoutGET)r   methodstatus   HTTP error  msg r   output	useCrumbsF)	r	   params	fail_jsonstrr
   readjsonloadsget)modulerespinfocontents       t/home/dcms/DCMS/lib/python3.12/site-packages/ansible_collections/community/general/plugins/modules/jenkins_script.pyis_csrf_protection_enabledr%   s   s    6!==/+=#)==#;"')JD$ H~]Sh-@@3FeT]_`		$G::g"";66    c                 $   t        | | j                  d   dz   d| j                  d   |      \  }}|d   dk7  r+| j                  dt        |d         z   d	z   |d
   z   d       t	        |j                               }t        j                  |      S )Nr   z/crumbIssuer/api/jsonr   r   )r   r   cookiesr   r   r   r   r   r   r   )r	   r   r   r   r
   r   r   r   )r    r(   r!   r"   r#   s        r$   	get_crumbr)      s    6!==/2II"'#)==#;#*	,JD$
 H~]Sh-@@3FeT]_`		$G::gr&   c                     t        t        t        dd      t        ddd      t        ddd      t        ddd       t        dddd       t        dd	d
      t        ddd                   } | j                  d   i| j                  d   | j                  dd       | j                  d   | j                  d<   | j                  d   | j                  d<   d| j                  d<   | j                  d   9ddlm} 	  || j                  d         j                  | j                  d         }n| j                  d   }i }d }t        |       r*t        j                         }t        | |      }|d   |d   i}t        | | j                  d   dz   t        di      |d| j                  d    |!      \  }}|d"   d#k7  r+| j                  d$t        |d"         z   d%z   |d&   z   d       t        |j!                               }	d'|	v rd(|	v r| j                  d)|	z   d       | j#                  |	*       y # t        $ r!}| j                  d|z  d       Y d }~d }~ww xY w)+NTr   )requiredtypeFzhttp://localhost:8080)r+   r,   defaultbool)r+   no_logr,   r-   int
   dict)scriptr   validate_certsuserpasswordr   args)argument_specr5   r6   z$password required when user providedr   r   url_usernameurl_passwordforce_basic_authr7   r   )Templater3   z"Error with templating variable: %scrumbRequestFieldcrumbr   z/scriptTextPOSTr   )dataheadersr   r   r(   r   r   r   r   r   z
Exception:zat java.lang.Threadz script failed with stacktrace:
 )r   )r   r2   r   r   stringr<   
substituteKeyErrorr%   	cookiejarLWPCookieJarr)   r	   r   r   r
   r   	exit_json)
r    r<   script_contentserrrA   r(   r>   r!   r"   results
             r$   mainrK      sm   E2e%9PQVTJu5$?5E4P%eR@u64@

F }}V(==$,!GPRS(.f(=n%(.j(An%,0()}}V(#	X&v}}X'>?JJ6==Y_K`aO !--1GG!&)((*&'*,-uW~>6!==/-? )8_*E F#*"(#)==#;#*,JD$ H~]Sh-@@3FeT]_`tyy{#Fv"76"A@6IRTU
  9  	X!E!KTVWW	Xs   ?1H3 3	I<II__main__)
__future__r   r   r   r,   __metaclass__DOCUMENTATIONEXAMPLESRETURNr   ansible.module_utils.basicr   ansible.module_utils.six.movesr   rE   +ansible.module_utils.six.moves.urllib.parser   ansible.module_utils.urlsr	   +ansible.module_utils.common.text.convertersr
   r%   r)   rK   __name__ r&   r$   <module>rY      sa    A @6p<
  4 F A / A	7
7t zF r&   