Puppet Class: jenkins

Defined in:
modules/jenkins/manifests/init.pp

Overview

Class jenkins

Set up a basic Jenkins master instance for CI.

Parameters:

prefix

Prefix for web access to use with Apache proxying. Must be the full path with a leading slash. Example: /ci

access_log

Whether to enable the web service access.log. Boolean. Default: false

log_group

Unix group for the log files under /var/log/jenkins. Default: 'wikidev'

http_port

HTTP port for the web service. Default: 8080

max_open_files

Maximum number of file descriptors. Passed to systemd LimitNOFILE. Default: 8192.

service_ensure

Passed to Puppet Service. If set to 'unmanaged', pass undef to prevent Puppet from managing the service. Default: 'running'.

service_enable

Passed to Puppet Service as 'enable'. Default: true.

umask

Control permission bits of files created by Jenkins. Passed to systemd. Default: '0002'

builds_dir

Change location of build records. This option was formerly set in the jenkins ui, now must be set via system properties Default: $ITEM_ROOTDIR/builds

workspaces_dir

Change location of master node workspaces. This option was formerly set in the jenkins ui, now must be set via system properties Default: $ITEM_ROOTDIR/workspace

Parameters:

  • prefix (String)
  • access_log (Boolean) (defaults to: false)
  • log_group (String) (defaults to: 'wikidev')
  • http_port (Stdlib::Port) (defaults to: 8080)
  • max_open_files (Integer) (defaults to: 8192)
  • service_ensure (Enum['running', 'stopped', 'unmanaged']) (defaults to: 'running')
  • service_enable (Boolean) (defaults to: true)
  • service_monitor (Boolean) (defaults to: true)
  • umask (Stdlib::Filemode) (defaults to: '0002')
  • builds_dir (String) (defaults to: "\${ITEM_ROOTDIR}/builds")
  • workspaces_dir (String) (defaults to: "\${ITEM_ROOTDIR}/workspace")


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
# File 'modules/jenkins/manifests/init.pp', line 45

class jenkins(
    String $prefix,
    Boolean $access_log = false,
    String $log_group = 'wikidev',
    Stdlib::Port $http_port = 8080,
    Integer $max_open_files = 8192,
    Enum['running', 'stopped', 'unmanaged'] $service_ensure = 'running',
    Boolean $service_enable = true,
    Boolean $service_monitor = true,
    Stdlib::Filemode $umask = '0002',
    String $builds_dir = "\${ITEM_ROOTDIR}/builds",
    String $workspaces_dir = "\${ITEM_ROOTDIR}/workspace"
)
{
    user { 'jenkins':
        home       => '/var/lib/jenkins',
        shell      => '/bin/bash',  # admins need to be able to login
        gid        => 'jenkins',
        system     => true,
        managehome => false,
        require    => Group['jenkins'],
    }

    group { 'jenkins':
        ensure    => present,
        name      => 'jenkins',
        system    => true,
        allowdupe => false,
    }

    apt::package_from_component { 'jenkins-thirdparty-ci':
        component => 'thirdparty/ci',
        packages  => ['jenkins']
    }

    # Initially use Java 8 on Buster, switch to Java 11 separately, T224591
    if os_version('debian == buster') {
        apt::package_from_component { 'wikimedia-openjdk8':
            component => 'component/jdk8',
            packages  => ['openjdk-8-jdk', 'openjdk-8-dbg']
        }
    } else {
        require_package('openjdk-8-jdk')
    }
    Package['openjdk-8-jdk'] ~> Service['jenkins']

    $java_path = '/usr/lib/jvm/java-8-openjdk-amd64/jre/bin/java'

    file { '/var/lib/jenkins/.daemonrc':
        ensure  => 'absent',
    }

    file { '/etc/jenkins':
        ensure => directory,
        owner  => 'jenkins',
        group  => 'jenkins',
        mode   => '0755',
    }
    file { '/etc/jenkins/logging.properties':
        content => template('jenkins/logging.properties.erb'),
        owner   => 'jenkins',
        group   => 'jenkins',
        mode    => '0755',
    }

    systemd::syslog { 'jenkins':
        base_dir     => '/var/log',
        owner        => 'jenkins',
        group        => $log_group,
        readable_by  => 'group',
        log_filename => 'jenkins.log',
    }

    if $access_log {
        $jenkins_access_log_arg = '--accessLoggerClassName=winstone.accesslog.SimpleAccessLogger --simpleAccessLogger.format=combined --simpleAccessLogger.file=/var/log/jenkins/access.log'
        file { '/var/log/jenkins/access.log':
            ensure  => present,
            replace => false,
            owner   => 'jenkins',
            group   => $log_group,
            mode    => '0640',
            before  => Service['jenkins'],
        }
    } else {
        $jenkins_access_log_arg = undef
    }

    $builds_dir_for_systemd = regsubst( $builds_dir, '\$', '$$', 'G' )
    $workspaces_dir_for_systemd = regsubst( $workspaces_dir, '\$', '$$', 'G' )

    $java_args = join([
        # Allow graphs etc. to work even when an X server is present
        '-Djava.awt.headless=true',
        # Make Git plugin verbose which dramatically helps debugging
        '-Dhudson.plugins.git.GitSCM.verbose=true',
        # Prevents Jenkins 1.651.2+ from stripping parameters injected by the
        # Gearman plugin.
        #
        # References:
        #   https://phabricator.wikimedia.org/T133737
        #   https://jenkins.io/blog/2016/05/11/security-update/
        #   https://wiki.jenkins-ci.org/display/SECURITY/Jenkins+Security+Advisory+2016-05-11
        '-Dhudson.model.ParametersAction.keepUndefinedParameters=true',
        '-Djava.util.logging.config.file=/etc/jenkins/logging.properties',
        # Disable auto discovery T178608
        '-Dhudson.udp=-1',
        '-Dhudson.DNSMultiCast.disabled=true',
        "-Djenkins.model.Jenkins.buildsDir=${builds_dir_for_systemd}",
        "-Djenkins.model.Jenkins.workspacesDir=${workspaces_dir_for_systemd}",
        # T245658 Allow inline CSS and playing MP4 videos of test results.
        # To accomodate with systemd on Jessie, the whole argument has to be
        # double quoted to prevent word splitting.
        "\"-Dhudson.model.DirectoryBrowserSupport.CSP=sandbox; default-src 'none'; img-src 'self'; style-src 'self' 'unsafe-inline'; media-src 'self'\""
    ], ' ')

    $real_service_ensure = $service_ensure ? {
        'unmanaged' => undef,
        # Normalize to 'running' or 'stopped'
        default     => ensure_service($service_ensure),
    }

    systemd::service { 'jenkins':
        ensure         => 'present',
        content        => systemd_template('jenkins'),
        service_params => {
            enable => $service_enable,
            ensure => $real_service_ensure,
        },
        require        => [
            Systemd::Syslog['jenkins'],
            File['/etc/default/jenkins'],
        ],
    }

    if $service_monitor {
        nrpe::monitor_service { 'jenkins':
            description   => 'jenkins_service_running',
            contact_group => 'contint',
            nrpe_command  => "/usr/lib/nagios/plugins/check_procs -w 1:1 -c 1:1 --ereg-argument-array '.*/bin/java .*-jar /usr/share/jenkins/jenkins.war'",
            notes_url     => 'https://wikitech.wikimedia.org/wiki/Jenkins',
        }
    }

    file { '/srv/jenkins':
        ensure => directory,
        mode   => '0755',
        owner  => 'root',
        group  => 'root',
    }

    file { '/var/lib/jenkins':
        ensure => directory,
        mode   => '2775',  # group sticky bit
        owner  => 'jenkins',
        group  => 'jenkins',
    }
    # Top level jobs folder
    file { '/var/lib/jenkins/jobs':
        ensure => directory,
        mode   => '2775',  # group sticky bit
        owner  => 'jenkins',
        group  => 'jenkins',
    }
    # SECURITY-829 / CVE-2019-1003051
    # Plugin stores credentials unencrypted.
    file { '/var/lib/jenkins/hudson.plugins.ircbot.IrcPublisher.xml':
        ensure => present,
        mode   => '0660',
        owner  => 'jenkins',
        group  => 'adm',
    }

    file { '/var/lib/jenkins/logs':
        ensure => directory,
        mode   => '0700',
        owner  => 'jenkins',
        group  => 'adm',
    }
    file { '/var/lib/jenkins/secrets':
        ensure => directory,
        mode   => '0700',
        owner  => 'jenkins',
        group  => 'adm',
    }

    file { '/etc/default/jenkins':
        ensure  => absent,
        require => Package['jenkins'],
    }

}