Advertisement
Guest User

Untitled

a guest
Sep 8th, 2016
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.83 KB | None | 0 0
  1. #!/usr/bin/python
  2.  
  3. from ovirtsdk.api import API
  4. from ovirtsdk.xml import params
  5. import argparse
  6. import time
  7.  
  8. # parse arguments
  9.  
  10. parser = argparse.ArgumentParser(description="Script to create RHEV boxes")
  11.  
  12. parser.add_argument("--url", help="RHEV_URL", required=True)
  13. parser.add_argument("--rhevusername", help="RHEV username", required=True)
  14. parser.add_argument("--rhevpassword", help="RHEV password", required=True)
  15. parser.add_argument("--rhevcafile", help="Path to RHEV ca file", default="/etc/pki/ovirt-engine/ca.pem")
  16. parser.add_argument("--memory", help="Memory size for RHEV VM, eg 2 means virtual machine will get 2GB of memory ", default=1)
  17. parser.add_argument("--cluster", help="RHEV Cluster - in which cluster create vm", default="Default")
  18. parser.add_argument("--vmtemplate", help="RHEV template")
  19. parser.add_argument("--action", help="action can be create or attach")
  20. parser.add_argument("--vmlistfile", help="location where to write list of machines added")
  21. parser.add_argument("--nicname", help="NIC name for vm", default="eth0")
  22. parser.add_argument("--num", help="how many virtual machines to create", default=1)
  23. parser.add_argument("--vmprefix", help="virtual machine name prefix", required=True, default="myvirtmachine")
  24. parser.add_argument("--disksize", help="disk size to attach to virtual machine in [MB] - passing 100 will create 100MB "
  25. "and attach it to virtual machine, default is 1000 MB = 1 GB ", default=1000)
  26.  
  27. parser.add_argument("--storagedomain", help="which storage domain to use for space when allocating storage for VM"
  28. "If not sure which one - check web interface and/or contact RHEV admin", required=True, default="iSCSI")
  29.  
  30.  
  31. args = parser.parse_args()
  32. url=args.url
  33. rhevusarname = args.rhevusername
  34. rhevpassword = args.rhevpassword
  35. rhevcafile = args.rhevcafile
  36. memory = args.memory
  37. cluster = args.cluster
  38. vmtemplate = args.vmtemplate
  39. vmlistfile = args.vmlistfile
  40. nicname = args.nicname
  41. vmprefix = args.vmprefix
  42. num = args.num
  43. action = args.action
  44. disksize = args.disksize
  45. storagedomain = args.storagedomain
  46.  
  47. # basic configurations / functions
  48.  
  49. # api definition
  50. api = API(url=url,username=rhevusarname, password=rhevpassword, ca_file=rhevcafile)
  51.  
  52. # wait functions
  53.  
  54. def wait_vm_state(vm_name, state):
  55. while api.vms.get(vm_name).status.state != state:
  56. time.sleep (1)
  57.  
  58. def wait_disk_state(disk_name, state):
  59. while api.disks.get(disk_name).status.state != state:
  60. time.sleep(1)
  61.  
  62. def create_vm(vmprefix):
  63.  
  64. print ("------------------------------------------------------")
  65. print ("Creating", num, "RHEV based virtual machines")
  66. print ("-------------------------------------------------------")
  67. try:
  68. # crete virtual machines
  69. for machine in range(0,int(num)):
  70. vm_name = str(vmprefix) + "_" + str(machine)
  71. vm_memory = int(memory)*1024*1024*1024
  72. vm_cluster = api.clusters.get(name=cluster)
  73. vm_template = api.templates.get(name=vmtemplate)
  74. vm_os = params.OperatingSystem(boot=[params.Boot(dev="hd")])
  75.  
  76. vm_params = params.VM(name=vm_name,
  77. memory=vm_memory,
  78. cluster=vm_cluster,
  79. template=vm_template,
  80. os=vm_os)
  81. api.vms.add(vm=vm_params)
  82. api.vms.get(vm_name).nics.add(params.NIC(name=nicname, network=params.Network(name='ovirtmgmt'), interface='virtio'))
  83. print ("Virtual machine created: ", vm_name, "To start machines run vm_start.py script")
  84. wait_vm_state(vm_name, "down")
  85. except Exception as ex:
  86. print ("Adding virtual machine '%s' failed: %s", vm_name, ex)
  87. api.disconnect()
  88.  
  89. def attach_disk(vmprefix,disksize, storagedomain):
  90. vmlist = api.vms.list(max=500)
  91. try:
  92. for machine in vmlist:
  93. if api.vms.get(machine.name).status.state != 'up' and machine.name.startswith(vmprefix):
  94. diskname = machine.name
  95. api.vms.get(machine.name).disks.add(params.Disk(name=diskname, storage_domains=params.StorageDomains(storage_domain=[api.storagedomains.get(name=storagedomain)]),
  96. size=int(disksize)*1000000,
  97. # type_='system', - disk type is deprecated
  98. status=None,
  99. interface='virtio',
  100. format='cow',
  101. sparse=True,
  102. bootable=False))
  103.  
  104. # if disk is not in ready state ... wait here - we cannot start machine if this is not the case
  105. wait_disk_state(diskname,"ok")
  106.  
  107.  
  108. except Exception as e:
  109. print ("Adding disk to virtual machine failed")
  110. api.disconnect()
  111.  
  112.  
  113. # start machine
  114. def vm_start(vmprefix):
  115. try:
  116. vmlist = api.vms.list(max=500)
  117. for machine in vmlist:
  118. if api.vms.get(machine.name).status.state != 'up' and machine.name.startswith(vmprefix):
  119. #if machine.name.startswith(vmprefix) and api.vms.get(machine.name).status.state != 'up':
  120. print ("Necessary to start machine:", machine.name, "doing it now...")
  121. api.vms.get(machine.name).start()
  122. elif api.vms.get(machine.name).status.state == 'up' and machine.name.startswith(vmprefix):
  123. print ("Machine:", machine.name , "is already up and running")
  124. else:
  125. print ("Machine:", machine.name, "is already stopped")
  126.  
  127.  
  128. except Exception as e:
  129. print ("Failed to start Virtual machine,check it via web interface ", url)
  130.  
  131.  
  132. # create machine and attach disk
  133. create_vm(vmprefix)
  134. attach_disk(vmprefix,disksize,storagedomain="iSCSI")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement