Skip to content

Commit

Permalink
move-to-concat - Move runner configuration from exec to concat::fragment
Browse files Browse the repository at this point in the history
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
baurmatt committed Jan 15, 2020
1 parent f44cf35 commit a06ecc5
Show file tree
Hide file tree
Showing 11 changed files with 516 additions and 206 deletions.
3 changes: 3 additions & 0 deletions .sync.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,6 @@ Gemfile:
optional:
':test':
- gem: 'toml-rb'
spec/spec_helper.rb:
spec_overrides:
- require 'spec_helper_local'
88 changes: 57 additions & 31 deletions REFERENCE.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ _Private Classes_

**Defined types**

* [`gitlab_ci_runner::runner`](#gitlab_ci_runnerrunner): This module installs and configures Gitlab CI Runners.
* [`gitlab_ci_runner::runner`](#gitlab_ci_runnerrunner): This configures a Gitlab CI runner.

**Data types**

Expand Down Expand Up @@ -181,18 +181,67 @@ Default value: '/etc/gitlab-runner/config.toml'

### gitlab_ci_runner::runner

This module installs and configures Gitlab CI Runners.
This configures a Gitlab CI runner.

#### Examples

##### Simple runner registration
##### Add a simple runner

```puppet
gitlab_ci_runner::runner { example_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',
},
}
```

##### Add a autoscaling runner with DigitalOcean as IaaS

```puppet
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,
},
},
},
}
```
Expand All @@ -207,30 +256,7 @@ Data type: `Hash`

Hash with configuration options.
See https://docs.gitlab.com/runner/configuration/advanced-configuration.html for all possible options.

##### `ensure`

Data type: `Enum['present', 'absent']`

If the runner should be 'present' or 'absent'. Will register/unregister the runner from Gitlab.

Default value: 'present'

##### `runner_name`

Data type: `String[1]`

The name of the runner.

Default value: $title

##### `binary`

Data type: `String[1]`

The name of the Gitlab runner binary.

Default value: 'gitlab-runner'
If you omit the 'name' configuration, we will automatically use the $title of this define class.

## Data types

Expand Down
3 changes: 0 additions & 3 deletions manifests/init.pp
Original file line number Diff line number Diff line change
Expand Up @@ -94,16 +94,13 @@

$runners.each |$runner_name,$config| {
$_config = merge($runner_defaults, $config)
$ensure = $_config['ensure']
$title = $_config['name'] ? {
undef => $runner_name,
default => $_config['name'],
}

gitlab_ci_runner::runner { $title:
ensure => $ensure,
config => $_config - ['ensure', 'name'],
binary => $package_name,
require => Class['gitlab_ci_runner::config'],
notify => Class['gitlab_ci_runner::service'],
}
Expand Down
116 changes: 67 additions & 49 deletions manifests/runner.pp
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) %>"),
}
}
Loading

0 comments on commit a06ecc5

Please sign in to comment.