Clone from Template (RHEV)

Enable CloudFORMS to clone a template, and retaining the disk layout. So CloudFORMS currently deploys new virtual machines in RHEV either by PXE or ISO. It does this by cloning a BLANK template and attaching new disks, where a PXE or ISO process will install an operating system. Those from the VMware world and those in Windows land will want to deploy directly from a template a clone, without having to install an operating system, because the template already has it installed in its disk. Reasonable request…. this is how…

Point to take note on,

  • This is a quick and dirty example.
  • I would in a real implementation take the normal VM_Provision statemachine and amend the process that calls Provision to be replaced with this process, thus retaining all the approval and quota bits that make lifecycle management possible.

You will need a dialog looking something like this…

Screen Shot 2013-05-09 at 12.17.34

Include a Submit and Cancel button, my dropdown list is numbered 1-5, you can choose what ever you wish. the only important thing here is to name the two controls;

  • dialog_vm_count
  • dialog_vm_prefix

You will need to create a custom button for Templates, link the button to this dialog and to a system/request called cloneTemplate

Create the usual bits as follows;

/system/request/cloneTemplate instance.

/Sample/Methods/cloneTemplate instance.

/Sample/Methods/cloneTemplate method.

Place the following into your method.


#            Automate Method


$evm.log("info", "cloneTemplate -- Automate Method Started")


#            Method Code Goes here




require 'rubygems'

require 'rest_client'


vm = $evm.root['vm']

ext_management_system = vm.ext_management_system

ems_cluster = vm.ems_cluster

$evm.log("info", "VM Name - #{}")

$evm.log("info", "RHEVM Name - #{}")

$evm.log("info", "Cluster Name - #{}")

$dialog_vm_count = ' '

$dialog_vm_prefix = ' '

$evm.root.attributes.sort.each { |k, v|


if "#{k}" == "dialog_vm_count"

$dialog_vm_count = "#{v}"

$evm.log("info", "Found #{$dialog_vm_count}")


if "#{k}" == "dialog_vm_prefix"

$dialog_vm_prefix = "#{v}"

$evm.log("info", "Found #{$dialog_vm_prefix}")



rhevm = "https://#{ext_management_system.ipaddress}/api/vms"

rhevadmin = 'admin@internal'

rhevadminpass = 'monster'

resource =, :user => rhevadmin, :password => rhevadminpass)

$i = 1

while $i < ($dialog_vm_count.to_i + 1)  do

$evm.log("info", "******** <vm><name>#{$dialog_vm_prefix}#{$i}</name><cluster><name>#{}</name></cluster><template><name>#{}</name></template><memory>4294967296</memory><os><boot dev='hd'/></os></vm>****")

cloneTemplate = "<vm><name>#{$dialog_vm_prefix}#{$i}</name><cluster><name>#{}</name></cluster><template><name>#{}</name></template><memory>4294967296</memory><os><boot dev='hd'/></os></vm>", :content_type => 'application/xml', :accept => 'application/xml'

$evm.log("info", "Result - #{cloneTemplate}")

$i +=1





$evm.log("info", "Automate Method Ended")

exit MIQ_OK

You can download the methods code from here…

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s