Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Runners add support for multiple runners #439

Closed
wants to merge 13 commits into from
12 changes: 5 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -181,17 +181,15 @@ gitlab::cirunner::concurrent: 4
gitlab::cirunner::metrics_server: "localhost:8888"

gitlab_ci_runners:
gitlab::cirunner::default_token: '1234567890abcdef'
gitlab::cirunner::default_url: https://git.example.com/ci
gitlab::cirunner::docker_runners:
test_runner1:{}
test_runner2:{}
test_runner3:
url: "https://git.alternative.org/ci"
registration-token: "abcdef1234567890"

gitlab_ci_runners_defaults:
url: "https://git.example.com/ci"
registration-token: "1234567890abcdef"
executor: "docker"
docker-image: "ubuntu:trusty"
token: "abcdef1234567890"
gitlab::cirunner::docker::default_image: 'ubuntu_trusty'
```

### NGINX Configuration
Expand Down
18 changes: 18 additions & 0 deletions lib/puppet/functions/gitlab/cirunner/cmd_str.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
Puppet::Functions.create_function('gitlab::cirunner::cmd_str') do
dispatch :cmd_str do
param 'String', :name
param 'Array', :values
# only avalible in puppet 4.7
# return_type 'String'
end
def cmd_str(name, values)
values.each do |value|
return "--#{name} #{value}" if value.is_a?(String) && !value.empty?
return "--#{name} #{value}" if value.is_a?(Integer)
return "--#{name} #{value.join(',')}" if value.is_a?(Array) && !value.empty?
return "--#{name}" if value.is_a?(TrueClass)
return '' if value.is_a?(FalseClass)
end
''
end
end
190 changes: 23 additions & 167 deletions manifests/cirunner.pp
Original file line number Diff line number Diff line change
@@ -1,178 +1,34 @@
# == Class: gitlab::cirunner
#
# This module installs and configures Gitlab CI Runners.
#
# === Parameters
#
# [*concurrent*]
# Default: `undef`
# The limit on the number of jobs that can run concurrently among
# all runners, or `undef` to leave unmanaged.
#
# [*metrics_server*]
# Default: `undef`
# [host]:<port> to enable metrics server as described in
# https://docs.gitlab.com/runner/monitoring/README.html#configuration-of-the-metrics-http-server
#
# [*hiera_default_config_key*]
# Default: gitlab_ci_runners_defaults
# Name of hiera hash with default configs for CI Runners.
# The config is the parameters for the /usr/bin/gitlab-ci-multi-runner register
# command (for version 10.x: /usr/bin/gitlab-runner).
#
# [*hiera_runners_key*]
# Default: gitlab_ci_runners
# Name of hiera hash with individual runners to be installed.
#
# === Authors
#
# Tobias Brunner <[email protected]>
# Matthias Indermuehle <[email protected]>
#
# === Copyright
#
# Copyright 2015 Tobias Brunner, VSHN AG
# @summary This module installs and configures Gitlab CI Runners.
# @param conf_file path to the config file
# @param url the url to the gitlab server
# @param runners the hash of runners
#
class gitlab::cirunner (
$concurrent = undef,
$metrics_server = undef,
$hiera_default_config_key = 'gitlab_ci_runners_defaults',
$hiera_runners_key = 'gitlab_ci_runners',
$manage_docker = true,
$manage_repo = true,
$xz_package_name = 'xz-utils',
$package_ensure = installed,
$package_name = 'gitlab-runner',
String $conf_file = '/etc/gitlab-runner/config.toml',
String $url = 'https://gitlab.com',
Hash[String, String] $runners = {},
) {

validate_string($hiera_default_config_key)
validate_string($hiera_runners_key)
validate_bool($manage_docker)
validate_bool($manage_repo)
validate_string($xz_package_name)
validate_string($package_ensure)
validate_string($package_name)

unless ($::osfamily == 'Debian' or $::osfamily == 'RedHat') {
fail ("OS family ${::osfamily} is not supported. Only Debian and Redhat is suppported.")
}

if $manage_docker {
include ::docker
# workaround for cirunner issue #1617
# https://gitlab.com/gitlab-org/gitlab-ci-multi-runner/issues/1617
ensure_packages($xz_package_name)

$docker_images = {
ubuntu_trusty => {
image => 'ubuntu',
image_tag => 'trusty',
},
}
class { '::docker::images':
images => $docker_images,
}
}

if $manage_repo {
$repo_base_url = 'https://packages.gitlab.com'

case $::osfamily {
'Debian': {
include apt
ensure_packages('apt-transport-https')

$distid = downcase($::lsbdistid)

::apt::source { 'apt_gitlabci':
comment => 'GitlabCI Runner Repo',
location => "${repo_base_url}/runner/${package_name}/${distid}/",
release => $::lsbdistcodename,
repos => 'main',
key => {
'id' => '1A4C919DB987D435939638B914219A96E15E78F4',
'server' => 'keys.gnupg.net',
},
include => {
'src' => false,
'deb' => true,
},
}
Apt::Source['apt_gitlabci'] -> Package[$package_name]
Exec['apt_update'] -> Package[$package_name]
}
'RedHat': {
yumrepo { "runner_${package_name}":
ensure => 'present',
baseurl => "${repo_base_url}/runner/${package_name}/el/${::operatingsystemmajrelease}/\$basearch",
descr => "runner_${package_name}",
enabled => '1',
gpgcheck => '0',
gpgkey => "${repo_base_url}/gpg.key",
repo_gpgcheck => '1',
sslcacert => '/etc/pki/tls/certs/ca-bundle.crt',
sslverify => '1',
}

yumrepo { "runner_${package_name}-source":
ensure => 'present',
baseurl => "${repo_base_url}/runner/${package_name}/el/${::operatingsystemmajrelease}/SRPMS",
descr => "runner_${package_name}-source",
enabled => '1',
gpgcheck => '0',
gpgkey => "${repo_base_url}/gpg.key",
repo_gpgcheck => '1',
sslcacert => '/etc/pki/tls/certs/ca-bundle.crt',
sslverify => '1',
}
}
default: {
fail ("gitlab::cirunner::manage_repo parameter for ${::osfamily} is not supported.")
}
}
}

package { $package_name:
ensure => $package_ensure,
}

if $concurrent {
validate_integer($concurrent, undef, 1)

file_line { 'gitlab-runner-concurrent':
path => '/etc/gitlab-runner/config.toml',
line => "concurrent = ${concurrent}",
match => '^concurrent = \d+',
require => Package[$package_name],
notify => Exec['gitlab-runner-restart'],
}
}

if $metrics_server {
validate_re($metrics_server, '.*:.+', 'metrics_server must be in the format [host]:<port>')

file_line { 'gitlab-runner-metrics-server':
path => '/etc/gitlab-runner/config.toml',
line => "metrics_server = \"${metrics_server}\"",
match => '^metrics_server = .+',
require => Package[$package_name],
notify => Exec['gitlab-runner-restart'],
}
}
$concurrent = $runners.size
$package_name = 'gitlab-runner'
ensure_packages([$package_name])

exec { 'gitlab-runner-restart':
command => "/usr/bin/${package_name} restart",
refreshonly => true,
require => Package[$package_name],
}

$runners_hash = hiera_hash($hiera_runners_key, {})
$runners = keys($runners_hash)
$default_config = hiera_hash($hiera_default_config_key, {})
gitlab::runner { $runners:
binary => $package_name,
default_config => $default_config,
runners_hash => $runners_hash,
require => Exec['gitlab-runner-restart'],
$runners.each |$name, $token| {
$command = "/usr/bin/gitlab-ci-multi-runner register -n --executor shell --token ${token} --url ${url}"
exec { "Register_runner ${name}":
command => $command,
unless => "/bin/grep ${token} ${conf_file}",
require => Package[$package_name],
}
}
file_line { 'gitlab-runner-concurrent':
path => $conf_file,
line => "concurrent = ${concurrent}",
match => '^concurrent = \d+',
notify => Exec['gitlab-runner-restart'],
}
}
70 changes: 70 additions & 0 deletions manifests/cirunner/docker.pp
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
# == Class: gitlab::cirunner::docker
#
class gitlab::cirunner::docker (
Boolean $manage_docker = true,
String $xz_package_name = 'xz-utils',
String $default_image = 'ubuntu_trusty',
Hash $docker_images = {},
Hash $runners = {},
Optional[String] $default_token = undef,
Optional[String] $default_url = undef,
Optional[Boolean] $default_run_untagged = undef,
Optional[Boolean] $default_locked = undef,
Optional[Array[String]] $default_tags = undef,
Optional[String] $default_host = undef,
Optional[String] $default_cert_path = undef,
Optional[Boolean] $default_tlsverify = undef,
Optional[String] $default_hostname = undef,
Optional[String] $default_cpuset_cpus = undef,
Optional[Integer] $default_cpus = undef,
Optional[Array[String]] $default_dns = undef,
Optional[Array[String]] $default_dns_search = undef,
Optional[Boolean] $default_privileged = undef,
Optional[String] $default_userns = undef,
Optional[Boolean] $default_cap_add = undef,
Optional[Boolean] $default_cap_drop = undef,
Optional[String] $default_security_opt = undef,
Optional[String] $default_devices = undef,
Optional[Boolean] $default_disable_cache = undef,
Optional[String] $default_volumes = undef,
Optional[String] $default_volume_driver = undef,
Optional[String] $default_cache_dir = undef,
Optional[String] $default_extra_hosts = undef,
Optional[Array[String]] $default_volumes_from = undef,
Optional[String] $default_network_mode = undef,
Optional[Array[String]] $default_links = undef,
Optional[Array[String]] $default_services = undef,
Optional[Integer] $default_wait_for_services_timeout = undef,
Optional[Array[String]] $default_allowed_images = undef,
Optional[Array[String]] $default_allowed_services = undef,
Optional[String] $default_pull_policy = undef,
Optional[Integer] $default_shm_size = undef,
) {
include ::gitlab::cirunner

if $manage_docker {
if ! empty($docker_images) and ! has_key($docker_images, $default_image) {
fail("Docker not configuered with default image: ${default_image}")
}
include ::docker
# workaround for cirunner issue #1617
# https://gitlab.com/gitlab-org/gitlab-ci-multi-runner/issues/1617
ensure_packages($xz_package_name)
if !defined(Class['::docker::images']) {
$_trusty_images = {
ubuntu_trusty => {
image => 'ubuntu',
image_tag => 'trusty',
},
}
$_docker_images = empty($docker_images) ? {
false => $docker_images,
default => $_trusty_images,
}
class { '::docker::images':
images => $_docker_images,
}
}
}
create_resources('gitlab::runner::docker', $runners)
}
75 changes: 75 additions & 0 deletions manifests/cirunner/docker_ssh.pp
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
# == Class: gitlab::cirunner::docker
#
class gitlab::cirunner::docker_ssh (
Boolean $manage_docker = true,
String $xz_package_name = 'xz-utils',
String $default_image = 'ubuntu_trusty',
Hash $docker_images = {},
Hash $runners = {},
Optional[String] $default_token = undef,
Optional[String] $default_url = undef,
Optional[Boolean] $default_run_untagged = undef,
Optional[Boolean] $default_locked = undef,
Optional[Array[String]] $default_tags = undef,
Optional[String] $default_host = undef,
Optional[String] $default_cert_path = undef,
Optional[Boolean] $default_tlsverify = undef,
Optional[String] $default_hostname = undef,
Optional[String] $default_cpuset_cpus = undef,
Optional[Integer] $default_cpus = undef,
Optional[Array[String]] $default_dns = undef,
Optional[Array[String]] $default_dns_search = undef,
Optional[Boolean] $default_privileged = undef,
Optional[String] $default_userns = undef,
Optional[Boolean] $default_cap_add = undef,
Optional[Boolean] $default_cap_drop = undef,
Optional[String] $default_security_opt = undef,
Optional[String] $default_devices = undef,
Optional[Boolean] $default_disable_cache = undef,
Optional[String] $default_volumes = undef,
Optional[String] $default_volume_driver = undef,
Optional[String] $default_cache_dir = undef,
Optional[String] $default_extra_hosts = undef,
Optional[Array[String]] $default_volumes_from = undef,
Optional[String] $default_network_mode = undef,
Optional[Array[String]] $default_links = undef,
Optional[Array[String]] $default_services = undef,
Optional[Integer] $default_wait_for_services_timeout = undef,
Optional[Array[String]] $default_allowed_images = undef,
Optional[Array[String]] $default_allowed_services = undef,
Optional[String] $default_pull_policy = undef,
Optional[Integer] $default_shm_size = undef,
Optional[String] $default_ssh_user = undef,
Optional[String] $default_ssh_password = undef,
Optional[String] $default_ssh_host = undef,
Optional[Integer[0, 65535]] $default_ssh_port = undef,
Optional[String] $default_ssh_identity_file = undef,
) {
include ::gitlab::cirunner

if $manage_docker {
if ! empty($docker_images) and ! has_key($docker_images, $default_image) {
fail("Docker not configuered with default image: ${default_image}")
}
include ::docker
# workaround for cirunner issue #1617
# https://gitlab.com/gitlab-org/gitlab-ci-multi-runner/issues/1617
ensure_packages($xz_package_name)
if !defined(Class['::docker::images']) {
$_trusty_images = {
ubuntu_trusty => {
image => 'ubuntu',
image_tag => 'trusty',
},
}
$_docker_images = empty($docker_images) ? {
false => $docker_images,
default => $_trusty_images,
}
class { '::docker::images':
images => $_docker_images,
}
}
}
create_resources('gitlab::runner::docker_ssh', $runners)
}
Loading