Skip to content

Instantly share code, notes, and snippets.

@tenstormavi
Created November 29, 2017 12:18
Show Gist options
  • Save tenstormavi/20a6c2fc90e00d1b9917f1a20596edba to your computer and use it in GitHub Desktop.
Save tenstormavi/20a6c2fc90e00d1b9917f1a20596edba to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import ovirtsdk4 as sdk
import ovirtsdk4.types as types
import time
import sys
def create_all():
try:
# Use the "add" method to create a new virtual machine:
vms_service.add(
types.Vm(
name=VM_NAME,
memory=1*GB,
cluster=types.Cluster(
name=CLUSTER_NAME,
),
template=types.Template(
name='Blank',
),
),
)
#print 'VM created'
# Locate the virtual machines service and use it to find the virtual
# machine:
vm = vms_service.list(search='name=%s' % VM_NAME)[0]
# In order to specify the network that the new interface will be
# connected to we need to specify the identifier of the virtual network
# interface profile, so we need to find it:
profiles_service = connection.system_service().vnic_profiles_service()
profile_id = None
for profile in profiles_service.list():
if profile.name == 'mynetwork':
profile_id = profile.id
break
# Locate the service that manages the network interface cards of the
# virtual machine:
nics_service = vms_service.vm_service(vm.id).nics_service()
# Use the "add" method of the network interface cards service to add the
# new network interface card:
nics_service.add(
types.Nic(
name=NIC_NAME,
description='My network interface card',
#interface='virtio',
network=types.Network(
name='ovirtmgmt',
),
vnic_profile=types.VnicProfile(
id=profile_id,
),
),
)
#print 'NIC added to vm'
# Locate the service that manages the disk attachments of the virtual
# machine:
disk_attachments_service = vms_service.vm_service(vm.id).disk_attachments_service()
# Use the "add" method of the disk attachments service to add the disk.
# Note that the size of the disk, the `provisioned_size` attribute, is
# specified in bytes, so to create a disk of 10 GiB the value should
# be 10 * 2^30.
disk_attachment = disk_attachments_service.add(
types.DiskAttachment(
disk=types.Disk(
name=DISK_NAME,
description='My disk',
format=types.DiskFormat.COW,
provisioned_size=512*MB,
storage_domains=[
types.StorageDomain(
name=STORAGE_NAME,
),
],
),
interface=types.DiskInterface.VIRTIO,
bootable=True,
active=True,
),
)
# Wait till the disk is OK:
disks_service = connection.system_service().disks_service()
disk_service = disks_service.disk_service(disk_attachment.disk.id)
while True:
time.sleep(5)
disk = disk_service.get()
if disk.status == types.DiskStatus.OK:
break
#print 'DISK added to VM'
# Locate the service that manages the virtual machine, as that is where
# the action methods are defined:
vm_service = vms_service.vm_service(vm.id)
# Call the "start" method of the service to start it:
vm_service.start()
# Wait till the virtual machine is up:
while True:
time.sleep(5)
vm = vm_service.get()
if vm.status == types.VmStatus.UP:
break
print '%s Created' % VM_NAME
# Close the connection to the server:
connection.close()
except Exception as e:
print 'Failed to create VM with disk and NIC\n%s' % str(e)
for i in range(int(sys.argv[3]), int(sys.argv[4])):
URL='https://perf-rhevm.perf.lab.eng.bos.redhat.com/ovirt-engine/api'
CLUSTER_NAME=sys.argv[1]
STORAGE_NAME=sys.argv[2]
VM_NAME='vm-%s' % i
NIC_NAME='new_nic-%s' % i
DISK_NAME='new_disk-%s' % i
# Create the connection to the server:
connection = sdk.Connection(
url=URL,
username='admin@internal',
password='100yard-',
insecure=True
)
# Get the reference to the "vms" service:
vms_service = connection.system_service().vms_service()
MB = 1024*1024
GB = 1024*MB
create_all()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment