Skip to content

Commit 9ac3889

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 61a380e commit 9ac3889

File tree

5 files changed

+63
-10
lines changed

5 files changed

+63
-10
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 $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: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -33,26 +33,34 @@
3333
# Copyright 2014 CoverMyMeds, unless otherwise noted
3434
#
3535
define gluster::volume::option (
36-
$value = undef,
37-
$ensure = true,
36+
Optional $value = undef,
37+
Boolean $ensure = true,
3838
) {
3939

40-
$arr = split( $title, ':' )
41-
$count = count($arr)
40+
$arr = $title.split(':')
4241
# do we have more than one array element?
43-
if $count != 2 {
42+
if count($arr) != 2 {
4443
fail("${title} does not parse as volume:option")
4544
}
46-
$vol = $arr[0]
47-
$opt = $arr[1]
45+
[$vol, $opt] = $arr
4846

49-
if $ensure == 'absent' {
50-
$cmd = "reset ${vol} ${opt}"
47+
$cmd = if $ensure == 'absent' {
48+
"reset ${vol} ${opt}"
5149
} else {
52-
$cmd = "set ${vol} ${opt} ${value}"
50+
"set ${vol} ${opt} ${value}"
51+
}
52+
53+
$_value = if $value =~ Boolean {
54+
gluster::onoff($value)
55+
} else {
56+
String($value)
5357
}
5458

5559
exec { "gluster option ${vol} ${opt} ${value}":
60+
path => '/usr/bin:/usr/sbin:/bin',
5661
command => "${::gluster_binary} volume ${cmd}",
62+
unless => unless $ensure == 'absent' {
63+
gluster::cmd_volume_get_option($vol, $opt, $_value)
64+
},
5765
}
5866
}

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-zA-Z0-9]+\.[a-zA-Z0-9-]+$/]

0 commit comments

Comments
 (0)