Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use structured-facts for array values instead of joining them by , #120

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,11 @@ Also provided with this module are a number of custom Gluster-related facts.

* `gluster_binary`: the full pathname of the Gluster CLI command
* `gluster_peer_count`: the number of peers to which this server is connected in the pool.
* `gluster_peer_list`: a comma-separated list of peer hostnames
* `gluster_volume_list`: a comma-separated list of volumes being served by this server
* `gluster_volume_#{vol}_bricks`: a comma-separated list of bricks in each volume being served by this server
* `gluster_volume_#{vol}_options`: a comma-separared list of options enabled on each volume
* `gluster_volume_#{vol}_ports`: a comma-separated list of ports used by the bricks in the specified volume.
* `gluster_peer_list`: an array of peer hostnames
* `gluster_volume_list`: an array of volumes being served by this server
* `gluster_volume_#{vol}_bricks`: an array of bricks in each volume being served by this server
* `gluster_volume_#{vol}_options`: an array of options enabled on each volume
* `gluster_volume_#{vol}_ports`: an array of ports used by the bricks in the specified volume.

The `gluster_binary` fact will look for an [external fact](http://docs.puppetlabs.com/guides/custom_facts.html#external-facts) named `gluster_custom_binary`. If this fact is defined, `gluster_binary` will use that value. Otherwise the path will be searched until the gluster command is found.

Expand Down
14 changes: 7 additions & 7 deletions lib/facter/gluster.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
peer_count = 0
peer_list = ''
peer_list = []
volume_bricks = {}
volume_options = {}
volume_ports = {}
Expand Down Expand Up @@ -28,7 +28,7 @@
output = Facter::Util::Resolution.exec("#{binary} peer status")
peer_count = Regexp.last_match[1].to_i if output =~ %r{^Number of Peers: (\d+)$}
if peer_count > 0
peer_list = output.scan(%r{^Hostname: (.+)$}).flatten.join(',')
peer_list = output.scan(%r{^Hostname: (.+)$}).flatten
# note the stderr redirection here
# `gluster volume list` spits to stderr :(
output = Facter::Util::Resolution.exec("#{binary} volume list 2>&1")
Expand Down Expand Up @@ -61,28 +61,28 @@
if peer_count > 0
Facter.add(:gluster_peer_list) do
setcode do
peer_list
peer_list.sort
end
end

unless volume_bricks.empty?
Facter.add(:gluster_volume_list) do
setcode do
volume_bricks.keys.join(',')
volume_bricks.keys.sort
end
end
volume_bricks.each do |vol, bricks|
Facter.add("gluster_volume_#{vol}_bricks".to_sym) do
setcode do
bricks.join(',')
bricks.sort
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Having read a bit more, I'm not actually sure this is the correct thing to do. Brick order is important?
See #121 (comment)

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are right. After a short look to the documentation I'd say the brick order is important for stripped and distributed volumes. So it looks like #121 isn't a bug in this module but in the puppet configuration of the author of #121.

end
end
end
if volume_options
volume_options.each do |vol, opts|
Facter.add("gluster_volume_#{vol}_options".to_sym) do
setcode do
opts.join(',')
opts.sort
end
end
end
Expand All @@ -91,7 +91,7 @@
volume_ports.each do |vol, ports|
Facter.add("gluster_volume_#{vol}_ports".to_sym) do
setcode do
ports.join(',')
ports.sort
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion manifests/peer.pp
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@
# and we don't want to attach a server that is already a member
# of the current pool
if getvar('::gluster_peer_list') {
$peers = split($::gluster_peer_list, ',' )
$peers = $::gluster_peer_list
if ! member($peers, $title) {
$already_in_pool = false
} else {
Expand Down
8 changes: 4 additions & 4 deletions manifests/volume.pp
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@
$minimal_requirements = false
}

if getvar('::gluster_volume_list') and member( split( $::gluster_volume_list, ',' ), $title ) {
if getvar('::gluster_volume_list') and member( $::gluster_volume_list, $title ) {
$already_exists = true
} else {
$already_exists = false
Expand All @@ -109,7 +109,7 @@
# first, get a list of unique server names hosting bricks
$brick_hosts = unique( regsubst( $bricks, '^([^:]+):(.+)$', '\1') )
# now get a list of all peers, including ourself
$pool_members = concat( split( $::gluster_peer_list, ','), [ $::fqdn ] )
$pool_members = concat($::gluster_peer_list, [ $::fqdn ] )
# now see what the difference is
$missing_bricks = difference( $brick_hosts, $pool_members)

Expand Down Expand Up @@ -163,7 +163,7 @@
# this volume exists

# our fact lists bricks comma-separated, but we need an array
$vol_bricks = split( getvar( "::gluster_volume_${title}_bricks" ), ',')
$vol_bricks = getvar( "::gluster_volume_${title}_bricks" )
if $bricks != $vol_bricks {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@coder-hugo Do you think the arrays can be sorted before they're compared (and operated on)?

This would fix #121 ?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd say it would be even better to sort the arrays already when generating the facts. I'll just try this now.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added a patch that sorts all array facts and also the $bricks array given to the gluster::volume.

# this resource's list of bricks does not match the existing
# volume's list of bricks
Expand Down Expand Up @@ -226,7 +226,7 @@
# did the options change?
$current_options = getvar("gluster_volume_${title}_options")
if $current_options {
$_current = sort( split($current_options, ',') )
$_current = sort( $current_options )
} else {
$_current = []
}
Expand Down
4 changes: 2 additions & 2 deletions spec/classes/init_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -126,8 +126,8 @@
let :facts do
super().merge(
gluster_binary: '/sbin/gluster',
gluster_peer_list: 'example1,example2',
gluster_volume_list: 'gl1.example.com:/glusterfs/backup,gl2.example.com:/glusterfs/backup'
gluster_peer_list: %w[example1 example2],
gluster_volume_list: %w[gl1.example.com:/glusterfs/backup gl2.example.com:/glusterfs/backup]
)
end
let :params do
Expand Down
10 changes: 5 additions & 5 deletions spec/defines/peer_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
{
gluster_binary: '/usr/sbin/gluster',
gluster_peer_count: 1,
gluster_peer_list: 'peer1.example.com'
gluster_peer_list: ['peer1.example.com']
}
end

Expand All @@ -36,7 +36,7 @@
{
gluster_binary: '/usr/sbin/gluster',
gluster_peer_count: 2,
gluster_peer_list: 'peer1.example.com,peer2.example.com'
gluster_peer_list: %w[peer1.example.com peer2.example.com]
}
end

Expand All @@ -51,7 +51,7 @@
{
gluster_binary: '/usr/sbin/gluster',
gluster_peer_count: 0,
gluster_peer_list: ''
gluster_peer_list: []
}
end

Expand All @@ -63,7 +63,7 @@
{
gluster_binary: '/usr/sbin/gluster',
gluster_peer_count: 1,
gluster_peer_list: 'peer2.example.com'
gluster_peer_list: ['peer2.example.com']
}
end

Expand All @@ -75,7 +75,7 @@
{
gluster_binary: '/usr/sbin/gluster',
gluster_peer_count: 2,
gluster_peer_list: 'peer2.example.com,peer3.example.com'
gluster_peer_list: %w[peer2.example.com peer3.example.com]
}
end

Expand Down
10 changes: 5 additions & 5 deletions spec/defines/volume_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
let(:facts) do
{
gluster_binary: '/usr/sbin/gluster',
gluster_peer_list: 'peer1.example.com,peer2.example.com'
gluster_peer_list: %w[peer1.example.com peer2.example.com]
}
end

Expand All @@ -48,8 +48,8 @@
let(:facts) do
{
gluster_binary: '/usr/sbin/gluster',
gluster_peer_list: 'peer1.example.com,peer2.example.com',
gluster_volume_list: 'gl1.example.com:/glusterfs/backup,gl2.example.com:/glusterfs/backup'
gluster_peer_list: %w[peer1.example.com peer2.example.com],
gluster_volume_list: %w[gl1.example.com:/glusterfs/backup gl2.example.com:/glusterfs/backup]
}
end

Expand All @@ -60,8 +60,8 @@
let(:facts) do
{
gluster_binary: '/usr/sbin/gluster',
gluster_peer_list: 'srv1.local,srv2.local',
gluster_volume_list: 'srv1.local:/glusterfs/backup,srv2.local:/glusterfs/backup'
gluster_peer_list: %w[srv1.local srv2.local],
gluster_volume_list: %w[srv1.local:/glusterfs/backup srv2.local:/glusterfs/backup]
}
end

Expand Down