Puppet Class: profile::gitlab::runner

Defined in:
modules/profile/manifests/gitlab/runner.pp

Summary

configure host to be a docker based GitLab runner

Overview

SPDX-License-Identifier: Apache-2.0 picks only jobs created on protected branches or protected tags, and ignores other jobs.

Parameters:

  • ensure (Wmflib::Ensure) (defaults to: lookup('profile::gitlab::runner::ensure'))

    Ensure of the resources that support it

  • access_level (Enum['not_protected', 'ref_protected']) (defaults to: lookup('profile::gitlab::runner::access_level'))

    Whether the runner is protected or not. Whenever a runner is protected, it

  • concurrent (Integer) (defaults to: lookup('profile::gitlab::runner::concurrent'))

    Number of concurrent jobs allowed by this runner.

  • docker_image (String) (defaults to: lookup('profile::gitlab::runner::docker_image'))

    Default Docker image to use for job execution

  • docker_volume (Boolean) (defaults to: lookup('profile::gitlab::runner::docker_volume'))

    Use a separate volume for docker data (for use on WMCS)

  • docker_volume_min (Integer) (defaults to: lookup('profile::gitlab::runner::docker_volume_min'))

    Minimum size (Gb) of attached volumes considered for the docker mount.

  • docker_volume_max (Integer) (defaults to: lookup('profile::gitlab::runner::docker_volume_max'))

    Maximum size (Gb) of attached volumes considered for the docker mount.

  • docker_network (String) (defaults to: lookup('profile::gitlab::runner::docker_network'))

    Name of the Docker network to provision for the runner.

  • docker_settings (Hash) (defaults to: lookup('profile::gitlab::runner::docker_settings'))

    Docker daemon settings

  • gitlab_url (Stdlib::HTTPSUrl) (defaults to: lookup('profile::gitlab::runner::gitlab_url'))

    URL of the GitLab instance on which to register

  • locked (Boolean) (defaults to: lookup('profile::gitlab::runner::locked'))

    Whether the runner is locked and can/cannot be enabled for projects

  • registration_token (String) (defaults to: lookup('profile::gitlab::runner::registration_token'))

    Token used to register the runner with the GitLab instance.

  • run_untagged (Boolean) (defaults to: lookup('profile::gitlab::runner::run_untagged'))

    Whether the runner should also run untagged jobs

  • tags (Array[String]) (defaults to: lookup('profile::gitlab::runner::tags'))

    Tags used to schedule matching jobs to this runner

  • enable_exporter (Boolean) (defaults to: lookup('profile::gitlab::runner::enable_exporter', {default_value => false}))

    Enable Prometheus metrics exporter

  • gitlab_runner_user (String) (defaults to: lookup('profile::gitlab::runner::user'))

    User which is used to execute gitlab-runner daemon

  • restrict_firewall (Boolean) (defaults to: lookup('profile::gitlab::runner::restrict_firewall'))

    Enable default REJECT rule for all egress Docker traffic to wmnet

  • allowed_services (Hash[String, Gitlab_runner::AllowedService]) (defaults to: lookup('profile::gitlab::runner::allowed_services'))

    List of TCP services (host and port) which can be accessed from Docker with restricted firewall enabled. Only used when restrict_firewall is True.

  • ensure_buildkitd (Wmflib::Ensure) (defaults to: lookup('profile::gitlab::runner::ensure_buildkitd'))

    Whether to provide buildkitd for image building.

  • buildkitd_image (String) (defaults to: lookup('profile::gitlab::runner::buildkitd_image'))

    Ref to buildkitd container image.

  • buildkitd_nameservers (Array[Stdlib::Host]) (defaults to: lookup('profile::gitlab::runner::buildkitd_nameservers'))

    Nameservers that buildkitd will use for its executors.

  • clear_interval (Systemd::Timer::Schedule) (defaults to: lookup('profile::gitlab::runner::clear_interval'))

    Interval for cleanup of docker cache/volumes from old jobs.

  • enable_clear_cache (Boolean) (defaults to: lookup('profile::gitlab::runner::enable_clear_cache'))

    Enable automatic cleanup of cached/old docker volumes.

  • enable_webproxy (Boolean) (defaults to: lookup('profile::gitlab::runner::enable_webproxy'))

    Enable usage of webproxy for buildkit to access external resources. When 'true', uses values from http_proxy, http_proxy and no_proxy.

  • http_proxy (Optional[String]) (defaults to: lookup('profile::gitlab::runner::http_proxy'))

    Proxy URL to use for http (requires enable_webproxy=true)

  • https_proxy (Optional[String]) (defaults to: lookup('profile::gitlab::runner::https_proxy'))

    Proxy URL to use for https (requires enable_webproxy=true)

  • no_proxy (Optional[String]) (defaults to: lookup('profile::gitlab::runner::no_proxy'))

    Domains and addresses that shouldn't go through the proxies (requires enable_webproxy=true)

  • allowed_images (Array[String]) (defaults to: lookup('profile::gitlab::runner::allowed_images'))

    Images which are allowed to be executed in CI containers

  • allowed_docker_services (Array[String]) (defaults to: lookup('profile::gitlab::runner::allowed_docker_services'))

    Images which are allowed to be executed as services parallel to CI jobs

  • internal_ip_range (Stdlib::IP::Address::V4::CIDR) (defaults to: lookup('profile::gitlab::runner::internal_ip_range'))

    IPv4 range which is blocked when restrict_firewall=true

  • docker_subnet (Stdlib::IP::Address) (defaults to: lookup('profile::gitlab::runner::docker_subnet'))
  • docker_gc_interval (String) (defaults to: lookup('profile::gitlab::runner::docker_gc_interval'))
  • docker_gc_images_high_water_mark (String) (defaults to: lookup('profile::gitlab::runner::docker_gc_images_high_water_mark'))
  • docker_gc_images_low_water_mark (String) (defaults to: lookup('profile::gitlab::runner::docker_gc_images_low_water_mark'))
  • docker_gc_volumes_high_water_mark (String) (defaults to: lookup('profile::gitlab::runner::docker_gc_volumes_high_water_mark'))
  • docker_gc_volumes_low_water_mark (String) (defaults to: lookup('profile::gitlab::runner::docker_gc_volumes_low_water_mark'))


38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
# File 'modules/profile/manifests/gitlab/runner.pp', line 38

class profile::gitlab::runner (
    Wmflib::Ensure                              $ensure             = lookup('profile::gitlab::runner::ensure'),
    Enum['not_protected', 'ref_protected']      $access_level       = lookup('profile::gitlab::runner::access_level'),
    Integer                                     $concurrent         = lookup('profile::gitlab::runner::concurrent'),
    String                                      $docker_image       = lookup('profile::gitlab::runner::docker_image'),
    Boolean                                     $docker_volume      = lookup('profile::gitlab::runner::docker_volume'),
    Integer                                     $docker_volume_min  = lookup('profile::gitlab::runner::docker_volume_min'),
    Integer                                     $docker_volume_max  = lookup('profile::gitlab::runner::docker_volume_max'),
    String                                      $docker_network     = lookup('profile::gitlab::runner::docker_network'),
    Stdlib::IP::Address                         $docker_subnet      = lookup('profile::gitlab::runner::docker_subnet'),
    Hash                                        $docker_settings    = lookup('profile::gitlab::runner::docker_settings'),
    String                                      $docker_gc_interval = lookup('profile::gitlab::runner::docker_gc_interval'),
    String                                      $docker_gc_images_high_water_mark  = lookup('profile::gitlab::runner::docker_gc_images_high_water_mark'),
    String                                      $docker_gc_images_low_water_mark   = lookup('profile::gitlab::runner::docker_gc_images_low_water_mark'),
    String                                      $docker_gc_volumes_high_water_mark = lookup('profile::gitlab::runner::docker_gc_volumes_high_water_mark'),
    String                                      $docker_gc_volumes_low_water_mark  = lookup('profile::gitlab::runner::docker_gc_volumes_low_water_mark'),
    Stdlib::HTTPSUrl                            $gitlab_url         = lookup('profile::gitlab::runner::gitlab_url'),
    Boolean                                     $locked             = lookup('profile::gitlab::runner::locked'),
    String                                      $registration_token = lookup('profile::gitlab::runner::registration_token'),
    Boolean                                     $run_untagged       = lookup('profile::gitlab::runner::run_untagged'),
    Array[String]                               $tags               = lookup('profile::gitlab::runner::tags'),
    Boolean                                     $enable_exporter    = lookup('profile::gitlab::runner::enable_exporter', {default_value => false}),
    String                                      $gitlab_runner_user = lookup('profile::gitlab::runner::user'),
    Boolean                                     $restrict_firewall  = lookup('profile::gitlab::runner::restrict_firewall'),
    Hash[String, Gitlab_runner::AllowedService] $allowed_services   = lookup('profile::gitlab::runner::allowed_services'),
    Wmflib::Ensure                              $ensure_buildkitd   = lookup('profile::gitlab::runner::ensure_buildkitd'),
    String                                      $buildkitd_image    = lookup('profile::gitlab::runner::buildkitd_image'),
    Array[Stdlib::Host]                         $buildkitd_nameservers = lookup('profile::gitlab::runner::buildkitd_nameservers'),
    Systemd::Timer::Schedule                    $clear_interval     = lookup('profile::gitlab::runner::clear_interval'),
    Boolean                                     $enable_clear_cache = lookup('profile::gitlab::runner::enable_clear_cache'),
    Boolean                                     $enable_webproxy    = lookup('profile::gitlab::runner::enable_webproxy'),
    Optional[String]                            $http_proxy         = lookup('profile::gitlab::runner::http_proxy'),
    Optional[String]                            $https_proxy        = lookup('profile::gitlab::runner::https_proxy'),
    Optional[String]                            $no_proxy           = lookup('profile::gitlab::runner::no_proxy'),
    Array[String]                               $allowed_images     = lookup('profile::gitlab::runner::allowed_images'),
    Array[String]                               $allowed_docker_services = lookup('profile::gitlab::runner::allowed_docker_services'),
    Stdlib::IP::Address::V4::CIDR               $internal_ip_range  = lookup('profile::gitlab::runner::internal_ip_range'),
) {
    class { 'docker::configuration':
        settings => $docker_settings,
    }

    ensure_packages('apparmor')

    class { 'docker':
        package_name => 'docker.io',
    }

    if $docker_volume {
        cinderutils::ensure { '/var/lib/docker':
            min_gb        => $docker_volume_min,
            max_gb        => $docker_volume_max,
            mount_point   => '/var/lib/docker',
            mount_mode    => '711',
            mount_options => 'discard,defaults',
            before        => Class['docker'],
        }
    }

    docker::network { $docker_network:
        ensure => $ensure,
        subnet => $docker_subnet,
    }

    class { 'gitlab_runner::firewall':
        subnet            => $docker_subnet,
        restrict_firewall => $restrict_firewall,
        allowed_services  => $allowed_services,
        internal_ip_range => $internal_ip_range,
    }

    if $gitlab_runner_user != 'root' {

        # setup dedicated gitlab-runner user
        wmflib::dir::mkdir_p(
            ["/home/${gitlab_runner_user}/.${gitlab_runner_user}"],
            {owner => $gitlab_runner_user},
        )
        systemd::sysuser { $gitlab_runner_user:
            description       => 'used by gitlab-runner',
            home_dir          => "/home/${gitlab_runner_user}",
            additional_groups => ['docker'],
        }

        # grant read-only access to /etc/gitlab-runner folder
        file { '/etc/gitlab-runner/':
            ensure => 'directory',
            owner  => $gitlab_runner_user,
            mode   => '0400',
        }
    }

    apt::package_from_component{ 'gitlab-runner':
        component => 'thirdparty/gitlab-runner',
        require   =>  Class['docker'],
    }

    class { 'docker::gc':
        ensure                  => $ensure,
        interval                => $docker_gc_interval,
        images_high_water_mark  => $docker_gc_images_high_water_mark,
        images_low_water_mark   => $docker_gc_images_low_water_mark,
        volumes_high_water_mark => $docker_gc_volumes_high_water_mark,
        volumes_low_water_mark  => $docker_gc_volumes_low_water_mark,
    }

    $runner_name = "${::hostname}.${::domain}"

    $exporter_listen_address = $::realm ? {
        'production' => $facts['ipaddress6'], # export metrics on IPv6 in production
        default      => $facts['ipaddress'],  # export metrics on IPv4 everywhere else
    }

    $config_dir = $gitlab_runner_user ? {
        'root'  => '/etc/gitlab-runner',
        default => "/home/${gitlab_runner_user}/.gitlab-runner"
    }

    # Define common proxy environment variables for both buildkitd and jobs
    # that are executed by the runner
    # See https://wikitech.wikimedia.org/wiki/HTTP_proxy
    $proxy_variables = $enable_webproxy ? {
        true    => {
            'http_proxy'  => $http_proxy,
            'https_proxy' => $https_proxy,
            'no_proxy'    => $no_proxy,
            'HTTP_PROXY'  => $http_proxy,
            'HTTPS_PROXY' => $https_proxy,
            'NO_PROXY'    => $no_proxy,
        },
        default =>  {},
    }

    if $ensure == 'present' {
        $tag_list = join($tags, ',')

        exec { 'gitlab-register-runner':
            user    => $gitlab_runner_user,
            command => @("CMD"/L$)
                /usr/bin/gitlab-runner register \
                --config "${config_dir}/registration.toml" \
                --non-interactive \
                --name "${runner_name}" \
                --url "${gitlab_url}" \
                --registration-token "${registration_token}" \
                --executor "docker" \
                --docker-image "${docker_image}" \
                --tag-list "${tag_list}" \
                --run-untagged="${run_untagged}" \
                --locked="${locked}" \
                --access-level="${access_level}"
                |- CMD
            ,
            unless  => "/usr/bin/gitlab-runner list 2>&1 | /bin/grep -q '^${runner_name}'",
            require => [Apt::Package_from_component['gitlab-runner'], Exec['gitlab-clear-registration-toml']],
            notify  => Exec['gitlab-runner-merge-configs'],
        }

        # registration.toml has to be cleared, otherwise old and new runner run simultaneously
        exec { 'gitlab-clear-registration-toml':
            user    => $gitlab_runner_user,
            command => "/usr/bin/truncate -s 0 ${config_dir}/registration.toml",
            unless  => "/usr/bin/gitlab-runner list 2>&1 | /bin/grep -q '^${runner_name}'",
            before  =>  Exec['gitlab-register-runner'],
        }

        class { 'gitlab_runner::config':
            directory               => $config_dir,
            concurrent              => $concurrent,
            docker_image            => $docker_image,
            docker_network          => $docker_network,
            ensure_buildkitd        => $ensure_buildkitd,
            environment             => $proxy_variables,
            gitlab_url              => $gitlab_url,
            runner_name             => $runner_name,
            exporter_listen_address => $exporter_listen_address,
            enable_exporter         => $enable_exporter,
            gitlab_runner_user      => $gitlab_runner_user,
            require                 => Docker::Network[$docker_network],
            allowed_images          => $allowed_images,
            allowed_docker_services => $allowed_docker_services,
        }
    } else {
        exec { 'gitlab-unregister-runner':
            user    => $gitlab_runner_user,
            command => "/usr/bin/gitlab-runner unregister --name '${runner_name}'",
            onlyif  => "/usr/bin/gitlab-runner list 2>&1 | /bin/grep -q '^${runner_name}'",
            before  =>  Package['gitlab-runner'],
        }
    }

    class { 'buildkitd':
        ensure      => $ensure_buildkitd,
        network     => $docker_network,
        image       => $buildkitd_image,
        nameservers => $buildkitd_nameservers,
        environment => $proxy_variables,
        require     => Docker::Network[$docker_network],
    }

    $ensure_clear_cache = $enable_clear_cache.bool2str('present','absent')
    systemd::timer::job { 'clear-docker-cache':
        ensure      => $ensure_clear_cache,
        user        => 'root',
        description => 'Clear docker cache/volumes',
        command     => '/usr/share/gitlab-runner/clear-docker-cache',
        interval    => $clear_interval,
        require     =>  Package['gitlab-runner'],
    }
}