Skip to content

Commit

Permalink
Add the option to create the exported directory automatically
Browse files Browse the repository at this point in the history
  • Loading branch information
bschonec committed Jan 14, 2025
1 parent a77b94a commit 1ca9169
Show file tree
Hide file tree
Showing 5 changed files with 89 additions and 25 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -153,10 +153,12 @@ This will mount /data on client in /share/data.
}
# ensure is passed to mount, which will make the client not mount it
# the directory automatically, just add it to fstab
# The directory on the NFS server is not created automatically.
nfs::server::export { '/media_library':
ensure => 'present',
nfstag => 'media',
clients => '10.0.0.0/24(rw,insecure,async,no_root_squash) localhost(rw)',
create_dir => false,
}
}
Expand Down
39 changes: 33 additions & 6 deletions REFERENCE.md
Original file line number Diff line number Diff line change
Expand Up @@ -1012,6 +1012,7 @@ The following parameters are available in the `nfs::functions::create_export` de
* [`owner`](#-nfs--functions--create_export--owner)
* [`group`](#-nfs--functions--create_export--group)
* [`mode`](#-nfs--functions--create_export--mode)
* [`create_dir`](#-nfs--functions--create_export--create_dir)

##### <a name="-nfs--functions--create_export--clients"></a>`clients`

Expand Down Expand Up @@ -1051,6 +1052,15 @@ Sets the permissions of the exported directory.

Default value: `undef`

##### <a name="-nfs--functions--create_export--create_dir"></a>`create_dir`

Data type: `Boolean`

Boolean. Create the directory to be exported.
Defaults to true.

Default value: `true`

### <a name="nfs--functions--mkdir"></a>`nfs::functions::mkdir`

Manage directory creation.
Expand Down Expand Up @@ -1103,7 +1113,19 @@ Default value: `'mounted'`

### <a name="nfs--server--export"></a>`nfs::server::export`

Manage all exported resources on a NFS server.
nfs::server::export { '/srv/nfs_exported/directory':
clients => '1.2.3.4/24(rw,insecure,no_subtree_check,async,no_root_squash) 5.6.7.8/24(ro)',
share => 'share_name_on_nfs_server',
}

Links

* {Puppet Docs: Using Parameterized Classes}[http://j.mp/nVpyWY]

nfs::server::export { '/srv/nfs_exported/directory':
clients => '1.2.3.4/24(rw,insecure,no_subtree_check,async,no_root_squash) 5.6.7.8/24(ro)',
share => 'share_name_on_nfs_server',
}

#### Examples

Expand All @@ -1116,11 +1138,6 @@ class { '::nfs':
nfs_v4_export_root => '/share',
nfs_v4_export_root_clients => '1.2.3.4/24(rw,fsid=root,insecure,no_subtree_check,async,no_root_squash)',
}
nfs::server::export { '/srv/nfs_exported/directory':
clients => '1.2.3.4/24(rw,insecure,no_subtree_check,async,no_root_squash) 5.6.7.8/24(ro)',
share => 'share_name_on_nfs_server',
}
```

#### Parameters
Expand All @@ -1144,6 +1161,7 @@ The following parameters are available in the `nfs::server::export` defined type
* [`v3_export_name`](#-nfs--server--export--v3_export_name)
* [`v4_export_name`](#-nfs--server--export--v4_export_name)
* [`nfsv4_bindmount_enable`](#-nfs--server--export--nfsv4_bindmount_enable)
* [`create_dir`](#-nfs--server--export--create_dir)

##### <a name="-nfs--server--export--clients"></a>`clients`

Expand Down Expand Up @@ -1282,3 +1300,12 @@ Data type: `Boolean`

Default value: `$nfs::nfsv4_bindmount_enable`

##### <a name="-nfs--server--export--create_dir"></a>`create_dir`

Data type: `Boolean`

Boolean. Create the directory to be exported.
Defaults to true.

Default value: `true`

21 changes: 15 additions & 6 deletions manifests/functions/create_export.pp
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,18 @@
# @param mode
# Sets the permissions of the exported directory.
#
# @param create_dir
# Boolean. Create the directory to be exported.
# Defaults to true.
#
# @author
# * Daniel Klockenkaemper <[email protected]>
# * Martin Alfke <[email protected]>
#
define nfs::functions::create_export (
Variant[String[1], Array[String[1]]] $clients,
String[1] $ensure = 'present',
Boolean $create_dir = true,
Optional[String[1]] $owner = undef,
Optional[String[1]] $group = undef,
Optional[String[1]] $mode = undef,
Expand All @@ -34,13 +39,17 @@
content => $line,
}

# Create the directory path only if a File resource isn't
# defined previously AND the $create_dir boolean is true.
unless defined(File[$name]) {
file { $name:
ensure => directory,
owner => $owner,
group => $group,
mode => $mode,
selinux_ignore_defaults => true,
if $create_dir {
file { $name:
ensure => directory,
owner => $owner,
group => $group,
mode => $mode,
selinux_ignore_defaults => true,
}
}
}
}
Expand Down
38 changes: 27 additions & 11 deletions manifests/server/export.pp
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,10 @@
# @param v4_export_name
# @param nfsv4_bindmount_enable
#
# @param create_dir
# Boolean. Create the directory to be exported.
# Defaults to true.
#
# @example
# class { '::nfs':
# server_enabled => true,
Expand All @@ -55,6 +59,15 @@
# nfs_v4_export_root_clients => '1.2.3.4/24(rw,fsid=root,insecure,no_subtree_check,async,no_root_squash)',
# }
#
# nfs::server::export { '/srv/nfs_exported/directory':
# clients => '1.2.3.4/24(rw,insecure,no_subtree_check,async,no_root_squash) 5.6.7.8/24(ro)',
# share => 'share_name_on_nfs_server',
# }
#
# Links
#
# * {Puppet Docs: Using Parameterized Classes}[http://j.mp/nVpyWY]
#
# nfs::server::export { '/srv/nfs_exported/directory':
# clients => '1.2.3.4/24(rw,insecure,no_subtree_check,async,no_root_squash) 5.6.7.8/24(ro)',
# share => 'share_name_on_nfs_server',
Expand Down Expand Up @@ -84,6 +97,7 @@
Optional[String[1]] $mode = undef,
String[1] $server = $facts['clientcert'],
Boolean $nfsv4_bindmount_enable = $nfs::nfsv4_bindmount_enable,
Boolean $create_dir = true,
) {
if $nfs::server::nfs_v4 {
if $nfsv4_bindmount_enable {
Expand All @@ -103,12 +117,13 @@
}
nfs::functions::create_export { $export_title:
ensure => $ensure,
clients => $clients,
owner => $owner,
group => $group,
mode => $mode,
require => $create_export_require,
ensure => $ensure,
clients => $clients,
create_dir => $create_dir,
owner => $owner,
group => $group,
mode => $mode,
require => $create_export_require,
}
if $mount != undef {
Expand Down Expand Up @@ -137,11 +152,12 @@
}
nfs::functions::create_export { $v3_export_name:
ensure => $ensure,
clients => $clients,
owner => $owner,
group => $group,
mode => $mode,
ensure => $ensure,
clients => $clients,
create_dir => $create_dir,
owner => $owner,
group => $group,
mode => $mode,
}
if $nfs::storeconfigs_enabled {
Expand Down
14 changes: 12 additions & 2 deletions spec/defines/server_export_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,15 @@

let(:params) { { clients: '1.2.3.4(rw)' } }

it { is_expected.to contain_nfs__functions__create_export('/srv/test').with('ensure' => 'mounted', 'clients' => '1.2.3.4(rw)') }
it do
is_expected.to contain_nfs__functions__create_export('/srv/test').with(
'ensure' => 'mounted',
'clients' => '1.2.3.4(rw)',
'create_dir' => true
)
end

it { is_expected.to contain_file('/srv/test').with('ensure' => 'directory') }

it do
expect(exported_resources).to contain_nfs__client__mount('/srv/test')
Expand Down Expand Up @@ -70,10 +78,12 @@

let(:pre_condition) { 'class {"nfs": server_enabled => true, nfs_v4 => false, storeconfigs_enabled => false}' }

let(:params) { { clients: '1.2.3.4(rw)' } }
let(:params) { { clients: '1.2.3.4(rw)', create_dir: false } }

it { is_expected.to contain_nfs__functions__create_export('/srv/test').with('ensure' => 'mounted', 'clients' => '1.2.3.4(rw)') }

it { is_expected.not_to contain_file('/srv/test') }

it { expect(exported_resources).not_to contain_nfs__client__mount('/srv/test') }
end

Expand Down

0 comments on commit 1ca9169

Please sign in to comment.