Guest User

Untitled

a guest
Nov 6th, 2017
388
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.10 KB | None | 0 0
  1. #!/usr/bin/env python
  2.  
  3.  
  4. # Quick and dirty script to configure cpu and memory on VMs
  5. # 11-2-2017
  6. # escapenguin@gmail.com
  7.  
  8. import atexit
  9. import argparse
  10. import getpass
  11. import ssl
  12. import sys
  13.  
  14. from pyVmomi import vim, vmodl
  15. from pyVim.connect import SmartConnect, Disconnect
  16. from pyVim.task import WaitForTask
  17.  
  18. # Disable SSL certificate verification... disable for production
  19. context = ssl.SSLContext(ssl.PROTOCOL_TLSv1)
  20. context.verify_mode = ssl.CERT_NONE
  21.  
  22. def get_args():
  23. parser = argparse.ArgumentParser(
  24. description='Arguments for talking to vCenter')
  25.  
  26. parser.add_argument('-s', '--host',
  27. required=True,
  28. action='store',
  29. help='vSphere server to connect to')
  30.  
  31. parser.add_argument('-o', '--port',
  32. type=int,
  33. default=443,
  34. action='store',
  35. help='Port to connect on')
  36.  
  37. parser.add_argument('-u', '--user',
  38. required=True,
  39. action='store',
  40. help='Username to use')
  41.  
  42. parser.add_argument('-p', '--password',
  43. required=False,
  44. action='store',
  45. help='Password to use')
  46.  
  47. parser.add_argument('-v', '--vm-name',
  48. required=True,
  49. action='store',
  50. help='Name of the vm')
  51.  
  52. parser.add_argument('--uuid',
  53. required=False,
  54. action='store',
  55. help='vmuuid of vm')
  56.  
  57. parser.add_argument('--vcpu_nu',
  58. required=False,
  59. action='store',
  60. help='Number of vcpu to configure on vm')
  61.  
  62. parser.add_argument('--mem_size',
  63. required=False,
  64. action='store',
  65. help='Amount of memory (MB) to configure on vm')
  66.  
  67. args = parser.parse_args()
  68.  
  69. if not args.password:
  70. args.password = getpass.getpass(
  71. prompt='Enter user password: ')
  72. return args
  73.  
  74. def get_obj(content, vimtype, name):
  75. obj = None
  76. container = content.viewManager.CreateContainerView(
  77. content.rootFolder, vimtype, True)
  78. for c in container.view:
  79. if c.name == name:
  80. obj = c
  81. break
  82. return obj
  83.  
  84. def change_vcpu(vm, si, vcpu_nu):
  85. vcpu_nu = int(vcpu_nu)
  86. cspec = vim.vm.ConfigSpec()
  87. cspec.numCPUs = vcpu_nu
  88. cspec.numCoresPerSocket = 1
  89. WaitForTask(vm.Reconfigure(cspec))
  90.  
  91. def change_memory(vm, si, mem_size):
  92. mem_size = int(mem_size)
  93. cspec = vim.vm.ConfigSpec()
  94. cspec.memoryMB = mem_size
  95. WaitForTask(vm.Reconfigure(cspec))
  96.  
  97. def main():
  98. args = get_args()
  99. try:
  100. si = SmartConnect(
  101. host=args.host,
  102. user=args.user,
  103. pwd=args.password,
  104. port=(args.port),
  105. sslContext=context)
  106. atexit.register(Disconnect, si)
  107. content = si.RetrieveContent()
  108. vm = None
  109. if args.uuid:
  110. search_index = si.content.searchIndex
  111. vm = search_index.FindByUuid(None, args.uuid, True)
  112. elif args.vm_name:
  113. #content = si.RetrieveContent()
  114. vm = get_obj(content, [vim.VirtualMachine], args.vm_name)
  115.  
  116. if vm:
  117. if args.vcpu_nu and args.mem_size:
  118. print(args.vcpu_nu)
  119. print(args.mem_size)
  120. change_vcpu(vm, si, args.vcpu_nu)
  121. change_memory(vm, si, args.mem_size)
  122. elif args.mem_size and not args.vcpu_nu:
  123. print(args.mem_size)
  124. change_memory(vm, si, args.mem_size)
  125. elif args.vcpu_nu and not args.mem_size:
  126. print(args.vcpu_nu)
  127. change_vcpu(vm, si, args.vcpu_nu)
  128. else:
  129. print("VM not found.")
  130. except IOError as e:
  131. print(e)
  132. pass
  133.  
  134. if __name__ == "__main__":
  135. main()
Add Comment
Please, Sign In to add comment