Skip to content

Commit aa5bb5c

Browse files
author
Dylan Ratcliffe
committed
Added structured facts
1 parent dbad802 commit aa5bb5c

File tree

8 files changed

+391
-6
lines changed

8 files changed

+391
-6
lines changed

lib/facter/logical_volumes.rb

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
Facter.add(:logical_volumes) do
2+
# Fact should be confined to only linux servers that have the lvs command
3+
confine do
4+
Facter.value('kernel') == 'Linux' &&
5+
Facter::Core::Execution.which('lvs')
6+
end
7+
8+
setcode do
9+
# Require the helper methods to reduce duplication
10+
require 'puppet_x/lvm/output'
11+
12+
# List columns here that can be passed to the lvs -o command. Dont't
13+
# include things in here that might be bland as we currently can't deal
14+
# with them
15+
columns = [
16+
'lv_uuid',
17+
'lv_name',
18+
'lv_full_name',
19+
'lv_path',
20+
'lv_dm_path',
21+
'lv_attr',
22+
'lv_layout',
23+
'lv_role',
24+
'lv_active',
25+
'lv_size',
26+
'lv_permissions',
27+
]
28+
29+
output = Facter::Core::Execution.exec("lvs -o #{columns.join(',')} --noheading --nosuffix")
30+
Puppet_X::LVM::Output.parse('lv_name', columns, output)
31+
end
32+
end

lib/facter/lvm_support.rb

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,8 @@
1515
Facter.add('lvm_vgs') do
1616
confine :lvm_support => true
1717

18-
if Facter.value(:lvm_support)
19-
vgs = Facter::Core::Execution.execute('vgs -o name --noheadings 2>/dev/null', timeout: 30)
20-
end
18+
vgs = Facter::Core::Execution.execute('vgs -o name --noheadings 2>/dev/null', timeout: 30)
19+
2120
if vgs.nil?
2221
setcode { 0 }
2322
else
@@ -48,9 +47,8 @@
4847
Facter.add('lvm_pvs') do
4948
confine :lvm_support => true
5049

51-
if Facter.value(:lvm_support)
52-
pvs = Facter::Core::Execution.execute('pvs -o name --noheadings 2>/dev/null', timeout: 30)
53-
end
50+
pvs = Facter::Core::Execution.execute('pvs -o name --noheadings 2>/dev/null', timeout: 30)
51+
5452
if pvs.nil?
5553
setcode { 0 }
5654
else

lib/facter/physical_volumes.rb

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
Facter.add(:physical_volumes) do
2+
# Fact should be confined to only linux servers that have the lvs command
3+
confine do
4+
Facter.value('kernel') == 'Linux' &&
5+
Facter::Core::Execution.which('pvs')
6+
end
7+
8+
setcode do
9+
# Require the helper methods to reduce duplication
10+
require 'puppet_x/lvm/output'
11+
12+
# List columns here that can be passed to the lvs -o command. Dont't
13+
# include things in here that might be bland as we currently can't deal
14+
# with them
15+
columns = [
16+
'pv_uuid',
17+
'dev_size',
18+
'pv_name',
19+
'pe_start',
20+
'pv_size',
21+
'pv_free',
22+
'pv_used',
23+
'pv_attr',
24+
'pv_pe_count',
25+
'pv_pe_alloc_count',
26+
'pv_mda_count',
27+
'pv_mda_used_count',
28+
'pv_ba_start',
29+
'pv_ba_size',
30+
]
31+
32+
output = Facter::Core::Execution.exec("pvs -o #{columns.join(',')} --noheading --nosuffix")
33+
Puppet_X::LVM::Output.parse('pv_name', columns, output)
34+
end
35+
end

lib/facter/volume_groups.rb

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
Facter.add(:volume_groups) do
2+
# Fact should be confined to only linux servers that have the lvs command
3+
confine do
4+
Facter.value('kernel') == 'Linux' &&
5+
Facter::Core::Execution.which('vgs')
6+
end
7+
8+
setcode do
9+
# Require the helper methods to reduce duplication
10+
require 'puppet_x/lvm/output'
11+
12+
# List columns here that can be passed to the lvs -o command. Dont't
13+
# include things in here that might be bland as we currently can't deal
14+
# with them
15+
columns = [
16+
'vg_uuid',
17+
'vg_name',
18+
'vg_attr',
19+
'vg_permissions',
20+
'vg_allocation_policy',
21+
'vg_size',
22+
'vg_free',
23+
]
24+
25+
output = Facter::Core::Execution.exec("vgs -o #{columns.join(',')} --noheading --nosuffix")
26+
Puppet_X::LVM::Output.parse('vg_name', columns, output)
27+
end
28+
end

lib/puppet_x/lvm/output.rb

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
module Puppet_X
2+
module LVM
3+
class Output
4+
# Parses the results of LVMs commands. This does not handle when columns
5+
# have no data and therefore these columns should be avoided. It returns
6+
# the data with the prefix removed i.e. "lv_free" would be come "free"
7+
# this however doesn't descriminate and will turn something like
8+
# "foo_bar" into "bar"
9+
def self.parse(key,columns,data)
10+
results = {}
11+
12+
# Remove prefixes
13+
columns = remove_prefixes(columns)
14+
key = remove_prefix(key)
15+
16+
data.split("\n").each do |line|
17+
parsed_line = line.gsub(/\s+/, ' ').strip.split(' ')
18+
values = Hash[columns.zip(parsed_line)]
19+
current_key = values[key]
20+
values.delete(key)
21+
results[current_key] = values
22+
end
23+
24+
results
25+
end
26+
27+
def self.remove_prefixes(array)
28+
array.map do |item|
29+
remove_prefix(item)
30+
end
31+
end
32+
33+
def self.remove_prefix(item)
34+
item.gsub(/^[A-Za-z]+_/,'')
35+
end
36+
end
37+
end
38+
end
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
require 'spec_helper'
2+
3+
describe 'logical_volumes fact' do
4+
before :each do
5+
Facter.clear
6+
end
7+
8+
context 'when not on Linux' do
9+
it 'should be set to nil' do
10+
Facter.fact(:kernel).expects(:value).at_least(1).returns('SunOs')
11+
Facter.value(:logical_volumes).should be_nil
12+
end
13+
end
14+
15+
context 'when on Linux' do
16+
before :each do
17+
Facter.fact(:kernel).expects(:value).at_least(1).returns('Linux')
18+
end
19+
20+
context 'when lvs is absent' do
21+
before :each do
22+
Facter::Core::Execution.stubs('exec') # All other calls
23+
Facter::Core::Execution.expects('which').with('lvs').at_least(1).returns(nil)
24+
end
25+
26+
it 'should be set to nil' do
27+
Facter.value(:logical_volumes).should be_nil
28+
end
29+
end
30+
31+
context 'when lvs is present' do
32+
before :each do
33+
Facter::Core::Execution.stubs('exec') # All other calls
34+
Facter::Core::Execution.expects('which').with('lvs').returns('/sbin/lvs')
35+
end
36+
37+
it 'should be able to resolve VGs' do
38+
lvs_output = <<~OUTPUT
39+
E7qan8-4NGf-jq2P-l11v-6fFe-MPHK-T6IGzl root centos/root /dev/centos/root /dev/mapper/centos-root -wi-ao---- linear public active 18.46g writeable
40+
buUXDX-GDUh-rN2t-y80n-vtCt-xhhu-XSZ5kA swap centos/swap /dev/centos/swap /dev/mapper/centos-swap -wi-ao---- linear public active 1.00g writeable
41+
uedsry-OTVv-wGW4-vaFf-c7IY-oH6Z-ig6IXB cool_tasks tasks/cool_tasks /dev/tasks/cool_tasks /dev/mapper/tasks-cool_tasks -wi-a----- linear public active 800.00m writeable
42+
gmNS3G-cAhA-vRj0-2Uf0-21yO-QVdy-LNXfBv lame_tasks tasks/lame_tasks /dev/tasks/lame_tasks /dev/mapper/tasks-lame_tasks -wi-a----- linear public active 400.00m writeable
43+
OUTPUT
44+
Facter::Core::Execution.expects(:exec).at_least(1).returns(lvs_output)
45+
Facter.value(:logical_volumes).should include({
46+
"cool_tasks" => {
47+
"uuid" => "uedsry-OTVv-wGW4-vaFf-c7IY-oH6Z-ig6IXB",
48+
"full_name" => "tasks/cool_tasks",
49+
"path" => "/dev/tasks/cool_tasks",
50+
"dm_path" => "/dev/mapper/tasks-cool_tasks",
51+
"attr" => "-wi-a-----",
52+
"layout" => "linear",
53+
"role" => "public",
54+
"active" => "active",
55+
"size" => "800.00m",
56+
"permissions" => "writeable"
57+
},
58+
"lame_tasks" => {
59+
"uuid" => "gmNS3G-cAhA-vRj0-2Uf0-21yO-QVdy-LNXfBv",
60+
"full_name" => "tasks/lame_tasks",
61+
"path" => "/dev/tasks/lame_tasks",
62+
"dm_path" => "/dev/mapper/tasks-lame_tasks",
63+
"attr" => "-wi-a-----",
64+
"layout" => "linear",
65+
"role" => "public",
66+
"active" => "active",
67+
"size" => "400.00m",
68+
"permissions" => "writeable"
69+
},
70+
"root" => {
71+
"uuid" => "E7qan8-4NGf-jq2P-l11v-6fFe-MPHK-T6IGzl",
72+
"full_name" => "centos/root",
73+
"path" => "/dev/centos/root",
74+
"dm_path" => "/dev/mapper/centos-root",
75+
"attr" => "-wi-ao----",
76+
"layout" => "linear",
77+
"role" => "public",
78+
"active" => "active",
79+
"size" => "18.46g",
80+
"permissions" => "writeable"
81+
},
82+
"swap" => {
83+
"uuid" => "buUXDX-GDUh-rN2t-y80n-vtCt-xhhu-XSZ5kA",
84+
"full_name" => "centos/swap",
85+
"path" => "/dev/centos/swap",
86+
"dm_path" => "/dev/mapper/centos-swap",
87+
"attr" => "-wi-ao----",
88+
"layout" => "linear",
89+
"role" => "public",
90+
"active" => "active",
91+
"size" => "1.00g",
92+
"permissions" => "writeable"
93+
},
94+
})
95+
end
96+
end
97+
end
98+
end
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
require 'spec_helper'
2+
3+
describe 'physical_volumes fact' do
4+
before :each do
5+
Facter.clear
6+
end
7+
8+
context 'when not on Linux' do
9+
it 'should be set to nil' do
10+
Facter.fact(:kernel).expects(:value).at_least(1).returns('SunOs')
11+
Facter.value(:physical_volumes).should be_nil
12+
end
13+
end
14+
15+
context 'when on Linux' do
16+
before :each do
17+
Facter.fact(:kernel).expects(:value).at_least(1).returns('Linux')
18+
end
19+
20+
context 'when pvs is absent' do
21+
before :each do
22+
Facter::Core::Execution.stubs('exec') # All other calls
23+
Facter::Core::Execution.expects('which').with('pvs').at_least(1).returns(nil)
24+
end
25+
26+
it 'should be set to nil' do
27+
Facter.value(:physical_volumes).should be_nil
28+
end
29+
end
30+
31+
context 'when pvs is present' do
32+
before :each do
33+
Facter::Core::Execution.stubs('exec') # All other calls
34+
Facter::Core::Execution.expects('which').with('pvs').returns('/sbin/pvs')
35+
end
36+
37+
it 'should be able to resolve PVs' do
38+
pvs_output = <<~OUTPUT
39+
dPziSO-573Z-9WuH-q22X-cuyM-gHQx-ZeGbfK 2.00g /dev/sda 1.00m 2.00g 844.00m 1.17g a-- 511 300 1 1 0 0
40+
09ksGm-Pt28-AR9H-NlgQ-QxtG-5uEH-Qzy1RR 2.00g /dev/sdc 1.00m 2.00g 2.00g 0 a-- 511 0 1 1 0 0
41+
PpSFVZ-SS3P-n3a6-ctPF-sb9H-6M85-i0TqBv 19.51g /dev/sdd2 1.00m 19.51g 44.00m 19.46g a-- 4994 4983 1 1 0 0
42+
OUTPUT
43+
Facter::Core::Execution.expects(:exec).at_least(1).returns(pvs_output)
44+
Facter.value(:physical_volumes).should include({
45+
"/dev/sda" => {
46+
"uuid" => "dPziSO-573Z-9WuH-q22X-cuyM-gHQx-ZeGbfK",
47+
"size" => "2.00g",
48+
"start" => "1.00m",
49+
"free" => "844.00m",
50+
"used" => "1.17g",
51+
"attr" => "a--",
52+
"pe_count" => "511",
53+
"pe_alloc_count" => "300",
54+
"mda_count" => "1",
55+
"mda_used_count" => "1",
56+
"ba_start" => "0",
57+
"ba_size" => "0"
58+
},
59+
"/dev/sdc" => {
60+
"uuid" => "09ksGm-Pt28-AR9H-NlgQ-QxtG-5uEH-Qzy1RR",
61+
"size" => "2.00g",
62+
"start" => "1.00m",
63+
"free" => "2.00g",
64+
"used" => "0",
65+
"attr" => "a--",
66+
"pe_count" => "511",
67+
"pe_alloc_count" => "0",
68+
"mda_count" => "1",
69+
"mda_used_count" => "1",
70+
"ba_start" => "0",
71+
"ba_size" => "0"
72+
},
73+
"/dev/sdd2" => {
74+
"uuid" => "PpSFVZ-SS3P-n3a6-ctPF-sb9H-6M85-i0TqBv",
75+
"size" => "19.51g",
76+
"start" => "1.00m",
77+
"free" => "44.00m",
78+
"used" => "19.46g",
79+
"attr" => "a--",
80+
"pe_count" => "4994",
81+
"pe_alloc_count" => "4983",
82+
"mda_count" => "1",
83+
"mda_used_count" => "1",
84+
"ba_start" => "0",
85+
"ba_size" => "0"
86+
},
87+
})
88+
end
89+
end
90+
end
91+
end

0 commit comments

Comments
 (0)