forked from voxpupuli/puppet-gitlab_ci_runner
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
move-to-concat - Move runner configuration from exec to concat::fragment
This uses the foundation laid in e99ffe0 and moves the runner configuration from execs to concat::fragments. This was done to be able to change (parts) of the runner configuration after the inital Puppet run which is not possible with the exec because there is no config update logic in the gitlab-runner binary. Sadly this drops the support for autoregistering a runner on a Gitlab instance as discussed in voxpupuli#18. The registration can be done with Bolt task provide in voxpupuli#73.
- Loading branch information
Showing
11 changed files
with
516 additions
and
206 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,3 +13,6 @@ Gemfile: | |
optional: | ||
':test': | ||
- gem: 'toml-rb' | ||
spec/spec_helper.rb: | ||
spec_overrides: | ||
- require 'spec_helper_local' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,65 +1,83 @@ | ||
# @summary This module installs and configures Gitlab CI Runners. | ||
# @summary This configures a Gitlab CI runner. | ||
# | ||
# @example Simple runner registration | ||
# gitlab_ci_runner::runner { example_runner: | ||
# @example Add a simple runner | ||
# gitlab_ci_runner::runner { 'testrunner': | ||
# config => { | ||
# 'url' => 'https://gitlab.com', | ||
# 'token' => '123456789abcdefgh', # Note this is different from the registration token used by `gitlab-runner register` | ||
# 'executor' => 'shell', | ||
# }, | ||
# } | ||
# | ||
# @example Add a autoscaling runner with DigitalOcean as IaaS | ||
# gitlab_ci_runner::runner { 'autoscale-runner': | ||
# config => { | ||
# 'registration-token' => 'gitlab-token', | ||
# 'url' => 'https://gitlab.com', | ||
# 'tag-list' => 'docker,aws', | ||
# url => 'https://gitlab.com', | ||
# token => 'RUNNER_TOKEN', # Note this is different from the registration token used by `gitlab-runner register` | ||
# name => 'autoscale-runner', | ||
# executor => 'docker+machine', | ||
# limit => 10, | ||
# docker => { | ||
# image => 'ruby:2.6', | ||
# }, | ||
# machine => { | ||
# OffPeakPeriods => [ | ||
# '* * 0-9,18-23 * * mon-fri *', | ||
# '* * * * * sat,sun *', | ||
# ], | ||
# OffPeakIdleCount => 1, | ||
# OffPeakIdleTime => 1200, | ||
# IdleCount => 5, | ||
# IdleTime => 600, | ||
# MaxBuilds => 100, | ||
# MachineName => 'auto-scale-%s', | ||
# MachineDriver => 'digitalocean', | ||
# MachineOptions => [ | ||
# 'digitalocean-image=coreos-stable', | ||
# 'digitalocean-ssh-user=core', | ||
# 'digitalocean-access-token=DO_ACCESS_TOKEN', | ||
# 'digitalocean-region=nyc2', | ||
# 'digitalocean-size=4gb', | ||
# 'digitalocean-private-networking', | ||
# 'engine-registry-mirror=http://10.11.12.13:12345', | ||
# ], | ||
# }, | ||
# cache => { | ||
# 'Type' => 's3', | ||
# s3 => { | ||
# ServerAddress => 's3-eu-west-1.amazonaws.com', | ||
# AccessKey => 'AMAZON_S3_ACCESS_KEY', | ||
# SecretKey => 'AMAZON_S3_SECRET_KEY', | ||
# BucketName => 'runner', | ||
# Insecure => false, | ||
# }, | ||
# }, | ||
# }, | ||
# } | ||
# | ||
# @param config | ||
# Hash with configuration options. | ||
# See https://docs.gitlab.com/runner/configuration/advanced-configuration.html for all possible options. | ||
# @param ensure | ||
# If the runner should be 'present' or 'absent'. Will register/unregister the runner from Gitlab. | ||
# @param runner_name | ||
# The name of the runner. | ||
# @param binary | ||
# The name of the Gitlab runner binary. | ||
# If you omit the 'name' configuration, we will automatically use the $title of this define class. | ||
# | ||
define gitlab_ci_runner::runner ( | ||
Hash $config, | ||
Enum['present', 'absent'] $ensure = 'present', | ||
String[1] $runner_name = $title, | ||
String[1] $binary = 'gitlab-runner', | ||
Hash $config, | ||
) { | ||
# Set resource name as name for the runner and replace under_scores for later use | ||
$_name = regsubst($runner_name, '_', '-', 'G') | ||
include gitlab_ci_runner | ||
|
||
# To be able to use all parameters as command line arguments, | ||
# we have to transform the configuration into something the gitlab-runner | ||
# binary accepts: | ||
# * Always prefix the options with '--' | ||
# * Always join option names and values with '=' | ||
# | ||
# In the end, flatten thewhole array and join all elements with a space as delimiter | ||
$__config = $config.map |$item| { | ||
# Ensure all keys use '-' instead of '_'. Needed for e.g. build_dir. | ||
$key = regsubst($item[0], '_', '-', 'G') | ||
$config_path = $gitlab_ci_runner::config_path | ||
|
||
# Use title parameter if config hash doesn't contain one. | ||
$_config = $config['name'] ? { | ||
undef => merge($config, { name => $title }), | ||
default => $config, | ||
} | ||
|
||
# If the value ($item[1]) is an Array multiple elements are added for each item | ||
if $item[1] =~ Array { | ||
$item[1].map |$nested| { | ||
"--${key}=${nested}" | ||
} | ||
} else { | ||
"--${key}=${item[1]}" | ||
} | ||
}.flatten.join(' ') | ||
$__config = { runners => [ $_config ], } | ||
|
||
if $ensure == 'absent' { | ||
# Execute gitlab ci multirunner unregister | ||
exec {"Unregister_runner_${title}": | ||
command => "/usr/bin/${binary} unregister -n ${_name}", | ||
onlyif => "/bin/grep \'${_name}\' /etc/gitlab-runner/config.toml", | ||
} | ||
} else { | ||
# Execute gitlab ci multirunner register | ||
exec {"Register_runner_${title}": | ||
command => "/usr/bin/${binary} register -n ${__config}", | ||
unless => "/bin/grep -F \'${_name}\' /etc/gitlab-runner/config.toml", | ||
} | ||
concat::fragment { "${config_path} - ${title}": | ||
target => $config_path, | ||
order => 2, | ||
content => inline_template("<%= require 'toml-rb'; TomlRB.dump(@__config) %>"), | ||
} | ||
} |
Oops, something went wrong.