chengpeng21186 / felix_shang Goto Github PK
View Code? Open in Web Editor NEWFelix_Shang
Felix_Shang
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"
if name == "main":
main()
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.