公司要本身開發私有云管理平臺,我這邊負責vmware部分的後臺接口編寫。html
主要基於vmware官方的 python 接口 pyvmomi進行二次封裝,前端
主要實現有虛擬機開關機註銷;
html5
虛擬機增刪磁盤;
python
虛擬機快照增刪還原;mysql
虛擬機 html5的console界面git
虛擬機網絡管理redis
zabbix 監控 esxi的磁盤使用率 sql
zabbix 接受轉發esxi的報警信息django
我把 克隆虛擬機,虛擬機 html5的console界面,虛擬機快照管理 這幾個代碼的放出,水平有限,大神勿噴,而且有部分涉及隱私的地方作了處理,供你們參考下貼出的只是初版,沒有錯誤捕捉vim
須要環境:vcenter 5.1-6.0, python3.5 ,django 1.8 ,redis,mysql,celery(python 異步後臺任務)等
首先介紹克隆虛擬機
流程 :前端發起請求,提交的參數有 源模板id,虛擬機的配置,後端提交請求到vcenter,提交成功的話,,給個反饋給前端,並在後臺啓用克隆虛擬機的進程,後臺處理用,celery實現,虛擬機克隆完成後,虛擬os裏面有個開機腳原本個人管理端註冊下ip地址
代碼以下
from pyVmomi import vim
from pyVim.connect import SmartConnect, Disconnect
from celery import Celery
from celery import platforms
import atexit
import argparse
import getpass
import ssl
import random
import cgi, cgitb
celery = Celery('tasks', broker='redis://:########redis鏈接信息/0')
platforms.C_FORCE_ROOT = True
def action(num,flavor,templateid): #主函數 被Celery調用
hardwareconfiglist={1:[2,8*1024],2:[4,16*1024],3:[8,32*1024]}
cpunum=hardwareconfiglist[int(flavor)][0]
memsize=hardwareconfiglist[int(flavor)][1]
si = None
context = ssl.SSLContext(ssl.PROTOCOL_TLSv1)
context.verify_mode = ssl.CERT_NONE
si = SmartConnect(host="###########",
user="##################",
pwd="############",
port=443,
sslContext=context)
atexit.register(Disconnect, si)
content = si.RetrieveContent()
template = None
template = templates[str(templateid)]
template = get_obj(content, [vim.VirtualMachine], template)
for i in range(0,int(num)):
clone_vm(
content, template, si,
"", "",
"存儲名字", '集羣名字 ',
"", False,cpunum,memsize)
def get_obj(content, vimtype, name): #把克隆源的虛擬機實例化
obj = None
container = content.viewManager.CreateContainerView(
content.rootFolder, vimtype, True)
for c in container.view:
if name:
if c.name == name:
obj = c
break
else:
obj = c
break
return obj
def clone_vm( #克隆虛擬機,克隆出來的虛擬機名字是隨機,
content, template, si,
datacenter_name, vm_folder, datastore_name,
cluster_name, resource_pool, power_on,cpunum,memsize):
datacenter = get_obj(content, [vim.Datacenter], datacenter_name)
if vm_folder:
destfolder = get_obj(content, [vim.Folder], vm_folder)
else:
destfolder = datacenter.vmFolder
if datastore_name:
datastore = get_obj(content, [vim.Datastore], datastore_name)
else:
datastore = get_obj(
content, [vim.Datastore], template.datastore[0].info.name)
# if None, get the first one
cluster = get_obj(content, [vim.ClusterComputeResource], cluster_name)
if resource_pool:
resource_pool = get_obj(content, [vim.ResourcePool], resource_pool)
else:
resource_pool = cluster.resourcePool
relospec = vim.vm.RelocateSpec()
relospec.datastore = datastore
relospec.pool = resource_pool
clonespec = vim.vm.CloneSpec()
clonespec.location = relospec
clonespec.powerOn = power_on
randomname=get_hostname()
task = template.Clone(folder=destfolder, name=randomname, spec=clonespec)
wait_for_task(task)
vm = get_obj(content, [vim.VirtualMachine], randomname)
spec = vim.vm.ConfigSpec()
spec.numCPUs=cpunum
spec.memoryMB=memsize
vm.ReconfigVM_Task(spec=spec) #設置克隆好的虛擬機的cpu 和 內存
vm.CreateSnapshot("fromstart","this created when it is been cloned by system",False,False) #克隆好了建立一個快照,以便實現重裝系統的功能
vm.PowerOn()