代码:
#encoding=utf-8 import libvirt import tkinter as tk def close_connection():#关闭连接 try: conn.close() except: print("--关闭连接失败--") return 1 print(‘--成功关闭连接--‘) def list_connection():#主机信息 print("---------主机信息---------") nodeinfo = conn.getInfo() # 获取虚拟化主机信息 print(‘模型: ‘ + str(nodeinfo[0])) # cpu模型 print(‘内存大小: ‘ + str(nodeinfo[1]) + ‘MB‘) # 内存 print(‘CPU数量: ‘ + str(nodeinfo[2])) # cpu数量 print(‘虚拟类型: ‘ + conn.getType()) # 虚拟化类型 uri = conn.getURI() # 目前链接的uri print(‘连接URI: : ‘ + uri) mem = conn.getFreeMemory() print("空闲内存: " + str(mem) + " bytes.") print("-------------------------") lableInit.config(text="--------主机信息--------\n" "模型:{}\n" "内存大小:{}MB\n" "CPU数量:{}个\n" "虚拟类型:{}\n" "连接URI:{}\n" "内存空间:{}bytes\n" .format(str(nodeinfo[0]),str(nodeinfo[1]),str(nodeinfo[2]),conn.getType(),uri,str(mem))) def show_machine():#显示虚拟机状态 #开启的域 vms_dict={} domain_list=conn.listDomainsID() for vm in domain_list: vms_dict[str(vm)]=conn.lookupByID(vm).name() print(""" 处于开启状态的虚拟机 ---------------- ID\tName ----------------""") if vms_dict: for id_part,name_part in vms_dict.items(): print("{}\t{}".format(id_part,name_part)) #lableInit.config(text="开启状态:".format(vms_dict)) else: print("None") print("----------------") #关闭的域 global vmd_list vmd_list=[] for i in conn.listDefinedDomains(): vmd_list.append(i) print(""" 处于关闭状态的虚拟机 ---------------- Name ----------------""") if vmd_list: for name_part in vmd_list: print("{}".format(name_part)) lableInit.config(text="开启状态:{}\n" "关机状态:{}\n".format(vms_dict,vmd_list)) else: print("None") print("----------------") #lableInit.config(text="虚拟机信息已在终端显示") #--------对虚拟机域进行操作--------- def temp(): name = entryName.get() f = open(‘/etc/libvirt/qemu/{}.xml‘.format(name)) # xml文件需要事先准备好 xml = f.read() conn.createXML(xml) f.close() lableInit.config(text="临时虚拟机 {} 创建完成".format(name)) def define(): name=entryName.get() f=open(‘/etc/libvirt/qemu/{}.xml‘.format(name))#xml文件需要事先准备好 xml=f.read() dom = conn.defineXML(xml) f.close() lableInit.config(text="虚拟机 {} 创建成功".format(name)) def undefine(): dom = conn.lookupByName(entryName.get()) dom.undefine() print("虚拟机 {} 已取消定义".format(entryName.get())) lableInit.config(text="虚拟机 {} 已取消定义".format(entryName.get())) def suspand(): dom = conn.lookupByName(entryName.get()) dom.suspend() print("虚拟机 {} 暂停成功".format(entryName.get())) lableInit.config(text="虚拟机 {} 已暂停".format(entryName.get())) def resume(): dom = conn.lookupByName(entryName.get()) dom.resume() print("虚拟机 {} 运行成功".format(entryName.get())) lableInit.config(text="虚拟机 {} 继续运行".format(entryName.get())) def destroy(): dom = conn.lookupByName(entryName.get()) dom.destroy() print("虚拟机 {} 销毁成功".format(entryName.get())) lableInit.config(text="虚拟机 {} 已销毁".format(entryName.get())) def start(): dom = conn.lookupByName(entryName.get()) dom.create() print("虚拟机 {} 启动成功".format(entryName.get())) lableInit.config(text="虚拟机 {} 已启动".format(entryName.get())) def shutdown(): dom = conn.lookupByName(entryName.get()) dom.shutdown() print("虚拟机 {} 关闭成功".format(entryName.get())) lableInit.config(text="虚拟机 {} 已关闭".format(entryName.get())) #------------菜单控制------------- def test(): name=str(entryName.get()) print(name) #-------------------------------- conn = libvirt.open("qemu:///system")#链接 top=tk.Tk() top.title("虚拟机管理平台") #top.geometry(‘602x‘)#窗体大小 lableTitle = tk.Label(top,bg=‘#00BFFF‘,fg="#EEE8AA",font = ‘Helvetica -24 bold‘,text="虚 拟 机 管 理 平 台",width=46) lableTitle.grid(row=0,column=0,columnspan=2,ipady=10) #------------------------------- labelHello = tk.Label(top,fg=‘#c71585‘,font = ‘Helvetica -18 bold‘, text = "请输入虚拟机的名称:") labelHello.grid(row=1,column=0,padx=2,pady=6,sticky=‘E‘) entryName=tk.Entry(top,width=24) entryName.grid(row=1,column=1,padx=2,sticky=‘W‘) #test #btn=tk.Button(top,text="显示",command=test) #btn.pack() #-------操作------ showHost =tk.Button(top,bg=‘#00BFFF‘,fg=‘white‘,width=18,height=2,font = ‘Helvetica -15 bold‘, text="显示主机信息",command=list_connection) showHost.grid(row=2,column=0,padx=4,pady=4,sticky=‘E‘) showVM =tk.Button(top,bg=‘#00BFFF‘,fg=‘white‘,width=18,height=2,font = ‘Helvetica -15 bold‘, text="显示虚拟机状态",command=show_machine) showVM.grid(row=2,column=1,padx=4,pady=4,sticky=‘W‘) tempVM =tk.Button(top,bg=‘#00BFFF‘,fg=‘white‘,width=18,height=2,font = ‘Helvetica -15 bold‘,text="创建虚拟机",command=temp) tempVM.grid(row=3,column=0,padx=4,pady=4,sticky=‘E‘) destroyVM = tk.Button(top,bg=‘#00BFFF‘,fg=‘white‘,width=18,height=2,font = ‘Helvetica -15 bold‘, text="销毁虚拟机", command=destroy) destroyVM.grid(row=3,column=1,padx=4,pady=4,sticky=‘W‘) suspandVM =tk.Button(top,bg=‘#00BFFF‘,fg=‘white‘,width=18,height=2,font = ‘Helvetica -15 bold‘,text="暂停虚拟机",command=suspand) suspandVM.grid(row=4,column=0,padx=4,pady=4,sticky=‘E‘) resumeVM = tk.Button(top,bg=‘#00BFFF‘,fg=‘white‘,width=18,height=2,font = ‘Helvetica -15 bold‘, text="运行虚拟机", command=resume) resumeVM.grid(row=4,column=1,padx=4,pady=4,sticky=‘W‘) startVM = tk.Button(top,bg=‘#00BFFF‘,fg=‘white‘,width=18,height=2,font = ‘Helvetica -15 bold‘, text="开启虚拟机", command=start) startVM.grid(row=5,column=0,padx=4,pady=4,sticky=‘E‘) shutdownVM = tk.Button(top,bg=‘#00BFFF‘,fg=‘white‘,width=18,height=2,font = ‘Helvetica -15 bold‘, text="关闭虚拟机", command=shutdown) shutdownVM.grid(row=5,column=1,padx=4,pady=4,sticky=‘W‘) defineVM =tk.Button(top,bg=‘#00BFFF‘,fg=‘white‘,width=18,height=2,font = ‘Helvetica -15 bold‘,text="创建虚拟机" "(永久创建)",command=define) defineVM.grid(row=6,column=0,padx=4,pady=4,sticky=‘E‘) undefineVM =tk.Button(top,bg=‘#00BFFF‘,fg=‘white‘,width=18,height=2,font = ‘Helvetica -15 bold‘,text="删除虚拟机" "(取消定义)",command=undefine) undefineVM.grid(row=6,column=1,padx=4,pady=4,sticky=‘W‘) lableInitTitle = tk.Label(top,bg=‘black‘,font = ‘Helvetica -13 bold‘,text="* * * 系 统 反 馈 信 息 * * *",width=85,fg="blue") lableInitTitle.grid(row=7,column=0,columnspan=2,ipady=10) lableInit = tk.Label(top,bg=‘black‘,font = ‘Helvetica -13 bold‘,text="NULL",width=85,fg="white") lableInit.grid(row=8,column=0,columnspan=2,ipady=10) photo=tk.PhotoImage(file="/home/ldw/kvmtest/copyright.png") labelphoto=tk.Label(image=photo) labelphoto.image=photo labelphoto.grid(row=9,column=0,columnspan=2) #quitVM = tk.Button(top,text="退出管理系统",command=close_connection()) #quitVM.pack() top.mainloop()
原文地址:https://www.cnblogs.com/nyist0/p/9157867.html
时间: 2024-10-31 07:27:57