Skip to content

Commit

Permalink
Merge pull request #119 from jcbollinger/mount_and_map_complex
Browse files Browse the repository at this point in the history
Wide refactoring of master map and map file management
  • Loading branch information
dhollinger authored Apr 21, 2018
2 parents 490888b + d13bb5c commit d35ae63
Show file tree
Hide file tree
Showing 21 changed files with 1,257 additions and 733 deletions.
496 changes: 332 additions & 164 deletions README.md

Large diffs are not rendered by default.

1 change: 0 additions & 1 deletion data/common.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
---
autofs::mounts: {}
autofs::maps: {}
autofs::package_ensure: installed
autofs::package_name: autofs
autofs::package_source: null
Expand Down
88 changes: 50 additions & 38 deletions manifests/init.pp
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
# Class: autofs
#
# Manages autofs mount points
# Manages the autofs facility, optionally including configuring
# mount points and maps.
#
# This class includes defined types for looping through arrays of configuration
# file names.
# Autofs mount points and their corresponding maps can also be
# managed via separate autofs::mount, autofs::mapfile, and
# autofs::mapping resources.
#
# @see https://voxpupuli.org/puppet-autofs Home
# @see https://www.github.com/voxpupuli/puppet-autofs Github
Expand All @@ -29,44 +31,44 @@
# service_enable => false,
# }
#
#
# @example using hiera with automatic lookup
# ---
# autofs::mounts:
# home:
# mount: '/home'
# mapfile: '/etc/auto.home'
# mapcontents:
# - '* -user,rw,soft,intr,rsize=32768,wsize=32768,tcp,nfsvers=3,noacl server.example.com:/path/to/home/shares'
# options: '--timeout=120'
# order: 01
# autofs::mapfiles:
# '/etc/auto.home':
# 'mappings':
# - key: '*'
# options: 'user,rw,soft,intr,rsize=32768,wsize=32768,tcp,nfsvers=3,noacl'
# fs: 'server.example.com:/path/to/home/shares'
#
#
# @param mounts the options to build the autofs config from
# @option mounts [String] :mount Location autofs will mount the share
# @option mounts [Integer] :order Order in which the config will be placed
# in auto.master
# @option mounts [String] :options Mount options
# @option mounts [String] :master Full path to the autofs master file,
# including filename
# @option mounts [String] :map_dir Full path to the master configuration
# directory. Only used with :use_dir
# @option mounts [Boolean] :use_dir Use the +dir option or not.
# @option mounts [Boolean] :direct Use direct mounts or not.
# @option mounts [Boolean] :execute Make the auto.* file executable or not.
# @option mounts [String] :mapfile Name of the "auto.*" configuration file to
# be generated.
# @option mounts [Array] :mapcontents Mount point options and parameters. Each
# array element represents a line in the configuration file.
# @option mounts [Boolean] :replace Enforce the configuration state or not.
# @param maps the options to build an autofs mapfile.
# @option maps [Array]:mapcontents Mount point options and parameters. Each
# array element represents a line in the mapfile.
# @option maps [Stdlib::Absolutepath] :mapfile Full path to where the mapfile will be placed
# @option maps [Enum['autofs/auto.map.erb', 'autofs/auto.map.exec.erb']] :template Template to use the generate the mapfile
# @option maps [String] :mapmode The UNIX permissions to apply to the mapfile.
# @option maps [Boolean] :replace Replace the file if it changes or not.
# @option maps [Integer] :order The order in which to place mapfile contents.
# @param mounts the options with which to manage the autofs master map
# @option mounts [String] :mount The autofs mount point to be managed
# @option mounts [Integer] :order The relative order in which the mount point's
# definition will appear in the master map
# @option mounts [String] :mapfile Name of the autofs map file for this mount point
# @option mounts [String] :options Mount options to be recorded for this mount point
# in the master map
# @option mounts [String] :master Full path, including file name, to the master map
# in which to manage this mount point
# @option mounts [String] :map_dir Full path, including file name, to the master map
# drop-in directory in which to manage this mount's definition. Relevant only when
# :use_dir is set to true
# @option mounts [Boolean] :use_dir Whether to manage this mount via a file in the
# master map's drop-in directory instead of directly in the master map
# @param mapfiles options with which to manage map files.
# @option mapfiles [String] path: Full path, including file name, to a
# map file to manage. Defaults to the key with which this value is associated.
# @option mapfiles [Array] mappings: an array of hashes defining specific, sun-format
# mappings that should appear in this map file. Each has a 'key', an option list in
# string or array form, and a filesystem specification as expected by the 'mount'
# command.
# @option mapfiles [Boolean] replace: whether to modify the map file if it already exists
# @param maps Deprecated. Use the mapfiles parameter instead.
# @param package_ensure Determines the state of the package. Can be set to: installed, absent, lastest, or a specific version string.
# @param package_name Determine the name of the package to install. Should be covered by hieradata.
# @param package_source Determine the source of the package, required on certain platforms (AIX)
Expand All @@ -81,18 +83,19 @@
# @param reload_command In lieu of a service reload capability in Puppet, exec this command to reload automount without restarting it.
#
class autofs (
Hash[String, Hash] $mounts,
Hash[String, Hash] $maps,
String $package_ensure,
Hash[String, Hash] $mounts,
Variant[String, Array[String]] $package_name,
Optional[String] $package_source,
Enum[ 'stopped', 'running' ] $service_ensure,
Boolean $service_enable,
String $service_name,
String $auto_master_map,
String $map_file_owner,
String $map_file_group,
Optional[String] $reload_command,
Optional[Hash[String, Hash]] $mapfiles = undef,
Optional[Hash[String, Hash]] $maps = undef, # deprecated
Optional[String] $package_source = undef,
Optional[String] $reload_command = undef,
) {
contain '::autofs::package'
unless $package_ensure == 'absent' {
Expand All @@ -103,7 +106,16 @@
autofs::mount { $mount: * => $attributes }
}

$maps.each |String $map, Hash $attributes| {
autofs::map { $map: * => $attributes }
if $mapfiles =~ NotUndef {
$mapfiles.each |String $file, Hash $attributes| {
autofs::mapfile { $file: * => $attributes }
}
}

if $maps =~ NotUndef {
deprecation('autofs::maps', 'Class parameter $autofs::maps is deprecated and will be removed in a future version. Define maps via the $mapfiles parameter instead')
$maps.each |String $map, Hash $attributes| {
autofs::map { $map: * => $attributes }
}
}
}
60 changes: 60 additions & 0 deletions manifests/mapfile.pp
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
# Define autofs::mapfile
#
# Defined type to manage overall autofs map files
#
# @see https://voxpupuli.org/puppet-autofs Home
# @see https://voxpupuli.org/puppet-autofs/puppet_classes/autofs.html puppet_classes::autofs
# @see https://www.github.com/voxpupuli/puppet-autofs Github
# @see https://forge.puppet.com/puppet/autofs Puppet Forge
#
# @author Vox Pupuli <voxpupuli@groups.io>
# @author David Hollinger III <david.hollinger@moduletux.com>
#
# @param ensure Whether the mapfile should be present on the target system
# @param path An absolute path to the map file
# @param mappings an array of mappings to enroll in the file. Additional
# mappings can be specified for this mapfile via autofs::mapping resources
# @param replace Whether to replace the contents of any an existing file
# at the specified path
#
define autofs::mapfile (
Enum['present', 'absent'] $ensure = 'present',
Stdlib::Absolutepath $path = $title,
Array[Autofs::Fs_mapping] $mappings = [],
Boolean $replace = true,
) {
include '::autofs'

unless $::autofs::package_ensure == 'absent' {
if $autofs::reload_command {
Concat {
before => Service[$autofs::service_name],
notify => Exec['automount-reload'],
}
} else {
Concat {
notify => Service[$autofs::service_name],
}
}
}

concat { $path:
ensure => $ensure,
owner => $autofs::map_file_owner,
group => $autofs::map_file_group,
mode => '0644',
replace => $replace,
require => Class['autofs::package'],
warn => template('autofs/mapfile.banner.erb'),
}

if $ensure == 'present' {
$mappings.each |$mapping| {
autofs::mapping { "${path}:${mapping[key]}":
ensure => 'present',
mapfile => $path,
* => $mapping,
}
}
}
}
98 changes: 98 additions & 0 deletions manifests/mapping.pp
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
# Define autofs::mapping
#
# Defined type to manage a single filesystem mapping in a single map file.
# When ensured 'present', a autofs::mapfile resource managing the overall
# target map file must also be present in the catalog. This resource
# implements Autofs's 'sun' map format, which is the default.
#
# It is not supported to declare multiple autofs::mappings with the
# same key, targetting the same map file, and ensured 'present'.
#
# @see https://voxpupuli.org/puppet-autofs Home
# @see https://voxpupuli.org/puppet-autofs/puppet_classes/autofs.html puppet_classes::autofs
# @see https://www.github.com/voxpupuli/puppet-autofs Github
# @see https://forge.puppet.com/puppet/autofs Puppet Forge
#
# @author Vox Pupuli <voxpupuli@groups.io>
# @author David Hollinger III <david.hollinger@moduletux.com>
#
# @param ensure Whether the mapping should be present in the target mapfile;
# ensuring 'absent' is not meaningfully different from omitting the
# resource declaration altogether
# @param fs the remote filesystem to mount
# @param key the autofs key for this mappingr. For indirect maps it is the
# basename of the mountpoint directory for $fs (not to be confused with
# an _autofs_ mount point, which is the parent directory). For direct
# maps it is the absolute path to the mountpoint directory.
# @param mapfile the absolute path to the file containing the Autofs map
# to which this mapping belongs
# @param options a comma-delimited mount options string or an array of
# individual mount options; neither individual options nor the overall
# option list should be specified with a leading hyphen (-); that is
# part of the map file format, not of the options themselves, and
# it is provided by this resource
# @param order an integer describing the relative order of the mapping
# represented by this resource within the target map file (default 10).
# The order matters only if the same kay is enrolled more than once
# in the map, in which case only the first is effective.
#
# @example Options given as a string
# autofs::mapping{ '/etc/auto.data_data':
# mapfile => '/etc/auto.data',
# key => 'data',
# options => 'rw,sync,suid',
# fs => 'storage_host.my.com:/path/to/data'
# }
#
# @example Options given as an array
# autofs::mapping{ '/etc/auto.data_data':
# mapfile => '/etc/auto.data',
# key => 'data',
# options => ['ro', 'noexec', 'nodev'],
# fs => 'storage_host.my.com:/path/to/data'
# }
#
# @example No options
# autofs::mapping{ '/etc/auto.data_data':
# mapfile => '/etc/auto.data',
# key => 'data',
# fs => 'storage_host.my.com:/path/to/data'
# }
#
define autofs::mapping (
Stdlib::Absolutepath $mapfile,
Pattern[/\A\S+\z/] $key,
Pattern[/\S/] $fs,
Enum['present', 'absent'] $ensure = 'present',
Optional[Autofs::Options] $options = undef,
Integer $order = 10,
) {
unless $ensure == 'absent' {
# Format the options string, relying to some extent on the
# $options parameter, if specified, to indeed match the
# Autofs::Options data type
if ($options =~ Undef) or ($options =~ Array[Any,0,0]) { # an empty array
$formatted_options = ''
} else {
$prelim_options = $options ? {
Array => join($options, ','), # a non-empty array
String => $options,
default => fail('Unexpected value for parameter $options')
}
$formatted_options = $prelim_options ? {
# even though the user *shouldn't* provide the hyphen, we accommodate
# them doing so. But only at the head of the option list, not
# internally.
/\A-/ => $prelim_options,
default => "-${prelim_options}",
}
}

# Declare an appropriate fragment of the target map file
concat::fragment { "autofs::mapping/${title}":
target => $mapfile,
content => "${key} ${formatted_options} ${fs}\n",
order => $order,
}
}
}
Loading

0 comments on commit d35ae63

Please sign in to comment.