2020-03-09 10:11:07 +01:00
|
|
|
#!/usr/bin/python
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
2022-08-05 12:28:29 +02:00
|
|
|
# Copyright (c) 2021, Raphaël Droz (raphael.droz@gmail.com)
|
|
|
|
# Copyright (c) 2019, Guillaume Martinez (lunik@tiwabbit.fr)
|
|
|
|
# Copyright (c) 2018, Samy Coenen <samy.coenen@nubera.be>
|
|
|
|
# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt)
|
|
|
|
# SPDX-License-Identifier: GPL-3.0-or-later
|
2020-03-09 10:11:07 +01:00
|
|
|
|
|
|
|
from __future__ import absolute_import, division, print_function
|
|
|
|
__metaclass__ = type
|
|
|
|
|
|
|
|
DOCUMENTATION = '''
|
|
|
|
---
|
|
|
|
module: gitlab_runner
|
2022-11-09 07:18:40 +01:00
|
|
|
short_description: Create, modify and delete GitLab Runners
|
2020-03-09 10:11:07 +01:00
|
|
|
description:
|
|
|
|
- Register, update and delete runners 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).
|
|
|
|
- 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/).
|
|
|
|
notes:
|
2023-06-15 15:46:44 +02:00
|
|
|
- To create a new runner at least the O(api_token), O(description) and O(api_url) options are required.
|
2020-03-09 10:11:07 +01:00
|
|
|
- Runners need to have unique descriptions.
|
|
|
|
author:
|
|
|
|
- Samy Coenen (@SamyCoenen)
|
|
|
|
- Guillaume Martinez (@Lunik)
|
|
|
|
requirements:
|
|
|
|
- python-gitlab >= 1.5.0
|
|
|
|
extends_documentation_fragment:
|
2021-12-20 21:59:12 +01:00
|
|
|
- community.general.auth_basic
|
|
|
|
- community.general.gitlab
|
2023-02-20 17:26:28 +01:00
|
|
|
- community.general.attributes
|
|
|
|
|
|
|
|
attributes:
|
|
|
|
check_mode:
|
|
|
|
support: full
|
|
|
|
diff_mode:
|
|
|
|
support: none
|
2020-03-09 10:11:07 +01:00
|
|
|
|
|
|
|
options:
|
2023-03-25 08:23:20 +01:00
|
|
|
group:
|
|
|
|
description:
|
|
|
|
- ID or full path of the group in the form group/subgroup.
|
2023-06-15 15:46:44 +02:00
|
|
|
- Mutually exclusive with O(owned) and O(project).
|
2023-11-13 20:22:33 +01:00
|
|
|
- Must be group's numeric ID if O(registration_token) is not set and O(state=present).
|
2023-03-25 08:23:20 +01:00
|
|
|
type: str
|
|
|
|
version_added: '6.5.0'
|
2021-09-13 07:16:06 +02:00
|
|
|
project:
|
|
|
|
description:
|
|
|
|
- ID or full path of the project in the form of group/name.
|
2023-06-15 15:46:44 +02:00
|
|
|
- Mutually exclusive with O(owned) since community.general 4.5.0.
|
|
|
|
- Mutually exclusive with O(group).
|
2023-11-13 20:22:33 +01:00
|
|
|
- Must be project's numeric ID if O(registration_token) is not set and O(state=present).
|
2021-09-13 07:16:06 +02:00
|
|
|
type: str
|
|
|
|
version_added: '3.7.0'
|
2020-03-09 10:11:07 +01:00
|
|
|
description:
|
|
|
|
description:
|
|
|
|
- The unique name of the runner.
|
2022-09-06 20:42:17 +02:00
|
|
|
required: true
|
2020-03-09 10:11:07 +01:00
|
|
|
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.
|
2022-09-06 20:42:17 +02:00
|
|
|
required: false
|
2020-03-09 10:11:07 +01:00
|
|
|
default: present
|
|
|
|
choices: ["present", "absent"]
|
|
|
|
type: str
|
|
|
|
registration_token:
|
|
|
|
description:
|
2023-11-13 20:22:33 +01:00
|
|
|
- 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.
|
2020-03-09 10:11:07 +01:00
|
|
|
type: str
|
2021-01-24 21:02:44 +01:00
|
|
|
owned:
|
|
|
|
description:
|
|
|
|
- Searches only runners available to the user when searching for existing, when false admin token required.
|
2023-06-15 15:46:44 +02:00
|
|
|
- Mutually exclusive with O(project) since community.general 4.5.0.
|
|
|
|
- Mutually exclusive with O(group).
|
2022-08-24 19:59:56 +02:00
|
|
|
default: false
|
2021-01-24 21:02:44 +01:00
|
|
|
type: bool
|
|
|
|
version_added: 2.0.0
|
2020-03-09 10:11:07 +01:00
|
|
|
active:
|
|
|
|
description:
|
|
|
|
- Define if the runners is immediately active after creation.
|
2023-11-13 20:22:33 +01:00
|
|
|
- Mutually exclusive with O(paused).
|
2022-09-06 20:42:17 +02:00
|
|
|
required: false
|
2022-08-24 19:59:56 +02:00
|
|
|
default: true
|
2020-03-09 10:11:07 +01:00
|
|
|
type: bool
|
2023-11-13 20:22:33 +01:00
|
|
|
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
|
2020-03-09 10:11:07 +01:00
|
|
|
locked:
|
|
|
|
description:
|
|
|
|
- Determines if the runner is locked or not.
|
2022-09-06 20:42:17 +02:00
|
|
|
required: false
|
|
|
|
default: false
|
2020-03-09 10:11:07 +01:00
|
|
|
type: bool
|
|
|
|
access_level:
|
|
|
|
description:
|
2021-06-13 08:25:50 +02:00
|
|
|
- Determines if a runner can pick up jobs only from protected branches.
|
2023-06-15 15:46:44 +02:00
|
|
|
- If O(access_level_on_creation) is not explicitly set to V(true), this option is ignored on registration and
|
2023-01-30 21:03:13 +01:00
|
|
|
is only applied on updates.
|
2023-06-15 15:46:44 +02:00
|
|
|
- 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.
|
2023-10-09 13:31:27 +02:00
|
|
|
- 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.
|
2022-09-06 20:42:17 +02:00
|
|
|
required: false
|
2023-02-10 13:54:24 +01:00
|
|
|
choices: ["not_protected", "ref_protected"]
|
2020-03-09 10:11:07 +01:00
|
|
|
type: str
|
2023-01-30 21:03:13 +01:00
|
|
|
access_level_on_creation:
|
|
|
|
description:
|
|
|
|
- Whether the runner should be registered with an access level or not.
|
2023-06-15 15:46:44 +02:00
|
|
|
- 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).
|
2023-01-30 21:03:13 +01:00
|
|
|
required: false
|
2023-04-26 07:32:00 +02:00
|
|
|
default: true
|
2023-01-30 21:03:13 +01:00
|
|
|
type: bool
|
|
|
|
version_added: 6.3.0
|
2020-03-09 10:11:07 +01:00
|
|
|
maximum_timeout:
|
|
|
|
description:
|
2021-10-30 16:44:22 +02:00
|
|
|
- The maximum time that a runner has to complete a specific job.
|
2022-09-06 20:42:17 +02:00
|
|
|
required: false
|
2020-03-09 10:11:07 +01:00
|
|
|
default: 3600
|
|
|
|
type: int
|
|
|
|
run_untagged:
|
|
|
|
description:
|
|
|
|
- Run untagged jobs or not.
|
2022-09-06 20:42:17 +02:00
|
|
|
required: false
|
2022-08-24 19:59:56 +02:00
|
|
|
default: true
|
2020-03-09 10:11:07 +01:00
|
|
|
type: bool
|
|
|
|
tag_list:
|
|
|
|
description: The tags that apply to the runner.
|
2022-09-06 20:42:17 +02:00
|
|
|
required: false
|
2020-03-09 10:11:07 +01:00
|
|
|
default: []
|
|
|
|
type: list
|
2021-02-16 07:11:37 +01:00
|
|
|
elements: str
|
2020-03-09 10:11:07 +01:00
|
|
|
'''
|
|
|
|
|
|
|
|
EXAMPLES = '''
|
|
|
|
- name: "Register runner"
|
2020-07-13 21:50:31 +02:00
|
|
|
community.general.gitlab_runner:
|
2020-03-09 10:11:07 +01:00
|
|
|
api_url: https://gitlab.example.com/
|
|
|
|
api_token: "{{ access_token }}"
|
|
|
|
registration_token: 4gfdsg345
|
|
|
|
description: Docker Machine t1
|
|
|
|
state: present
|
2022-09-06 20:42:17 +02:00
|
|
|
active: true
|
2020-03-09 10:11:07 +01:00
|
|
|
tag_list: ['docker']
|
2022-09-06 20:42:17 +02:00
|
|
|
run_untagged: false
|
|
|
|
locked: false
|
2020-03-09 10:11:07 +01:00
|
|
|
|
|
|
|
- name: "Delete runner"
|
2020-07-13 21:50:31 +02:00
|
|
|
community.general.gitlab_runner:
|
2020-03-09 10:11:07 +01:00
|
|
|
api_url: https://gitlab.example.com/
|
|
|
|
api_token: "{{ access_token }}"
|
|
|
|
description: Docker Machine t1
|
|
|
|
state: absent
|
2021-01-24 21:02:44 +01:00
|
|
|
|
|
|
|
- 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
|
2022-08-24 19:59:56 +02:00
|
|
|
owned: true
|
2021-01-24 21:02:44 +01:00
|
|
|
state: absent
|
2021-09-13 07:16:06 +02:00
|
|
|
|
|
|
|
- name: Register runner for a specific project
|
|
|
|
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
|
2020-03-09 10:11:07 +01:00
|
|
|
'''
|
|
|
|
|
|
|
|
RETURN = '''
|
|
|
|
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
|
|
|
|
'''
|
|
|
|
|
|
|
|
from ansible.module_utils.api import basic_auth_argument_spec
|
2022-09-12 20:30:02 +02:00
|
|
|
from ansible.module_utils.basic import AnsibleModule
|
2021-06-26 23:59:11 +02:00
|
|
|
from ansible.module_utils.common.text.converters import to_native
|
2020-03-09 10:11:07 +01:00
|
|
|
|
2022-09-12 20:30:02 +02:00
|
|
|
from ansible_collections.community.general.plugins.module_utils.gitlab import (
|
2024-02-25 21:26:37 +01:00
|
|
|
auth_argument_spec, gitlab_authentication, gitlab, list_all_kwargs
|
2022-09-12 20:30:02 +02:00
|
|
|
)
|
|
|
|
|
2020-03-09 10:11:07 +01:00
|
|
|
|
2023-11-13 20:22:33 +01:00
|
|
|
from ansible_collections.community.general.plugins.module_utils.version import LooseVersion
|
|
|
|
|
|
|
|
|
2020-03-09 10:11:07 +01:00
|
|
|
class GitLabRunner(object):
|
2023-03-25 08:23:20 +01:00
|
|
|
def __init__(self, module, gitlab_instance, group=None, project=None):
|
2020-03-09 10:11:07 +01:00
|
|
|
self._module = module
|
|
|
|
self._gitlab = gitlab_instance
|
2023-03-25 08:23:20 +01:00
|
|
|
self.runner_object = None
|
|
|
|
|
2021-09-13 07:16:06 +02:00
|
|
|
# Whether to operate on GitLab-instance-wide or project-wide runners
|
|
|
|
# See https://gitlab.com/gitlab-org/gitlab-ce/issues/60774
|
|
|
|
# for group runner token access
|
2022-02-16 22:54:46 +01:00
|
|
|
if project:
|
|
|
|
self._runners_endpoint = project.runners.list
|
2023-03-25 08:23:20 +01:00
|
|
|
elif group:
|
|
|
|
self._runners_endpoint = group.runners.list
|
2022-02-16 22:54:46 +01:00
|
|
|
elif module.params['owned']:
|
|
|
|
self._runners_endpoint = gitlab_instance.runners.list
|
|
|
|
else:
|
|
|
|
self._runners_endpoint = gitlab_instance.runners.all
|
|
|
|
|
2021-11-16 13:01:32 +01:00
|
|
|
def create_or_update_runner(self, description, options):
|
2020-03-09 10:11:07 +01:00
|
|
|
changed = False
|
|
|
|
|
2023-01-30 21:03:13 +01:00
|
|
|
arguments = {
|
|
|
|
'locked': options['locked'],
|
|
|
|
'run_untagged': options['run_untagged'],
|
|
|
|
'maximum_timeout': options['maximum_timeout'],
|
|
|
|
'tag_list': options['tag_list'],
|
|
|
|
}
|
2023-11-13 20:22:33 +01:00
|
|
|
|
|
|
|
if options.get('paused') is not None:
|
|
|
|
arguments['paused'] = options['paused']
|
|
|
|
else:
|
|
|
|
arguments['active'] = options['active']
|
|
|
|
|
2023-03-04 10:14:58 +01:00
|
|
|
if options.get('access_level') is not None:
|
2023-02-10 13:54:24 +01:00
|
|
|
arguments['access_level'] = options['access_level']
|
2020-03-09 10:11:07 +01:00
|
|
|
# Because we have already call userExists in main()
|
2021-11-16 13:01:32 +01:00
|
|
|
if self.runner_object is None:
|
2023-01-30 21:03:13 +01:00
|
|
|
arguments['description'] = description
|
2023-11-13 20:22:33 +01:00
|
|
|
if options.get('registration_token') is not None:
|
|
|
|
arguments['token'] = options['registration_token']
|
|
|
|
elif options.get('group') is not None:
|
|
|
|
arguments['runner_type'] = 'group_type'
|
|
|
|
arguments['group_id'] = options['group']
|
|
|
|
elif options.get('project') is not None:
|
|
|
|
arguments['runner_type'] = 'project_type'
|
|
|
|
arguments['project_id'] = options['project']
|
|
|
|
else:
|
|
|
|
arguments['runner_type'] = 'instance_type'
|
2023-01-30 21:03:13 +01:00
|
|
|
|
|
|
|
access_level_on_creation = self._module.params['access_level_on_creation']
|
2023-02-10 13:54:24 +01:00
|
|
|
if not access_level_on_creation:
|
2023-03-04 10:14:58 +01:00
|
|
|
arguments.pop('access_level', None)
|
2023-01-30 21:03:13 +01:00
|
|
|
|
|
|
|
runner = self.create_runner(arguments)
|
2020-03-09 10:11:07 +01:00
|
|
|
changed = True
|
|
|
|
else:
|
2023-01-30 21:03:13 +01:00
|
|
|
changed, runner = self.update_runner(self.runner_object, arguments)
|
2023-11-13 20:22:33 +01:00
|
|
|
if changed:
|
|
|
|
if self._module.check_mode:
|
|
|
|
self._module.exit_json(changed=True, msg="Successfully updated the runner %s" % description)
|
|
|
|
|
|
|
|
try:
|
|
|
|
runner.save()
|
|
|
|
except Exception as e:
|
|
|
|
self._module.fail_json(msg="Failed to update runner: %s " % to_native(e))
|
2020-03-09 10:11:07 +01:00
|
|
|
|
2021-11-16 13:01:32 +01:00
|
|
|
self.runner_object = runner
|
2023-11-13 20:22:33 +01:00
|
|
|
return changed
|
2020-03-09 10:11:07 +01:00
|
|
|
|
|
|
|
'''
|
|
|
|
@param arguments Attributes of the runner
|
|
|
|
'''
|
2021-11-16 13:01:32 +01:00
|
|
|
def create_runner(self, arguments):
|
2020-03-09 10:11:07 +01:00
|
|
|
if self._module.check_mode:
|
|
|
|
return True
|
|
|
|
|
|
|
|
try:
|
2023-11-13 20:22:33 +01:00
|
|
|
if arguments.get('token') is not None:
|
|
|
|
runner = self._gitlab.runners.create(arguments)
|
|
|
|
elif LooseVersion(gitlab.__version__) < LooseVersion('4.0.0'):
|
|
|
|
self._module.fail_json(msg="New runner creation workflow requires python-gitlab 4.0.0 or higher")
|
|
|
|
else:
|
|
|
|
runner = self._gitlab.user.runners.create(arguments)
|
2020-03-09 10:11:07 +01:00
|
|
|
except (gitlab.exceptions.GitlabCreateError) as e:
|
|
|
|
self._module.fail_json(msg="Failed to create runner: %s " % to_native(e))
|
|
|
|
|
|
|
|
return runner
|
|
|
|
|
|
|
|
'''
|
|
|
|
@param runner Runner object
|
|
|
|
@param arguments Attributes of the runner
|
|
|
|
'''
|
2021-11-16 13:01:32 +01:00
|
|
|
def update_runner(self, runner, arguments):
|
2020-03-09 10:11:07 +01:00
|
|
|
changed = False
|
|
|
|
|
|
|
|
for arg_key, arg_value in arguments.items():
|
|
|
|
if arguments[arg_key] is not None:
|
|
|
|
if isinstance(arguments[arg_key], list):
|
|
|
|
list1 = getattr(runner, arg_key)
|
|
|
|
list1.sort()
|
|
|
|
list2 = arguments[arg_key]
|
|
|
|
list2.sort()
|
2023-07-14 07:03:34 +02:00
|
|
|
if list1 != list2:
|
2020-03-09 10:11:07 +01:00
|
|
|
setattr(runner, arg_key, arguments[arg_key])
|
|
|
|
changed = True
|
|
|
|
else:
|
|
|
|
if getattr(runner, arg_key) != arguments[arg_key]:
|
|
|
|
setattr(runner, arg_key, arguments[arg_key])
|
|
|
|
changed = True
|
|
|
|
|
|
|
|
return (changed, runner)
|
|
|
|
|
|
|
|
'''
|
|
|
|
@param description Description of the runner
|
|
|
|
'''
|
2022-02-16 22:54:46 +01:00
|
|
|
def find_runner(self, description):
|
2024-02-25 21:26:37 +01:00
|
|
|
runners = self._runners_endpoint(**list_all_kwargs)
|
2021-01-24 21:02:44 +01:00
|
|
|
|
2020-03-09 10:11:07 +01:00
|
|
|
for runner in runners:
|
2021-01-24 21:02:44 +01:00
|
|
|
# python-gitlab 2.2 through at least 2.5 returns a list of dicts for list() instead of a Runner
|
|
|
|
# object, so we need to handle both
|
|
|
|
if hasattr(runner, "description"):
|
|
|
|
if (runner.description == description):
|
2022-01-31 05:58:00 +01:00
|
|
|
return self._gitlab.runners.get(runner.id)
|
2021-01-24 21:02:44 +01:00
|
|
|
else:
|
|
|
|
if (runner['description'] == description):
|
2022-01-31 05:58:00 +01:00
|
|
|
return self._gitlab.runners.get(runner['id'])
|
2020-03-09 10:11:07 +01:00
|
|
|
|
|
|
|
'''
|
|
|
|
@param description Description of the runner
|
|
|
|
'''
|
2022-02-16 22:54:46 +01:00
|
|
|
def exists_runner(self, description):
|
2021-11-16 13:01:32 +01:00
|
|
|
# When runner exists, object will be stored in self.runner_object.
|
2022-02-16 22:54:46 +01:00
|
|
|
runner = self.find_runner(description)
|
2020-03-09 10:11:07 +01:00
|
|
|
|
|
|
|
if runner:
|
2021-11-16 13:01:32 +01:00
|
|
|
self.runner_object = runner
|
2020-03-09 10:11:07 +01:00
|
|
|
return True
|
|
|
|
return False
|
|
|
|
|
2021-11-16 13:01:32 +01:00
|
|
|
def delete_runner(self):
|
2020-03-09 10:11:07 +01:00
|
|
|
if self._module.check_mode:
|
|
|
|
return True
|
|
|
|
|
2021-11-16 13:01:32 +01:00
|
|
|
runner = self.runner_object
|
2020-03-09 10:11:07 +01:00
|
|
|
|
|
|
|
return runner.delete()
|
|
|
|
|
|
|
|
|
|
|
|
def main():
|
|
|
|
argument_spec = basic_auth_argument_spec()
|
2021-12-20 21:59:12 +01:00
|
|
|
argument_spec.update(auth_argument_spec())
|
2020-03-09 10:11:07 +01:00
|
|
|
argument_spec.update(dict(
|
|
|
|
description=dict(type='str', required=True, aliases=["name"]),
|
|
|
|
active=dict(type='bool', default=True),
|
2023-11-13 20:22:33 +01:00
|
|
|
paused=dict(type='bool', default=False),
|
2021-01-24 21:02:44 +01:00
|
|
|
owned=dict(type='bool', default=False),
|
2021-02-16 07:11:37 +01:00
|
|
|
tag_list=dict(type='list', elements='str', default=[]),
|
2020-03-09 10:11:07 +01:00
|
|
|
run_untagged=dict(type='bool', default=True),
|
|
|
|
locked=dict(type='bool', default=False),
|
2023-02-10 13:54:24 +01:00
|
|
|
access_level=dict(type='str', choices=["not_protected", "ref_protected"]),
|
2023-04-26 07:32:00 +02:00
|
|
|
access_level_on_creation=dict(type='bool', default=True),
|
2020-03-09 10:11:07 +01:00
|
|
|
maximum_timeout=dict(type='int', default=3600),
|
2021-02-14 15:08:32 +01:00
|
|
|
registration_token=dict(type='str', no_log=True),
|
2021-09-13 07:16:06 +02:00
|
|
|
project=dict(type='str'),
|
2023-03-25 08:23:20 +01:00
|
|
|
group=dict(type='str'),
|
2020-03-09 10:11:07 +01:00
|
|
|
state=dict(type='str', default="present", choices=["absent", "present"]),
|
|
|
|
))
|
|
|
|
|
|
|
|
module = AnsibleModule(
|
|
|
|
argument_spec=argument_spec,
|
|
|
|
mutually_exclusive=[
|
|
|
|
['api_username', 'api_token'],
|
2021-12-20 21:59:12 +01:00
|
|
|
['api_username', 'api_oauth_token'],
|
|
|
|
['api_username', 'api_job_token'],
|
|
|
|
['api_token', 'api_oauth_token'],
|
|
|
|
['api_token', 'api_job_token'],
|
2022-02-16 22:54:46 +01:00
|
|
|
['project', 'owned'],
|
2023-03-25 08:23:20 +01:00
|
|
|
['group', 'owned'],
|
|
|
|
['project', 'group'],
|
2023-11-13 20:22:33 +01:00
|
|
|
['active', 'paused'],
|
2020-03-09 10:11:07 +01:00
|
|
|
],
|
|
|
|
required_together=[
|
|
|
|
['api_username', 'api_password'],
|
|
|
|
],
|
|
|
|
required_one_of=[
|
2021-12-20 21:59:12 +01:00
|
|
|
['api_username', 'api_token', 'api_oauth_token', 'api_job_token'],
|
2020-03-09 10:11:07 +01:00
|
|
|
],
|
|
|
|
supports_check_mode=True,
|
|
|
|
)
|
2023-11-13 20:42:35 +01:00
|
|
|
|
|
|
|
# check prerequisites and connect to gitlab server
|
|
|
|
gitlab_instance = gitlab_authentication(module)
|
2020-03-09 10:11:07 +01:00
|
|
|
|
|
|
|
state = module.params['state']
|
|
|
|
runner_description = module.params['description']
|
|
|
|
runner_active = module.params['active']
|
|
|
|
tag_list = module.params['tag_list']
|
|
|
|
run_untagged = module.params['run_untagged']
|
|
|
|
runner_locked = module.params['locked']
|
|
|
|
access_level = module.params['access_level']
|
|
|
|
maximum_timeout = module.params['maximum_timeout']
|
|
|
|
registration_token = module.params['registration_token']
|
2021-09-13 07:16:06 +02:00
|
|
|
project = module.params['project']
|
2023-03-25 08:23:20 +01:00
|
|
|
group = module.params['group']
|
2020-03-09 10:11:07 +01:00
|
|
|
|
2021-09-13 07:16:06 +02:00
|
|
|
gitlab_project = None
|
2023-03-25 08:23:20 +01:00
|
|
|
gitlab_group = None
|
|
|
|
|
2021-09-13 07:16:06 +02:00
|
|
|
if project:
|
|
|
|
try:
|
|
|
|
gitlab_project = gitlab_instance.projects.get(project)
|
|
|
|
except gitlab.exceptions.GitlabGetError as e:
|
|
|
|
module.fail_json(msg='No such a project %s' % project, exception=to_native(e))
|
2023-03-25 08:23:20 +01:00
|
|
|
elif group:
|
|
|
|
try:
|
|
|
|
gitlab_group = gitlab_instance.groups.get(group)
|
|
|
|
except gitlab.exceptions.GitlabGetError as e:
|
|
|
|
module.fail_json(msg='No such a group %s' % group, exception=to_native(e))
|
2020-03-09 10:11:07 +01:00
|
|
|
|
2023-03-25 08:23:20 +01:00
|
|
|
gitlab_runner = GitLabRunner(module, gitlab_instance, gitlab_group, gitlab_project)
|
2022-02-16 22:54:46 +01:00
|
|
|
runner_exists = gitlab_runner.exists_runner(runner_description)
|
2020-03-09 10:11:07 +01:00
|
|
|
|
|
|
|
if state == 'absent':
|
|
|
|
if runner_exists:
|
2021-11-16 13:01:32 +01:00
|
|
|
gitlab_runner.delete_runner()
|
2020-03-09 10:11:07 +01:00
|
|
|
module.exit_json(changed=True, msg="Successfully deleted runner %s" % runner_description)
|
|
|
|
else:
|
|
|
|
module.exit_json(changed=False, msg="Runner deleted or does not exists")
|
|
|
|
|
|
|
|
if state == 'present':
|
2021-11-16 13:01:32 +01:00
|
|
|
if gitlab_runner.create_or_update_runner(runner_description, {
|
|
|
|
"active": runner_active,
|
|
|
|
"tag_list": tag_list,
|
|
|
|
"run_untagged": run_untagged,
|
|
|
|
"locked": runner_locked,
|
|
|
|
"access_level": access_level,
|
|
|
|
"maximum_timeout": maximum_timeout,
|
|
|
|
"registration_token": registration_token,
|
2023-11-13 20:22:33 +01:00
|
|
|
"group": group,
|
|
|
|
"project": project,
|
2021-11-16 13:01:32 +01:00
|
|
|
}):
|
|
|
|
module.exit_json(changed=True, runner=gitlab_runner.runner_object._attrs,
|
2020-03-09 10:11:07 +01:00
|
|
|
msg="Successfully created or updated the runner %s" % runner_description)
|
|
|
|
else:
|
2021-11-16 13:01:32 +01:00
|
|
|
module.exit_json(changed=False, runner=gitlab_runner.runner_object._attrs,
|
2020-03-09 10:11:07 +01:00
|
|
|
msg="No need to update the runner %s" % runner_description)
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
main()
|