Skip to content

Commit

Permalink
Add physical_volume and filesystems, remove logical volume resizing.
Browse files Browse the repository at this point in the history
Basic functions should be in for all providers, removed logical volume
resizing for the moment (it also requires filesystem unmounting,
resizing, and remounting); for MVP testing purposes it makes sense to
get this out in create/destroy form first (size is only one of many
possible options that we may want to support as modifiable properties).
  • Loading branch information
Bruce Williams committed Jan 27, 2010
1 parent 1df8f51 commit 1918167
Show file tree
Hide file tree
Showing 21 changed files with 189 additions and 138 deletions.
26 changes: 26 additions & 0 deletions lib/puppet/provider/filesystem/lvm.rb
@@ -0,0 +1,26 @@
Puppet::Type.type(:filesystem).provide :lvm do
desc "Manages LVM volume groups"

commands :df => 'df'

def create(new_fstype)
mkfs(new_fstype)
end

def fstype
info[@resource[:name]]['fstype']
end

def mkfs(new_fstype)
execute ["mkfs.#{new_fstype}", @resource[:name]]
end

def info
fields = %w(fstype size used avail used_percentage mounted)
df('-h', '-T').split(/\n/)[1..-1].inject({}) do |records, line|
parts = line.split(/\s+/)
records[parts.shift] = Hash[*(fields.zip(parts).flatten)]
records
end
end
end
32 changes: 6 additions & 26 deletions lib/puppet/provider/logical_volume/lvm.rb
@@ -1,5 +1,4 @@
Puppet::Type.type(:logical_volume).provide :lvm do

desc "Manages LVM logical volumes"

commands :lvcreate => 'lvcreate',
Expand All @@ -8,11 +7,13 @@
:umount => 'umount',
:mount => 'mount'

confine :kernel => :linux
defaultfor :kernel => :linux

def create
lvcreate('-n', @resource[:name], '--size', @resource.should(:size), @resource[:volume_group])
args = ['-n', @resource[:name]]
if @resource[:size]
args.push('--size', @resource[:size])
end
args << @resource[:volume_group]
lvcreate(*args)
end

def destroy
Expand All @@ -23,30 +24,9 @@ def exists?
lvs(@resource[:name])
end

# The LV should be unmounted, resized, the filesystem resized,
# and then the LV be re-mounted.
def size=(new_size)
umount(path)
lvextend('--size', new_size, path)
# TODO: This is when the filesystem should be resized. -BW
mount(path)
end

def size
lines = lvs('-o', 'lv_name,vg_name,lv_size', '--separator', ',', @resource[:volume_group])
lines.each do |line|
lv, vg, current_size = line.split(',')
if lv == @resource[:name] && vg == @resource[:volume_group]
return current_size # TODO: Investigate formats -BW
end
end
nil
end

private

def path
"/dev/#{@resource[:volume_group]}/#{@resource[:name]}"
end

end
13 changes: 13 additions & 0 deletions lib/puppet/provider/physical_volume/lvm.rb
@@ -0,0 +1,13 @@
Puppet::Type.type(:physical_volume).provide(:lvm) do
desc "Manages LVM physical volumes"

commands :pvcreate => 'pvcreate', :pvdestroy => 'pvdestroy'

def create
pvcreate(@resource[:name])
end

def destroy
pvdestroy(@resource[:name])
end
end
4 changes: 0 additions & 4 deletions lib/puppet/provider/volume_group/lvm.rb
@@ -1,5 +1,4 @@
Puppet::Type.type(:volume_group).provide :lvm do

desc "Manages LVM volume groups"

commands :vgcreate => 'vgcreate',
Expand All @@ -8,9 +7,6 @@
:vgextend => 'vgextend',
:vgreduce => 'vgreduce'

confine :kernel => :linux
defaultfor :kernel => :linux

def create
vgcreate(@resource[:name], *@resource.should(:physical_volumes))
end
Expand Down
22 changes: 11 additions & 11 deletions lib/puppet/type/filesystem.rb
@@ -1,6 +1,17 @@
require 'pathname'

Puppet::Type.newtype(:filesystem) do
desc "The filesystem type"

ensurable do
newvalue(/^\w+$/, :event => :created_filesystem) do |fstype|
provider.create(fstype)
end

def insync?(desired_fstype)
provider.fstype == desired_fstype
end
end

newparam(:name) do
validate do |value|
Expand All @@ -10,18 +21,7 @@
end
end

newparam(:fstype) do
desc "The filesystem type. Valid values depend on the operating system."
end

newparam(:size) do
desc "The size of the logical volume. Set to undef to use all available space."
end

ensurable

autorequire :logical_volume do
[[:logical_volume, File.basename(self[:name])]]
end

end
6 changes: 2 additions & 4 deletions lib/puppet/type/logical_volume.rb
@@ -1,4 +1,5 @@
Puppet::Type.newtype(:logical_volume) do
ensurable

newparam(:name) do
desc "The name of the logical volume. This is the unqualified name and will be
Expand All @@ -17,10 +18,7 @@
volume_group resource type."
end

newproperty(:size) do
newparam(:size) do
desc "The size of the logical volume. Set to undef to use all available space"
end

ensurable

end
3 changes: 1 addition & 2 deletions lib/puppet/type/physical_volume.rb
@@ -1,6 +1,7 @@
require 'pathname'

Puppet::Type.newtype(:physical_volume) do
ensurable

newparam(:name) do
validate do |value|
Expand All @@ -9,6 +10,4 @@
end
end
end

ensurable
end
5 changes: 1 addition & 4 deletions lib/puppet/type/volume_group.rb
@@ -1,5 +1,5 @@
Puppet::Type.newtype(:volume_group) do
@depthfirst = true
ensurable

newparam(:name) do
desc "The name of the volume group."
Expand All @@ -11,12 +11,9 @@
using the physical_volume resource type."
end

ensurable

autorequire :physical_volume do
self[:physical_volumes].collect do |pv|
[:physical_volume, pv]
end
end

end
2 changes: 1 addition & 1 deletion manifests/init.pp
Expand Up @@ -2,5 +2,5 @@
physical_volume { $pv: ensure => present }
volume_group { $vg: ensure => present, physical_volume => $pv }
logical_volume { $lv: ensure => present, volume_group => $vg, size => $size }
filesystem { "/dev/${vg}/${lv}": fstype => $fstype, size => $size }
filesystem { "/dev/${vg}/${lv}": ensure => $fstype, logical_volume => $lv }
}
6 changes: 6 additions & 0 deletions spec/fixtures/#df.txt#
@@ -0,0 +1,6 @@
Filesystem Type Size Used Avail Use% Mounted on
/dev/sda1 ext3 19G 1.8G 16G 10% /
tmpfs tmpfs 257M 0 257M 0% /lib/init/rw
varrun tmpfs 257M 44K 257M 1% /var/run
varlock tmpfs 257M 0 257M 0% /var/lock
x
1 change: 1 addition & 0 deletions spec/fixtures/.#df.txt
5 changes: 5 additions & 0 deletions spec/fixtures/df.txt
@@ -0,0 +1,5 @@
Filesystem Type Size Used Avail Use% Mounted on
/dev/sda1 ext3 19G 1.8G 16G 10% /
tmpfs tmpfs 257M 0 257M 0% /lib/init/rw
varrun tmpfs 257M 44K 257M 1% /var/run
varlock tmpfs 257M 0 257M 0% /var/lock
6 changes: 6 additions & 0 deletions spec/lib/helpers.rb
@@ -1,4 +1,6 @@
module Helpers

TEST_DIR = Pathname.new(__FILE__).parent + '..'

TYPES = {
:pv => :physical_volume,
Expand Down Expand Up @@ -50,5 +52,9 @@ def stub_default_provider!
provider = @type.provider(:lvm)
@type.stubs(:defaultprovider => provider)
end

def fixture(name, ext = '.txt')
(TEST_DIR + 'fixtures' + "#{name}#{ext}").read
end

end
33 changes: 33 additions & 0 deletions spec/unit/puppet/provider/filesystem/lvm.rb
@@ -0,0 +1,33 @@
Dir.chdir(File.dirname(__FILE__)) { (s = lambda { |f| File.exist?(f) ? require(f) : Dir.chdir("..") { s.call(f) } }).call("spec/spec_helper.rb") }

provider_class = Puppet::Type.type(:filesystem).provider(:lvm)

describe provider_class do
before do
@resource = stub("resource")
@provider = provider_class.new(@resource)
end

describe 'when creating' do
it "should execute the correct filesystem command" do
@resource.expects(:[]).with(:name).returns('/dev/myvg/mylv')
@provider.expects(:execute).with(['mkfs.ext3', '/dev/myvg/mylv'])
@provider.create('ext3')
end
end

describe "when collecting info" do
it "should parse the output of 'df'" do
@provider.expects(:df).returns(fixture(:df))
info = @provider.info
info['/dev/sda1'].should == {
'fstype' => 'ext3',
'size' => '19G',
'used' => '1.8G',
'avail' => '16G',
'used_percentage' => '10%',
'mounted' => '/'
}
end
end
end
51 changes: 17 additions & 34 deletions spec/unit/puppet/provider/logical_volume/lvm.rb
Expand Up @@ -3,19 +3,29 @@
provider_class = Puppet::Type.type(:logical_volume).provider(:lvm)

describe provider_class do

before do
@resource = stub("resource")
@provider = provider_class.new(@resource)
end

describe 'when creating' do
it "should execute 'lvcreate'" do
@resource.expects(:[]).with(:name).returns('mylv')
@resource.expects(:[]).with(:volume_group).returns('myvg')
@resource.expects(:should).with(:size).returns('1g')
@provider.expects(:lvcreate).with('-n', 'mylv', '--size', '1g', 'myvg')
@provider.create
context 'with size' do
it "should execute 'lvcreate' with a '--size' option" do
@resource.expects(:[]).with(:name).returns('mylv')
@resource.expects(:[]).with(:volume_group).returns('myvg')
@resource.expects(:[]).with(:size).returns('1g').at_least_once
@provider.expects(:lvcreate).with('-n', 'mylv', '--size', '1g', 'myvg')
@provider.create
end
end
context 'without size' do
it "should execute 'lvcreate' without a '--size' option" do
@resource.expects(:[]).with(:name).returns('mylv')
@resource.expects(:[]).with(:volume_group).returns('myvg')
@resource.expects(:[]).with(:size).returns(nil).at_least_once
@provider.expects(:lvcreate).with('-n', 'mylv', 'myvg')
@provider.create
end
end
end

Expand All @@ -27,31 +37,4 @@
@provider.destroy
end
end

describe "when changing the size" do

describe "to a defined value" do

it "should call lvextend and related commands" do
@resource.expects(:[]).with(:volume_group).returns('myvg').at_least_once
@resource.expects(:[]).with(:name).returns('mylv').at_least_once
@provider.expects(:umount).with('/dev/myvg/mylv')
@provider.expects(:lvextend).with('--size', '2g', '/dev/myvg/mylv')
@provider.expects(:mount).with('/dev/myvg/mylv')
# TODO: Test filesystem resizing?
@provider.size = '2g'
end

end

describe "to undef" do

it "should extend the logical volume to fill all avialable space" do
pending
end

end

end

end
26 changes: 26 additions & 0 deletions spec/unit/puppet/provider/physical_volume/lvm.rb
@@ -0,0 +1,26 @@
Dir.chdir(File.dirname(__FILE__)) { (s = lambda { |f| File.exist?(f) ? require(f) : Dir.chdir("..") { s.call(f) } }).call("spec/spec_helper.rb") }

provider_class = Puppet::Type.type(:physical_volume).provider(:lvm)

describe provider_class do
before do
@resource = stub("resource")
@provider = provider_class.new(@resource)
end

describe 'when creating' do
it "should execute the 'pvcreate'" do
@resource.expects(:[]).with(:name).returns('/dev/hdx')
@provider.expects(:pvcreate).with('/dev/hdx')
@provider.create
end
end

describe 'when destroying' do
it "should execute 'pvdestroy'" do
@resource.expects(:[]).with(:name).returns('/dev/hdx')
@provider.expects(:pvdestroy).with('/dev/hdx')
@provider.destroy
end
end
end
3 changes: 0 additions & 3 deletions spec/unit/puppet/provider/volume_group/lvm.rb
Expand Up @@ -3,7 +3,6 @@
provider_class = Puppet::Type.type(:volume_group).provider(:lvm)

describe provider_class do

before do
@resource = stub("resource")
@provider = provider_class.new(@resource)
Expand All @@ -25,6 +24,4 @@
@provider.destroy
end
end


end

0 comments on commit 1918167

Please sign in to comment.