|
17 | 17 | # @param bricks |
18 | 18 | # an array of bricks to use for this volume |
19 | 19 | # @param options |
20 | | -# an array of volume options for the volume |
| 20 | +# an hash of volume options for the volume, also accepts array for BC |
21 | 21 | # @param remove_options |
22 | 22 | # whether to permit the removal of active options that are not defined for |
23 | 23 | # this volume. |
|
33 | 33 | # 'srv1.local:/export/brick2/brick', |
34 | 34 | # 'srv2.local:/export/brick2/brick', |
35 | 35 | # ], |
36 | | -# options => [ |
37 | | -# 'server.allow-insecure: on', |
38 | | -# 'nfs.ports-insecure: on', |
39 | | -# ], |
| 36 | +# options => { |
| 37 | +# 'server.allow-insecure': on', |
| 38 | +# 'nfs.ports-insecure': 'on', |
| 39 | +# }, |
40 | 40 | # } |
41 | 41 | # |
42 | 42 | # @author Scott Merrill <[email protected]> |
|
45 | 45 | define gluster::volume ( |
46 | 46 | Array[String, 1] $bricks, |
47 | 47 |
|
48 | | - Enum['present', 'absent'] $ensure = 'present', |
49 | | - Boolean $force = false, |
50 | | - Enum['tcp', 'rdma', 'tcp,rdma'] $transport = 'tcp', |
51 | | - Boolean $rebalance = true, |
52 | | - Boolean $heal = true, |
53 | | - Boolean $remove_options = false, |
54 | | - Optional[Array] $options = undef, |
55 | | - Optional[Integer] $stripe = undef, |
56 | | - Optional[Integer] $replica = undef, |
57 | | - Optional[Integer] $arbiter = undef, |
| 48 | + Enum['present', 'absent'] $ensure = 'present', |
| 49 | + Boolean $force = false, |
| 50 | + Enum['tcp', 'rdma', 'tcp,rdma'] $transport = 'tcp', |
| 51 | + Boolean $rebalance = true, |
| 52 | + Boolean $heal = true, |
| 53 | + Boolean $remove_options = false, |
| 54 | + Variant[Array[String],Hash] $options = {}, |
| 55 | + Optional[Integer] $stripe = undef, |
| 56 | + Optional[Integer] $replica = undef, |
| 57 | + Optional[Integer] $arbiter = undef, |
58 | 58 | ) { |
59 | 59 | $_force = if $force { |
60 | 60 | 'force' |
|
76 | 76 |
|
77 | 77 | $_transport = "transport ${transport}" |
78 | 78 |
|
79 | | - if $options and ! empty( $options ) { |
80 | | - $_options = sort( $options ) |
| 79 | + if $options =~ Array { |
| 80 | + $_options = Hash($options.map |$item| { |
| 81 | + $m = $item.match(/^([^:]+):(.*)/) |
| 82 | + [ |
| 83 | + strip($m[1]), |
| 84 | + strip($m[2]) |
| 85 | + ] |
| 86 | + }) |
81 | 87 | } else { |
82 | | - $_options = undef |
| 88 | + $_options = $options |
83 | 89 | } |
84 | 90 |
|
85 | 91 | if $arbiter { |
|
120 | 126 | } |
121 | 127 |
|
122 | 128 | # if we have volume options, activate them now |
123 | | - # |
124 | | - # Note: $options is an array, but create_resources requires |
125 | | - # a hash of hashes. We do some contortions to get the |
126 | | - # array into the hash of hashes that looks like: |
127 | | - # |
128 | | - # option.name: |
129 | | - # value: value |
130 | | - # |
131 | | - # Note 2: we're using the $_options variable, which contains the |
132 | | - # sorted list of options. |
133 | | - if $_options { |
134 | | - # first we need to prefix each array element with the volume name |
135 | | - # so that we match the gluster::volume::option title format of |
136 | | - # volume:option |
137 | | - $vol_opts = prefix( $_options, "${title}:" ) |
138 | | - # now we make some YAML, and then parse that to get a Puppet hash |
139 | | - $yaml = join( regsubst( $vol_opts, ': ', ":\n value: ", 'G'), "\n") |
140 | | - $hoh = parseyaml($yaml) |
| 129 | + # first we need to prefix each array element with the volume name |
| 130 | + # so that we match the gluster::volume::option title format of |
| 131 | + # volume:option |
141 | 132 |
|
142 | | - # safety check |
143 | | - assert_type(Hash, $hoh) |
144 | | - # we need to ensure that these are applied AFTER the volume is created |
145 | | - # but BEFORE the volume is started |
146 | | - $new_volume_defaults = { |
147 | | - require => Exec["gluster create volume ${title}"], |
148 | | - before => Exec["gluster start volume ${title}"], |
149 | | - } |
| 133 | + # we need to ensure that these are applied AFTER the volume is created |
| 134 | + # but BEFORE the volume is started |
| 135 | + $new_volume_defaults = { |
| 136 | + require => Exec["gluster create volume ${title}"], |
| 137 | + before => Exec["gluster start volume ${title}"], |
| 138 | + } |
150 | 139 |
|
151 | | - create_resources(::gluster::volume::option, $hoh, $new_volume_defaults) |
| 140 | + $_options.each |$opt_name, $opt_value| { |
| 141 | + gluster::volume::option { "${title}:${opt_name}": |
| 142 | + value => $opt_value, |
| 143 | + * => $new_volume_defaults |
| 144 | + } |
152 | 145 | } |
153 | 146 |
|
154 | 147 | # don't forget to start the new volume! |
|
226 | 219 | } |
227 | 220 |
|
228 | 221 | # did the options change? |
229 | | - $current_options_hash = pick(fact("gluster_volumes.${title}.options"), {}) |
230 | | - $_current = sort(join_keys_to_values($current_options_hash, ': ')) |
| 222 | + $current_options_keys = sort(keys(pick(fact("gluster_volumes.${title}.options"), {}))) |
| 223 | + $provided_options_keys = sort(keys($_options)) |
231 | 224 |
|
232 | | - if $_current != $_options { |
| 225 | + if $current_options_keys != $provided_options_keys { |
233 | 226 | # |
234 | 227 | # either of $current_options or $_options may be empty. |
235 | 228 | # we need to account for this situation |
236 | 229 | # |
237 | | - if is_array($_current) and is_array($_options) { |
238 | | - $to_remove = difference($_current, $_options) |
239 | | - $to_add = difference($_options, $_current) |
240 | | - } else { |
241 | | - if is_array($_current) { |
242 | | - # $_options is not an array, so remove all currently set options |
243 | | - $to_remove = $_current |
244 | | - } elsif is_array($_options) { |
245 | | - # $current_options is not an array, so add all our defined options |
246 | | - $to_add = $_options |
247 | | - } |
248 | | - } |
249 | | - if ! empty($to_remove) { |
250 | | - # we have some options active that are not defined here. Remove them |
251 | | - # |
252 | | - # the syntax to remove ::gluster::volume::options is a little different |
253 | | - # so build up the hash correctly |
254 | | - # |
255 | | - $remove_opts = prefix( $to_remove, "${title}:" ) |
256 | | - $remove_yaml = join( regsubst( $remove_opts, ': .+$', ":\n ensure: absent", 'G' ), "\n" ) |
257 | | - $remove = parseyaml($remove_yaml) |
| 230 | + $to_remove = difference($current_options_keys, $provided_options_keys) |
| 231 | + $to_add = difference($provided_options_keys, $current_options_keys) |
| 232 | + |
| 233 | + # we have some options active that are not defined here. Remove them |
| 234 | + # |
| 235 | + # the syntax to remove ::gluster::volume::options is a little different |
| 236 | + # so build up the hash correctly |
| 237 | + # |
| 238 | + $to_remove.each |$opt_name| { |
258 | 239 | if $remove_options { |
259 | | - create_resources( ::gluster::volume::option, $remove ) |
| 240 | + gluster::volume::option { "${title}:${opt_name}": |
| 241 | + ensure => absent |
| 242 | + } |
260 | 243 | } else { |
261 | | - $remove_str = join( keys($remove), ', ' ) |
262 | | - notice("NOT REMOVING the following options for volume ${title}: ${remove_str}.") |
| 244 | + notice("NOT REMOVING the following option for volume ${title}: ${opt_name}.") |
263 | 245 | } |
264 | 246 | } |
265 | | - if ! empty($to_add) { |
266 | | - # we have some options defined that are not active. Add them |
267 | | - $add_opts = prefix( $to_add, "${title}:" ) |
268 | | - $add_yaml = join( regsubst( $add_opts, ': ', ":\n value: ", 'G' ), "\n" ) |
269 | | - $add = parseyaml($add_yaml) |
270 | | - create_resources( ::gluster::volume::option, $add ) |
| 247 | + # we have some options defined that are not active. Add them |
| 248 | + $to_add.each |$opt_name| { |
| 249 | + gluster::volume::option { "${title}:${opt_name}": |
| 250 | + value => $provided_options_keys[$opt_name] |
| 251 | + } |
271 | 252 | } |
272 | 253 | } |
273 | 254 | } else { |
|
0 commit comments