Puppet Class: puppetserver

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

Overview

SPDX-License-Identifier: Apache-2.0

Parameters:

  • ensure (Wmflib::Ensure) (defaults to: 'present')

    ensurable parameter

  • server_id (Stdlib::Fqdn) (defaults to: $facts['networking']['fqdn'])

    the server id to use for the host

  • ca_server (Stdlib::Fqdn) (defaults to: $server_id)

    the ca server

  • max_active_instances (Integer[1]) (defaults to: $facts['processors']['count'])

    number of instances to run

  • java_start_mem (Stdlib::Datasize) (defaults to: '1g')

    the value to use for the java args -Xms

  • java_max_mem (Stdlib::Datasize) (defaults to: '1g')

    the value to use for the java args -Xmx

  • config_dir (Stdlib::Unixpath) (defaults to: '/etc/puppet')

    the directory where config is stored

  • code_dir (Stdlib::Unixpath) (defaults to: "${config_dir}/code")

    the directory where code is stored

  • hiera_data_dir (Stdlib::Unixpath) (defaults to: "${config_dir}/hieradata")

    the default location for hiera data

  • hierarchy (Array[Puppetserver::Hierarchy]) (defaults to: [])

    a hash of hierarchy to add to the hiera file

  • puppetdb_urls (Array[Stdlib::HTTPUrl]) (defaults to: [])

    if present puppetdb will be configured using these urls

  • puppetdb_submit_only_urls (Array[Stdlib::HTTPUrl]) (defaults to: [])

    if present puppetdb will be configured using these urls for writes only

  • reports (Array[Puppetserver::Report,1]) (defaults to: ['store'])

    list of reports to configure

  • enc_path (Optional[Stdlib::Unixpath]) (defaults to: undef)

    path to an ENC script

  • listen_host (Stdlib::Host) (defaults to: $facts['networking']['ip'])

    host to bind webserver socket

  • autosign (Variant[Boolean, Stdlib::Unixpath]) (defaults to: false)

    if true autosign agent all certs, if a path then execute that script to validate

  • enable_jmx (Boolean) (defaults to: false)

    add the jmx java agent parameter

  • jmx_port (Stdlib::Port) (defaults to: 8141)

    the port for jmx to bind to

  • ssldir_on_srv (Boolean) (defaults to: false)

    used on cloud-vps; it allows storing certs on a detachable volume

  • separate_ssldir (Boolean) (defaults to: true)

    used when the puppetserver is managed by a different puppet server #TODO remove this setting in favor of ssldir_on_srv

  • auto_restart (Boolean) (defaults to: true)

    if true changes to config files will cause the puppetserver to either restart or reload the puppetserver service

  • extra_mounts (Hash[String, Stdlib::Unixpath]) (defaults to: {})

    hash of mount point name to path, mount point name will used in puppet:///<MOUNT POINT>

  • environment_timeout,

    number of seconds to cache code from an environment, or unlimited to never evict the cache

  • ca_allow_san (Boolean) (defaults to: false)

    whether to allow agents to request SANs

  • ca_name (Optional[String[1]]) (defaults to: undef)

    override the default Puppet CA name

  • strict_mode (Boolean) (defaults to: true)

    enable “strict mode”, same as defaults in Puppet 8, github.com/puppetlabs/puppet/wiki/Puppet-8-Compatibility#strict-mode

  • environment_timeout (Variant[Enum['unlimited'], Integer]) (defaults to: 0)


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
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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
# File 'modules/puppetserver/manifests/init.pp', line 29

class puppetserver (
    Wmflib::Ensure                           $ensure                    = 'present',
    Stdlib::Fqdn                             $server_id                 = $facts['networking']['fqdn'],
    Stdlib::Fqdn                             $ca_server                 = $server_id,
    Integer[1]                               $max_active_instances      = $facts['processors']['count'],
    Stdlib::Unixpath                         $config_dir                = '/etc/puppet',
    Stdlib::Unixpath                         $code_dir                  = "${config_dir}/code",
    Stdlib::Unixpath                         $hiera_data_dir            = "${config_dir}/hieradata",
    Stdlib::Datasize                         $java_start_mem            = '1g',
    Stdlib::Datasize                         $java_max_mem              = '1g',
    Array[Puppetserver::Hierarchy]           $hierarchy                 = [],
    Array[Stdlib::HTTPUrl]                   $puppetdb_urls             = [],
    Array[Stdlib::HTTPUrl]                   $puppetdb_submit_only_urls = [],
    Array[Puppetserver::Report,1]            $reports                   = ['store'],
    Optional[Stdlib::Unixpath]               $enc_path                  = undef,
    Stdlib::Host                             $listen_host               = $facts['networking']['ip'],
    Variant[Boolean, Stdlib::Unixpath]       $autosign                  = false,
    Boolean                                  $ssldir_on_srv             = false,
    Boolean                                  $separate_ssldir           = true,
    Boolean                                  $enable_jmx                = false,
    Boolean                                  $auto_restart              = true,
    Stdlib::Port                             $jmx_port                  = 8141,
    Hash[String, Stdlib::Unixpath]           $extra_mounts              = {},
    Variant[Enum['unlimited'], Integer]      $environment_timeout       = 0,
    Boolean                                  $ca_allow_san              = false,
    Optional[String[1]]                      $ca_name                   = undef,
    Boolean                                  $strict_mode               = true,
) {
    systemd::mask { 'puppetserver.service':
        unless => '/usr/bin/dpkg -s puppetserver | /bin/grep -q "^Status: install ok installed$"',
    }
    ensure_packages(['puppetserver'])
    systemd::unmask { 'puppetserver.service':
        refreshonly => true,
    }
    # Ensure puppetserver is not started on the first install
    # As we first need to configure the CA
    Systemd::Mask['puppetserver.service'] -> Package['puppetserver'] ~> Systemd::Unmask['puppetserver.service']

    $owner = 'puppet'
    $group = 'puppet'
    $ruby_load_path = '/usr/lib/puppetserver/ruby/vendor_ruby'
    $puppetserver_config_dir = "${config_dir}/puppetserver"
    # This is defined in /puppetserver.conf
    # This is used in systemd
    $config_d_dir = "${puppetserver_config_dir}/conf.d"
    $ca_dir = "${puppetserver_config_dir}/ca"
    $bootstap_config_dir = "${puppetserver_config_dir}/services.d"
    if $ssldir_on_srv {
        $ssl_dir = '/srv/puppet/server/ssl'
    } else {
        $ssl_dir = $separate_ssldir.bool2str('/var/lib/puppet/server/ssl', '/var/lib/puppet/ssl')
    }
    $environments_dir = "${code_dir}/environments"

    $service_reload_notify = $auto_restart ? {
        true  => Exec['reload puppetserver'],
        false => Exec['restart puppetserver required'],
    }
    $service_restart_notify = $auto_restart ? {
        true  => Service['puppetserver'],
        false => Exec['restart puppetserver required'],
    }

    # Frustratingly, puppet requires us to specify 'none' when
    #  we don't want reports (e.g. on cloud-vps.). When appending
    #  to that list we need to remove the 'none' to avoid a weird
    #  situation of having ['none', 'something'] in the list of
    #  report types.
    $_reports = $puppetdb_urls.empty ? {
        true  => $reports,
        false => $reports.delete('none') + 'puppetdb',
    }

    wmflib::dir::mkdir_p(
        [
            $code_dir,
            $environments_dir,
            $config_dir,
        ],
        {
            'mode'  => '0755',
        },
    )

    file { $config_d_dir:
        ensure  => directory,
        recurse => true,
        purge   => true,
        owner   => 'root',
        group   => 'root',
        mode    => '0755',
    }

    # Shared by profile::puppet::agent, but needs to have the correct
    # permissions prior to starting Puppet
    ensure_resource(
        'file',
        '/var/lib/puppet',
        {
            'ensure' => 'directory',
            'owner'  => 'puppet',
            'group'  => 'puppet',
            'mode'   => '0751',
        },
    )

    if $ssldir_on_srv {
        ensure_resource(
            'file',
            '/srv/puppet/server',
            {
                'ensure' => 'directory',
                'owner'  => 'puppet',
                'group'  => 'puppet',
                'mode'   => '0751',
            },
        )
        ensure_resource(
            'file',
            '/etc/puppet/puppetserver/ca',
            {
                'ensure' => link,
                'target' => '/srv/puppet/server/ssl/ca'
            },
        )
    } elsif $separate_ssldir {
        ensure_resource(
            'file',
            '/var/lib/puppet/server',
            {
                'ensure' => 'directory',
                'owner'  => 'puppet',
                'group'  => 'puppet',
                'mode'   => '0751',
            },
        )
    }


    # The puppetserver process itself enforces mode 0771 on the ssl dir, so this
    # should not be changed or it will create a perma-diff
    file { $ssl_dir:
        ensure => directory,
        owner  => $owner,
        group  => $group,
        mode   => '0771',
    }

    wmflib::dir::mkdir_p(
        [
            $puppetserver_config_dir,
            $bootstap_config_dir,
        ],
        {
            'owner' => $owner,
            'group' => $group,
            'mode'  => '0755',
        },
    )

    $config = @("CONFIG")

    [server]
    ca_server = ${ca_server}
    reports = ${_reports.unique.join(',')}
    codedir = ${code_dir}
    environment_timeout = ${environment_timeout}
    | CONFIG
    concat::fragment { 'server':
        target  => '/etc/puppet/puppet.conf',
        order   => '20',
        content => $config,
        notify  => $service_reload_notify,
        require => Systemd::Unmask['puppetserver.service'],
    }
    if ! $puppetdb_urls.empty {
        concat::fragment { 'server-storeconfigs':
            target  => '/etc/puppet/puppet.conf',
            order   => '21',
            content => "storeconfigs = true\nstoreconfigs_backend = puppetdb\n",
            notify  => $service_reload_notify,
            require => Systemd::Unmask['puppetserver.service'],
        }
    }
    if $enc_path {
        concat::fragment { 'server-enc':
            target  => '/etc/puppet/puppet.conf',
            order   => '22',
            content => "node_terminus = exec\nexternal_nodes = ${enc_path}\n",
            notify  => $service_reload_notify,
            require => Systemd::Unmask['puppetserver.service'],
        }
    }
    if $autosign {
        concat::fragment { 'server-autosign':
            target  => '/etc/puppet/puppet.conf',
            order   => '23',
            content => "autosign = ${autosign}\n",
            notify  => $service_reload_notify,
            require => Systemd::Unmask['puppetserver.service'],
        }
    }
    if $separate_ssldir {
        concat::fragment { 'separate-ssldir':
            target  => '/etc/puppet/puppet.conf',
            order   => '24',
            content => "ssldir = ${ssl_dir}\n",
            notify  => $service_reload_notify,
            require => Systemd::Unmask['puppetserver.service'],
        }
    }
    if $ca_name {
        concat::fragment { 'ca-name':
            target  => '/etc/puppet/puppet.conf',
            order   => '25',
            content => "ca_name = ${ca_name}\n",
            notify  => $service_reload_notify,
            require => Systemd::Unmask['puppetserver.service'],
        }
    }
    if $strict_mode {
        $strict_mode_config = @("STRICT_MODE_CONFIG")
        # Puppet 8 strict mode defaults for Puppet 7, these
        # settings may be removed when we have upgraded to
        # Puppet 8
        strict_variables = true
        strict = error
        | STRICT_MODE_CONFIG
        concat::fragment { 'server-strict-mode':
            target  => '/etc/puppet/puppet.conf',
            order   => '26',
            content => $strict_mode_config,
            notify  => $service_reload_notify,
            require => Systemd::Unmask['puppetserver.service'],
        }
    }

    # TODO: puppetserver has support for graphite and jmx (with jolokia)
    # we will need to work out which is best
    $metrics_params = { 'server_id'                    => $server_id }
    $puppetserver_params = {
        'ruby_load_path'       => $ruby_load_path,
        'config_dir'           => $config_dir,
        'code_dir'             => $code_dir,
        'max_active_instances' => $max_active_instances,
    }
    $jmx_config = "${puppetserver_config_dir}/jmx_exporter.yaml"
    $environment_file_params = {
        'java_start_mem'      => $java_start_mem,
        'java_max_mem'        => $java_max_mem,
        'config_d_dir'        => $config_d_dir,
        'bootstap_config_dir' => $bootstap_config_dir,
        'enable_jmx'          => $enable_jmx,
        'jmx_port'            => $jmx_port,
        'jmx_config'          => $jmx_config,
    }
    $hiera_config = {
        'hierarchy' => $hierarchy,
        'version'   => 5,
        'defaults'  => {
            'datadir'   => $hiera_data_dir,
            'data_hash' => 'yaml_data',
        },
    }
    $web_server_params = {
        'listen_host' => $listen_host,
    }
    $auth_params = {
        'fqdn' => $facts['networking']['fqdn'],
    }
    $ca_params = {
        'allow_san' => $ca_allow_san,
    }
    # Ensure additional mounts exist
    unless $extra_mounts.empty {
        wmflib::dir::mkdir_p($extra_mounts.values(), {mode => '0555'})
    }
    $fileserver_content = $extra_mounts.reduce("# Managed by puppet\n") |$memo, $value| {
        $tmp = @("CONTENT")
        [${value[0]}]
            path ${value[1]}
        | CONTENT
        # Note If we add memo to the heredoc above we get the following error
        #  Syntax error at '['
        # Have not been able to recreate in a simple repro i.e. the following works
        # https://phabricator.wikimedia.org/P50573
        "${memo}${tmp}"
    }
    file {
        default:
            ensure  => stdlib::ensure($ensure, 'file'),
            require => Systemd::Unmask['puppetserver.service'],
            notify  => $service_reload_notify;
        "${config_d_dir}/metrics.conf":
            content => epp('puppetserver/metrics.conf.epp', $metrics_params);
        "${config_d_dir}/puppetserver.conf":
            content => epp('puppetserver/puppetserver.conf.epp', $puppetserver_params);
        # TODO: do we need to manage this?  possibly disable/change admin api end point
        "${config_d_dir}/web-routes.conf":
            content => epp('puppetserver/web-routes.conf.epp');
        "${config_d_dir}/webserver.conf":
            content => epp('puppetserver/webserver.conf.epp', $web_server_params);
        "${config_d_dir}/auth.conf":
            content => epp('puppetserver/auth.conf.epp', $auth_params);
        "${config_d_dir}/ca.conf":
            content => epp('puppetserver/ca.conf.epp', $ca_params);
        "${config_d_dir}/global.conf":
            source =>  'puppet:///modules/puppetserver/global.conf';
        '/etc/puppet/hiera.yaml':
            content => $hiera_config.to_yaml;
        '/etc/puppet/fileserver.conf':
            content => $fileserver_content;
        '/etc/default/puppetserver':
            content => epp('puppetserver/environment_file.epp', $environment_file_params),
            notify  => $service_restart_notify;
    }
    include puppetserver::puppetdb
    if $enable_jmx {
        ensure_packages(['prometheus-jmx-exporter'])
        file { $jmx_config:
            ensure  => file,
            content => epp('puppetserver/jmx_exporter.yaml.epp'),
        }
        $service_require = [File[$ssl_dir], Package['prometheus-jmx-exporter']]
        # This is quite specific to the WMF systems it might be better to move this to some profile
        prometheus::jmx_exporter_instance { 'puppetserver':
            hostname => $facts['networking']['hostname'],
            port     => $jmx_port,
        }
    } else {
        $service_require = File[$ssl_dir]
    }
    service { 'puppetserver':
        ensure  => stdlib::ensure($ensure, 'service'),
        enable  => true,
        require => $service_require,
    }
    exec { 'reload puppetserver':
        command     => '/usr/bin/systemctl reload puppetserver',
        refreshonly => true,
    }
    # Add a file indicating that a restart or reload is required this file exists for other tooling to consume
    exec { 'restart puppetserver required':
        path        => '/usr/bin',
        command     => 'printf "Restart required from %s\n" "$(date)" >> /run/puppetserver/restart_required',
        refreshonly => true,
    }

    file { '/usr/local/bin/puppetserver-deploy-code':
        ensure => file,
        owner  => 'root',
        group  => 'root',
        source => 'puppet:///modules/puppetserver/puppetserver-deploy-code.sh',
        mode   => '0555',
    }
}