Skip to content

Commit 871de1b

Browse files
authored
Merge pull request #127 from glorpen/singlecluster
Fixed logic for single node gluster servers
2 parents d972a64 + 3959a63 commit 871de1b

File tree

5 files changed

+122
-106
lines changed

5 files changed

+122
-106
lines changed

lib/facter/gluster.rb

Lines changed: 42 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -29,24 +29,25 @@
2929
peer_count = Regexp.last_match[1].to_i if output =~ %r{^Number of Peers: (\d+)$}
3030
if peer_count > 0
3131
peer_list = output.scan(%r{^Hostname: (.+)$}).flatten.join(',')
32-
# note the stderr redirection here
33-
# `gluster volume list` spits to stderr :(
34-
output = Facter::Util::Resolution.exec("#{binary} volume list 2>&1")
35-
if output != 'No volumes present in cluster'
36-
output.split.each do |vol|
37-
# If a brick has trailing informaion such as (arbiter) remove it
38-
info = Facter::Util::Resolution.exec("#{binary} volume info #{vol} | sed 's/ (arbiter)//g'")
39-
# rubocop:disable Metrics/BlockNesting
40-
vol_status = Regexp.last_match[1] if info =~ %r{^Status: (.+)$}
41-
bricks = info.scan(%r{^Brick[^:]+: (.+)$}).flatten
42-
volume_bricks[vol] = bricks
43-
options = info.scan(%r{^(\w+\.[^:]+: .+)$}).flatten
44-
volume_options[vol] = options if options
45-
next unless vol_status == 'Started'
46-
status = Facter::Util::Resolution.exec("#{binary} volume status #{vol} 2>/dev/null")
47-
if status =~ %r{^Brick}
48-
volume_ports[vol] = status.scan(%r{^Brick [^\t]+\t+(\d+)}).flatten.uniq.sort
49-
end
32+
other_names = output.scan(%r{^Other names:\n((.+\n)+)}).flatten.join.scan(%r{(.+)\n?}).sort.uniq.flatten.join(',')
33+
peer_list += ',' + other_names if other_names
34+
end
35+
# note the stderr redirection here
36+
# `gluster volume list` spits to stderr :(
37+
output = Facter::Util::Resolution.exec("#{binary} volume list 2>&1")
38+
if output != 'No volumes present in cluster'
39+
output.split.each do |vol|
40+
# If a brick has trailing informaion such as (arbiter) remove it
41+
info = Facter::Util::Resolution.exec("#{binary} volume info #{vol} | sed 's/ (arbiter)//g'")
42+
vol_status = Regexp.last_match[1] if info =~ %r{^Status: (.+)$}
43+
bricks = info.scan(%r{^Brick[^:]+: (.+)$}).flatten
44+
volume_bricks[vol] = bricks
45+
options = info.scan(%r{^(\w+\.[^:]+: .+)$}).flatten
46+
volume_options[vol] = options if options
47+
next unless vol_status == 'Started'
48+
status = Facter::Util::Resolution.exec("#{binary} volume status #{vol} 2>/dev/null")
49+
if status =~ %r{^Brick}
50+
volume_ports[vol] = status.scan(%r{^Brick [^\t]+\t+(\d+)}).flatten.uniq.sort
5051
end
5152
end
5253
end
@@ -58,42 +59,39 @@
5859
end
5960
end
6061

61-
# these facts doesn't make sense without peers
62-
if peer_count > 0
63-
Facter.add(:gluster_peer_list) do
62+
Facter.add(:gluster_peer_list) do
63+
setcode do
64+
peer_list
65+
end
66+
end
67+
68+
unless volume_bricks.empty?
69+
Facter.add(:gluster_volume_list) do
6470
setcode do
65-
peer_list
71+
volume_bricks.keys.join(',')
6672
end
6773
end
68-
69-
unless volume_bricks.empty?
70-
Facter.add(:gluster_volume_list) do
74+
volume_bricks.each do |vol, bricks|
75+
Facter.add("gluster_volume_#{vol}_bricks".to_sym) do
7176
setcode do
72-
volume_bricks.keys.join(',')
77+
bricks.join(',')
7378
end
7479
end
75-
volume_bricks.each do |vol, bricks|
76-
Facter.add("gluster_volume_#{vol}_bricks".to_sym) do
80+
end
81+
if volume_options
82+
volume_options.each do |vol, opts|
83+
Facter.add("gluster_volume_#{vol}_options".to_sym) do
7784
setcode do
78-
bricks.join(',')
85+
opts.join(',')
7986
end
8087
end
8188
end
82-
if volume_options
83-
volume_options.each do |vol, opts|
84-
Facter.add("gluster_volume_#{vol}_options".to_sym) do
85-
setcode do
86-
opts.join(',')
87-
end
88-
end
89-
end
90-
end
91-
if volume_ports
92-
volume_ports.each do |vol, ports|
93-
Facter.add("gluster_volume_#{vol}_ports".to_sym) do
94-
setcode do
95-
ports.join(',')
96-
end
89+
end
90+
if volume_ports
91+
volume_ports.each do |vol, ports|
92+
Facter.add("gluster_volume_#{vol}_ports".to_sym) do
93+
setcode do
94+
ports.join(',')
9795
end
9896
end
9997
end

manifests/peer.pp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,8 @@
4242
# peering attempt only resolves a cosmetic issue, not a functional one.
4343
#
4444
define gluster::peer (
45-
$pool = 'default'
45+
$pool = 'default',
46+
$fqdn = $::fqdn,
4647
) {
4748

4849
# we can't do much without the Gluster binary
@@ -51,7 +52,7 @@
5152
if getvar('::gluster_binary') {
5253
# we can't join to ourselves, so it only makes sense to operate
5354
# on other gluster servers in the same pool
54-
if $title != $::fqdn {
55+
if $fqdn != $::fqdn {
5556

5657
# and we don't want to attach a server that is already a member
5758
# of the current pool

manifests/volume.pp

Lines changed: 36 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -102,75 +102,55 @@
102102
if getvar('::gluster_binary'){
103103
# we need the Gluster binary to do anything!
104104

105-
if getvar('::gluster_peer_list') {
106-
$minimal_requirements = true
107-
} else {
108-
$minimal_requirements = false
109-
}
110-
111105
if getvar('::gluster_volume_list') and member( split( $::gluster_volume_list, ',' ), $title ) {
112106
$already_exists = true
113107
} else {
114108
$already_exists = false
115109
}
116110

117-
if $minimal_requirements and $already_exists == false {
111+
if $already_exists == false {
118112
# this volume has not yet been created
119113

120-
# before we can create it, we need to ensure that all the
121-
# servers hosting bricks are members of the storage pool
114+
exec { "gluster create volume ${title}":
115+
command => "${::gluster_binary} volume create ${title} ${args}",
116+
}
117+
118+
# if we have volume options, activate them now
122119
#
123-
# first, get a list of unique server names hosting bricks
124-
$brick_hosts = unique( regsubst( $bricks, '^([^:]+):(.+)$', '\1') )
125-
# now get a list of all peers, including ourself
126-
$pool_members = concat( split( $::gluster_peer_list, ','), [ $::fqdn ] )
127-
# now see what the difference is
128-
$missing_bricks = difference( $brick_hosts, $pool_members)
120+
# Note: $options is an array, but create_resources requires
121+
# a hash of hashes. We do some contortions to get the
122+
# array into the hash of hashes that looks like:
123+
#
124+
# option.name:
125+
# value: value
126+
#
127+
# Note 2: we're using the $_options variable, which contains the
128+
# sorted list of options.
129+
if $_options {
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
133+
$vol_opts = prefix( $_options, "${title}:" )
134+
# now we make some YAML, and then parse that to get a Puppet hash
135+
$yaml = join( regsubst( $vol_opts, ': ', ":\n value: ", 'G'), "\n")
136+
$hoh = parseyaml($yaml)
129137

130-
if ! empty($missing_bricks) {
131-
notice("Not creating Gluster volume ${title}: some bricks are not in the pool")
132-
} else {
133-
exec { "gluster create volume ${title}":
134-
command => "${::gluster_binary} volume create ${title} ${args}",
138+
# safety check
139+
assert_type(Hash, $hoh)
140+
# we need to ensure that these are applied AFTER the volume is created
141+
# but BEFORE the volume is started
142+
$new_volume_defaults = {
143+
require => Exec["gluster create volume ${title}"],
144+
before => Exec["gluster start volume ${title}"],
135145
}
136146

137-
# if we have volume options, activate them now
138-
#
139-
# Note: $options is an array, but create_resources requires
140-
# a hash of hashes. We do some contortions to get the
141-
# array into the hash of hashes that looks like:
142-
#
143-
# option.name:
144-
# value: value
145-
#
146-
# Note 2: we're using the $_options variable, which contains the
147-
# sorted list of options.
148-
if $_options {
149-
# first we need to prefix each array element with the volume name
150-
# so that we match the gluster::volume::option title format of
151-
# volume:option
152-
$vol_opts = prefix( $_options, "${title}:" )
153-
# now we make some YAML, and then parse that to get a Puppet hash
154-
$yaml = join( regsubst( $vol_opts, ': ', ":\n value: ", 'G'), "\n")
155-
$hoh = parseyaml($yaml)
156-
157-
# safety check
158-
assert_type(Hash, $hoh)
159-
# we need to ensure that these are applied AFTER the volume is created
160-
# but BEFORE the volume is started
161-
$new_volume_defaults = {
162-
require => Exec["gluster create volume ${title}"],
163-
before => Exec["gluster start volume ${title}"],
164-
}
165-
166-
create_resources(::gluster::volume::option, $hoh, $new_volume_defaults)
167-
}
147+
create_resources(::gluster::volume::option, $hoh, $new_volume_defaults)
148+
}
168149

169-
# don't forget to start the new volume!
170-
exec { "gluster start volume ${title}":
171-
command => "${::gluster_binary} volume start ${title}",
172-
require => Exec["gluster create volume ${title}"],
173-
}
150+
# don't forget to start the new volume!
151+
exec { "gluster start volume ${title}":
152+
command => "${::gluster_binary} volume start ${title}",
153+
require => Exec["gluster create volume ${title}"],
174154
}
175155

176156
} elsif $already_exists {

spec/defines/peer_spec.rb

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,12 @@
33
describe 'gluster::peer', type: :define do
44
let(:title) { 'peer1.example.com' }
55

6+
let(:params) do
7+
{
8+
fqdn: 'peer1.example.com'
9+
}
10+
end
11+
612
describe 'missing gluster_binary fact' do
713
it { is_expected.to compile }
814
it { is_expected.not_to contain_exec('gluster peer probe peer1.example.com') }
@@ -51,7 +57,8 @@
5157
{
5258
gluster_binary: '/usr/sbin/gluster',
5359
gluster_peer_count: 0,
54-
gluster_peer_list: ''
60+
gluster_peer_list: '',
61+
fqdn: 'peer99.example.com'
5562
}
5663
end
5764

@@ -63,7 +70,8 @@
6370
{
6471
gluster_binary: '/usr/sbin/gluster',
6572
gluster_peer_count: 1,
66-
gluster_peer_list: 'peer2.example.com'
73+
gluster_peer_list: 'peer2.example',
74+
fqdn: 'peer99.example.com'
6775
}
6876
end
6977

@@ -75,7 +83,8 @@
7583
{
7684
gluster_binary: '/usr/sbin/gluster',
7785
gluster_peer_count: 2,
78-
gluster_peer_list: 'peer2.example.com,peer3.example.com'
86+
gluster_peer_list: 'peer2.example,peer3.example',
87+
fqdn: 'peer99.example.com'
7988
}
8089
end
8190

@@ -84,7 +93,7 @@
8493
end
8594
end
8695

87-
describe 'self joining (fqdn matches resource title)' do
96+
describe 'self joining' do
8897
let(:facts) do
8998
{
9099
gluster_binary: '/usr/sbin/gluster',
@@ -99,4 +108,19 @@
99108
is_expected.not_to contain_exec('gluster peer probe peer1.example.com')
100109
end
101110
end
111+
112+
describe 'use custom host name (not fqdn)' do
113+
let(:title) { 'peer1.example' }
114+
let(:facts) do
115+
{
116+
gluster_binary: '/usr/sbin/gluster',
117+
gluster_peer_count: 0,
118+
gluster_peer_list: '',
119+
fqdn: 'peer99.example.com'
120+
}
121+
end
122+
123+
it { is_expected.to compile }
124+
it { is_expected.to contain_exec('gluster peer probe peer1.example') }
125+
end
102126
end

spec/defines/volume_spec.rb

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,4 +94,17 @@
9494
end
9595
end
9696
end
97+
98+
describe 'single node' do
99+
let(:facts) do
100+
{
101+
gluster_binary: '/usr/sbin/gluster',
102+
gluster_peer_count: 0,
103+
gluster_peer_list: ''
104+
}
105+
end
106+
107+
it { is_expected.to compile.with_all_deps }
108+
it { is_expected.to contain_exec("gluster create volume #{title}") }
109+
end
97110
end

0 commit comments

Comments
 (0)