1
0
Fork 0
mirror of https://github.com/ansible-collections/community.general.git synced 2024-09-14 20:13:21 +02:00

[PR #7199/6b00b76f backport][stable-8] feat(gitlab_runner module): Add support for the new runner creation workflow (#7511)

feat(gitlab_runner module): Add support for the new runner creation workflow (#7199)

feat(gitlab_runner module): Handle new runner creation workflow

(cherry picked from commit 6b00b76f32)

Co-authored-by: Léo GATELLIER <26511053+lgatellier@users.noreply.github.com>
This commit is contained in:
patchback[bot] 2023-11-13 20:22:33 +01:00 committed by GitHub
parent f2e2157bcc
commit 1a5ad80589
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 56 additions and 19 deletions

View file

@ -0,0 +1,2 @@
minor_changes:
- gitlab_runner - add support for new runner creation workflow (https://github.com/ansible-collections/community.general/pull/7199).

View file

@ -48,6 +48,7 @@ options:
description: description:
- ID or full path of the group in the form group/subgroup. - ID or full path of the group in the form group/subgroup.
- Mutually exclusive with O(owned) and O(project). - 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 type: str
version_added: '6.5.0' version_added: '6.5.0'
project: project:
@ -55,6 +56,7 @@ options:
- ID or full path of the project in the form of group/name. - 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(owned) since community.general 4.5.0.
- Mutually exclusive with O(group). - Mutually exclusive with O(group).
- Must be project's numeric ID if O(registration_token) is not set and O(state=present).
type: str type: str
version_added: '3.7.0' version_added: '3.7.0'
description: description:
@ -73,8 +75,11 @@ options:
type: str type: str
registration_token: registration_token:
description: description:
- The registration token is used to register new runners. - The registration token is used to register new runners before GitLab 16.0.
- Required if O(state=present). - 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 type: str
owned: owned:
description: description:
@ -87,9 +92,18 @@ options:
active: active:
description: description:
- Define if the runners is immediately active after creation. - Define if the runners is immediately active after creation.
- Mutually exclusive with O(paused).
required: false required: false
default: true default: true
type: bool 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: locked:
description: description:
- Determines if the runner is locked or not. - Determines if the runner is locked or not.
@ -210,6 +224,9 @@ from ansible_collections.community.general.plugins.module_utils.gitlab import (
) )
from ansible_collections.community.general.plugins.module_utils.version import LooseVersion
class GitLabRunner(object): class GitLabRunner(object):
def __init__(self, module, gitlab_instance, group=None, project=None): def __init__(self, module, gitlab_instance, group=None, project=None):
self._module = module self._module = module
@ -232,18 +249,32 @@ class GitLabRunner(object):
changed = False changed = False
arguments = { arguments = {
'active': options['active'],
'locked': options['locked'], 'locked': options['locked'],
'run_untagged': options['run_untagged'], 'run_untagged': options['run_untagged'],
'maximum_timeout': options['maximum_timeout'], 'maximum_timeout': options['maximum_timeout'],
'tag_list': options['tag_list'], 'tag_list': options['tag_list'],
} }
if options.get('paused') is not None:
arguments['paused'] = options['paused']
else:
arguments['active'] = options['active']
if options.get('access_level') is not None: if options.get('access_level') is not None:
arguments['access_level'] = options['access_level'] arguments['access_level'] = options['access_level']
# Because we have already call userExists in main() # Because we have already call userExists in main()
if self.runner_object is None: if self.runner_object is None:
arguments['description'] = description arguments['description'] = description
if options.get('registration_token') is not None:
arguments['token'] = options['registration_token'] 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'
access_level_on_creation = self._module.params['access_level_on_creation'] access_level_on_creation = self._module.params['access_level_on_creation']
if not access_level_on_creation: if not access_level_on_creation:
@ -253,19 +284,17 @@ class GitLabRunner(object):
changed = True changed = True
else: else:
changed, runner = self.update_runner(self.runner_object, arguments) changed, runner = self.update_runner(self.runner_object, arguments)
self.runner_object = runner
if changed: if changed:
if self._module.check_mode: if self._module.check_mode:
self._module.exit_json(changed=True, msg="Successfully created or updated the runner %s" % description) self._module.exit_json(changed=True, msg="Successfully updated the runner %s" % description)
try: try:
runner.save() runner.save()
except Exception as e: except Exception as e:
self._module.fail_json(msg="Failed to update runner: %s " % to_native(e)) self._module.fail_json(msg="Failed to update runner: %s " % to_native(e))
return True
else: self.runner_object = runner
return False return changed
''' '''
@param arguments Attributes of the runner @param arguments Attributes of the runner
@ -275,7 +304,12 @@ class GitLabRunner(object):
return True return True
try: try:
if arguments.get('token') is not None:
runner = self._gitlab.runners.create(arguments) 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)
except (gitlab.exceptions.GitlabCreateError) as e: except (gitlab.exceptions.GitlabCreateError) as e:
self._module.fail_json(msg="Failed to create runner: %s " % to_native(e)) self._module.fail_json(msg="Failed to create runner: %s " % to_native(e))
@ -348,6 +382,7 @@ def main():
argument_spec.update(dict( argument_spec.update(dict(
description=dict(type='str', required=True, aliases=["name"]), description=dict(type='str', required=True, aliases=["name"]),
active=dict(type='bool', default=True), active=dict(type='bool', default=True),
paused=dict(type='bool', default=False),
owned=dict(type='bool', default=False), owned=dict(type='bool', default=False),
tag_list=dict(type='list', elements='str', default=[]), tag_list=dict(type='list', elements='str', default=[]),
run_untagged=dict(type='bool', default=True), run_untagged=dict(type='bool', default=True),
@ -372,6 +407,7 @@ def main():
['project', 'owned'], ['project', 'owned'],
['group', 'owned'], ['group', 'owned'],
['project', 'group'], ['project', 'group'],
['active', 'paused'],
], ],
required_together=[ required_together=[
['api_username', 'api_password'], ['api_username', 'api_password'],
@ -379,9 +415,6 @@ def main():
required_one_of=[ required_one_of=[
['api_username', 'api_token', 'api_oauth_token', 'api_job_token'], ['api_username', 'api_token', 'api_oauth_token', 'api_job_token'],
], ],
required_if=[
('state', 'present', ['registration_token']),
],
supports_check_mode=True, supports_check_mode=True,
) )
ensure_gitlab_package(module) ensure_gitlab_package(module)
@ -432,6 +465,8 @@ def main():
"access_level": access_level, "access_level": access_level,
"maximum_timeout": maximum_timeout, "maximum_timeout": maximum_timeout,
"registration_token": registration_token, "registration_token": registration_token,
"group": group,
"project": project,
}): }):
module.exit_json(changed=True, runner=gitlab_runner.runner_object._attrs, module.exit_json(changed=True, runner=gitlab_runner.runner_object._attrs,
msg="Successfully created or updated the runner %s" % runner_description) msg="Successfully created or updated the runner %s" % runner_description)