Skip to content

Commit

Permalink
Merge pull request #420 from traylenator/slice
Browse files Browse the repository at this point in the history
Support `[Slice]` in manage_unit and manage_dropin
  • Loading branch information
traylenator authored Mar 5, 2024
2 parents d7a0907 + 12d3822 commit 7b7d722
Show file tree
Hide file tree
Showing 14 changed files with 319 additions and 2 deletions.
106 changes: 106 additions & 0 deletions REFERENCE.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,14 @@
* [`Systemd::OomdSettings`](#Systemd--OomdSettings): Configurations for oomd.conf
* [`Systemd::ServiceLimits`](#Systemd--ServiceLimits): Matches Systemd Service Limit Struct
* [`Systemd::Unit`](#Systemd--Unit): custom datatype that validates different filenames for systemd units and unit templates
* [`Systemd::Unit::Amount`](#Systemd--Unit--Amount): Systemd definition of amount, often bytes or united bytes
* [`Systemd::Unit::AmountOrPercent`](#Systemd--Unit--AmountOrPercent): Systemd definition of amount, often bytes or united bytes
* [`Systemd::Unit::Install`](#Systemd--Unit--Install): Possible keys for the [Install] section of a unit file
* [`Systemd::Unit::Path`](#Systemd--Unit--Path): Possible keys for the [Path] section of a unit file
* [`Systemd::Unit::Percent`](#Systemd--Unit--Percent): Systemd definition of a percentage
* [`Systemd::Unit::Service`](#Systemd--Unit--Service): Possible keys for the [Service] section of a unit file
* [`Systemd::Unit::Service::Exec`](#Systemd--Unit--Service--Exec): Possible strings for ExecStart, ExecStartPrep, ...
* [`Systemd::Unit::Slice`](#Systemd--Unit--Slice): Possible keys for the [Slice] section of a unit file
* [`Systemd::Unit::Socket`](#Systemd--Unit--Socket): Possible keys for the [Socket] section of a unit file
* [`Systemd::Unit::Timer`](#Systemd--Unit--Timer): Possible keys for the [Timer] section of a unit file
* [`Systemd::Unit::Timespan`](#Systemd--Unit--Timespan): Timer specification for systemd time spans, e.g. timers.
Expand Down Expand Up @@ -851,6 +855,18 @@ systemd::manage_dropin { 'user-aklog.conf':
}
```

##### set memory limits on the user slices

```puppet
systemd::manage_dropin { 'userlimits.conf':
unit => 'user-.slice',
slice_entry => {
MemoryMax => '10G',
MemoryAccounting => true,
}
}
```

#### Parameters

The following parameters are available in the `systemd::manage_dropin` defined type:
Expand All @@ -867,6 +883,7 @@ The following parameters are available in the `systemd::manage_dropin` defined t
* [`notify_service`](#-systemd--manage_dropin--notify_service)
* [`daemon_reload`](#-systemd--manage_dropin--daemon_reload)
* [`unit_entry`](#-systemd--manage_dropin--unit_entry)
* [`slice_entry`](#-systemd--manage_dropin--slice_entry)
* [`service_entry`](#-systemd--manage_dropin--service_entry)
* [`install_entry`](#-systemd--manage_dropin--install_entry)
* [`timer_entry`](#-systemd--manage_dropin--timer_entry)
Expand Down Expand Up @@ -967,6 +984,14 @@ key value pairs for [Unit] section of the unit file

Default value: `undef`

##### <a name="-systemd--manage_dropin--slice_entry"></a>`slice_entry`

Data type: `Optional[Systemd::Unit::Slice]`

key value pairs for [Slice] section of the unit file

Default value: `undef`

##### <a name="-systemd--manage_dropin--service_entry"></a>`service_entry`

Data type: `Optional[Systemd::Unit::Service]`
Expand Down Expand Up @@ -1110,6 +1135,7 @@ The following parameters are available in the `systemd::manage_unit` defined typ
* [`service_parameters`](#-systemd--manage_unit--service_parameters)
* [`daemon_reload`](#-systemd--manage_unit--daemon_reload)
* [`unit_entry`](#-systemd--manage_unit--unit_entry)
* [`slice_entry`](#-systemd--manage_unit--slice_entry)
* [`service_entry`](#-systemd--manage_unit--service_entry)
* [`install_entry`](#-systemd--manage_unit--install_entry)
* [`timer_entry`](#-systemd--manage_unit--timer_entry)
Expand Down Expand Up @@ -1226,6 +1252,14 @@ key value pairs for [Unit] section of the unit file.

Default value: `undef`

##### <a name="-systemd--manage_unit--slice_entry"></a>`slice_entry`

Data type: `Optional[Systemd::Unit::Slice]`

key value pairs for [Slice] section of the unit file

Default value: `undef`

##### <a name="-systemd--manage_unit--service_entry"></a>`service_entry`

Data type: `Optional[Systemd::Unit::Service]`
Expand Down Expand Up @@ -2465,6 +2499,26 @@ custom datatype that validates different filenames for systemd units and unit te

Alias of `Pattern[/^[a-zA-Z0-9:\-_.\\@%]+\.(service|socket|device|mount|automount|swap|target|path|timer|slice|scope)$/]`

### <a name="Systemd--Unit--Amount"></a>`Systemd::Unit::Amount`

Systemd definition of amount, often bytes or united bytes

* **See also**
* https://www.freedesktop.org/software/systemd/man/systemd.service.html
* https://www.freedesktop.org/software/systemd/man/systemd.slice.html

Alias of `Variant[Integer[0], Pattern['\A(infinity|\d+(K|M|G|T)?(:\d+(K|M|G|T)?)?)\z']]`

### <a name="Systemd--Unit--AmountOrPercent"></a>`Systemd::Unit::AmountOrPercent`

Systemd definition of amount, often bytes or united bytes

* **See also**
* https://www.freedesktop.org/software/systemd/man/systemd.service.html
* https://www.freedesktop.org/software/systemd/man/systemd.slice.html

Alias of `Variant[Systemd::Unit::Amount, Systemd::Unit::Percent]`

### <a name="Systemd--Unit--Install"></a>`Systemd::Unit::Install`

Possible keys for the [Install] section of a unit file
Expand Down Expand Up @@ -2507,6 +2561,16 @@ Struct[{
}]
```

### <a name="Systemd--Unit--Percent"></a>`Systemd::Unit::Percent`

Systemd definition of a percentage

* **See also**
* https://www.freedesktop.org/software/systemd/man/systemd.service.html
* https://www.freedesktop.org/software/systemd/man/systemd.slice.html

Alias of `Pattern['\A([0-9][0-9]?|100)%\z']`

### <a name="Systemd--Unit--Service"></a>`Systemd::Unit::Service`

Possible keys for the [Service] section of a unit file
Expand Down Expand Up @@ -2657,6 +2721,48 @@ Possible strings for ExecStart, ExecStartPrep, ...

Alias of `Variant[Enum[''], Pattern[/^[@\-:]*(\+|!|!!)?[@\-:]*\/.*/], Pattern[/^[@\-:]*(\+|!|!!)?[@\-:]*[^\/]*(\s.*)?$/]]`

### <a name="Systemd--Unit--Slice"></a>`Systemd::Unit::Slice`

Possible keys for the [Slice] section of a unit file

* **See also**
* https://www.freedesktop.org/software/systemd/man/systemd.slice.html
* https://www.freedesktop.org/software/systemd/man/systemd.resource-control.html

Alias of

```puppet
Struct[{
Optional['CPUAccounting'] => Boolean,
Optional['CPUQuota'] => Pattern['^([1-9][0-9]*)%$'],
Optional['CPUShares'] => Integer[2,262144],
Optional['CPUWeight'] => Variant[Enum['idle'],Integer[1,10000]],
Optional['Delegate'] => Boolean,
Optional['DeviceAllow'] => Pattern['^(/dev/)|(char-)|(block-).*$'],
Optional['DevicePolicy'] => Enum['auto','closed','strict'],
Optional['IOAccounting'] => Boolean,
Optional['IODeviceWeight'] => Array[Hash[Stdlib::Absolutepath, Integer[1,10000], 1, 1]],
Optional['IOReadBandwidthMax'] => Array[Hash[Stdlib::Absolutepath, Systemd::Unit::Amount], 1, 1],
Optional['IOReadIOPSMax'] => Array[Hash[Stdlib::Absolutepath, Systemd::Unit::Amount], 1, 1],
Optional['IOWeight'] => Integer[1,10000],
Optional['IOWriteBandwidthMax'] => Array[Hash[Stdlib::Absolutepath, Systemd::Unit::Amount], 1, 1],
Optional['IOWriteIOPSMax'] => Array[Hash[Stdlib::Absolutepath, Systemd::Unit::Amount], 1, 1],
Optional['IPAccounting'] => Boolean,
Optional['MemoryAccounting'] => Boolean,
Optional['MemoryHigh'] => Systemd::Unit::AmountOrPercent,
Optional['MemoryLimit'] => Systemd::Unit::AmountOrPercent, # depprecated in systemd
Optional['MemoryLow'] => Systemd::Unit::AmountOrPercent,
Optional['MemoryMax'] => Systemd::Unit::AmountOrPercent,
Optional['MemoryMin'] => Systemd::Unit::AmountOrPercent,
Optional['MemorySwapMax'] => Systemd::Unit::AmountOrPercent,
Optional['Slice'] => String[1],
Optional['StartupCPUShares'] => Integer[2,262144],
Optional['StartupIOWeight'] => Integer[1,10000],
Optional['TasksAccounting'] => Boolean,
Optional['TasksMax'] => Systemd::Unit::AmountOrPercent,
}]
```

### <a name="Systemd--Unit--Socket"></a>`Systemd::Unit::Socket`

Possible keys for the [Socket] section of a unit file
Expand Down
16 changes: 16 additions & 0 deletions manifests/manage_dropin.pp
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,15 @@
# }
# }
#
# @example set memory limits on the user slices
# systemd::manage_dropin { 'userlimits.conf':
# unit => 'user-.slice',
# slice_entry => {
# MemoryMax => '10G',
# MemoryAccounting => true,
# }
# }
#
# @param unit The unit to create a dropfile for
# @param filename The target unit file to create. The filename of the drop in. The full path is determined using the path, unit and this filename.
# @param ensure The state of this dropin file
Expand All @@ -62,6 +71,7 @@
# @param notify_service Notify a service for the unit, if it exists
# @param daemon_reload Call systemd::daemon_reload
# @param unit_entry key value pairs for [Unit] section of the unit file
# @param slice_entry key value pairs for [Slice] section of the unit file
# @param service_entry key value pairs for [Service] section of the unit file
# @param install_entry key value pairs for [Install] section of the unit file
# @param timer_entry key value pairs for [Timer] section of the unit file
Expand All @@ -82,6 +92,7 @@
Boolean $daemon_reload = true,
Optional[Systemd::Unit::Install] $install_entry = undef,
Optional[Systemd::Unit::Unit] $unit_entry = undef,
Optional[Systemd::Unit::Slice] $slice_entry = undef,
Optional[Systemd::Unit::Service] $service_entry = undef,
Optional[Systemd::Unit::Timer] $timer_entry = undef,
Optional[Systemd::Unit::Path] $path_entry = undef,
Expand All @@ -99,6 +110,10 @@
fail("Systemd::Manage_dropin[${name}]: for unit ${unit} socket_entry is only valid for socket units")
}

if $slice_entry and $unit !~ Pattern['^[^/]+\.slice'] {
fail("Systemd::Manage_dropin[${name}]: for unit ${unit} slice_entry is only valid for slice units")
}

systemd::dropin_file { $name:
ensure => $ensure,
filename => $filename,
Expand All @@ -113,6 +128,7 @@
daemon_reload => $daemon_reload,
content => epp('systemd/unit_file.epp', {
'unit_entry' => $unit_entry,
'slice_entry' => $slice_entry,
'service_entry' => $service_entry,
'install_entry' => $install_entry,
'timer_entry' => $timer_entry,
Expand Down
7 changes: 7 additions & 0 deletions manifests/manage_unit.pp
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@
# call `systemd::daemon-reload` to ensure that the modified unit file is loaded
#
# @param unit_entry key value pairs for [Unit] section of the unit file.
# @param slice_entry key value pairs for [Slice] section of the unit file
# @param service_entry key value pairs for [Service] section of the unit file.
# @param install_entry key value pairs for [Install] section of the unit file.
# @param timer_entry key value pairs for [Timer] section of the unit file
Expand All @@ -109,6 +110,7 @@
Boolean $daemon_reload = true,
Optional[Systemd::Unit::Install] $install_entry = undef,
Optional[Systemd::Unit::Unit] $unit_entry = undef,
Optional[Systemd::Unit::Slice] $slice_entry = undef,
Optional[Systemd::Unit::Service] $service_entry = undef,
Optional[Systemd::Unit::Timer] $timer_entry = undef,
Optional[Systemd::Unit::Path] $path_entry = undef,
Expand All @@ -128,6 +130,10 @@
fail("Systemd::Manage_unit[${name}]: socket_entry is only valid for socket units")
}

if $slice_entry and $name !~ Pattern['^[^/]+\.slice'] {
fail("Systemd::Manage_unit[${name}]: slice_entry is only valid for slice units")
}

if $ensure != 'absent' and $name =~ Pattern['^[^/]+\.service'] and !$service_entry {
fail("Systemd::Manage_unit[${name}]: service_entry is required for service units")
}
Expand All @@ -146,6 +152,7 @@
daemon_reload => $daemon_reload,
content => epp('systemd/unit_file.epp', {
'unit_entry' => $unit_entry,
'slice_entry' => $slice_entry,
'service_entry' => $service_entry,
'install_entry' => $install_entry,
'timer_entry' => $timer_entry,
Expand Down
36 changes: 35 additions & 1 deletion spec/defines/manage_dropin_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@
is_expected.to contain_systemd__dropin_file('foobar.conf').
with_content(%r{^LimitCORE=infinity$}).
with_content(%r{^DefaultDependencies=true$}).
with_content(%r{^SyslogIdentifier=simple$})
with_content(%r{^SyslogIdentifier=simple$}).
without_content(%r{^\[Slice\]$})
}
end

Expand Down Expand Up @@ -92,6 +93,18 @@

it { is_expected.to compile.and_raise_error(%r{timer_entry is only valid for timer units}) }
end

context 'with a slice entry' do
let(:params) do
super().merge(
slice_entry: {
'MemoryMax' => '100G',
}
)
end

it { is_expected.to compile.and_raise_error(%r{slice_entry is only valid for slice units}) }
end
end

context 'on a timer' do
Expand All @@ -113,6 +126,27 @@
}
end

context 'on a slice' do
let(:params) do
{
unit: 'user-.slice',
slice_entry: {
'MemoryMax' => '10G',
'MemoryAccounting' => true,
}
}
end

it { is_expected.to compile.with_all_deps }

it {
is_expected.to contain_systemd__dropin_file('foobar.conf').
with_unit('user-.slice').
with_content(%r{^MemoryMax=10G$}).
with_content(%r{^MemoryAccounting=true$})
}
end

context 'on a path unit' do
let(:params) do
{
Expand Down
35 changes: 34 additions & 1 deletion spec/defines/manage_unit_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@
with_content(%r{^\[Install\]$}).
with_content(%r{^Description=My great service$}).
with_content(%r{^Description=has two lines of description$}).
with_content(%r{^Type=oneshot$})
with_content(%r{^Type=oneshot$}).
without_content(%r{^\[Slice\]$})
}

context 'with no service_entry' do
Expand Down Expand Up @@ -71,6 +72,14 @@
it { is_expected.to compile.and_raise_error(%r{timer_entry is only valid for timer units}) }
end

context 'with a slice entry' do
let(:params) do
super().merge(slice_entry: { 'IOWeight' => 100 })
end

it { is_expected.to compile.and_raise_error(%r{slice_entry is only valid for slice units}) }
end

context 'with a path entry' do
let(:params) do
super().merge(path_entry: { 'PathExists' => '/etc/passwd' })
Expand Down Expand Up @@ -161,6 +170,30 @@
}
end

context 'on a slice unit' do
let(:title) { 'myslice.slice' }
let(:params) do
{
unit_entry: {
Description: 'A crazy slice',
},
slice_entry: {
'MemoryMax' => '10G',
'IOAccounting' => true,
}
}
end

it { is_expected.to compile.with_all_deps }

it {
is_expected.to contain_systemd__unit_file('myslice.slice').
with_content(%r{^\[Slice\]$}).
with_content(%r{^MemoryMax=10G$}).
with_content(%r{^IOAccounting=true$})
}
end

context 'on a path unit' do
let(:title) { 'etc-passwd.path' }

Expand Down
14 changes: 14 additions & 0 deletions spec/type_aliases/systemd_unit_amount_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# frozen_string_literal: true

require 'spec_helper'

describe 'Systemd::Unit::Amount' do
it { is_expected.to allow_value(100) }
it { is_expected.to allow_value('200') }
it { is_expected.to allow_value('8G') }
it { is_expected.to allow_value('1T') }
it { is_expected.to allow_value('infinity') }

it { is_expected.not_to allow_value('1P') }
it { is_expected.not_to allow_value('10%') }
end
Loading

0 comments on commit 7b7d722

Please sign in to comment.