On 4/28/14, 3:34 PM, Alex Scoble wrote:
> Hi All,
> 
> I'm working on a module that builds KVM/libvirt hosts and populates them
> with predefined VMs.
> 
> So far I have the module to where it can create any number of virtual
> nets, storage pools and volumes using virsh, but it isn't pretty.
> 
> I've read on various threads here that create_resources is not a good
> function to use. This was stated quite emphatically by R.I. Pienaar and
> others.
> 
> Already I've run into the situation where it's hard to control order in
> which two separate create_resources functions are run and I've seen some
> kludges to fix it, but I'm looking for a better way and hoping that it
> doesn't involve the use of custom types because that's currently more
> than I want to deal with.
> 
> Here's what I have and hopefully someone can help me do this in a
> cleaner way. Sorry that it's not in Github as I'm not ready to put it
> there yet.
> 
> *This is an example of the hiera data that I'm working with:*
> 
> kvm::servertype:'kvm'
> kvm::virtnet_name:'br0'
> kvm::virtnet_forwardmode:'bridge'
> kvm::virtbridge_name:'br0'
> kvm::virtnet_macaddress:'52:54:00:1F:95:6C'
> kvm::virtpool_hash:
>   default:
>     virtpool_size:'429496729601'
>   iso-images:
>     virtpool_target:'/var/lib/libvirt/iso-images'
>     virtpool_format:'iso'
> kvm::virtvol_hash:
>   dtlrazorts1.img:
>     volcapacity:'80G'
>     volformat:'qcow2'
>   dtlrepots1.img:
>     volcapacity:'80G'
>     volformat:'qcow2'
>   dtlwebvirtmants1.img:
>     volcapacity:'60G'
>     volformat:'qcow2'
> 
> *My init.pp*
> 
> class kvm (
>   $servertype          = $kvm::params::servertype,
>   $virtnet_name        = 'undef',
>   $virtnet_forwardmode = 'undef',
>   $virtbridge_name     = 'undef',
>   $virtnet_macaddress  = 'undef',
>   $virtpool_hash       = 'undef',
>   $virtvol_hash        = 'undef',
> ) inherits kvm::params {
> 
>   include kvm::fw
> 
>   if ($servertype == 'kvm') {
>     
>     file { '/var/opt/lib/pe-puppet/temp':
>       ensure => directory,
>       owner  => 'pe-puppet',
>       group  => 'pe-puppet',      
>     }
>     
>     package { ['libvirt',
>                'python-virtinst',
>                'qemu-kvm',
>                'qemu-kvm-tools',
>                'bridge-utils',
>                'virt-manager',
>                'libguestfs-tools',]:
>       ensure => present,
>     }
> 
>     service { 'libvirtd':
>       ensure     => running,
>       enable     => true,
>       hasstatus  => true,
>       hasrestart => true,
>       require    => Package['libvirt'],
>     }
>     
>     if $virtpool_hash {
>     create_resources('kvm::virtpool', $virtpool_hash)
>     }
>     include kvm::virtnet
>     if $virtvol_hash {
>         create_resources('kvm::virtvol', $virtvol_hash)
>     }
>   }
> 
>   if $servertype == 'kvmwebmgr' {
>     include kvm::kvmwebmgr
>   }
> 
> }
> 
> *The virtpool.pp file:*
> 
> define kvm::virtpool (
>   $virtpool        = $name,
>   $virtpool_size   = undef,
>   $virtpool_target = '/var/lib/libvirt/images',
>   $virtpool_type   = 'dir',
>   $virtpool_format = 'raw',
> ) {
>   
>   file { "${virtpool_target}":
>     ensure => directory,
>   } ->  
>   exec { "virsh pool-define-as ${name} --target ${virtpool_target}
> --type ${virtpool_type} --source-format ${virtpool_format} && virsh
> pool-autostart ${name} && virsh pool-start ${name}":
>     path        => '/usr/bin',
>     unless      => "virsh pool-list | /bin/grep ${name}",
> #    refreshonly => true,
>   }
>     
> }
> 
> *The virtnet.pp file:*
> 
> class kvm::virtnet inherits kvm {
>   
>   file {
> "/var/opt/lib/pe-puppet/temp/${hostname}_virtnet_${virtnet_name}.xml":
>     ensure  => file,
>     content => template('kvm/virtnet.xml.erb'),
>     owner   => 'pe-puppet',
>     group   => 'pe-puppet',
>   } ->  
>   exec { 'virsh net-destroy default && virsh net-undefine default':
>     path   => '/usr/bin',
>     onlyif => 'virsh net-list | /bin/grep default',
>   } ->  
>   exec { "virsh net-define
> /var/opt/lib/pe-puppet/temp/${hostname}_virtnet_${virtnet_name}.xml &&
> virsh net-autostart ${virtnet_name} && virsh net-start ${virtnet_name}":
>     path   => '/usr/bin',
>     unless => "virsh net-list | /bin/grep ${virtnet_name}",
>   }
>     
> }
> 
> *The virtvol.pp file:*
> 
> define kvm::virtvol (
>   $virtvol     = $name,
>   $pool        = 'default',
>   $volcapacity = '60G',
>   $volformat   = 'qcow2',
> ) {
>   
>   exec { "virsh vol-create-as ${pool} ${name} ${volcapacity} --format
> ${volformat}":
>     path   => '/usr/bin',
>     unless => "virsh vol-list ${name} | /bin/grep ${name}",
>     onlyif => "virsh pool-list ${pool} | /bin/grep ${pool}",    
>   } 
>   
> }
> 
> *The virtnet.xml.erb:*
> 
> <network>
>   <name><%= @virtnet_name %></name>
>   <forward mode='<%= @virtnet_forwardmode %>' />
>   <bridge name='<%= @virtbridge_name %>'/>
>   <mac address='<%= @virtnet_macaddress %>'/>
> </network>
> 
> -----------------------------------------------------------------------------
> end files
> -----------------------------------------------------------------------------
> 
> It would be super helpful if anyone could point me to a puppet module on
> github that presents me with a better pattern to use with the hiera data.
> 
> Any other help or criticisms are also welcome.
> 
> Thanks,
> 
> Alex

Hi,

The create_resources() function allows you to have a data driven design.
This gives you the ability to specify arbitrary data in Hiera and
decouple data from modules. It means you can change the data without the
code. I would argue that any define in your module should have a
corresponding create_resources() function, so that it can be accessed
through data in Hiera.

As for ordering, your defines should probably take care of that, though
you can use collections with chaining to help, such as

  Virt::Foo <||> -> Virt::Bar <||>

When you are using create_resources(), I recommend also specifying an
option to use hiera_hash() to collect your hashes. This does a deep hash
merge and will return values from all levels of the hierarchy. My types
module[1] is an example of this that lets you define file, mount, and
cron resources as hashes.

[1] - https://github.com/ghoneycutt/puppet-module-types

Best regards,
-g

-- 
Garrett Honeycutt
@learnpuppet
Puppet Training with LearnPuppet.com
Mobile: +1.206.414.8658

-- 
You received this message because you are subscribed to the Google Groups 
"Puppet Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to puppet-users+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/puppet-users/535EB274.5040709%40garretthoneycutt.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to