# -*- coding: utf-8 -*-
import Check_Disk
import ConfigParser
import time
import sys
if __name__=='__main__':
#时间格式
format="%Y%m%d-%H%M%S"
#输出查找的ip
son_ip=raw_input("Please enter vm ip:")
# son_ip="192.168.3.69"
#取配置文件的值
cf=ConfigParser.ConfigParser()
cf.read("e:\snap.conf.txt")
port=(cf.getint("snapshot","port"))
username=cf.get("snapshot","username")
pkey=cf.get("snapshot","pkey")
cmd=cf.get("snapshot","cmd")
hostname=(cf.get("snapshot","hostname")).split(",")
#查找ip,查看快照
while True:
for i in hostname:
result_list=Check_Disk.ssh2(i,port,username,pkey,cmd)
for j in range(0,len(result_list.split('\n'))):
if son_ip in result_list.split('\n')[j]:
print result_list.split('\n')[j]+" in "+i
#取镜像实例名
count=0
new_dict={}
a=result_list.split('\n')[j].split('.xml')[0]
cmd2="virsh snapshot-list"+" "+(result_list.split('\n')[j]).split('.xml')[0]
www=Check_Disk.ssh2(i,port,username,pkey,cmd2)
fo=open("e:\yanchao.txt","w")
fo.writelines(())
fo.close()
fo=open("e:\yanchao.txt","r+")
b=fo.readlines()
for j in range(2,len(b)):
count+=1
c=b[j]
d=c.split()
print "[",count,"]",d
new_dict[count]=d[0]
fo.truncate()
fo.close()
#进行快照还是恢复快照
choice=raw_input("1.Restore or 2.Snapshot or 3.Quit:")
if choice == "1":
print new_dict
choice1=int(raw_input("please enter which one to be restored?"))
choice2=raw_input("Are you ture? yes/no")
if choice2=="yes":
cmd3="virsh snapshot-revert "+a+" "+new_dict[choice1]+" --force"+" && "+"virsh destroy "+a+" && "+"virsh start "+a
Check_Disk.ssh2(i,port,username,pkey,cmd3)
continue
else:
continue
elif choice == "2":
choice2=raw_input("Are you ture? yes/no")
if choice2=="yes":
cmd3="virsh snapshot-create-as --domain "+a+" --name "+time.strftime(format,time.localtime(time.time()))
Check_Disk.ssh2(i,port,username,pkey,cmd3)
continue
else:
continue
elif choice == "3":
sys.exit()
snap.conf.txt