Skip to content

Commit 875065d

Browse files
committed
Fix unnecessarily setting gluster volume option repeatedly
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`
1 parent 85cb37e commit 875065d

File tree

5 files changed

+64
-11
lines changed

5 files changed

+64
-11
lines changed

functions/cmd_volume_get_option.pp

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# Create a command string to get option `$opt` from gluster volume `$vol`, and
2+
# optionally compare it against `$comparison`.
3+
#
4+
# @param vol [Gluster::VolumeName] Gluster volume name
5+
# @param opt [Gluster::OptionName] Gluster volume option name
6+
# @param comparison [Optional[String]] Optional string to compare the existing
7+
# value against
8+
# @return [String]
9+
#
10+
# @example Usage
11+
#
12+
# ```puppet
13+
# gluster::cmd_volume_get_option('data', 'nfs.disable', String(true))
14+
# ```
15+
#
16+
function gluster::cmd_volume_get_option(
17+
Gluster::VolumeName $vol,
18+
Gluster::VolumeOption $opt,
19+
Optional[Any] $comparison = undef,
20+
) {
21+
$_cmd = "${::gluster_binary} volume get ${vol} ${opt}"
22+
23+
unless $comparison {
24+
return $_cmd
25+
}
26+
27+
$_comparison = $comparison ? {
28+
Undef => '\(null\)',
29+
Boolean => gluster::onoff($comparison),
30+
default => $comparison,
31+
}
32+
33+
"${_cmd} | tail -n1 | grep -E '^${opt} +${_comparison} *\$'"
34+
}

functions/onoff.pp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
function gluster::onoff (
2+
Boolean $value,
3+
) {
4+
if $value {
5+
'on'
6+
} else {
7+
'off'
8+
}
9+
}

manifests/volume/option.pp

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@
33
# @param title
44
# the name of the volume, a colon, and the name of the option
55
# @param value
6-
# the value to set for this option
6+
# the value to set for this option. Boolean values will be coerced to
7+
# 'on'/'off'.
78
# @param ensure
89
# whether to set or remove an option
910
#
@@ -26,26 +27,33 @@
2627
# @note Copyright 2014 CoverMyMeds, unless otherwise noted
2728
#
2829
define gluster::volume::option (
29-
Optional[String] $value = undef,
30+
Optional[Boolean, String] $value = undef,
3031
Enum['present', 'absent'] $ensure = 'present',
3132
) {
3233

33-
$arr = split( $title, ':' )
34-
$count = count($arr)
34+
$arr = $title.split(':')
3535
# do we have more than one array element?
36-
if $count != 2 {
36+
if count($arr) != 2 {
3737
fail("${title} does not parse as volume:option")
3838
}
39-
$vol = $arr[0]
40-
$opt = $arr[1]
39+
[$vol, $opt] = $arr
4140

42-
if $ensure == 'absent' {
43-
$cmd = "reset ${vol} ${opt}"
41+
$_value = $value ? {
42+
Boolean => gluster::onoff($value),
43+
default => String($value),
44+
}
45+
46+
$cmd = if $ensure == 'absent' {
47+
"reset ${vol} ${opt}"
4448
} else {
45-
$cmd = "set ${vol} ${opt} ${value}"
49+
"set ${vol} ${opt} ${_value}"
4650
}
4751

48-
exec { "gluster option ${vol} ${opt} ${value}":
52+
exec { "gluster option ${vol} ${opt} ${_value}":
53+
path => '/usr/bin:/usr/sbin:/bin',
4954
command => "${::gluster_binary} volume ${cmd}",
55+
unless => unless $ensure == 'absent' {
56+
gluster::cmd_volume_get_option($vol, $opt, $_value)
57+
},
5058
}
5159
}

types/volumename.pp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
type Gluster::VolumeName = Pattern[/^[a-zA-Z0-9_-]+$/]

types/volumeoption.pp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
type Gluster::VolumeOption = Pattern[/^[a-z0-9]+\.[a-z0-9-]+$/]

0 commit comments

Comments
 (0)