Skip to content

Commit

Permalink
[PR #7199/6b00b76f backport][stable-8] feat(gitlab_runner module): Ad…
Browse files Browse the repository at this point in the history
…d 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 6b00b76)

Co-authored-by: Léo GATELLIER <[email protected]>
  • Loading branch information
patchback[bot] and lgatellier authored Nov 13, 2023
1 parent f2e2157 commit 1a5ad80
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 19 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
minor_changes:
- gitlab_runner - add support for new runner creation workflow (https://github.com/ansible-collections/community.general/pull/7199).
73 changes: 54 additions & 19 deletions plugins/modules/gitlab_runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,15 @@
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:
Expand All @@ -73,8 +75,11 @@
type: str
registration_token:
description:
- The registration token is used to register new runners.
- Required if O(state=present).
- 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:
Expand All @@ -87,9 +92,18 @@
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.
Expand Down Expand Up @@ -210,6 +224,9 @@
)


from ansible_collections.community.general.plugins.module_utils.version import LooseVersion


class GitLabRunner(object):
def __init__(self, module, gitlab_instance, group=None, project=None):
self._module = module
Expand All @@ -232,18 +249,32 @@ def create_or_update_runner(self, description, options):
changed = False

arguments = {
'active': options['active'],
'locked': options['locked'],
'run_untagged': options['run_untagged'],
'maximum_timeout': options['maximum_timeout'],
'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:
arguments['access_level'] = options['access_level']
# Because we have already call userExists in main()
if self.runner_object is None:
arguments['description'] = description
arguments['token'] = options['registration_token']
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'

access_level_on_creation = self._module.params['access_level_on_creation']
if not access_level_on_creation:
Expand All @@ -253,19 +284,17 @@ def create_or_update_runner(self, description, options):
changed = True
else:
changed, runner = self.update_runner(self.runner_object, arguments)
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))

self.runner_object = runner
if changed:
if self._module.check_mode:
self._module.exit_json(changed=True, msg="Successfully created or 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))
return True
else:
return False
return changed

'''
@param arguments Attributes of the runner
Expand All @@ -275,7 +304,12 @@ def create_runner(self, arguments):
return True

try:
runner = self._gitlab.runners.create(arguments)
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)
except (gitlab.exceptions.GitlabCreateError) as e:
self._module.fail_json(msg="Failed to create runner: %s " % to_native(e))

Expand Down Expand Up @@ -348,6 +382,7 @@ def main():
argument_spec.update(dict(
description=dict(type='str', required=True, aliases=["name"]),
active=dict(type='bool', default=True),
paused=dict(type='bool', default=False),
owned=dict(type='bool', default=False),
tag_list=dict(type='list', elements='str', default=[]),
run_untagged=dict(type='bool', default=True),
Expand All @@ -372,16 +407,14 @@ def main():
['project', 'owned'],
['group', 'owned'],
['project', 'group'],
['active', 'paused'],
],
required_together=[
['api_username', 'api_password'],
],
required_one_of=[
['api_username', 'api_token', 'api_oauth_token', 'api_job_token'],
],
required_if=[
('state', 'present', ['registration_token']),
],
supports_check_mode=True,
)
ensure_gitlab_package(module)
Expand Down Expand Up @@ -432,6 +465,8 @@ def main():
"access_level": access_level,
"maximum_timeout": maximum_timeout,
"registration_token": registration_token,
"group": group,
"project": project,
}):
module.exit_json(changed=True, runner=gitlab_runner.runner_object._attrs,
msg="Successfully created or updated the runner %s" % runner_description)
Expand Down

0 comments on commit 1a5ad80

Please sign in to comment.