Skip to content

Commit 59b4813

Browse files
authored
Merge pull request #215 from dylanratcliffe/add_facts
Add facts, functions, tasks, plans
2 parents 898a427 + ed08924 commit 59b4813

34 files changed

+1476
-32
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,5 @@
33
Gemfile.lock
44
.bundle/
55
.fixtures/modules
6-
.fixtures/manifests
6+
.fixtures/manifests
7+
spec/fixtures

README.md

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -287,6 +287,40 @@ parameters documented above also apply to AIX systems.
287287
* range (Parameter) - Sets the inter-physical volume allocation policy. AIX only
288288
* type (Parameter) - Configures the logical volume type. AIX only
289289

290+
## Functions
291+
292+
`lvm::bytes_to_size`: Converts a number of bytes to a size format that LVM can understand e.g. `lvm::bytes_to_size(214748364800)` would return `200g`
293+
294+
`lvm::size_to_bytes`: Converts an LVM size to bytes, the opposite of `lvm::bytes_to_size`.
295+
296+
## Tasks
297+
298+
Documented in the ["Tasks" tab](https://forge.puppet.com/puppetlabs/lvm/tasks) on the forge
299+
300+
## Plans
301+
302+
### `lvm::expand`
303+
304+
Executes common tasks for expanding mounts, this can include:
305+
306+
* Creating PVs
307+
* Adding PVs to VG
308+
* Extending LV
309+
* Extending filesystem
310+
311+
**Parameters:**
312+
313+
`server`: *String* The target for the plan
314+
315+
`volume_group`: *String* The volume group to which the logical volume belongs
316+
317+
`logical_volume`: *String* The logical volume which is to be expanded
318+
319+
`additional_size`: *String* How much size to add to the LV. This should be specified in LVM format i.e. "200m" or "2.5g"
320+
321+
`disks`: *Array[String]* Any physical disks that should be added to the volume group as part of the expand process
322+
323+
`resize_fs`: *Boolean* Whether or not to resize the filesystem
290324

291325
## Limitations
292326

Rakefile

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,5 @@ require 'puppet-lint/tasks/puppet-lint'
44
PuppetLint.configuration.send('disable_80chars')
55
PuppetLint.configuration.relative = true
66
PuppetLint.configuration.ignore_paths = ["spec/**/*.pp", "pkg/**/*.pp"]
7+
PuppetSyntax.exclude_paths = ["plans/**/*.pp"]
78

8-
desc 'Run puppet in noop mode and check for syntax errors.'
9-
task :validate do
10-
Dir['manifests/**/*.pp'].each do |manifest|
11-
sh "puppet parser validate --noop #{manifest}"
12-
end
13-
Dir['spec/**/*.rb','lib/**/*.rb'].each do |ruby_file|
14-
sh "ruby -c #{ruby_file}" unless ruby_file =~ /spec\/fixtures/
15-
end
16-
Dir['templates/**/*.erb'].each do |template|
17-
sh "erb -P -x -T '-' #{template} | ruby -c"
18-
end
19-
end

functions/bytes_to_size.pp

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
function lvm::bytes_to_size (
2+
Numeric $size,
3+
) {
4+
$units = {
5+
'k' => 1024,
6+
'm' => 1048576,
7+
'g' => 1073741824,
8+
't' => 1099511627776,
9+
'p' => 1.12589991e15,
10+
'e' => 1.1529215e18,
11+
}
12+
$remaining_units = $units.filter |$name, $number| {
13+
# Run all the calculation and return only units that are greater than one
14+
$size_as_unit = $size / $number
15+
$size_as_unit >= 1
16+
}
17+
18+
# Use the last unit
19+
$largest_unit = $remaining_units.keys[-1]
20+
21+
$value = ($size / $units[$largest_unit])
22+
23+
# # Return the string
24+
"${value}${largest_unit}"
25+
}

functions/size_to_bytes.pp

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
function lvm::size_to_bytes (
2+
String $size,
3+
) {
4+
$units = {
5+
'K' => 1024,
6+
'M' => 1048576,
7+
'G' => 1073741824,
8+
'T' => 1099511627776,
9+
'P' => 1.12589991e15,
10+
'E' => 1.1529215e18,
11+
}
12+
# Check if the size is valid and if so, extract the units
13+
if $size =~ /^([0-9]+(\.[0-9]+)?)([KMGTPEkmgtpe])/ {
14+
$unit = String($3, '%u') # Store the units in uppercase
15+
$number = Float($1) # Store the number as a float
16+
17+
# Multiply the number by the units to get bytes
18+
$number * $units[$unit]
19+
} else {
20+
fail("${size} is not a valid LVM size")
21+
}
22+
}

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/provider/logical_volume/lvm.rb

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,10 @@
2323
def self.instances
2424
get_logical_volumes.collect do |logical_volumes_line|
2525
logical_volumes_properties = get_logical_volume_properties(logical_volumes_line)
26-
new(logical_volumes_properties)
26+
instance = new(logical_volumes_properties)
27+
# Save the volume group in the provider so the type can find it
28+
instance.volume_group = logical_volumes_properties[:volume_group]
29+
instance
2730
end
2831
end
2932

@@ -46,13 +49,21 @@ def self.get_logical_volume_properties(logical_volumes_line)
4649
output_array = logical_volumes_line.gsub(/\s+/m, ' ').strip.split(" ")
4750

4851
# Assign properties based on headers
49-
# Just doing name for now...
50-
logical_volumes_properties[:ensure] = :present
51-
logical_volumes_properties[:name] = output_array[0]
52+
logical_volumes_properties[:ensure] = :present
53+
logical_volumes_properties[:name] = output_array[0]
54+
logical_volumes_properties[:volume_group] = output_array[1]
5255

5356
logical_volumes_properties
5457
end
5558

59+
# Just assume that the volume group is correct, we don't support changing
60+
# it anyway.
61+
attr_writer :volume_group
62+
63+
def volume_group
64+
@resource ? @resource[:volume_group] : @volume_group
65+
end
66+
5667
def create
5768
args = []
5869

@@ -148,12 +159,23 @@ def destroy
148159
end
149160

150161
def exists?
151-
lvs(@resource[:volume_group]) =~ lvs_pattern
162+
begin
163+
lvs(@resource[:volume_group]) =~ /#{@resource[:name]}/
164+
rescue Puppet::ExecutionFailure
165+
# lvs fails if we give it an empty volume group name, as would
166+
# happen if we were running `puppet resource`. This should be
167+
# interpreted as "The resource does not exist"
168+
nil
169+
end
152170
end
153171

154172
def size
155173
if @resource[:size] =~ /^\d+\.?\d{0,2}([KMGTPE])/i
156174
unit = $1.downcase
175+
else
176+
# If we are getting the size initially we don't know what the
177+
# units will be, default to GB
178+
unit = 'g'
157179
end
158180

159181
raw = lvs('--noheading', '--unit', unit, path)
@@ -303,9 +325,6 @@ def mirrorlog=( new_mirror_log_location )
303325
end
304326
end
305327

306-
307-
308-
309328
private
310329

311330
def lvs_pattern

0 commit comments

Comments
 (0)