Create Template from VM (RHEV)

CloudFORMS lacks currently the ability to create a template from an existing VM, here is the method to enable this function from a custom button.

First you need a dialog to collect from the new user the name of the template, so create a dialog of your liking, including at least;

Textbox – Name = dialog_name

It will look something like this when completed;

Screen Shot 2013-05-08 at 13.06.45

Next is to create the custom button, so create a new group to take the button, and in it a button that calls your new dialog you created in the previous step, along with a request instance called create_template. Don’t forget you are creating the button in the VM group. It will look something like;

Screen Shot 2013-05-08 at 13.08.22

Now for the automate bits,

  1. Create an instance in /system/request called create_template.
  2. Edit your new instance and wire it to /Sample/Methods/create_template
  3. Create an instance in /Sample/Methods called create_template.
  4. Create an method in /Sample/Methods called create_template.
  5. add the following code to your new method;
</pre>
#

#            Automate Method

#

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

#

#            Method Code Goes here

#

&nbsp;

#!/usr/bin/ruby

require 'rubygems'

require 'rest_client'

vm = $evm.root['vm']

ext_management_system = vm.ext_management_system

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

$evm.log("info", "VM UID - #{vm.uid_ems}")

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

$dialog_name = ' '

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

$evm.log("info","#{k}---#{v}")

if "#{k}" == "dialog_name"

$dialog_name = "#{v}"

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

end

}

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

rhevadmin = 'admin@internal'

rhevadminpass = 'monster'

resource = RestClient::Resource.new(rhevm, :user => rhevadmin, :password => rhevadminpass)

createTemplate = resource.post "<template><name>#{$dialog_name}</name><vm id="#{vm.uid_ems}"></vm></template>", :content_type => 'application/xml', :accept => 'application/xml'

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

#

#

#

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

exit MIQ_OK

So to test, select a VM and execute your button, in the log you should see something like;

</pre>
[----] I, [2013-05-08T11:00:27.509399 #4118:64a4ba0] INFO -- : Q-task_id([resource_action_3]) <User-Defined Method> Automate Method Started
[----] I, [2013-05-08T11:00:27.623669 #4118:64a4ba0] INFO -- : Q-task_id([resource_action_3]) <User-Defined Method> VM Name - temtest1
[----] I, [2013-05-08T11:00:27.625774 #4118:64a4ba0] INFO -- : Q-task_id([resource_action_3]) <User-Defined Method> VM UID - 42916780-a571-44a6-aae4-a4ae3c07ee20
[----] I, [2013-05-08T11:00:27.628009 #4118:64a4ba0] INFO -- : Q-task_id([resource_action_3]) <User-Defined Method> RHEVM Name - RHEV-M (192.168.201.51)
[----] I, [2013-05-08T11:00:27.639567 #4118:64be4c4] INFO -- : Q-task_id([resource_action_3]) <User-Defined Method> dialog_name---Template1
[----] I, [2013-05-08T11:00:27.640451 #4118:64be4c4] INFO -- : Q-task_id([resource_action_3]) <User-Defined Method> Found Template1
[----] I, [2013-05-08T11:00:27.642538 #4118:64be4c4] INFO -- : Q-task_id([resource_action_3]) <User-Defined Method> miq_server---#<MiqAeMethodService::MiqAeServiceMiqServer:0x0000000c4f3510>
[----] I, [2013-05-08T11:00:27.643877 #4118:64be4c4] INFO -- : Q-task_id([resource_action_3]) <User-Defined Method> miq_server_id---1
[----] I, [2013-05-08T11:00:27.645208 #4118:64be4c4] INFO -- : Q-task_id([resource_action_3]) <User-Defined Method> object_name---Request
[----] I, [2013-05-08T11:00:27.646342 #4118:64be4c4] INFO -- : Q-task_id([resource_action_3]) <User-Defined Method> request---createTemplate
[----] I, [2013-05-08T11:00:27.648066 #4118:64be4c4] INFO -- : Q-task_id([resource_action_3]) <User-Defined Method> user---#<MiqAeMethodService::MiqAeServiceUser:0x0000000c511718>
[----] I, [2013-05-08T11:00:27.650808 #4118:64be4c4] INFO -- : Q-task_id([resource_action_3]) <User-Defined Method> user_id---1
[----] I, [2013-05-08T11:00:27.654278 #4118:64be4c4] INFO -- : Q-task_id([resource_action_3]) <User-Defined Method> vm---temtest1
[----] I, [2013-05-08T11:00:27.655532 #4118:64be4c4] INFO -- : Q-task_id([resource_action_3]) <User-Defined Method> vm_id---40
[----] I, [2013-05-08T11:00:27.656746 #4118:64be4c4] INFO -- : Q-task_id([resource_action_3]) <User-Defined Method> vmdb_object_type---vm
[----] I, [2013-05-08T11:00:28.135843 #4118:64a4ba0] INFO -- : Q-task_id([resource_action_3]) <User-Defined Method> Result - <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<template href="/api/templates/56527a40-9fbf-4f46-a652-e68ee0521679" id="56527a40-9fbf-4f46-a652-e68ee0521679">
 <actions>
 <link href="/api/templates/56527a40-9fbf-4f46-a652-e68ee0521679/export" rel="export"/>
 </actions>
 <name>Template1</name>
 <creation_status>
 <state>pending</state>
 </creation_status>
 <link href="/api/templates/56527a40-9fbf-4f46-a652-e68ee0521679/disks" rel="disks"/>
 <link href="/api/templates/56527a40-9fbf-4f46-a652-e68ee0521679/nics" rel="nics"/>
 <link href="/api/templates/56527a40-9fbf-4f46-a652-e68ee0521679/cdroms" rel="cdroms"/>
 <link href="/api/templates/56527a40-9fbf-4f46-a652-e68ee0521679/permissions" rel="permissions"/>
 <link href="/api/templates/56527a40-9fbf-4f46-a652-e68ee0521679/creation_status/c036ecce-facf-479a-9b02-2cb7da154252" rel="creation_status"/>
 <type>server</type>
 <status>
 <state>locked</state>
 </status>
 <memory>4294967296</memory>
 <cpu>
 <topology sockets="1" cores="1"/>
 </cpu>
 <os type="rhel_6x64">
 <boot dev="hd"/>
 <kernel></kernel>
 <initrd></initrd>
 <cmdline></cmdline>
 </os>
 <cluster href="/api/clusters/af2f210e-6ab2-11e2-8023-000c292b2e63" id="af2f210e-6ab2-11e2-8023-000c292b2e63"/>
 <creation_time>2013-05-08T12:00:27.780+01:00</creation_time>
 <origin>rhev</origin>
 <high_availability>
 <enabled>false</enabled>
 <priority>1</priority>
 </high_availability>
 <display>
 <type>spice</type>
 <monitors>1</monitors>
 <allow_override>false</allow_override>
 </display>
 <stateless>false</stateless>
 <usb>
 <enabled>false</enabled>
 </usb>
</template>

[----] I, [2013-05-08T11:00:28.136449 #4118:64a4ba0] INFO -- : Q-task_id([resource_action_3]) <User-Defined Method> Automate Method Ended
[----] I, [2013-05-08T11:00:28.144107 #4118:28ef7f4] INFO -- : Q-task_id([resource_action_3]) Method exited with rc=MIQ_OK

and  in RHEV-M you should see your new template…

template threre

Method code available here…https://github.com/jonnyfiveiq/CloudFORMSNOW/blob/master/RHEV/Templates/create_template.rb

Credit

Matteo Bernacchi – How nice are these RHEV RestAPI’s !

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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 )

Google+ photo

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

Connecting to %s