
    VhM                         d dl mZmZmZ eZdZdZdZd dl	m
Z
 d dlmZ d dlmZ d dlmZmZmZmZ d d	lmZ  G d
 de      Zd Zedk(  r e        yy)    )absolute_importdivisionprint_functiona  
module: gitlab_runner
short_description: Create, modify and delete GitLab Runners
description:
  - Register, update and delete runners on GitLab Server side with the GitLab API.
  - All operations are performed using the GitLab API v4.
  - For details, consult the full API documentation at U(https://docs.gitlab.com/ee/api/runners.html) and
    U(https://docs.gitlab.com/ee/api/users.html#create-a-runner-linked-to-a-user).
  - A valid private API token is required for all operations. You can create as many tokens as you like using the GitLab web
    interface at U(https://$GITLAB_URL/profile/personal_access_tokens).
  - A valid registration token is required for registering a new runner. To create shared runners, you need to ask your administrator
    to give you this token. It can be found at U(https://$GITLAB_URL/admin/runners/).
  - This module does not handle the C(gitlab-runner) process part, but only manages the runner on GitLab Server side through
    its API. Once the module has created the runner, you may use the generated token to run C(gitlab-runner register) command.
notes:
  - To create a new runner at least the O(api_token), O(description) and O(api_url) options are required.
  - Runners need to have unique descriptions, since this attribute is used as key for idempotency.
author:
  - Samy Coenen (@SamyCoenen)
  - Guillaume Martinez (@Lunik)
requirements:
  - python-gitlab >= 1.5.0 for legacy runner registration workflow (runner registration token -
    U(https://docs.gitlab.com/runner/register/#register-with-a-runner-registration-token-deprecated))
  - python-gitlab >= 4.0.0 for new runner registration workflow (runner authentication token -
    U(https://docs.gitlab.com/runner/register/#register-with-a-runner-authentication-token))
extends_documentation_fragment:
  - community.general.auth_basic
  - community.general.gitlab
  - community.general.attributes

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

options:
  group:
    description:
      - ID or full path of the group in the form group/subgroup.
      - Mutually exclusive with O(owned) and O(project).
      - Must be group's numeric ID if O(registration_token) is not set and O(state=present).
    type: str
    version_added: '6.5.0'
  project:
    description:
      - ID or full path of the project in the form of group/name.
      - Mutually exclusive with O(owned) since community.general 4.5.0.
      - Mutually exclusive with O(group).
      - Must be project's numeric ID if O(registration_token) is not set and O(state=present).
    type: str
    version_added: '3.7.0'
  description:
    description:
      - The unique name of the runner.
    required: true
    type: str
    aliases:
      - name
  state:
    description:
      - Make sure that the runner with the same name exists with the same configuration or delete the runner with the same
        name.
    required: false
    default: present
    choices: ["present", "absent"]
    type: str
  registration_token:
    description:
      - The registration token is used to register new runners before GitLab 16.0.
      - Required if O(state=present) for GitLab < 16.0.
      - If set, the runner will be created using the old runner creation workflow.
      - If not set, the runner will be created using the new runner creation workflow, introduced in GitLab 16.0.
      - If not set, requires python-gitlab >= 4.0.0.
    type: str
  owned:
    description:
      - Searches only runners available to the user when searching for existing, when false admin token required.
      - Mutually exclusive with O(project) since community.general 4.5.0.
      - Mutually exclusive with O(group).
    default: false
    type: bool
    version_added: 2.0.0
  active:
    description:
      - Define if the runners is immediately active after creation.
      - Mutually exclusive with O(paused).
    required: false
    default: true
    type: bool
  paused:
    description:
      - Define if the runners is active or paused after creation.
      - Mutually exclusive with O(active).
    required: false
    default: false
    type: bool
    version_added: 8.1.0
  locked:
    description:
      - Determines if the runner is locked or not.
    required: false
    default: false
    type: bool
  access_level:
    description:
      - Determines if a runner can pick up jobs only from protected branches.
      - If O(access_level_on_creation) is not explicitly set to V(true), this option is ignored on registration and is only
        applied on updates.
      - If set to V(not_protected), runner can pick up jobs from both protected and unprotected branches.
      - If set to V(ref_protected), runner can pick up jobs only from protected branches.
      - Before community.general 8.0.0 the default was V(ref_protected). This was changed to no default in community.general
        8.0.0. If this option is not specified explicitly, GitLab will use V(not_protected) on creation, and the value set
        will not be changed on any updates.
    required: false
    choices: ["not_protected", "ref_protected"]
    type: str
  access_level_on_creation:
    description:
      - Whether the runner should be registered with an access level or not.
      - If set to V(true), the value of O(access_level) is used for runner registration.
      - If set to V(false), GitLab registers the runner with the default access level.
      - The default of this option changed to V(true) in community.general 7.0.0. Before, it was V(false).
    required: false
    default: true
    type: bool
    version_added: 6.3.0
  maximum_timeout:
    description:
      - The maximum time that a runner has to complete a specific job.
    required: false
    default: 3600
    type: int
  run_untagged:
    description:
      - Run untagged jobs or not.
    required: false
    default: true
    type: bool
  tag_list:
    description: The tags that apply to the runner.
    required: false
    default: []
    type: list
    elements: str
a	  
- name: Create an instance-level runner
  community.general.gitlab_runner:
    api_url: https://gitlab.example.com/
    api_token: "{{ access_token }}"
    description: Docker Machine t1
    state: present
    active: true
    tag_list: ['docker']
    run_untagged: false
    locked: false
  register: runner # Register module output to run C(gitlab-runner register) command in another task

- name: Create a group-level runner
  community.general.gitlab_runner:
    api_url: https://gitlab.example.com/
    api_token: "{{ access_token }}"
    description: Docker Machine t1
    state: present
    active: true
    tag_list: ['docker']
    run_untagged: false
    locked: false
    group: top-level-group/subgroup
  register: runner # Register module output to run C(gitlab-runner register) command in another task

- name: Create a project-level runner
  community.general.gitlab_runner:
    api_url: https://gitlab.example.com/
    api_token: "{{ access_token }}"
    description: Docker Machine t1
    state: present
    active: true
    tag_list: ['docker']
    run_untagged: false
    locked: false
    project: top-level-group/subgroup/project
  register: runner # Register module output to run C(gitlab-runner register) command in another task

- name: "Register instance-level runner with registration token (deprecated)"
  community.general.gitlab_runner:
    api_url: https://gitlab.example.com/
    api_token: "{{ access_token }}"
    registration_token: 4gfdsg345
    description: Docker Machine t1
    state: present
    active: true
    tag_list: ['docker']
    run_untagged: false
    locked: false
  register: runner # Register module output to run C(gitlab-runner register) command in another task

- name: "Delete runner"
  community.general.gitlab_runner:
    api_url: https://gitlab.example.com/
    api_token: "{{ access_token }}"
    description: Docker Machine t1
    state: absent

- name: Delete an owned runner as a non-admin
  community.general.gitlab_runner:
    api_url: https://gitlab.example.com/
    api_token: "{{ access_token }}"
    description: Docker Machine t1
    owned: true
    state: absent

- name: "Register a project-level runner with registration token (deprecated)"
  community.general.gitlab_runner:
    api_url: https://gitlab.example.com/
    api_token: "{{ access_token }}"
    registration_token: 4gfdsg345
    description: MyProject runner
    state: present
    project: mygroup/mysubgroup/myproject
  register: runner # Register module output to run C(gitlab-runner register) command in another task
a  
msg:
  description: Success or failure message.
  returned: always
  type: str
  sample: "Success"

result:
  description: JSON-parsed response from the server.
  returned: always
  type: dict

error:
  description: The error message returned by the GitLab API.
  returned: failed
  type: str
  sample: "400: path is already in use"

runner:
  description: API object.
  returned: always
  type: dict
)basic_auth_argument_spec)AnsibleModule)	to_native)auth_argument_specgitlab_authenticationgitlablist_all_kwargs)LooseVersionc                   @    e Zd Zd	dZd Z	 d Z	 d Z	 d Z	 d Zd Z	y)
GitLabRunnerNc                 2   || _         || _        d | _        |r|j                  j                  | _        y |r|j                  j                  | _        y |j                  d   r|j                  j                  | _        y |j                  j                  | _        y )Nowned)_module_gitlabrunner_objectrunnerslist_runners_endpointparamsall)selfmodulegitlab_instancegroupprojects        s/home/dcms/DCMS/lib/python3.12/site-packages/ansible_collections/community/general/plugins/modules/gitlab_runner.py__init__zGitLabRunner.__init__  sx    &!
 %,__%9%9D"%*]]%7%7D"]]7#%4%<%<%A%AD"%4%<%<%@%@D"    c                 ^   d}|d   |d   |d   |d   d}|j                  d      	|d   |d<   n|d   |d<   |j                  d	      |d	   |d	<   | j                  ||d
<   |j                  d      	|d   |d<   nC|j                  d      d|d<   |d   |d<   n$|j                  d      d|d<   |d   |d<   nd|d<   | j                  j                  d   }|s|j	                  d	d        | j                  |      }d}nh| j                  | j                  |      \  }}|rG| j                  j                  r | j                  j                  dd|z         	 |j                          || _        |S # t        $ r2}| j                  j                  dt        |      z         Y d }~?d }~ww xY w)NFlockedrun_untaggedmaximum_timeouttag_list)r#   r$   r%   r&   pausedactiveaccess_leveldescriptionregistration_tokentokenr   
group_typerunner_typegroup_idr   project_type
project_idinstance_typeaccess_level_on_creationTz"Successfully updated the runner %schangedmsgzFailed to update runner: %s r6   )getr   r   r   popcreate_runnerupdate_runner
check_mode	exit_jsonsave	Exception	fail_jsonr   )r   r*   optionsr5   	argumentsr3   runneres           r   create_or_update_runnerz$GitLabRunner.create_or_update_runner$  s    h'#N3&'89
+	
	 ;;x ,")("3Ih")("3Ih;;~&2(/(?In%%'2Im${{/0<%,-A%B	'"W%1+7	-((/(8	*%Y'3+9	-(*1)*<	,'+:	-('+||':':;U'V$+nd3''	2FG"001C1CYOOGV<<**LL**4=ado=o*p^KKM $	 ! ^LL**/MPYZ[P\/\*]]^s   E1 1	F,:(F''F,c                 2   | j                   j                  ry	 |j                  d      '| j                  j                  j                  |      }|S t        t        j                        t        d      k  r| j                   j                  d       S | j                  j                  j                  j                  |      }	 |S # t        j                  j                  $ r3}| j                   j                  dt        |      z         Y d }~S d }~ww xY w)NTr,   z4.0.0zCNew runner creation workflow requires python-gitlab 4.0.0 or higherr7   zFailed to create runner: %s )r   r<   r8   r   r   creater   r   __version__r@   user
exceptionsGitlabCreateErrorr   )r   rB   rC   rD   s       r   r:   zGitLabRunner.create_runnerZ  s    <<""	V}}W%1--44Y?  f001L4II&&+p&q 	 **2299)D  !!33 	VLL""'E	RS'T"UU	Vs$   6C AC /C D#(DDc                 *   d}|j                         D ]{  \  }}|	t        |t              rDt        ||      }|j	                          |}|j	                          ||k7  sMt        |||       d}]t        ||      |k7  smt        |||       d}} ||fS )NFT)items
isinstancer   getattrsortsetattr)r   rC   rB   r5   arg_key	arg_valuelist1list2s           r   r;   zGitLabRunner.update_runnern  s    "+//"3 	'GY$i.#FG4EJJL%EJJL~;"&vw/9<;"&	'   r!   c                 <    | j                   di t        }|D ]  }t        |d      rA|j                  |k(  s| j                  j
                  j                  |j                        c S |d   |k(  sY| j                  j
                  j                  |d         c S  y )Nr*   id )r   r   hasattrr*   r   r   r8   rW   )r   r*   r   rC   s       r   find_runnerzGitLabRunner.find_runner  s    ($((;?; 	BF v}-&&+5<<//33FII>>=)[8<<//33F4LAA	Br!   c                 :    | j                  |      }|r|| _        yy)NTF)rZ   r   )r   r*   rC   s      r   exists_runnerzGitLabRunner.exists_runner  s#    !!+.!'Dr!   c                 h    | j                   j                  ry| j                  }|j                         S )NT)r   r<   r   delete)r   rC   s     r   delete_runnerzGitLabRunner.delete_runner  s)    <<""##}}r!   )NN)
__name__
__module____qualname__r    rE   r:   r;   rZ   r\   r_   rX   r!   r   r   r     s<    A"1f !(Br!   r   c                     t               } | j                  t                      | j                  t        t        dddg      t        dd      t        dd      t        dd      t        ddg 	      t        dd      t        dd      t        dd
dg      t        dd      t        dd      t        dd      t        d      t        d      t        ddddg                   t	        | ddgddgddgddgddgddgddgddgddgg	ddggg dgd       }t        |      }|j                  d!   }|j                  d"   }|j                  d   }|j                  d   }|j                  d#   }|j                  d$   }|j                  d%   }	|j                  d&   }
|j                  d'   }|j                  d(   }|j                  d   }|j                  d   }d }d }|r	 |j                  j                  |      }n|r	 |j                  j                  |      }t        ||||      }|j!                  |      }|dk(  r<|r'|j#                          |j%                  dd,|z  -       n|j%                  dd.-       |dk(  r||||	|
||||d/	}t'        |j(                  j+                         d0         t'        d1      k\  r||d<   |j-                  ||      r,|j%                  d|j.                  j0                  d2|z  3       y |j%                  d|j.                  j0                  d4|z  3       y y # t        j                  j                  $ r*}|j                  d)|z  t        |      *       Y d }~Ud }~ww xY w# t        j                  j                  $ r*}|j                  d+|z  t        |      *       Y d }~d }~ww xY w)5NstrTname)typerequiredaliasesbool)rf   defaultFr   )rf   elementsrj   not_protectedref_protected)rf   choicesinti  )rf   no_log)rf   presentabsent)rf   rj   rn   )r*   r(   r'   r   r&   r$   r#   r)   r3   r%   r+   r   r   stateapi_username	api_tokenapi_oauth_tokenapi_job_tokenr   r   r   r(   r'   api_password)rt   ru   rv   rw   )argument_specmutually_exclusiverequired_togetherrequired_one_ofsupports_check_moders   r*   r&   r$   r#   r)   r%   r+   zNo such a project %s)r6   	exceptionzNo such a group %szSuccessfully deleted runner %sr4   z!Runner deleted or does not exists)	r(   r&   r$   r#   r)   r%   r+   r   r   r   z14.8.0z-Successfully created or updated the runner %s)r5   rC   r6   zNo need to update the runner %s)r   updater	   dictr   r
   r   projectsr8   r   rJ   GitlabGetErrorr@   r   groupsr   r\   r_   r=   r   r   versionrE   r   _attrs)ry   r   r   rs   runner_descriptionrunner_activerunner_pausedr&   r$   runner_lockedr)   r%   r+   r   r   gitlab_projectgitlab_grouprD   gitlab_runnerrunner_existsrunner_valuess                        r   mainr     s   ,.M+-.edVHE./.6E2>vt4/u.PQ!%64!@%6U48% y8Y:OP " #[)./_-+,/* g x 

 ^,
 N
 !'F. ,F3OMM'"E}5MM(+MMM(+M}}Z(H==0LMM(+M==0Lmm$56O';<mmI&GMM'"ENL	[,5599'BN 
	W*1155e<L !,WM!//0BCM'')T/ORd/deU0ST	# (#(."4

 --557:;|H?UU&3M(#001C]ST-2M2M2T2T!PSe!e  g U=3N3N3U3U!BEW!W  Y' %   // 	[!7'!AYWX\ZZ	[
   // 	W!5!=STVV	Ws0   #L0 M: 0M7M22M7:ON<<O__main__N)
__future__r   r   r   rf   __metaclass__DOCUMENTATIONEXAMPLESRETURNansible.module_utils.apir   ansible.module_utils.basicr   +ansible.module_utils.common.text.convertersr   Aansible_collections.community.general.plugins.module_utils.gitlabr	   r
   r   r   Bansible_collections.community.general.plugins.module_utils.versionr   objectr   r   r`   rX   r!   r   <module>r      sr    A @QfL\
0 > 4 A 
 \R6 RjfYR zF r!   