Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

converting libvirt::network to a type, step 1 #25

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
110 changes: 110 additions & 0 deletions lib/puppet/provider/libvirt_network/virsh.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
require 'rexml/document'
require 'tempfile'

Puppet::Type.type(:libvirt_network).provide(:virsh) do

commands :virsh => 'virsh'

def self.instances
networks = []
hash = {}
list = virsh '-q', 'net-list', '--all'
list.split(/\n/)[0..-1].map do |line|
values = line.split(/ +/)
hash = {
:name => values[0],
:active => values[1].match(/^act/)? :true : :false,
:autostart => values[2].match(/no/) ? :false : :true,
:provider => self.name
}
networks << new(hash)
hash = {}
end
return networks
end

def status
list = virsh '-q', 'net-list', '--all'
list.split(/\n/)[0..-1].detect do |line|
fields = line.split(/ +/)
if (fields[0].match(/^#{resource[:name]}$/))
return :present
end
end
return :absent

end

def self.prefetch(resources)
networks = instances
resources.keys.each do |name|
if provider = networks.find{ |network| network.name == name}
resources[name].provider = provider
end
end
end

def destroy
self.destroy_network
@property_hash.clear
end

def defineNetwork
result = false
begin
tmpFile = Tempfile.new("network.#{resource[:name]}")
xml = createNetworkXML resource
tmpFile.write(xml)
tmpFile.rewind
virsh 'net-define', tmpFile.path
result = true
ensure
tmpFile.close
tmpFile.unlink
end
return result
end

def destroy_network
begin
virsh 'net-destroy', resource[:name]
rescue Puppet::ExecutionFailure => e
notice(e.message)
end
virsh 'net-undefine', resource[:name]
end

def active
@property_hash[:active] || :false
end

def active=(active)
if (active == :true)
virsh 'net-start', '--network', resource[:name]
@property_hash[:active] = 'true'
else
virsh 'net-destroy', '--network', resource[:name]
@property_hash[:active] = 'false'
end
end

def autostart
@property_hash[:autostart] || :false
end

def autostart=(autostart)
if (autostart == :true)
virsh 'net-autostart', '--network', resource[:name]
@property_hash[:autostart] = :true
else
virsh 'net-autostart', '--network', resource[:name], '--disable'
@property_hash[:autostart] = :false
end
end


def exists?
@property_hash[:ensure] != :absent
end

end
73 changes: 73 additions & 0 deletions lib/puppet/type/libvirt_network.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
Puppet::Type.newtype(:libvirt_network) do
@doc = %q{Manages libvirt networks

Example :
libvirt_network { 'default' :
ensure => absent
}


$dhcp = {
'start' => '192.168.122.2',
'end' => '192.168.122.254',
'bootp_file' => 'pxelinux.0',
}
$ip = {
'address' => '192.168.122.1',
'netmask' => '255.255.255.0',
'dhcp' => $dhcp,
}
libvirt_network { 'pxe' :
ensure => present,
active => true,
autostart => true,
forward_mode => 'nat',
forward_dev => 'virbr0',
ip => [ $ip ],
}

}

ensurable do

desc 'Manages the creation or the removal of a network
`present` means that the network will be defined and created
`absent` means that the network will be purged from the system'

defaultto(:present)
newvalue(:present) do
provider.create
end

newvalue(:absent) do
if (provider.exists?)
provider.destroy
end
end

def retrieve
provider.status
end

end

newparam(:name, :namevar => true) do
desc 'The network name.'
newvalues(/^\S+$/)
end

newproperty(:active) do
desc 'Whether the network should be started.'
defaultto(:true)
newvalues(:true)
newvalues(:false)
end

newproperty(:autostart) do
desc 'Whether the network should be autostarted.'
defaultto(:false)
newvalues(:true)
newvalues(:false)
end

end