Skip to content

Commit

Permalink
Fix unnecessarily setting gluster volume option repeatedly
Browse files Browse the repository at this point in the history
Checks Gluster volume options against existing values to avoid repeatedly
setting values

Also adds these types:
+ `Gluster::VolumeName`
+ `Gluster::VolumeOption`

And these functions:
+ `gluster::cmd_volume_get_option`
+ `gluster::onoff`
  • Loading branch information
runejuhl committed May 24, 2019
1 parent 61a380e commit f98ee4e
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 11 deletions.
34 changes: 34 additions & 0 deletions functions/cmd_volume_get_option.pp
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# Create a command string to get option `$opt` from gluster volume `$vol`, and
# optionally compare it against `$comparison`.
#
# @param vol [Gluster::VolumeName] Gluster volume name
# @param opt [Gluster::OptionName] Gluster volume option name
# @param comparison [Optional[String]] Optional string to compare the existing
# value against
# @return [String]
#
# @example Usage
#
# ```puppet
# gluster::cmd_volume_get_option('data', 'nfs.disable', String(true))
# ```
#
function gluster::cmd_volume_get_option(
Gluster::VolumeName $vol,
Gluster::VolumeOption $opt,
Optional[Any] $comparison = undef,
) {
$_cmd = "${::gluster_binary} volume get ${vol} ${opt}"

unless $comparison {
return $_cmd
}

$_comparison = $comparison ? {
Undef => '\(null\)',
Boolean => gluster::onoff($comparison),
default => $comparison,
}

"${_cmd} | tail -n1 | grep -E '^${opt} +${_comparison} *\$'"
}
9 changes: 9 additions & 0 deletions functions/onoff.pp
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
function gluster::onoff (
Boolean $value,
) {
if $value {
'on'
} else {
'off'
}
}
29 changes: 18 additions & 11 deletions manifests/volume/option.pp
Original file line number Diff line number Diff line change
Expand Up @@ -33,26 +33,33 @@
# Copyright 2014 CoverMyMeds, unless otherwise noted
#
define gluster::volume::option (
$value = undef,
$ensure = true,
Optional $value = undef,
Variant[Boolean, Enum['absent']] $ensure = true,
) {

$arr = split( $title, ':' )
$count = count($arr)
$arr = $title.split(':')
# do we have more than one array element?
if $count != 2 {
if count($arr) != 2 {
fail("${title} does not parse as volume:option")
}
$vol = $arr[0]
$opt = $arr[1]
[$vol, $opt] = $arr

if $ensure == 'absent' {
$cmd = "reset ${vol} ${opt}"
$_value = $value ? {
Boolean => gluster::onoff($value),
default => String($value),
}

$cmd = if $ensure == 'absent' {
"reset ${vol} ${opt}"
} else {
$cmd = "set ${vol} ${opt} ${value}"
"set ${vol} ${opt} ${_value}"
}

exec { "gluster option ${vol} ${opt} ${value}":
exec { "gluster option ${vol} ${opt} ${_value}":
path => '/usr/bin:/usr/sbin:/bin',
command => "${::gluster_binary} volume ${cmd}",
unless => unless $ensure == 'absent' {
gluster::cmd_volume_get_option($vol, $opt, $_value)
},
}
}
1 change: 1 addition & 0 deletions types/volumename.pp
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
type Gluster::VolumeName = Pattern[/^[a-zA-Z0-9_-]+$/]
1 change: 1 addition & 0 deletions types/volumeoption.pp
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
type Gluster::VolumeOption = Pattern[/^[a-z0-9]+\.[a-z0-9-]+$/]

0 comments on commit f98ee4e

Please sign in to comment.