Coder Social home page Coder Social logo

felix_shang's Introduction

Felix_Shang

felix_shang's People

Contributors

chengpeng21186 avatar

felix_shang's Issues

I want hot add vcpu and memory for existing vmware, hotadd vcpu is ok, but hotadd memory have errors, who can help me

!/usr/bin/env python

-- coding: utf-8 --

Email: [email protected]

QQ: 279379936

from pyVmomi import vim
from pyVmomi import vmodl
from pyVim.connect import SmartConnect, Disconnect
import atexit
import argparse
import getpass
from pyVim.task import WaitForTask
import sys

def get_args():
parser = argparse.ArgumentParser(
description='Arguments for talking to vCenter')

parser.add_argument('-s', '--host',
                    required=True,
                    action='store',
                    help='vSpehre service to connect to')

parser.add_argument('-o', '--port',
                    type=int,
                    default=443,
                    action='store',
                    help='Port to connect on')

parser.add_argument('-u', '--user',
                    required=True,
                    action='store',
                    help='User name to use')

parser.add_argument('-p', '--password',
                    required=False,
                    action='store',
                    help='Password to use')

parser.add_argument('-v', '--vm-name',
                    required=True,
                    action='store',
                    help='name of the vm')

parser.add_argument('--uuid',
                    required=False,
                    action='store',
                    help='vmuuid of vm')

parser.add_argument('--cpu',
                   required=False,
                   action='store',
                   help='vcpu of vm')

parser.add_argument('--memory',
                   required=False,
                   action='store',
                   help='memory of vm')

args = parser.parse_args()
if not args.password:
    args.password = getpass.getpass(
        prompt='Enter password')
return args

def get_obj(content, vimtype, name):
obj = None
container = content.viewManager.CreateContainerView(
content.rootFolder, vimtype, True)
for c in container.view:
if c.name == name:
obj = c
break
return obj

def check_hotadd_cpu_mem(vm, si, dev_type):
# open hot add cpu and memory
envBrowser = vm.environmentBrowser
configOption = envBrowser.QueryConfigOption()
osDescription = filter(lambda x: x.id == vm.config.guestId, configOption.guestOSDescriptor)[0]
if dev_type == 'cpu':
print "Supports cpuAdd: %s" % osDescription.supportsCpuHotAdd
elif dev_type == 'memory':
print "Supports memoryAdd: %s" % osDescription.supportsMemoryHotAdd

def change_vcpu(vm, si, vcpu_nu):
check_hotadd_cpu_mem(vm, si, 'cpu')
vcpu_nu=int(vcpu_nu)
cspec = vim.vm.ConfigSpec()
cspec.cpuHotAddEnabled = True
cspec.numCPUs = vcpu_nu # (虚拟插槽数)
cspec.numCoresPerSocket = 1 #(每个插槽的内核总数,每个插槽的内核数不能大于虚拟CPU的数量)
WaitForTask(vm.Reconfigure(cspec))

def change_memory(vm, si, mem_size):
check_hotadd_cpu_mem(vm, si, 'memory')
mem_size=int(mem_size)
cspec = vim.vm.ConfigSpec()
cspec.memoryHotAddEnabled = True
cspec.memoryMB = mem_size # MB of memory
WaitForTask(vm.Reconfigure(cspec))

def main():
args = get_args()
# connect this thing
si = SmartConnect(
host=args.host,
user=args.user,
pwd=args.password,
port=args.port)
# disconnect this thing
atexit.register(Disconnect, si)
vm = None
if args.uuid:
search_index = si.content.searchIndex
vm = search_index.FindByUuid(None, args.uuid, True)
elif args.vm_name:
content = si.RetrieveContent()
vm = get_obj(content, [vim.VirtualMachine], args.vm_name)
if vm:
if args.cpu and args.memory:
change_vcpu(vm, si, args.cpu)
change_memory(vm, si, args.memory)
elif args.memory and not args.cpu:
change_memory(vm, si, args.memory)
elif args.cpu and not args.memory:
change_vcpu(vm, si, args.cpu)
else:
print '-h, --help show help messages for you!'
else:
print "VM not found"

start this thing

if name == "main":
main()

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.