Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/python
- from ovirtsdk.api import API
- from ovirtsdk.xml import params
- import argparse
- import time
- # parse arguments
- parser = argparse.ArgumentParser(description="Script to create RHEV boxes")
- parser.add_argument("--url", help="RHEV_URL", required=True)
- parser.add_argument("--rhevusername", help="RHEV username", required=True)
- parser.add_argument("--rhevpassword", help="RHEV password", required=True)
- parser.add_argument("--rhevcafile", help="Path to RHEV ca file", default="/etc/pki/ovirt-engine/ca.pem")
- parser.add_argument("--memory", help="Memory size for RHEV VM, eg 2 means virtual machine will get 2GB of memory ", default=1)
- parser.add_argument("--cluster", help="RHEV Cluster - in which cluster create vm", default="Default")
- parser.add_argument("--vmtemplate", help="RHEV template")
- parser.add_argument("--action", help="action can be create or attach")
- parser.add_argument("--vmlistfile", help="location where to write list of machines added")
- parser.add_argument("--nicname", help="NIC name for vm", default="eth0")
- parser.add_argument("--num", help="how many virtual machines to create", default=1)
- parser.add_argument("--vmprefix", help="virtual machine name prefix", required=True, default="myvirtmachine")
- parser.add_argument("--disksize", help="disk size to attach to virtual machine in [MB] - passing 100 will create 100MB "
- "and attach it to virtual machine, default is 1000 MB = 1 GB ", default=1000)
- parser.add_argument("--storagedomain", help="which storage domain to use for space when allocating storage for VM"
- "If not sure which one - check web interface and/or contact RHEV admin", required=True, default="iSCSI")
- args = parser.parse_args()
- url=args.url
- rhevusarname = args.rhevusername
- rhevpassword = args.rhevpassword
- rhevcafile = args.rhevcafile
- memory = args.memory
- cluster = args.cluster
- vmtemplate = args.vmtemplate
- vmlistfile = args.vmlistfile
- nicname = args.nicname
- vmprefix = args.vmprefix
- num = args.num
- action = args.action
- disksize = args.disksize
- storagedomain = args.storagedomain
- # basic configurations / functions
- # api definition
- api = API(url=url,username=rhevusarname, password=rhevpassword, ca_file=rhevcafile)
- # wait functions
- def wait_vm_state(vm_name, state):
- while api.vms.get(vm_name).status.state != state:
- time.sleep (1)
- def wait_disk_state(disk_name, state):
- while api.disks.get(disk_name).status.state != state:
- time.sleep(1)
- def create_vm(vmprefix):
- print ("------------------------------------------------------")
- print ("Creating", num, "RHEV based virtual machines")
- print ("-------------------------------------------------------")
- try:
- # crete virtual machines
- for machine in range(0,int(num)):
- vm_name = str(vmprefix) + "_" + str(machine)
- vm_memory = int(memory)*1024*1024*1024
- vm_cluster = api.clusters.get(name=cluster)
- vm_template = api.templates.get(name=vmtemplate)
- vm_os = params.OperatingSystem(boot=[params.Boot(dev="hd")])
- vm_params = params.VM(name=vm_name,
- memory=vm_memory,
- cluster=vm_cluster,
- template=vm_template,
- os=vm_os)
- api.vms.add(vm=vm_params)
- api.vms.get(vm_name).nics.add(params.NIC(name=nicname, network=params.Network(name='ovirtmgmt'), interface='virtio'))
- print ("Virtual machine created: ", vm_name, "To start machines run vm_start.py script")
- wait_vm_state(vm_name, "down")
- except Exception as ex:
- print ("Adding virtual machine '%s' failed: %s", vm_name, ex)
- api.disconnect()
- def attach_disk(vmprefix,disksize, storagedomain):
- vmlist = api.vms.list(max=500)
- try:
- for machine in vmlist:
- if api.vms.get(machine.name).status.state != 'up' and machine.name.startswith(vmprefix):
- diskname = machine.name
- api.vms.get(machine.name).disks.add(params.Disk(name=diskname, storage_domains=params.StorageDomains(storage_domain=[api.storagedomains.get(name=storagedomain)]),
- size=int(disksize)*1000000,
- # type_='system', - disk type is deprecated
- status=None,
- interface='virtio',
- format='cow',
- sparse=True,
- bootable=False))
- # if disk is not in ready state ... wait here - we cannot start machine if this is not the case
- wait_disk_state(diskname,"ok")
- except Exception as e:
- print ("Adding disk to virtual machine failed")
- api.disconnect()
- # start machine
- def vm_start(vmprefix):
- try:
- vmlist = api.vms.list(max=500)
- for machine in vmlist:
- if api.vms.get(machine.name).status.state != 'up' and machine.name.startswith(vmprefix):
- #if machine.name.startswith(vmprefix) and api.vms.get(machine.name).status.state != 'up':
- print ("Necessary to start machine:", machine.name, "doing it now...")
- api.vms.get(machine.name).start()
- elif api.vms.get(machine.name).status.state == 'up' and machine.name.startswith(vmprefix):
- print ("Machine:", machine.name , "is already up and running")
- else:
- print ("Machine:", machine.name, "is already stopped")
- except Exception as e:
- print ("Failed to start Virtual machine,check it via web interface ", url)
- # create machine and attach disk
- create_vm(vmprefix)
- attach_disk(vmprefix,disksize,storagedomain="iSCSI")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement