Skip to content

Commit

Permalink
Add Gitlab group runners support
Browse files Browse the repository at this point in the history
  • Loading branch information
lgatellier committed Dec 28, 2021
1 parent 8f12205 commit ce9c245
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 8 deletions.
4 changes: 4 additions & 0 deletions changelogs/fragments/3935-add-gitlab-group-runner.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
minor_changes:
- 'gitlab_runner - allow to register group runner (https://github.com/ansible-collections/community.general/pull/3935).'
bugfixes:
- 'gitlab_runner module - fix project/group runner CRUD operations (https://github.com/ansible-collections/community.general/pull/3935).'
36 changes: 28 additions & 8 deletions plugins/modules/source_control/gitlab/gitlab_runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,14 @@
- community.general.gitlab
options:
group:
description:
- ID or full path of the group in the form of C(group/subgroup).
type: str
version_added: '4.3.0'
project:
description:
- ID or full path of the project in the form of group/name.
- ID or full path of the project in the form of C(group/name).
type: str
version_added: '3.7.0'
description:
Expand Down Expand Up @@ -193,13 +198,18 @@ def cmp(a, b):


class GitLabRunner(object):
def __init__(self, module, gitlab_instance, project=None):
def __init__(self, module, gitlab_instance, group=None, project=None):
self._module = module
self._gitlab = gitlab_instance
# 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
self._runners_endpoint = project.runners if project else gitlab_instance.runners
if group:
self._runners_endpoint = group.runners
elif project:
self._runners_endpoint = project.runners
else:
self._runners_endpoint = gitlab_instance.runners
self.runner_object = None

def create_or_update_runner(self, description, options):
Expand Down Expand Up @@ -248,7 +258,7 @@ def create_runner(self, arguments):
return True

try:
runner = self._runners_endpoint.create(arguments)
runner = self._gitlab.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 @@ -292,10 +302,10 @@ def find_runner(self, description, owned=False):
# object, so we need to handle both
if hasattr(runner, "description"):
if (runner.description == description):
return self._runners_endpoint.get(runner.id)
return self._gitlab.runners.get(runner.id)
else:
if (runner['description'] == description):
return self._runners_endpoint.get(runner['id'])
return self._gitlab.runners.get(runner['id'])

'''
@param description Description of the runner
Expand Down Expand Up @@ -332,6 +342,7 @@ def main():
maximum_timeout=dict(type='int', default=3600),
registration_token=dict(type='str', no_log=True),
project=dict(type='str'),
group=dict(type='str'),
state=dict(type='str', default="present", choices=["absent", "present"]),
))

Expand All @@ -343,6 +354,7 @@ def main():
['api_username', 'api_job_token'],
['api_token', 'api_oauth_token'],
['api_token', 'api_job_token'],
['group', 'project'],
],
required_together=[
['api_username', 'api_password'],
Expand All @@ -366,20 +378,28 @@ def main():
access_level = module.params['access_level']
maximum_timeout = module.params['maximum_timeout']
registration_token = module.params['registration_token']
group = module.params['group']
project = module.params['project']

if not HAS_GITLAB_PACKAGE:
module.fail_json(msg=missing_required_lib("python-gitlab"), exception=GITLAB_IMP_ERR)

gitlab_instance = gitlab_authentication(module)
gitlab_project = None
if project:
gitlab_group = None

if 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))
elif 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))

gitlab_runner = GitLabRunner(module, gitlab_instance, gitlab_project)
gitlab_runner = GitLabRunner(module, gitlab_instance, gitlab_group, gitlab_project)
runner_exists = gitlab_runner.exists_runner(runner_description, owned)

if state == 'absent':
Expand Down

0 comments on commit ce9c245

Please sign in to comment.