
    Vh'                         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Z	 d dlZdZd d	lmZmZ d d
lmZ  G d d      Zd Zd Zedk(  r e        yy# e$ r  e	j                          ZdZY Fw xY w)    )absolute_importdivisionprint_functiona;  
module: jenkins_build
short_description: Manage jenkins builds
version_added: 2.2.0
description:
  - Manage Jenkins builds with Jenkins REST API.
requirements:
  - "python-jenkins >= 0.4.12"
author:
  - Brett Milford (@brettmilford)
  - Tong He (@unnecessary-username)
  - Juan Casanova (@juanmcasanova)
extends_documentation_fragment:
  - community.general.attributes
attributes:
  check_mode:
    support: none
  diff_mode:
    support: none
options:
  args:
    description:
      - A list of parameters to pass to the build.
    type: dict
  name:
    description:
      - Name of the Jenkins job to build.
    required: true
    type: str
  build_number:
    description:
      - An integer which specifies a build of a job. Is required to remove a build from the queue.
    type: int
  password:
    description:
      - Password to authenticate with the Jenkins server.
    type: str
  state:
    description:
      - Attribute that specifies if the build is to be created, deleted or stopped.
      - The V(stopped) state has been added in community.general 3.3.0.
    default: present
    choices: ['present', 'absent', 'stopped']
    type: str
  token:
    description:
      - API token used to authenticate with the Jenkins server.
    type: str
  url:
    description:
      - URL of the Jenkins server.
    default: http://localhost:8080
    type: str
  user:
    description:
      - User to authenticate with the Jenkins server.
    type: str
  detach:
    description:
      - Enable detached mode to not wait for the build end.
    default: false
    type: bool
    version_added: 7.4.0
  time_between_checks:
    description:
      - Time in seconds to wait between requests to the Jenkins server.
      - This times must be higher than the configured quiet time for the job.
    default: 10
    type: int
    version_added: 7.4.0
a  
- name: Create a jenkins build using basic authentication
  community.general.jenkins_build:
    name: "test-check"
    args:
      cloud: "test"
      availability_zone: "test_az"
    state: present
    user: admin
    password: asdfg
    url: http://localhost:8080

- name: Stop a running jenkins build anonymously
  community.general.jenkins_build:
    name: "stop-check"
    build_number: 3
    state: stopped
    url: http://localhost:8080

- name: Delete a jenkins build using token authentication
  community.general.jenkins_build:
    name: "delete-experiment"
    build_number: 30
    state: absent
    user: Jenkins
    token: abcdefghijklmnopqrstuvwxyz123456
    url: http://localhost:8080
a  
name:
  description: Name of the jenkins job.
  returned: success
  type: str
  sample: "test-job"
state:
  description: State of the jenkins job.
  returned: success
  type: str
  sample: present
user:
  description: User used for authentication.
  returned: success
  type: str
  sample: admin
url:
  description: URL to connect to the Jenkins server.
  returned: success
  type: str
  sample: https://jenkins.mydomain.com
build_info:
  description: Build info of the jenkins job.
  returned: success
  type: dict
N)sleepTF)AnsibleModulemissing_required_lib)	to_nativec                   <    e Zd Zd Zd Zd Zd Zd Zd Zd Z	d Z
y	)
JenkinsBuildc                 4   || _         |j                  j                  d      | _        |j                  j                  d      | _        |j                  j                  d      | _        |j                  j                  d      | _        |j                  j                  d      | _        |j                  j                  d      | _        |j                  j                  d      | _	        |j                  j                  d      | _
        |j                  j                  d	      | _        |j                  j                  d
      | _        | j                         | _        d| j                  | j                  | j                  | j                  d| _        d| _        y )Nnamepasswordargsstatetokenuserurlbuild_numberdetachtime_between_checksF)changedr   r   r   r   zexcluded state)moduleparamsgetr   r   r   r   r   r   jenkins_urlr   r   r   get_jenkins_connectionserverresult
EXCL_STATE)selfr   s     s/home/dcms/DCMS/lib/python3.12/site-packages/ansible_collections/community/general/plugins/modules/jenkins_build.py__init__zJenkinsBuild.__init__   s.   MM%%f-	))*5MM%%f-	]]&&w/
]]&&w/
MM%%f-	!==,,U3"MM--n=mm''1#)==#4#45J#K 113 ##IIIIZZ
 +    c                    	 | j                   rA| j                  r5t        j                  | j                  | j                   | j                        S | j                   rA| j
                  r5t        j                  | j                  | j                   | j
                        S | j                   rB| j                  s6| j
                  s*t        j                  | j                  | j                         S t        j                  | j                        S # t        $ r2}| j                  j                  dt        |      z         Y d }~y d }~ww xY w)Nz'Unable to connect to Jenkins server, %s)msg)
r   r   jenkinsJenkinsr   r   	Exceptionr   	fail_jsonr	   r    es     r!   r   z#JenkinsBuild.get_jenkins_connection   s    
	`		dmmt'7'7DMMRR))

t'7'7DJJOO))T]]djjt'7'7CCt'7'788 	`KK!!&OR[\]R^&^!__	`s,   AD	 AD	 AD	 *D	 		E(D??Ec                     	 | j                   j                  | j                        d   }|S # t        $ rF}| j                  j                  dt        |      z  t        j                                Y d }~S d }~ww xY w)NnextBuildNumberz.Unable to get job info from Jenkins server, %sr%   	exception)	r   get_job_infor   r(   r   r)   r	   	traceback
format_exc)r    r   r+   s      r!   get_next_buildzJenkinsBuild.get_next_build   s    	D;;33DII>?PQL
 	  	DKK!!&VYbcdYe&e,5,@,@,B " D D 		Ds   (, 	A;;A66A;c                 R   	 | j                   j                  | j                  | j                        }|S # t        j
                  $ r}i }d|d<   |cY d }~S d }~wt        $ rE}| j                  j                  dt        |      z  t        j                                Y d }~y d }~ww xY w)NABSENTr   z%Unable to fetch build information, %sr.   )r   get_build_infor   r   r&   JenkinsExceptionr(   r   r)   r	   r1   r2   )r    responser+   s      r!   get_build_statuszJenkinsBuild.get_build_status   s    		D{{11$))T=N=NOHO'' 	H!)HXO 	DKK!!&MPYZ[P\&\,5,@,@,B " D D	Ds'   14 B&AB&B&!;B!!B&c           	         | j                         | _        	 | j                  &| j                  j	                  | j
                         y | j                  j	                  | j
                  | j                         y # t        $ rR}| j                  j                  d| j                  dt        |      t        j                                Y d }~y d }~ww xY w)NzUnable to create build for : r.   )r3   r   r   r   	build_jobr   r(   r   r)   r   r	   r1   r2   r*   s     r!   present_buildzJenkinsBuild.present_build   s     //1	Dyy %%dii0%%dii; 	DKK!!TM]M]_hij_k&l,5,@,@,B " D D	Ds   1A: 	0A: :	CACCc           	         d }	 | j                   j                  | j                  | j                        }|d   du r0| j                   j	                  | j                  | j                         |d   du r' | j
                  j                  di | j                   y y # t        $ rR}| j
                  j                  d| j                  dt        |      t        j                                Y d }~y d }~ww xY w)NbuildingTFzUnable to stop build for r;   r.    )r   r6   r   r   
stop_buildr   	exit_jsonr   r(   r)   r   r	   r1   r2   )r    
build_infor+   s      r!   stopped_buildzJenkinsBuild.stopped_build   s    
		533DIIt?P?PQJ*%-&&tyy$2C2CD
 *%.%%%44 /	  	DKK!!4K[K[]fgh]i&j,5,@,@,B " D D	Ds   A'B 	C5#AC00C5c           	      "   	 | j                   j                  | j                  | j                         y # t        $ rR}| j
                  j                  d| j                  dt        |      t        j                                Y d }~y d }~ww xY w)NzUnable to delete build for r;   r.   )r   delete_buildr   r   r(   r   r)   r   r	   r1   r2   r*   s     r!   absent_buildzJenkinsBuild.absent_build   sv    	DKK$$TYY0A0AB 	DKK!!TM]M]_hij_k&l,5,@,@,B " D D	Ds   03 	BAB		Bc                    | j                   }| j                         }|d   ?| j                  rd|d<   ||d<   |S t        | j                         | j                          |S | j                  dk(  r|d   dk(  rd|d<   ||d<   |S | j                  dk(  r|d   dk(  rd|d<   ||d<   |S | j                  dk7  r|d   d	k(  rd|d<   ||d<   |S d|d
<   ||d<   |S )Nr   Tr   rC   stoppedABORTEDabsentr5   SUCCESSfailed)r   r9   r   r   r   
get_resultr   )r    r   build_statuss      r!   rN   zJenkinsBuild.get_result   s   ,,.!){{$(y!'3|$$**+OO  zzY&<+AY+N$(y!'3|$  x'L,Bh,N$(y!'3|$  x'L,Bi,O$(y!'3|$
  $(x '3|$r#   N)__name__
__module____qualname__r"   r   r3   r9   r=   rD   rG   rN   r@   r#   r!   r   r      s.    +2`
D
D5Dr#   r   c                 V    t         s#| j                  t        dd      t               y y )Nzpython-jenkinsz<https://python-jenkins.readthedocs.io/en/latest/install.html)r   r.   )python_jenkins_installedr)   r   JENKINS_IMP_ERR)r   s    r!   test_dependenciesrV     s/    #$%5)gi% 	 	' $r#   c                     t        t        t        d      t        d      t        d      t        d      t        g dd	      t        d      t        d
      t               t        dd      t        dd      
      ddggdddgdgdddgdgg      } t        |        t        |       }| j                  j                  d      dk(  r|j                          n?| j                  j                  d      dk(  r|j                          n|j                          t        |j                         |j                         } | j                  di | y )Ndict)typeintT)required)no_log)presentrK   rI   r]   )choicesdefaultzhttp://localhost:8080)r_   boolF)rY   r_   
   )
r   r   r   r   r   r   r   r   r   r   r   r   r   rK   r   rI   )argument_specmutually_exclusiverequired_ifr@   )r   rX   rV   r   r   r   r=   rD   rG   r   r   rN   rB   )r   jenkins_buildr   s      r!   mainrf     s-   6"5)t$&?Sd#45VU3 $% <
 (12x.)94@7IXfWgimBnoF" f (M}}!Y.##%			7	#y	0##%""$	-
+
+,%%'FFvr#   __main__)
__future__r   r   r   rY   __metaclass__DOCUMENTATIONEXAMPLESRETURNr1   timer   rU   r&   rT   ImportErrorr2   ansible.module_utils.basicr   r   +ansible.module_utils.common.text.convertersr	   r   rV   rf   rP   r@   r#   r!   <module>rq      s    A @FP:
6  %#
 K Ay yx'B zF [  %*i**,O$%s   A A-,A-