Puppet Class: profile::gitlab::runner

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

Overview

picks only jobs created on protected branches or protected tags, and ignores other jobs. with restricted firewall enabled. Only used when restrict_firewall is True.

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

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

    Version of Docker to install

  • 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

  • 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.

  • 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.

  • 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'))


27
28
29
30
31
32
33
34
35
36
37
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
# File 'modules/profile/manifests/gitlab/runner.pp', line 27

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_version     = lookup('profile::gitlab::runner::docker_version'),
    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'),
    Systemd::Timer::Schedule                    $clear_interval     = lookup('profile::gitlab::runner::clear_interval'),
    Boolean                                     $enable_clear_cache = lookup('profile::gitlab::runner::enable_clear_cache'),
) {
    class { 'docker::configuration':
        settings => $docker_settings,
    }

    if debian::codename::ge('buster') {
        ensure_packages('apparmor')
    }

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

    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,
    }

    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
    }

    class { 'gitlab_runner::config':
        concurrent              => $concurrent,
        docker_image            => $docker_image,
        docker_network          => $docker_network,
        ensure_buildkitd        => $ensure_buildkitd,
        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],
    }

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

        exec { 'gitlab-register-runner':
            user    => $gitlab_runner_user,
            command => @("CMD"/L$)
                /usr/bin/gitlab-runner register \
                --template-config /etc/gitlab-runner/config-template.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'],
        }

    } 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,
        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'],
    }
}