From 16803e54471f69908604b75da62dd7eb61d31fd6 Mon Sep 17 00:00:00 2001
From: Erick Hitter <git-contrib@ethitter.com>
Date: Sun, 5 Aug 2018 14:39:54 -0700
Subject: [PATCH] Support multiple GitLab runners, with option for
 runner-specific tokens

---
 README.md                 | 60 ++++++++++++++++++++++++++-------------
 defaults/main.yml         | 12 ++++++--
 meta/main.yml             | 14 +++------
 tasks/register-runner.yml | 11 +++----
 templates/runner.repo.j2  | 19 -------------
 5 files changed, 60 insertions(+), 56 deletions(-)
 delete mode 100644 templates/runner.repo.j2

diff --git a/README.md b/README.md
index 956d7da..574c580 100644
--- a/README.md
+++ b/README.md
@@ -12,47 +12,69 @@ This role requires Ansible 2.0 or higher.
 Role Variables
 --------------
 
-`gitlab_runner_package_name`
+###### `gitlab_runner_package_name`
+
 **Since Gitlab 10.x** The package name of `gitlab-ci-multi-runner` has been renamed to `gitlab-runner`. In order to install a version >= 10.x you will need to define this variable `gitlab_runner_package_name: gitlab-runner`.
 
-`gitlab_runner_concurrent`
+###### `gitlab_runner_concurrent`
+
 The maximum number of global jobs to run concurrently.
 Defaults to the number of processor cores.
 
-`gitlab_runner_registration_token`
+###### `gitlab_runner_registration_token`
+
 The GitLab registration token. If this is specified, a runner will be registered to a GitLab server.
 
-`gitlab_runner_coordinator_url`
+###### `gitlab_runner_coordinator_url`
+
 The GitLab coordinator URL.
 Defaults to `https://gitlab.com/ci`.
 
-`gitlab_runner_description`
-The description of the runner.
+###### `gitlab_runner_description_base`
+Base description of the runner.
 Defaults to the hostname.
 
-`gitlab_runner_executor`
+###### `gitlab_runner_executor`
+
 The executor used by the runner.
 Defaults to `shell`.
 
-`gitlab_runner_concurrent_specific`
+###### `gitlab_runner_concurrent_specific`
+
 The maximum number of jobs to run concurrently on this specific runner.
 Defaults to 0, simply means don't limit.
 
-`gitlab_runner_docker_image`
+###### `gitlab_runner_docker_image`
+
 The default Docker image to use. Required when executor is `docker`.
 
-`gitlab_runner_tags`
-The tags assigned to the runner,
+###### `gitlab_runner_tags`
+
+Default tags assigned to the runner,
 Defaults to an empty list.
 
-`gitlab_runner_cache_type`
-Variables to set s3 as a shared cache server. If set it requires variables listed below:
-`gitlab_runner_cache_s3_server_address`
-`gitlab_runner_cache_s3_access_key`
-`gitlab_runner_cache_s3_access_key`
-`gitlab_runner_cache_s3_bucket_name`
-`gitlab_runner_cache_s3_insecure`
-`gitlab_runner_cache_cache_shared`
+###### `gitlab_runners`
+
+Dict of runners
+
+```yml
+gitlab_runners:
+  - {
+      description: '{{ gitlab_runner_description_base }}',
+      tags: [],
+      locked: false,
+      token: ''
+  }
+```
+
+###### `gitlab_runner_cache_type`
+Variables to set `s3` as a shared cache server. If set it requires variables listed below:
+* `gitlab_runner_cache_s3_server_address`
+* `gitlab_runner_cache_s3_access_key`
+* `gitlab_runner_cache_s3_secret_key`
+* `gitlab_runner_cache_s3_bucket_name`
+* `gitlab_runner_cache_s3_insecure`
+* `gitlab_runner_cache_cache_shared`
 
 See the [config for more options](https://github.com/riemers/ansible-gitlab-runner/blob/master/tasks/register-runner.yml)
 
diff --git a/defaults/main.yml b/defaults/main.yml
index 951897e..b88f84b 100644
--- a/defaults/main.yml
+++ b/defaults/main.yml
@@ -10,7 +10,7 @@ gitlab_runner_coordinator_url: 'https://gitlab.com/ci'
 # GitLab registration token
 gitlab_runner_registration_token: ''
 # Runner description
-gitlab_runner_description: '{{ ansible_hostname }}'
+gitlab_runner_description_base: '{{ ansible_hostname }}'
 # Runner executor
 gitlab_runner_executor: 'shell'
 # Maximum number of jobs to run concurrently on this specific runner
@@ -32,11 +32,17 @@ gitlab_runner_ssh_password: ''
 # Runner SSH identity file
 gitlab_runner_ssh_identity_file: ''
 # Runner Locked
-gitlab_runner_locked: 'false'
+gitlab_runner_locked: false
+
+# Individual runners
+gitlab_runners:
+  - {
+      description: '{{ gitlab_runner_description_base }}'
+  }
 
 # Runner machine
 gitlab_runner_machine_idle_nodes: 0
-gitlab_runner_machine_idle_time: 2
+gitlab_runner_machine_idle_time: 3
 
 # Docker Machine
 docker_machine_version: 'v0.15.0'
diff --git a/meta/main.yml b/meta/main.yml
index 124a476..4a51f43 100644
--- a/meta/main.yml
+++ b/meta/main.yml
@@ -1,19 +1,13 @@
 ---
 galaxy_info:
-  author: Erik-jan Riemers
+  author: Erick Hitter, Erik-jan Riemers
   description: GitLab Runner
   license: MIT
   min_ansible_version: 2.0
   platforms:
-  - name: EL
-    versions:
-    - all
-  - name: Ubuntu
-    versions:
-    - all
-  - name: Debian
-    version:
-    - all
+    - name: Debian
+      version:
+        - all
   galaxy_tags:
     - gitlab
     - runner
diff --git a/tasks/register-runner.yml b/tasks/register-runner.yml
index c44bf3f..1ebf008 100644
--- a/tasks/register-runner.yml
+++ b/tasks/register-runner.yml
@@ -9,12 +9,12 @@
   command: gitlab-runner register >
     --non-interactive
     --url '{{ gitlab_runner_coordinator_url }}'
-    --registration-token '{{ gitlab_runner_registration_token }}'
-    --description '{{ gitlab_runner_description }}'
-    --tag-list '{{ gitlab_runner_tags | join(",") }}'
+    --registration-token '{{ item.token | default( gitlab_runner_registration_token ) }}'
+    --description '{{ item.description }}'
+    --tag-list '{{ ( item.tags | default( gitlab_runner_tags ) ) | join(",") }}'
     --executor '{{ gitlab_runner_executor }}'
     --limit '{{ gitlab_runner_concurrent_specific }}'
-    --locked='{{gitlab_runner_locked}}'
+    --locked='{{ item.locked | default( gitlab_runner_locked ) }}'
     --docker-image '{{ gitlab_runner_docker_image }}'
     {% if gitlab_runner_docker_volumes is defined %}
     --docker-volumes "{{ gitlab_runner_docker_volumes | join('" "') }}"
@@ -43,4 +43,5 @@
     --machine-machine-options 'digitalocean-region={{ gitlab_runner_digital_ocean_region }}'
     --machine-machine-options 'digitalocean-size={{ gitlab_runner_digital_ocean_size }}'
     --machine-machine-options 'digitalocean-private-networking'
-  when: configured_runners.stderr.find('\n' + gitlab_runner_description) == -1
+  with_items: '{{ gitlab_runners }}'
+  when: configured_runners.stderr.find('\n' + item.description) == -1
diff --git a/templates/runner.repo.j2 b/templates/runner.repo.j2
deleted file mode 100644
index 97da482..0000000
--- a/templates/runner.repo.j2
+++ /dev/null
@@ -1,19 +0,0 @@
-[runner_{{ gitlab_runner_package_name }}]
-name=runner_{{ gitlab_runner_package_name }}
-baseurl=https://packages.gitlab.com/runner/{{ gitlab_runner_package_name }}/el/{{ ansible_distribution_major_version }}/$basearch
-repo_gpgcheck=1
-gpgcheck=0
-enabled=1
-gpgkey=https://packages.gitlab.com/gpg.key
-sslverify=1
-sslcacert=/etc/pki/tls/certs/ca-bundle.crt
-
-[runner_{{ gitlab_runner_package_name }}-source]
-name=runner_{{ gitlab_runner_package_name }}-source
-baseurl=https://packages.gitlab.com/runner/{{ gitlab_runner_package_name }}/el/{{ ansible_distribution_major_version }}/SRPMS
-repo_gpgcheck=1
-gpgcheck=0
-enabled=1
-gpgkey=https://packages.gitlab.com/gpg.key
-sslverify=1
-sslcacert=/etc/pki/tls/certs/ca-bundle.crt
-- 
GitLab