|
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 |
|
60 | 60 | $_force = if $force { |
|
77 | 77 |
|
78 | 78 | $_transport = "transport ${transport}" |
79 | 79 |
|
80 | | - if $options and ! empty( $options ) { |
81 | | - $_options = sort( $options ) |
| 80 | + if $options =~ Array { |
| 81 | + $_options = Hash($options.map |$item| { |
| 82 | + $m = $item.match(/^([^:]+):(.*)/) |
| 83 | + [ |
| 84 | + strip($m[1]), |
| 85 | + strip($m[2]) |
| 86 | + ] |
| 87 | + }) |
82 | 88 | } else { |
83 | | - $_options = undef |
| 89 | + $_options = $options |
84 | 90 | } |
85 | 91 |
|
86 | 92 | if $arbiter { |
|
121 | 127 | } |
122 | 128 |
|
123 | 129 | # if we have volume options, activate them now |
124 | | - # |
125 | | - # Note: $options is an array, but create_resources requires |
126 | | - # a hash of hashes. We do some contortions to get the |
127 | | - # array into the hash of hashes that looks like: |
128 | | - # |
129 | | - # option.name: |
130 | | - # value: value |
131 | | - # |
132 | | - # Note 2: we're using the $_options variable, which contains the |
133 | | - # sorted list of options. |
134 | | - if $_options { |
135 | | - # first we need to prefix each array element with the volume name |
136 | | - # so that we match the gluster::volume::option title format of |
137 | | - # volume:option |
138 | | - $vol_opts = prefix( $_options, "${title}:" ) |
139 | | - # now we make some YAML, and then parse that to get a Puppet hash |
140 | | - $yaml = join( regsubst( $vol_opts, ': ', ":\n value: ", 'G'), "\n") |
141 | | - $hoh = parseyaml($yaml) |
| 130 | + # first we need to prefix each array element with the volume name |
| 131 | + # so that we match the gluster::volume::option title format of |
| 132 | + # volume:option |
142 | 133 |
|
143 | | - # safety check |
144 | | - assert_type(Hash, $hoh) |
145 | | - # we need to ensure that these are applied AFTER the volume is created |
146 | | - # but BEFORE the volume is started |
147 | | - $new_volume_defaults = { |
148 | | - require => Exec["gluster create volume ${title}"], |
149 | | - before => Exec["gluster start volume ${title}"], |
150 | | - } |
| 134 | + # we need to ensure that these are applied AFTER the volume is created |
| 135 | + # but BEFORE the volume is started |
| 136 | + $new_volume_defaults = { |
| 137 | + require => Exec["gluster create volume ${title}"], |
| 138 | + before => Exec["gluster start volume ${title}"], |
| 139 | + } |
151 | 140 |
|
152 | | - create_resources(::gluster::volume::option, $hoh, $new_volume_defaults) |
| 141 | + $_options.each |$opt_name, $opt_value| { |
| 142 | + gluster::volume::option { "${title}:${opt_name}": |
| 143 | + value => $opt_value, |
| 144 | + * => $new_volume_defaults |
| 145 | + } |
153 | 146 | } |
154 | 147 |
|
155 | 148 | # don't forget to start the new volume! |
|
229 | 222 | } |
230 | 223 |
|
231 | 224 | # did the options change? |
232 | | - $current_options_hash = pick(fact("gluster_volumes.${title}.options"), {}) |
233 | | - $_current = sort(join_keys_to_values($current_options_hash, ': ')) |
| 225 | + $current_options_keys = sort(keys(pick(fact("gluster_volumes.${title}.options"), {}))) |
| 226 | + $provided_options_keys = sort(keys($_options)) |
234 | 227 |
|
235 | | - if $_current != $_options { |
| 228 | + if $current_options_keys != $provided_options_keys { |
236 | 229 | # |
237 | 230 | # either of $current_options or $_options may be empty. |
238 | 231 | # we need to account for this situation |
239 | 232 | # |
240 | | - if is_array($_current) and is_array($_options) { |
241 | | - $to_remove = difference($_current, $_options) |
242 | | - $to_add = difference($_options, $_current) |
243 | | - } else { |
244 | | - if is_array($_current) { |
245 | | - # $_options is not an array, so remove all currently set options |
246 | | - $to_remove = $_current |
247 | | - } elsif is_array($_options) { |
248 | | - # $current_options is not an array, so add all our defined options |
249 | | - $to_add = $_options |
250 | | - } |
251 | | - } |
252 | | - if ! empty($to_remove) { |
253 | | - # we have some options active that are not defined here. Remove them |
254 | | - # |
255 | | - # the syntax to remove ::gluster::volume::options is a little different |
256 | | - # so build up the hash correctly |
257 | | - # |
258 | | - $remove_opts = prefix( $to_remove, "${title}:" ) |
259 | | - $remove_yaml = join( regsubst( $remove_opts, ': .+$', ":\n ensure: absent", 'G' ), "\n" ) |
260 | | - $remove = parseyaml($remove_yaml) |
| 233 | + $to_remove = difference($current_options_keys, $provided_options_keys) |
| 234 | + $to_add = difference($provided_options_keys, $current_options_keys) |
| 235 | + |
| 236 | + # we have some options active that are not defined here. Remove them |
| 237 | + # |
| 238 | + # the syntax to remove ::gluster::volume::options is a little different |
| 239 | + # so build up the hash correctly |
| 240 | + # |
| 241 | + $to_remove.each |$opt_name| { |
261 | 242 | if $remove_options { |
262 | | - create_resources( ::gluster::volume::option, $remove ) |
| 243 | + gluster::volume::option { "${title}:${opt_name}": |
| 244 | + ensure => absent |
| 245 | + } |
263 | 246 | } else { |
264 | | - $remove_str = join( keys($remove), ', ' ) |
265 | | - notice("NOT REMOVING the following options for volume ${title}: ${remove_str}.") |
| 247 | + notice("NOT REMOVING the following option for volume ${title}: ${opt_name}.") |
266 | 248 | } |
267 | 249 | } |
268 | | - if ! empty($to_add) { |
269 | | - # we have some options defined that are not active. Add them |
270 | | - $add_opts = prefix( $to_add, "${title}:" ) |
271 | | - $add_yaml = join( regsubst( $add_opts, ': ', ":\n value: ", 'G' ), "\n" ) |
272 | | - $add = parseyaml($add_yaml) |
273 | | - create_resources( ::gluster::volume::option, $add ) |
| 250 | + # we have some options defined that are not active. Add them |
| 251 | + $to_add.each |$opt_name| { |
| 252 | + gluster::volume::option { "${title}:${opt_name}": |
| 253 | + value => $provided_options_keys[$opt_name] |
| 254 | + } |
274 | 255 | } |
275 | 256 | } |
276 | 257 | } else { |
|
0 commit comments