Skip to content

Commit dbad802

Browse files
author
Dylan Ratcliffe
committedSep 4, 2018
Fixed instanced method for logical_volumes, enables puppet resource
·
v3.0.11.1.0
1 parent 120218a commit dbad802

File tree

4 files changed

+42
-13
lines changed

4 files changed

+42
-13
lines changed
 

‎lib/puppet/provider/logical_volume/lvm.rb

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,10 @@
2121
def self.instances
2222
get_logical_volumes.collect do |logical_volumes_line|
2323
logical_volumes_properties = get_logical_volume_properties(logical_volumes_line)
24-
new(logical_volumes_properties)
24+
instance = new(logical_volumes_properties)
25+
# Save the volume group in the provider so the type can find it
26+
instance.volume_group = logical_volumes_properties[:volume_group]
27+
instance
2528
end
2629
end
2730

@@ -44,13 +47,21 @@ def self.get_logical_volume_properties(logical_volumes_line)
4447
output_array = logical_volumes_line.gsub(/\s+/m, ' ').strip.split(" ")
4548

4649
# Assign properties based on headers
47-
# Just doing name for now...
48-
logical_volumes_properties[:ensure] = :present
49-
logical_volumes_properties[:name] = output_array[0]
50+
logical_volumes_properties[:ensure] = :present
51+
logical_volumes_properties[:name] = output_array[0]
52+
logical_volumes_properties[:volume_group] = output_array[1]
5053

5154
logical_volumes_properties
5255
end
5356

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

@@ -146,12 +157,23 @@ def destroy
146157
end
147158

148159
def exists?
149-
lvs(@resource[:volume_group]) =~ lvs_pattern
160+
begin
161+
lvs(@resource[:volume_group]) =~ /#{@resource[:name]}/
Code has comments. Press enter to view.
162+
rescue Puppet::ExecutionFailure
163+
# lvs fails if we give it an empty volume group name, as would
164+
# happen if we were running `puppet resource`. This should be
165+
# interpreted as "The resource does not exist"
166+
nil
167+
end
150168
end
151169

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

157179
raw = lvs('--noheading', '--unit', unit, path)
@@ -301,9 +323,6 @@ def mirrorlog=( new_mirror_log_location )
301323
end
302324
end
303325

304-
305-
306-
307326
private
308327

309328
def lvs_pattern

‎lib/puppet/type/logical_volume.rb

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,19 @@
1313
raise ArgumentError, "Volume names must be entirely unqualified"
1414
end
1515
end
16+
17+
# This is a bit of a hack, drags the volume_group up from the provider if
18+
# it was specified, allowing `puppet resource` and other things that rely
19+
# on an indirector search to work
20+
munge do |name|
21+
if @resource.original_parameters[:provider].respond_to? :volume_group
22+
@resource[:volume_group] = @resource.original_parameters[:provider].volume_group
23+
end
24+
name
25+
end
1626
end
1727

18-
newparam(:volume_group) do
28+
newproperty(:volume_group) do
1929
desc "The volume group name associated with this logical volume. This will automatically
2030
set this volume group as a dependency, but it must be defined elsewhere using the
2131
volume_group resource type."

‎spec/unit/classes/lvm_spec.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
'backup' => {
2222
'size' => '5G',
2323
'mountpath' => '/var/backups',
24-
'mountpath_require' => true
24+
'mountpath_require' => false
2525
}
2626
}
2727
}
@@ -62,7 +62,7 @@
6262
'size' => '5G',
6363
'mounted' => false,
6464
'mountpath' => '/mnt/not_mounted',
65-
'mountpath_require' => true
65+
'mountpath_require' => false
6666
}
6767
}
6868
}

‎spec/unit/puppet/provider/logical_volume/lvm_spec.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,8 @@
3636
expect(@provider.exists?).to be > 10
3737
end
3838
it "should return 'nil', lv 'data' in vg 'myvg' does not exist" do
39-
@resource.expects(:[]).with(:name).returns('data')
4039
@resource.expects(:[]).with(:volume_group).returns('myvg').at_least_once
41-
@provider.class.stubs(:lvs).with('myvg').returns(lvs_output)
40+
@provider.class.stubs(:lvs).with('myvg').raises(Puppet::ExecutionFailure, 'Execution of \'/sbin/lvs myvg\' returned 5')
4241
expect(@provider.exists?).to be_nil
4342
end
4443
end
@@ -205,6 +204,7 @@
205204
@resource.expects(:[]).with(:volume_group).returns('myvg').at_least_once
206205
@resource.expects(:[]).with(:size).returns('1g').at_least_once
207206
@resource.expects(:[]).with(:thinpool).returns('mythinpool').at_least_once
207+
@provider.expects(:blkid).with('/dev/myvg/mylv').returns('TYPE=ext4')
208208
@provider.expects(:lvcreate).with('-n', 'mylv', '--virtualsize', '1g', '--thin', 'myvg/mythinpool')
209209
@provider.create
210210
@provider.expects(:lvs).with('--noheading', '--unit', 'g', '/dev/myvg/mylv').returns(' 1.00g').at_least_once

0 commit comments

Comments
 (0)
Please sign in to comment.