Skip to content

Commit

Permalink
Merge pull request #490 from traylenator/mount
Browse files Browse the repository at this point in the history
Support Mount units for manage_unit or dropin types
  • Loading branch information
traylenator authored Sep 13, 2024
2 parents 259c26b + df38742 commit 7ecb058
Show file tree
Hide file tree
Showing 8 changed files with 210 additions and 0 deletions.
67 changes: 67 additions & 0 deletions REFERENCE.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@
* [`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::Mount`](#Systemd--Unit--Mount): Possible keys for the [Mount] 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
Expand Down Expand Up @@ -1031,6 +1032,7 @@ The following parameters are available in the `systemd::manage_dropin` defined t
* [`timer_entry`](#-systemd--manage_dropin--timer_entry)
* [`path_entry`](#-systemd--manage_dropin--path_entry)
* [`socket_entry`](#-systemd--manage_dropin--socket_entry)
* [`mount_entry`](#-systemd--manage_dropin--mount_entry)

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

Expand Down Expand Up @@ -1174,6 +1176,14 @@ key value pairs for the [Socket] section of the unit file

Default value: `undef`

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

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

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

Default value: `undef`

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

Generate unit file from template
Expand Down Expand Up @@ -1251,6 +1261,30 @@ systemd::manage_unit{'[email protected]':
}
```

##### Mount a Filesystem and Use for a Service

```puppet
systemd::manage_unit { 'var-lib-sss-db.mount':
ensure => present,
unit_entry => {
'Description' => 'Mount sss tmpfs db',
},
mount_entry => {
'What' => 'tmpfs',
'Where' => '/var/lib/sss/db',
'Type' => 'tmpfs',
'Options' => 'size=300M,mode=0700,uid=sssd,gid=sssd,rootcontext=system_u:object_r:sssd_var_lib_t:s0',
},
}
systemd::manage_dropin { 'tmpfs-db.conf':
ensure => present,
unit => 'sssd.service',
unit_entry => {
'RequiresMountsFor' => '/var/lib/sss/db',
},
}
```

##### Remove a unit file

```puppet
Expand Down Expand Up @@ -1284,6 +1318,7 @@ The following parameters are available in the `systemd::manage_unit` defined typ
* [`timer_entry`](#-systemd--manage_unit--timer_entry)
* [`path_entry`](#-systemd--manage_unit--path_entry)
* [`socket_entry`](#-systemd--manage_unit--socket_entry)
* [`mount_entry`](#-systemd--manage_unit--mount_entry)

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

Expand Down Expand Up @@ -1451,6 +1486,14 @@ kev value paors for [Socket] section of the unit file.

Default value: `undef`

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

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

kev value pairs for [Mount] section of the unit file.

Default value: `undef`

### <a name="systemd--modules_load"></a>`systemd::modules_load`

Creates a modules-load.d drop file
Expand Down Expand Up @@ -2798,6 +2841,30 @@ Struct[{
}]
```

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

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

* **See also**
* https://www.freedesktop.org/software/systemd/man/latest/systemd.mount.html

Alias of

```puppet
Struct[{
Optional['What'] => String[1],
Optional['Where'] => Stdlib::Unixpath,
Optional['Type'] => String[1],
Optional['Options'] => String[1],
Optional['SloppyOptions'] => Boolean,
Optional['LazyUnmount'] => Boolean,
Optional['ReadWriteOnly'] => Boolean,
Optional['ForceUnmount'] => Boolean,
Optional['DirectoryMode'] => Stdlib::Filemode,
Optional['TimeoutSec'] => String[0],
}]
```

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

Possible keys for the [Path] section of a unit file
Expand Down
7 changes: 7 additions & 0 deletions manifests/manage_dropin.pp
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@
# @param timer_entry key value pairs for [Timer] section of the unit file
# @param path_entry key value pairs for [Path] section of the unit file
# @param socket_entry key value pairs for the [Socket] section of the unit file
# @param mount_entry key value pairs for the [Mount] section of the unit file
#
define systemd::manage_dropin (
Systemd::Unit $unit,
Expand All @@ -108,6 +109,7 @@
Optional[Systemd::Unit::Timer] $timer_entry = undef,
Optional[Systemd::Unit::Path] $path_entry = undef,
Optional[Systemd::Unit::Socket] $socket_entry = undef,
Optional[Systemd::Unit::Mount] $mount_entry = undef,
) {
if $timer_entry and $unit !~ Pattern['^[^/]+\.timer'] {
fail("Systemd::Manage_dropin[${name}]: for unit ${unit} timer_entry is only valid for timer units")
Expand All @@ -125,6 +127,10 @@
fail("Systemd::Manage_dropin[${name}]: for unit ${unit} slice_entry is only valid for slice units")
}

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

systemd::dropin_file { $name:
ensure => $ensure,
filename => $filename,
Expand All @@ -145,6 +151,7 @@
'timer_entry' => $timer_entry,
'path_entry' => $path_entry,
'socket_entry' => $socket_entry,
'mount_entry' => $mount_entry,
}),
}
}
28 changes: 28 additions & 0 deletions manifests/manage_unit.pp
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,27 @@
# },
# }
#
# @example Mount a Filesystem and Use for a Service
# systemd::manage_unit { 'var-lib-sss-db.mount':
# ensure => present,
# unit_entry => {
# 'Description' => 'Mount sss tmpfs db',
# },
# mount_entry => {
# 'What' => 'tmpfs',
# 'Where' => '/var/lib/sss/db',
# 'Type' => 'tmpfs',
# 'Options' => 'size=300M,mode=0700,uid=sssd,gid=sssd,rootcontext=system_u:object_r:sssd_var_lib_t:s0',
# },
# }
# systemd::manage_dropin { 'tmpfs-db.conf':
# ensure => present,
# unit => 'sssd.service',
# unit_entry => {
# 'RequiresMountsFor' => '/var/lib/sss/db',
# },
# }
#
# @example Remove a unit file
# systemd::manage_unit { 'my.service':
# ensure => 'absent',
Expand Down Expand Up @@ -96,6 +117,7 @@
# @param timer_entry key value pairs for [Timer] section of the unit file
# @param path_entry key value pairs for [Path] section of the unit file.
# @param socket_entry kev value paors for [Socket] section of the unit file.
# @param mount_entry kev value pairs for [Mount] section of the unit file.
#
define systemd::manage_unit (
Enum['present', 'absent'] $ensure = 'present',
Expand All @@ -118,6 +140,7 @@
Optional[Systemd::Unit::Timer] $timer_entry = undef,
Optional[Systemd::Unit::Path] $path_entry = undef,
Optional[Systemd::Unit::Socket] $socket_entry = undef,
Optional[Systemd::Unit::Mount] $mount_entry = undef,
) {
assert_type(Systemd::Unit, $name)

Expand All @@ -137,6 +160,10 @@
fail("Systemd::Manage_unit[${name}]: slice_entry is only valid for slice units")
}

if $mount_entry and $name !~ Pattern['^[^/]+\.mount'] {
fail("Systemd::Manage_unit[${name}]: mount_entry is only valid for mount 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 @@ -162,6 +189,7 @@
'timer_entry' => $timer_entry,
'path_entry' => $path_entry,
'socket_entry' => $socket_entry,
'mount_entry' => $mount_entry,
}),
}
}
19 changes: 19 additions & 0 deletions spec/defines/manage_dropin_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,25 @@
}
end

context 'on a mount' do
let(:params) do
{
unit: 'var-lib-sss-db.mount',
mount_entry: {
'SloppyOptions' => true,
}
}
end

it { is_expected.to compile.with_all_deps }

it {
is_expected.to contain_systemd__dropin_file('foobar.conf').
with_unit('var-lib-sss-db.mount').
with_content(%r{^SloppyOptions=true$})
}
end

context 'on a slice' do
let(:params) do
{
Expand Down
28 changes: 28 additions & 0 deletions spec/defines/manage_unit_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,34 @@
end
end

context 'on a mount' do
let(:title) { 'var-lib-sss-db.mount' }

let(:params) do
{
unit_entry: {
Description: 'Mount sssd dir',
},
mount_entry: {
'What' => 'tmpfs',
'Where' => '/var/lib/sss/db',
'Type' => 'tmpfs',
'Options' => 'size=300M',
},
}
end

it { is_expected.to compile.with_all_deps }

it {
is_expected.to contain_systemd__unit_file('var-lib-sss-db.mount').
with_content(%r{^\[Mount\]$}).
with_content(%r{^What=tmpfs$}).
with_content(%r{^Where=/var/lib/sss/db$}).
with_content(%r{^Options=size=300M$})
}
end

context 'on a timer' do
let(:title) { 'winter.timer' }

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

require 'spec_helper'

describe 'Systemd::Unit::Mount' do
context 'with a key of What can have thing to mount' do
it { is_expected.to allow_value({ 'What' => 'tmpfs' }) }
it { is_expected.to allow_value({ 'What' => 'nfs://example.org/exports/home' }) }
it { is_expected.to allow_value({ 'What' => '/dev/vda1' }) }
end

context 'with a key of Where can have a path to mount on' do
it { is_expected.to allow_value({ 'Where' => '/mnt/foo' }) }
it { is_expected.to allow_value({ 'Where' => '/mnt/foo/file.txt' }) }
end

context 'with a key of Type can have a path to mount on' do
it { is_expected.to allow_value({ 'Type' => 'tmpfs' }) }
it { is_expected.to allow_value({ 'Type' => 'ext2' }) }
end

context 'with a key of Options can have a path to mount on' do
it { is_expected.to allow_value({ 'Options' => 'size=300M,mode=0700,uid=sssd,gid=sssd,root' }) }
end

context 'with a key of DirectoryMode can have a mode of' do
it { is_expected.to allow_value({ 'DirectoryMode' => '0700' }) }
end

context 'with a key of TimeoutSec can have a mode of' do
it { is_expected.to allow_value({ 'TimeoutSec' => '100' }) }
it { is_expected.to allow_value({ 'TimeoutSec' => '5min 20s' }) }
it { is_expected.to allow_value({ 'TimeoutSec' => '' }) }
end

%w[SloppyOptions LazyUnmount ReadWriteOnly ForceUnmount].each do |assert|
context "with a key of #{assert} can have values of a path" do
it { is_expected.to allow_value({ assert => false }) }
it { is_expected.to allow_value({ assert => true }) }
end
end
end
2 changes: 2 additions & 0 deletions templates/unit_file.epp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
Optional[Hash] $timer_entry,
Optional[Hash] $path_entry,
Optional[Hash] $socket_entry,
Optional[Hash] $mount_entry,
| -%>
<%-

Expand All @@ -20,6 +21,7 @@
'Path',
'Socket',
'Install',
'Mount',
]

# Directives which are pair of items to be expressed as a space seperated pair.
Expand Down
17 changes: 17 additions & 0 deletions types/unit/mount.pp
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# @summary Possible keys for the [Mount] section of a unit file
# @see https://www.freedesktop.org/software/systemd/man/latest/systemd.mount.html
#
type Systemd::Unit::Mount = Struct[
{
Optional['What'] => String[1],
Optional['Where'] => Stdlib::Unixpath,
Optional['Type'] => String[1],
Optional['Options'] => String[1],
Optional['SloppyOptions'] => Boolean,
Optional['LazyUnmount'] => Boolean,
Optional['ReadWriteOnly'] => Boolean,
Optional['ForceUnmount'] => Boolean,
Optional['DirectoryMode'] => Stdlib::Filemode,
Optional['TimeoutSec'] => String[0],
}
]

0 comments on commit 7ecb058

Please sign in to comment.