Skip to content

Commit

Permalink
Merge pull request #250 from guenhter/clean-no-longer-existing-runners
Browse files Browse the repository at this point in the history
Fix problem with deleted runners
  • Loading branch information
riemers authored Jan 31, 2023
2 parents 83bb194 + b1aff0d commit 831fb29
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 101 deletions.
27 changes: 9 additions & 18 deletions tasks/Container.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
import_tasks: install-container.yml
when: gitlab_runner_container_install

- name: (Container) List configured runners
- name: (Container) Delete no longer existing runners
docker_container:
name: "{{ gitlab_runner_container_name }}-list"
name: "{{ gitlab_runner_container_name }}-check"
image: "{{ gitlab_runner_container_image }}:{{ gitlab_runner_container_tag }}"
command: list
command: verify --delete
mounts:
- type: bind
source: "{{ gitlab_runner_container_mount_path }}"
Expand All @@ -16,15 +16,15 @@
interactive: yes
tty: yes
detach: no
register: configured_runners
changed_when: False
register: verified_runners
changed_when: '"Updated " in verified_runners.container.Output'
check_mode: no

- name: (Container) Check runner is registered
- name: (Container) List configured runners
docker_container:
name: "{{ gitlab_runner_container_name }}-check"
name: "{{ gitlab_runner_container_name }}-list"
image: "{{ gitlab_runner_container_image }}:{{ gitlab_runner_container_tag }}"
command: verify
command: list
mounts:
- type: bind
source: "{{ gitlab_runner_container_mount_path }}"
Expand All @@ -33,19 +33,10 @@
interactive: yes
tty: yes
detach: no
register: verified_runners
ignore_errors: True
register: configured_runners
changed_when: False
check_mode: no

- name: configured_runners?
debug:
msg: "{{ configured_runners.container.Output }}"

- name: verified_runners?
debug:
msg: "{{ verified_runners.container.Output }}"

- name: (Container) Register GitLab Runner
include_tasks: register-runner-container.yml
when: gitlab_runner.token is defined or gitlab_runner_registration_token | string | length > 0 # Ensure value is set
Expand Down
15 changes: 7 additions & 8 deletions tasks/Unix.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,16 @@
import_tasks: install-arch.yml
when: ansible_os_family == 'Archlinux'

- name: (Unix) List configured runners
command: "{{ gitlab_runner_executable }} list"
register: configured_runners
changed_when: False
- name: (Unix) Delete no longer existing runners
command: "{{ gitlab_runner_executable }} verify --delete"
register: verified_runners
changed_when: '"Updated " in verified_runners.stderr'
check_mode: no
become: "{{ gitlab_runner_system_mode }}"

- name: (Unix) Check runner is registered
command: "{{ gitlab_runner_executable }} verify"
register: verified_runners
ignore_errors: True
- name: (Unix) List configured runners
command: "{{ gitlab_runner_executable }} list"
register: configured_runners
changed_when: False
check_mode: no
become: "{{ gitlab_runner_system_mode }}"
Expand Down
15 changes: 7 additions & 8 deletions tasks/Windows.yml
Original file line number Diff line number Diff line change
@@ -1,20 +1,19 @@
- name: Install GitLab Runner (Windows)
import_tasks: install-windows.yml

- name: (Windows) List configured runners
win_command: "{{ gitlab_runner_executable }} list"
- name: (Windows) Delete no longer existing runners
win_command: "{{ gitlab_runner_executable }} verify --delete"
args:
chdir: "{{ gitlab_runner_config_file_location }}"
register: configured_runners
changed_when: False
register: verified_runners
changed_when: '"Updated " in verified_runners.stderr'
check_mode: no

- name: (Windows) Check runner is registered
win_command: "{{ gitlab_runner_executable }} verify"
- name: (Windows) List configured runners
win_command: "{{ gitlab_runner_executable }} list"
args:
chdir: "{{ gitlab_runner_config_file_location }}"
register: verified_runners
ignore_errors: True
register: configured_runners
changed_when: False
check_mode: no

Expand Down
21 changes: 3 additions & 18 deletions tasks/register-runner-container.yml
Original file line number Diff line number Diff line change
@@ -1,18 +1,4 @@
---
- name: Clear Config File
block:
- name: remove config.toml file
file:
path: "{{ gitlab_runner_config_file }}"
state: absent

- name: Ensure config.toml exists
file:
path: "{{ gitlab_runner_config_file }}"
state: touch
modification_time: preserve
access_time: preserve
when: (verified_runners.container.Output.find("Verifying runner... is removed") != -1)

- name: Register runner to GitLab
docker_container:
Expand Down Expand Up @@ -123,7 +109,6 @@
cleanup: yes
auto_remove: yes
network_mode: "{{ gitlab_runner_container_network }}"
when: (verified_runners.container.Output.find("Verifying runner... is removed") != -1) or
((configured_runners.container.Output.find('\n' + gitlab_runner.name|default(ansible_hostname+'-'+gitlab_runner_index|string)) == -1) and
(gitlab_runner.state|default('present') == 'present'))
no_log: false
when: ('\n' + gitlab_runner.name|default(ansible_hostname+'-'+gitlab_runner_index|string)) not in configured_runners.container.Output and
gitlab_runner.state|default('present') == 'present'
no_log: false
25 changes: 2 additions & 23 deletions tasks/register-runner-windows.yml
Original file line number Diff line number Diff line change
@@ -1,23 +1,4 @@
---
- name: (Windows) Clear Config File
block:
- name: (Windows) remove config.toml file
win_file:
path: "{{ gitlab_runner_config_file }}"
state: absent

- name: (Windows) Create .gitlab-runner dir
win_file:
path: "{{ gitlab_runner_config_file_location }}"
state: directory

- name: (Windows) Ensure config.toml exists
win_file:
path: "{{ gitlab_runner_config_file }}"
state: touch
modification_time: preserve
access_time: preserve
when: (verified_runners.stderr.find("Verifying runner... is removed") != -1)

- name: (Windows) Register runner to GitLab
win_shell: >
Expand Down Expand Up @@ -119,9 +100,7 @@
{% if gitlab_runner.extra_registration_option is defined %}
{{ gitlab_runner.extra_registration_option }}
{% endif %}
when: (verified_runners.stderr.find("Verifying runner... is removed") != -1) or
((configured_runners.stderr.find('\n' + gitlab_runner.name|default(ansible_hostname+'-'+gitlab_runner_index|string)) == -1) and
(gitlab_runner.state|default('present') == 'present'))
when: ('\n' + gitlab_runner.name|default(ansible_hostname+'-'+gitlab_runner_index|string)) not in configured_runners.stderr and
gitlab_runner.state|default('present') == 'present'
args:
chdir: "{{ gitlab_runner_config_file_location }}"
#no_log: true
28 changes: 2 additions & 26 deletions tasks/register-runner.yml
Original file line number Diff line number Diff line change
@@ -1,27 +1,4 @@
---
- name: Clear Config File
block:
- name: remove config.toml file
file:
path: "{{ gitlab_runner_config_file }}"
state: absent
become: "{{ gitlab_runner_system_mode }}"

- name: Create .gitlab-runner dir
file:
path: "{{ gitlab_runner_config_file_location }}"
state: directory
mode: '0700'
become: "{{ gitlab_runner_system_mode }}"

- name: Ensure config.toml exists
file:
path: "{{ gitlab_runner_config_file }}"
state: touch
modification_time: preserve
access_time: preserve
become: "{{ gitlab_runner_system_mode }}"
when: (verified_runners.stderr.find("Verifying runner... is removed") != -1)

- name: Accept gitlab server self signed cert as valid CA
shell: "openssl s_client -connect {{gitlab_server_ip}}:443 -showcerts </dev/null 2>/dev/null | sed -e '/-----BEGIN/,/-----END/!d' | tee {{tls_ca_file}} >/dev/null"
Expand Down Expand Up @@ -163,8 +140,7 @@
--cache-s3-secret-key '{{ gitlab_runner.cache_s3_secret_key }}'
{% endif %}
--ssh-password '{{ gitlab_runner.ssh_password|default("") }}'
when: (verified_runners.stderr.find("Verifying runner... is removed") != -1) or
((configured_runners.stderr.find('\n' + gitlab_runner.name|default(ansible_hostname+'-'+gitlab_runner_index|string)) == -1) and
(gitlab_runner.state|default('present') == 'present'))
when: ('\n' + gitlab_runner.name|default(ansible_hostname+'-'+gitlab_runner_index|string)) not in configured_runners.stderr and
gitlab_runner.state|default('present') == 'present'
no_log: true
become: "{{ gitlab_runner_system_mode }}"

0 comments on commit 831fb29

Please sign in to comment.