<br><div class="gmail_quote">Hello ,<br><div><br></div><div><font color="#000000"><b>I want to write a script to attach a '.iso' image to a vm </b></font></div><div>so ,can you please help me or tell me some api so that i can continue my work !!!</div>
<div>I have some '.iso ' file in my storage .</div><div><br></div><div><div>script <b>rhevm_test.py</b></div></div><div><br></div><div><font color="#0000ff">from ovirtsdk.api import API</font></div><div><font color="#0000ff">from ovirtsdk.xml import params</font></div>
<div><font color="#0000ff">import time</font></div><div><font color="#0000ff"><br></font></div><div><font color="#0000ff">rhevm_uri = "<a href="https://rhevm301.xxx.xx.com:8443/api" target="_blank">https://rhevm301.xxx.xx.com:8443/api</a>"</font></div>
<div><font color="#0000ff">rhevm_username = "<a href="mailto:admin@rhevm301.xxx.xx.com" target="_blank">admin@rhevm301.xxx.xx.com</a>"</font></div><div><font color="#0000ff">rhevm_password = "iso*help"</font></div>
<div>
<font color="#0000ff"><br></font></div><div><font color="#0000ff">api = API(url=rhevm_uri, username=rhevm_username, password=rhevm_password)</font></div><div><font color="#0000ff">print "Connected to RHEVM Successful"</font></div>
<div><font color="#0000ff"><br></font></div><div><font color="#0000ff">MB = 1024*1024</font></div><div><font color="#0000ff">GB = 1024*MB</font></div><div><font color="#0000ff">VM_NAME = 'test_vm'</font></div><div>
<font color="#0000ff">DESCRIP = 'testing vm'</font></div><div><font color="#0000ff">CLUSTER_NAME = 'Default'<br></font></div><div><font color="#0000ff">STORAGE_NAME = 'rhevmVMdata'</font></div><div>
<div><font color="#0000ff"><br></font></div><div><font color="#0000ff">domain_name= 'rhevmiso' </font></div><div><font color="#0000ff">domain_type = 'ISO'</font></div></div><div><font color="#0000ff"><br>
</font></div><div><font color="#0000ff"><br></font></div><div><font color="#0000ff">try:</font></div><div><font color="#0000ff"> api.vms.add(params.VM(name=VM_NAME,description=DESCRIP, memory=2*GB,cluster=api.clusters.get(CLUSTER_NAME), template=api.templates.get('Blank')</font><span style="color:rgb(0,0,255)">))</span></div>
<div><font color="#0000ff"> </font></div><div><font color="#0000ff"> print 'VM created'</font></div><div><font color="#0000ff"><br></font></div><div><font color="#0000ff"> api.vms.get(VM_NAME).nics.add(params.NIC(name='nic1', network=params.Network(name='rhevm'), interface='Red Hat VirtIO'))</font></div>
<div><font color="#0000ff"> print 'NIC added to VM'</font></div><div><font color="#0000ff"><br></font></div><div><font color="#0000ff"> api.vms.get(VM_NAME).disks.add(params.Disk(storage_domains=params.StorageDomains(storage_domain=[api.storagedomains.get(STORAGE_NAME)]),size=512*MB,status=None,interface='VirtIO',format='Preallocated',sparse=True,bootable=True))</font></div>
<div><font color="#0000ff"><br></font></div><div><font color="#0000ff"> print 'Disk added to VM'</font></div><div><font color="#0000ff"> print 'Waiting for VM to reach Down status'</font></div><div>
<font color="#0000ff"> while api.vms.get(VM_NAME).status.state != 'down':</font></div>
<div><font color="#0000ff"> time.sleep(1)</font></div><div><font color="#0000ff">except Exception as e:</font></div><div><font color="#0000ff"> print 'Failed to create VM with disk and NIC\n%s' % str(e)</font></div>
<div><font color="#0000ff"><br></font></div><div><font color="#0000ff">time.sleep(10)</font></div><div><div style="color:rgb(0,0,255)"><br></div><div><font color="#ff0000">try :</font></div><div><font color="#ff0000"> api.vms.get(VM_NAME).cdroms.add(params.CdRom(name='CD-ROM' , file=params.File(name='CentOS-6.2-x86_64-LiveDVD.iso')))</font></div>
<div><font color="#ff0000">except Exception as e:</font></div><div><font color="#ff0000"> print 'Failed to attach disk :\n%s' % str(e)</font></div><div style="color:rgb(0,0,255)"><br></div></div><div><font color="#0000ff"><br>
</font></div><div><font color="#0000ff"><br></font></div><div><font color="#0000ff">try:</font></div><div><font color="#0000ff"> if api.vms.get(VM_NAME).status.state != 'up':</font></div>
<div><font color="#0000ff"> print 'Starting VM'</font></div><div><font color="#0000ff"> api.vms.get(VM_NAME).start()</font></div><div><font color="#0000ff"> print 'Waiting for VM to reach Up status'</font></div>
<div><font color="#0000ff"> while api.vms.get(VM_NAME).status.state != 'up':</font></div><div><font color="#0000ff"> time.sleep(1)</font></div><div><font color="#0000ff"> else:</font></div><div>
<font color="#0000ff"> print 'VM already up'</font></div><div><font color="#0000ff">except Exception as e:</font></div><div><font color="#0000ff"> print 'Failed to Start VM:\n%s' % str(e)</font></div>
<div><br></div><div> </div><div>and got some following output :</div><div><br></div><div><div>Connected to RHEVM Successful</div><div>VM created</div><div>NIC added to VM</div><div>Disk added to VM</div><div>Waiting for VM to reach Down status</div>
<div>Failed to attach disk :</div><div><br></div><div>status: 400</div><div>reason: Bad Request</div><div>detail: CdRom [<a href="http://file.id" target="_blank">file.id</a>] required for add</div><div>Starting VM</div><div>
Failed to Start VM:</div>
<div><br></div><div>status: 400</div><div>reason: Bad Request</div><div>detail: Cannot run VM without at least one bootable disk.</div></div><div><br></div><div><br></div><div><br></div><div>if i remove this line in <font color="#ff0000">red color</font><span style="color:rgb(0,0,255)"> </span><font color="#000000">the code will works fine and it will create a vm with blank template w/o any '.iso' attached !!</font></div>
<br>
<div><br></div><div><b><font color="#9900ff"><br></font></b></div><div><b><font color="#9900ff"><br></font></b></div><div><b><font color="#9900ff"><br></font></b></div><div><b><font color="#9900ff"><br></font></b></div><div>
<b><font color="#9900ff">With Regards,</font></b></div><div><b><font color="#9900ff">Romil</font></b></div><div><br></div><div><br></div><div><br></div>
</div><br><br clear="all"><div><br></div><br>