Defined Type: systemd::unit

Defined in:
modules/systemd/manifests/unit.pp

Summary

This define creates a file on the filesystem at $path, schedules a daemon-reload of systemd and, if requested, schedules a subsequent refresh of the service.

Overview

systemd::unit { 'hhvm':

ensure   => present,
content  => template('hhvm/initscripts/hhvm.systemd.erb'),
restart  => false,
override => true,

}

# A socket for nginx systemd::unit { 'nginx.socket':

ensure   => present,
content  => template('nginx/nginx.socket.erb'),
restart  => true, # This will work only if you have service{ `nginx.socket`: }

}

Examples:

A systemd override for the hhvm.service unit

Parameters:

  • title

    The resource title is assumed to be the corresponding full unit name. If no valid unit suffix is present, 'service' will be assumed.

  • content (Optional[String]) (defaults to: undef)

    The content of the file. Required.

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

    The usual meta-parameter, defaults to present.

  • unit (String) (defaults to: $title)

    The name of the unit by default use the title

  • restart (Boolean) (defaults to: false)

    Whether to handle restarting the service when the file changes.

  • override (Boolean) (defaults to: false)

    If the are creating an override to system-provided units or not.

  • override_filename (String[1]) (defaults to: 'puppet-override.conf')

    When creating an override, filename to use for the override. The given filename would have the `.conf` extension added if missing. Defaults to undef (use `puppet-override.conf`)

  • team (Optional[Wmflib::Team]) (defaults to: undef)

    The team which owns this service

  • source (Optional[Stdlib::Filesource]) (defaults to: undef)


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
# File 'modules/systemd/manifests/unit.pp', line 32

define systemd::unit (
    Optional[String]             $content           = undef,
    Optional[Stdlib::Filesource] $source            = undef,
    Wmflib::Ensure               $ensure            = present,
    String                       $unit              = $title,
    Boolean                      $restart           = false,
    Boolean                      $override          = false,
    String[1]                    $override_filename = 'puppet-override.conf',
    Optional[Wmflib::Team]       $team              = undef,
) {
    require systemd

    if ($source == undef) == ($content == undef) {
        fail("systemd::unit: ${title}: either source or content must be provided, but not both")
    }

    if ($unit =~ /^(.+)\.(\w+)$/ and $2 =~ Systemd::Unit::Type) {
        $unit_name = $unit
    } else {
        $unit_name = "${unit}.service"
    }

    if ($override) {
        # Define the override dir if not defined.
        $override_dir = "${systemd::override_dir}/${unit_name}.d"
        if $ensure == 'present' {
            # Only manage this directory on creation.  This means that we
            # may end up with some empty directories but that shuoldn't mater
            # TODO: Add recurs/purge => true
            ensure_resource('file', $override_dir, {
                ensure => directory,
                owner  => 'root',
                group  => 'root',
                mode   => '0555',
            })
        }

        $path = $override_filename ? {
            /\.conf$/ => "${override_dir}/${override_filename}",
            default   => "${override_dir}/${override_filename}.conf",
        }
    } else {
        $path = "${systemd::base_dir}/${unit_name}"
    }

    $exec_label = "systemd daemon-reload for ${unit_name} (${title})"
    file { $path:
        ensure  => $ensure,
        source  => $source,
        content => $content,
        mode    => '0444',
        owner   => 'root',
        group   => 'root',
        notify  => Exec[$exec_label],
    }

    exec { $exec_label:
        refreshonly => true,
        command     => '/bin/systemctl daemon-reload',
    }

    # If the unit  is defined as a service, add a dependency.

    if defined(Service[$unit]) {
        if $ensure == 'present' {
            # systemd must reload units before the service is managed
            if $restart {
                # Refresh the service if restarts are required
                Exec[$exec_label] ~> Service[$unit]
            } else {
                Exec[$exec_label] -> Service[$unit]
            }
        } else {
            # the service should be managed before the daemon-reload
            Service[$unit] -> Exec[$exec_label]
        }
    }
    $drop_in_file = "/var/lib/prometheus/node.d/systemd_unit_${unit_name}_owner.prom"
    if $team {
        $_team = $team.regsubst('\W', '-', 'G').downcase
        $unit_team_metric = @("METRIC")
        # HELP systemd_unit_owner The team owner of the systemd unit
        # TYPE systemd_unit_owner gauge
        systemd_unit_owner{team="${_team}", name="${unit_name}"} 1.0
        | METRIC
        file { $drop_in_file:
            ensure  => stdlib::ensure($ensure, 'file'),
            content => $unit_team_metric,
        }
    }

}