Skip to content

Commit 628c160

Browse files
committed
Merge branch 'drybjed-role-updates'
2 parents f5bfc0d + fa40a30 commit 628c160

7 files changed

Lines changed: 200 additions & 52 deletions

File tree

CHANGES.rst

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,52 @@ The current role maintainer_ is drybjed.
1414
`debops.gitlab_runner master`_ - unreleased
1515
-------------------------------------------
1616

17-
.. _debops.gitlab_runner master: https://github.com/debops/ansible-gitlab_runner/compare/v0.1.2...master
17+
.. _debops.gitlab_runner master: https://github.com/debops/ansible-gitlab_runner/compare/v0.2.0...master
18+
19+
20+
`debops.gitlab_runner v0.2.0`_ - 2017-08-16
21+
-------------------------------------------
22+
23+
.. _debops.gitlab_runner v0.2.0: https://github.com/debops/ansible-gitlab_runner/compare/v0.1.2...v0.2.0
24+
25+
Added
26+
~~~~~
27+
28+
- Add improved autoscaling off-peak support. [azman0101]
29+
30+
- Add a separate list of tags defined for shell executors that describe the
31+
host in a greater detail. [drybjed_]
32+
33+
Changed
34+
~~~~~~~
35+
36+
- Fixed Ansible warnings during task execution. [drybjed_]
37+
38+
- Update APT cache before first package installation. [azman0101, drybjed_]
39+
40+
- Convert the Ansible local facts to a Python script. The GitLab Runner state
41+
is stored in a separate, secure JSON file. [drybjed_]
42+
43+
- Redesign the list of default GitLab Runner executors. The role will create
44+
a 'shell' executor, unless Docker is detected in which case the 'shell'
45+
executor will be disabled. Two Docker executors will be created, one
46+
privileged and one unprivileged, with respective tags. [drybjed_]
47+
48+
- If Docker is detected, the ``gitlab-runner`` user will be added to the
49+
``docker`` group to allow access to Docker containers. [drybjed_]
50+
51+
- On hosts with Docker executors, number of concurrent jobs will depend on
52+
number of vCPUs available. Hosts with 'shell' executor will be allowed to run
53+
only 1 job at a time. [drybjed_]
54+
55+
- The default 'shell' and privileged Docker executors will not be allowed to
56+
run untagged jobs for security reasons. The default unprivileged Docker
57+
executor can still run untagged jobs. [drybjed_]
58+
59+
Fixed
60+
~~~~~
61+
62+
- Fixed an error with missing Ansible facts breaking GitLab Runner registration. [drybjed_]
1863

1964

2065
`debops.gitlab_runner v0.1.2`_ - 2016-10-05

defaults/main.yml

Lines changed: 49 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,10 @@ gitlab_runner__shell: '/bin/bash'
107107
# .. envvar:: gitlab_runner__concurrent [[[
108108
#
109109
# Global number of jobs that can run concurrently on all configured runners.
110-
gitlab_runner__concurrent: '{{ ansible_processor_cores }}'
110+
gitlab_runner__concurrent: '{{ ansible_processor_vcpus
111+
if (ansible_local|d() and ansible_local.docker|d() and
112+
(ansible_local.docker.installed|d()) | bool)
113+
else "1" }}'
111114

112115
# ]]]
113116
# .. envvar:: gitlab_runner__domain [[[
@@ -159,6 +162,14 @@ gitlab_runner__environment: {}
159162
# GitLab Runner tags [[[
160163
# ----------------------
161164

165+
# .. envvar:: gitlab_runner__shell_tags [[[
166+
#
167+
# List of tags automatically generated by the role, based on Ansible facts.
168+
# These tags are used with shell executors.
169+
gitlab_runner__shell_tags: '{{ lookup("template",
170+
"lookup/gitlab_runner__shell_tags.j2") }}'
171+
172+
# ]]]
162173
# .. envvar:: gitlab_runner__default_tags [[[
163174
#
164175
# List of default tags applied to the Runner at registration in GitLab CI.
@@ -182,6 +193,15 @@ gitlab_runner__group_tags: []
182193
#
183194
# List of per-host tags applied to the Runner at registration in GitLab CI.
184195
gitlab_runner__host_tags: []
196+
197+
# ]]]
198+
# .. envvar:: gitlab_runner__combined_tags [[[
199+
#
200+
# Combined list of system-wide tags for easier use in the role tasks.
201+
gitlab_runner__combined_tags: '{{ gitlab_runner__default_tags
202+
+ gitlab_runner__tags
203+
+ gitlab_runner__group_tags
204+
+ gitlab_runner__host_tags }}'
185205
# ]]]
186206
# ]]]
187207
# GitLab Runner instances [[[
@@ -200,7 +220,34 @@ gitlab_runner__host_tags: []
200220
# .. envvar:: gitlab_runner__default_instances [[[
201221
#
202222
# List of default GitLab Runner instances.
203-
gitlab_runner__default_instances: [ '{{ gitlab_runner__instance_shell }}' ]
223+
gitlab_runner__default_instances:
224+
225+
- name: '{{ ansible_hostname + "-shell" }}'
226+
executor: 'shell'
227+
run_untagged: False
228+
state: '{{ "absent"
229+
if (ansible_local|d() and ansible_local.docker|d() and
230+
(ansible_local.docker.installed|d()) | bool)
231+
else "present" }}'
232+
233+
- name: '{{ ansible_hostname + "-docker" }}'
234+
executor: 'docker'
235+
tags: [ 'docker' ]
236+
state: '{{ "present"
237+
if (ansible_local|d() and ansible_local.docker|d() and
238+
(ansible_local.docker.installed|d()) | bool)
239+
else "absent" }}'
240+
241+
- name: '{{ ansible_hostname + "-docker-root" }}'
242+
executor: 'docker'
243+
concurrent: '{{ ansible_processor_vcpus }}'
244+
docker_privileged: True
245+
run_untagged: False
246+
tags: [ 'docker-privileged' ]
247+
state: '{{ "present"
248+
if (ansible_local|d() and ansible_local.docker|d() and
249+
(ansible_local.docker.installed|d()) | bool)
250+
else "absent" }}'
204251

205252
# ]]]
206253
# .. envvar:: gitlab_runner__instances [[[
@@ -219,14 +266,6 @@ gitlab_runner__group_instances: []
219266
#
220267
# List of GitLab Runner instances on a particular host.
221268
gitlab_runner__host_instances: []
222-
223-
# ]]]
224-
# .. envvar:: gitlab_runner__instance_shell [[[
225-
#
226-
# The default GitLab Runner instance.
227-
gitlab_runner__instance_shell:
228-
name: '{{ gitlab_runner__fqdn }}'
229-
executor: 'shell'
230269
# ]]]
231270
# ]]]
232271
# Custom file upload [[[

docs/getting-started.rst

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,11 @@ Initial configuration
3939
---------------------
4040

4141
By default, ``debops.gitlab_runner`` will configure a single Runner instance
42-
which uses a shell executor. To use different executors like SSH or Docker, you
43-
need to provide additional configuration and ensure that required software
44-
(:command:`ssh`, Docker, Docker Machine, etc.) is installed if required.
42+
which uses a shell executor. If a Docker installation is detected via Ansible
43+
local facts, the role will disable the shell executor and configure two Docker
44+
executors - one unprivileged, and one privileged. The executors will have a set
45+
of tags that identify them, shell executors will have additional tags that
46+
describe the host's architecture, OS release, etc.
4547

4648
The Runner instances can be configured with variables specified as the keys of
4749
the dictionary that holds the specific Runner configuration. If any required

tasks/main.yml

Lines changed: 27 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,14 @@
2323
generate_ssh_key: '{{ gitlab_runner__ssh_generate | bool }}'
2424
ssh_key_bits: '{{ gitlab_runner__ssh_generate_bits }}'
2525

26+
- name: Allow Docker access for gitlab-runner user
27+
user:
28+
name: '{{ gitlab_runner__user }}'
29+
groups: 'docker'
30+
append: True
31+
when: (ansible_local|d() and ansible_local.docker|d() and
32+
(ansible_local.docker.installed|d()) | bool)
33+
2634
- name: Copy custom files to GitLab Runner host
2735
copy:
2836
src: '{{ item.src | d(omit) }}'
@@ -45,6 +53,11 @@
4553
name: '{{ item }}'
4654
state: 'present'
4755
install_recommends: False
56+
update_cache: True
57+
cache_valid_time: '{{ ansible_local.core.cache_valid_time
58+
if (ansible_local|d() and ansible_local.core|d() and
59+
ansible_local.core.cache_valid_time|d())
60+
else "86400" }}'
4861
with_items: [ 'apt-transport-https', 'openssl', 'ca-certificates' ]
4962

5063
- name: Add gitlab-runner APT key
@@ -79,12 +92,13 @@
7992
register: gitlab_runner__register_api
8093
when: gitlab_runner__api_url|d() and gitlab_runner__api_url
8194
failed_when: False
95+
no_log: True
8296

8397
- name: Register new GitLab Runners
8498
uri:
8599
url: '{{ (item.api_url | d(gitlab_runner__api_url)) + "/api/v1/runners/register.json" }}'
86100
method: 'POST'
87-
body: 'token={{ item.token | d(gitlab_runner__token) }}&description={{ item.name | urlencode }}&tag_list={{ ((item.tags|d([]) + gitlab_runner__default_tags + gitlab_runner__tags + gitlab_runner__group_tags + gitlab_runner__host_tags) | unique | join(",")) | urlencode }}&run_untagged={{ gitlab_runner__run_untagged }}'
101+
body: 'token={{ item.token | d(gitlab_runner__token) }}&description={{ item.name | urlencode }}&tag_list={{ ((item.tags|d([]) + (gitlab_runner__shell_tags if (item.executor == "shell") else []) + gitlab_runner__combined_tags) | unique | join(",")) | urlencode }}&run_untagged={{ item.run_untagged | d(gitlab_runner__run_untagged) }}'
88102
status_code: '200,201'
89103
register: gitlab_runner__register_new_instances
90104
with_flattened:
@@ -95,18 +109,19 @@
95109
when: (gitlab_runner__register_api.status|d() and gitlab_runner__register_api.status == 200 and
96110
(item.token | d() or gitlab_runner__token) and item.name and
97111
(item.state is undefined or item.state != 'absent') and
98-
(ansible_local|d(True) and
99-
(ansible_local.gitlab_runner is undefined or
100-
item.name not in ansible_local.gitlab_runner.instances)) and
101-
(gitlab_runner__run_untagged|d()))
112+
(ansible_local is undefined or
113+
(ansible_local|d() and (ansible_local.gitlab_runner is undefined or
114+
(ansible_local.gitlab_runner|d() and ansible_local.gitlab_runner.instances is defined and
115+
item.name not in ansible_local.gitlab_runner.instances)))))
102116

103-
- name: Generate gitlab-runner configuration file
117+
- name: Generate GitLab Runner configuration files
104118
template:
105-
src: 'etc/gitlab-runner/config.toml.j2'
106-
dest: '/etc/gitlab-runner/config.toml'
119+
src: 'etc/gitlab-runner/{{ item }}.j2'
120+
dest: '/etc/gitlab-runner/{{ item }}'
107121
owner: 'root'
108122
group: 'root'
109123
mode: '0600'
124+
with_items: [ 'config.toml', 'ansible.json' ]
110125

111126
- name: Delete GitLab Runners if requested
112127
uri:
@@ -145,7 +160,7 @@
145160
owner: '{{ gitlab_runner__user }}'
146161
group: '{{ gitlab_runner__group }}'
147162
mode: '0700'
148-
when: gitlab_runner__ssh_known_hosts
163+
when: gitlab_runner__ssh_known_hosts|d()
149164

150165
- name: Make sure the ~/.ssh/known_hosts file exists
151166
copy:
@@ -155,12 +170,12 @@
155170
group: '{{ gitlab_runner__group }}'
156171
mode: '0644'
157172
force: False
158-
when: gitlab_runner__ssh_known_hosts
173+
when: gitlab_runner__ssh_known_hosts|d()
159174

160175
- name: Get list of already scanned host fingerprints
161176
shell: ssh-keygen -f {{ gitlab_runner__home }}/.ssh/known_hosts -F {{ item }} | grep -q '^# Host {{ item }} found'
162177
with_items: '{{ gitlab_runner__ssh_known_hosts }}'
163-
when: gitlab_runner__ssh_known_hosts
178+
when: gitlab_runner__ssh_known_hosts|d()
164179
register: gitlab_runner__register_known_hosts
165180
changed_when: False
166181
failed_when: False
@@ -185,4 +200,4 @@
185200
dest: '/etc/ansible/facts.d/gitlab_runner.fact'
186201
owner: 'root'
187202
group: 'root'
188-
mode: '0600'
203+
mode: '0755'
Lines changed: 22 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,22 @@
1-
{% set gitlab_runner__tpl_instances = [] %}
2-
{% set gitlab_runner__tpl_instance_tokens = [] %}
3-
{% if gitlab_runner__register_new_instances|d() and gitlab_runner__register_new_instances.results %}
4-
{% for item in gitlab_runner__register_new_instances.results %}
5-
{% if item.item.name|d() and item.json|d() and item.json.token|d() %}
6-
{% set _ = gitlab_runner__tpl_instances.append(item.item.name) %}
7-
{% set _ = gitlab_runner__tpl_instance_tokens.append({ "name": item.item.name, "token": item.json.token }) %}
8-
{% endif %}
9-
{% endfor %}
10-
{% endif %}
11-
{% if ansible_local|d() and ansible_local.gitlab_runner|d() and ansible_local.gitlab_runner.instance_tokens|d() %}
12-
{% for item in ansible_local.gitlab_runner.instance_tokens %}
13-
{% if item.name not in gitlab_runner__tpl_instances %}
14-
{% for element in (gitlab_runner__default_instances + gitlab_runner__instances + gitlab_runner__group_instances + gitlab_runner__host_instances) %}
15-
{% if (item.name == element.name and (element.state is undefined or element.state != 'absent')) %}
16-
{% set _ = gitlab_runner__tpl_instances.append(item.name) %}
17-
{% set _ = gitlab_runner__tpl_instance_tokens.append({ "name": item.name, "token": item.token }) %}
18-
{% endif %}
19-
{% endfor %}
20-
{% endif %}
21-
{% endfor %}
22-
{% endif %}
23-
{
24-
"instance_tokens": {{ gitlab_runner__tpl_instance_tokens | to_nice_json }},
25-
"instances": {{ gitlab_runner__tpl_instances | to_nice_json }}
26-
}
1+
#!/usr/bin/env python
2+
3+
# {{ ansible_managed }}
4+
5+
from __future__ import print_function
6+
from json import load, loads, dumps
7+
from sys import exit
8+
import os
9+
10+
output = {}
11+
state_file = '/etc/gitlab-runner/ansible.json'
12+
13+
if os.path.isfile(state_file):
14+
try:
15+
fh = open(state_file)
16+
output = load(fh)
17+
except Exception:
18+
pass
19+
20+
output['installed'] = True
21+
22+
print(dumps(output, sort_keys=True, indent=2))
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
{% set gitlab_runner__tpl_instances = [] %}
2+
{% set gitlab_runner__tpl_instance_tokens = [] %}
3+
{% if gitlab_runner__register_new_instances|d() and gitlab_runner__register_new_instances.results %}
4+
{% for item in gitlab_runner__register_new_instances.results %}
5+
{% if item.item.name|d() and item.json|d() and item.json.token|d() %}
6+
{% set _ = gitlab_runner__tpl_instances.append(item.item.name) %}
7+
{% set _ = gitlab_runner__tpl_instance_tokens.append({ "name": item.item.name, "token": item.json.token }) %}
8+
{% endif %}
9+
{% endfor %}
10+
{% endif %}
11+
{% if ansible_local|d() and ansible_local.gitlab_runner|d() and ansible_local.gitlab_runner.instance_tokens|d() %}
12+
{% for item in ansible_local.gitlab_runner.instance_tokens %}
13+
{% if item.name not in gitlab_runner__tpl_instances %}
14+
{% for element in (gitlab_runner__default_instances + gitlab_runner__instances + gitlab_runner__group_instances + gitlab_runner__host_instances) %}
15+
{% if (item.name == element.name and (element.state is undefined or element.state != 'absent')) %}
16+
{% set _ = gitlab_runner__tpl_instances.append(item.name) %}
17+
{% set _ = gitlab_runner__tpl_instance_tokens.append({ "name": item.name, "token": item.token }) %}
18+
{% endif %}
19+
{% endfor %}
20+
{% endif %}
21+
{% endfor %}
22+
{% endif %}
23+
{% set output = {
24+
"instance_tokens": gitlab_runner__tpl_instance_tokens,
25+
"instances": gitlab_runner__tpl_instances
26+
} %}
27+
{{ output | to_nice_json }}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
{% set output = [ 'shell' ] %}
2+
{% set _ = output.append(ansible_pkg_mgr) %}
3+
{% set _ = output.append(ansible_service_mgr) %}
4+
{% set _ = output.append(ansible_system | lower) %}
5+
{% if (ansible_local|d() and ansible_local.tags|d()) %}
6+
{% set _ = output.extend(ansible_local.tags) %}
7+
{% endif %}
8+
{% if (ansible_local|d() and ansible_local.docker|d() and
9+
(ansible_local.docker.installed|d()) | bool) %}
10+
{% set _ = output.append('docker-host') %}
11+
{% endif %}
12+
{% set _ = output.append((ansible_local.core.distribution
13+
if (ansible_local|d() and ansible_local.core|d() and
14+
ansible_local.core.distribution|d())
15+
else ansible_distribution) | lower) %}
16+
{% set _ = output.append((ansible_local.core.distribution_release
17+
if (ansible_local|d() and ansible_local.core|d() and
18+
ansible_local.core.distribution_release|d())
19+
else ansible_distribution_release) | lower) %}
20+
{% if ansible_virtualization_type not in [ 'NA' ] %}
21+
{% set _ = output.append(ansible_virtualization_type + '-' + ansible_virtualization_role) %}
22+
{% endif %}
23+
{% set _ = output.append('amd64' if (ansible_architecture == 'x86_64') else ansible_architecture) %}
24+
{{ output | unique }}

0 commit comments

Comments
 (0)