changes!
This commit is contained in:
parent
a21d87384c
commit
8417bb4255
52
index.py
52
index.py
@ -1,52 +0,0 @@
|
|||||||
#!/usr/bin/python3
|
|
||||||
|
|
||||||
from bottle import run, route, request, template,get ,post
|
|
||||||
import libvirt
|
|
||||||
session = libvirt.open('qemu:///system')
|
|
||||||
|
|
||||||
def listVDS():
|
|
||||||
VdsListVar = list()
|
|
||||||
for i in session.listDefinedDomains():
|
|
||||||
VdsListVar.append(i)
|
|
||||||
return VdsListVar
|
|
||||||
|
|
||||||
@route('/')
|
|
||||||
def index():
|
|
||||||
# return 'Привет, мир!'
|
|
||||||
return str(listVDS())
|
|
||||||
|
|
||||||
|
|
||||||
@get('/action') # or @route('/login')
|
|
||||||
def login():
|
|
||||||
return '''
|
|
||||||
<form action="/action" method="post">
|
|
||||||
VDS Name: <input name="username" type="text" /> <br>
|
|
||||||
Action (start/stop): <br>
|
|
||||||
<input type="radio" name="act" value="start"> Start<Br>
|
|
||||||
<input type="radio" name="act" value="stop"> Stop<Br>
|
|
||||||
<input type="radio" name="act" value="destroy"> destroy<Br>
|
|
||||||
<input value="Exec" type="submit" />
|
|
||||||
</form>
|
|
||||||
'''
|
|
||||||
|
|
||||||
|
|
||||||
@post('/action') # or @route('/login', method='POST')
|
|
||||||
def do_action():
|
|
||||||
act = request.forms.get('act')
|
|
||||||
dom = session.lookupByName(request.forms.get('username'))
|
|
||||||
|
|
||||||
|
|
||||||
if (act == "start"):
|
|
||||||
dom.create()
|
|
||||||
return "<p>Started "+request.forms.get('username')+" succesfully!</p><a href='/action'>back</a>"
|
|
||||||
elif (act == "stop"):
|
|
||||||
dom.shutdown()
|
|
||||||
return "<p>Stopped "+request.forms.get('username')+"</p> <a href='/action'>back</a>"
|
|
||||||
elif (act == "destroy"):
|
|
||||||
dom.destroy()
|
|
||||||
return "<p>Destroyed "+request.forms.get('username')+"</p> <a href='/action'>back</a>"
|
|
||||||
else:
|
|
||||||
return "Error"
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
|
||||||
run(host='0.0.0.0', port=5000, debug=True)
|
|
114
main.py
114
main.py
@ -1,80 +1,52 @@
|
|||||||
#!/usr/bin/python3
|
#!/usr/bin/python3
|
||||||
|
|
||||||
import time
|
from bottle import run, route, request, template,get ,post
|
||||||
import argparse
|
|
||||||
import libvirt
|
import libvirt
|
||||||
from lxml import etree
|
session = libvirt.open('qemu:///system')
|
||||||
from lxml.builder import E
|
|
||||||
|
|
||||||
|
def listVDS():
|
||||||
def parse_args():
|
VdsListVar = list()
|
||||||
parser = argparse.ArgumentParser(description='Create snapshot for libvirt machine')
|
|
||||||
parser.add_argument('--name', required=True, help='Machine name')
|
|
||||||
parser.add_argument('--device', required=True, help='Device path (like vda)')
|
|
||||||
return parser.parse_args()
|
|
||||||
|
|
||||||
def create_snapshot_node(disk_node):
|
|
||||||
device = disk_node.xpath('/domain/devices/disk/target/@dev')[0]
|
|
||||||
disk_path = disk_node.xpath('/domain/devices/disk/source/@file')[0]
|
|
||||||
|
|
||||||
now = int(time.time())
|
|
||||||
snap_path = disk_path.rsplit('.', 1)[0]
|
|
||||||
snap_path = f'{snap_path}-{now}.snap'
|
|
||||||
snap_name = snap_path.rsplit('/', 1)[1]
|
|
||||||
|
|
||||||
snap = E.domainsnapshot()
|
|
||||||
snap.append(E.name(snap_name))
|
|
||||||
|
|
||||||
disks = E.disks()
|
|
||||||
disk = E.disk(name=device)
|
|
||||||
disk.append(E.source(file=snap_path))
|
|
||||||
disks.append(disk)
|
|
||||||
|
|
||||||
snap.append(disks)
|
|
||||||
return snap
|
|
||||||
|
|
||||||
def get_disk_by_name(vm, name):
|
|
||||||
raw_xml = vm.XMLDesc(0)
|
|
||||||
xml = etree.XML(raw_xml)
|
|
||||||
disks = xml.xpath(f'/domain/devices/disk/target[@dev="{name}"]/..')
|
|
||||||
return disks[0]
|
|
||||||
|
|
||||||
def main():
|
|
||||||
# args = parse_args()
|
|
||||||
session = libvirt.open('qemu:///system')
|
|
||||||
tgtDom = 1
|
|
||||||
from time import sleep
|
|
||||||
for i in session.listDefinedDomains():
|
for i in session.listDefinedDomains():
|
||||||
print(i)
|
VdsListVar.append(i)
|
||||||
dom = session.lookupByName(i)
|
return VdsListVar
|
||||||
|
|
||||||
print("Is active? "+ str(dom.isActive()))
|
@route('/')
|
||||||
|
def index():
|
||||||
|
# return 'Привет, мир!'
|
||||||
|
return str(listVDS())
|
||||||
|
|
||||||
|
|
||||||
|
@get('/action') # or @route('/login')
|
||||||
|
def login():
|
||||||
|
return '''
|
||||||
|
<form action="/action" method="post">
|
||||||
|
VDS Name: <input name="username" type="text" /> <br>
|
||||||
|
Action (start/stop): <br>
|
||||||
|
<input type="radio" name="act" value="start"> Start<Br>
|
||||||
|
<input type="radio" name="act" value="stop"> Stop<Br>
|
||||||
|
<input type="radio" name="act" value="destroy"> destroy<Br>
|
||||||
|
<input value="Exec" type="submit" />
|
||||||
|
</form>
|
||||||
|
'''
|
||||||
|
|
||||||
|
|
||||||
|
@post('/action') # or @route('/login', method='POST')
|
||||||
|
def do_action():
|
||||||
|
act = request.forms.get('act')
|
||||||
|
dom = session.lookupByName(request.forms.get('username'))
|
||||||
|
|
||||||
|
|
||||||
|
if (act == "start"):
|
||||||
dom.create()
|
dom.create()
|
||||||
sleep(15)
|
return "<p>Started "+request.forms.get('username')+" succesfully!</p><a href='/action'>back</a>"
|
||||||
|
elif (act == "stop"):
|
||||||
print(session.listDomainsID())
|
|
||||||
print("Active? "+str(dom.isActive()))
|
|
||||||
sleep(15)
|
|
||||||
dom.shutdown()
|
dom.shutdown()
|
||||||
|
return "<p>Stopped "+request.forms.get('username')+"</p> <a href='/action'>back</a>"
|
||||||
sleep(15)
|
elif (act == "destroy"):
|
||||||
print("Active? "+str(dom.isActive()))
|
dom.destroy()
|
||||||
|
return "<p>Destroyed "+request.forms.get('username')+"</p> <a href='/action'>back</a>"
|
||||||
# print(session.getHostname())
|
else:
|
||||||
|
return "Error"
|
||||||
|
|
||||||
|
|
||||||
# try:
|
|
||||||
# vm = session.lookupByName(args.name)
|
|
||||||
# except libvirt.libvirtError:
|
|
||||||
# print(f'Machine with name {args.name} not found!')
|
|
||||||
# session.close()
|
|
||||||
# return
|
|
||||||
# disk = get_disk_by_name(vm, args.device)
|
|
||||||
# snapshot = create_snapshot_node(disk)
|
|
||||||
# vm.snapshotCreateXML(etree.tostring(snapshot, encoding='unicode'),
|
|
||||||
# libvirt.VIR_DOMAIN_SNAPSHOT_CREATE_DISK_ONLY | libvirt.VIR_DOMAIN_SNAPSHOT_CREATE_ATOMIC)
|
|
||||||
session.close()
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
main()
|
run(host='0.0.0.0', port=5000, debug=True)
|
80
snap-pg.py
Normal file
80
snap-pg.py
Normal file
@ -0,0 +1,80 @@
|
|||||||
|
#!/usr/bin/python3
|
||||||
|
|
||||||
|
import time
|
||||||
|
import argparse
|
||||||
|
import libvirt
|
||||||
|
from lxml import etree
|
||||||
|
from lxml.builder import E
|
||||||
|
|
||||||
|
|
||||||
|
def parse_args():
|
||||||
|
parser = argparse.ArgumentParser(description='Create snapshot for libvirt machine')
|
||||||
|
parser.add_argument('--name', required=True, help='Machine name')
|
||||||
|
parser.add_argument('--device', required=True, help='Device path (like vda)')
|
||||||
|
return parser.parse_args()
|
||||||
|
|
||||||
|
def create_snapshot_node(disk_node):
|
||||||
|
device = disk_node.xpath('/domain/devices/disk/target/@dev')[0]
|
||||||
|
disk_path = disk_node.xpath('/domain/devices/disk/source/@file')[0]
|
||||||
|
|
||||||
|
now = int(time.time())
|
||||||
|
snap_path = disk_path.rsplit('.', 1)[0]
|
||||||
|
snap_path = f'{snap_path}-{now}.snap'
|
||||||
|
snap_name = snap_path.rsplit('/', 1)[1]
|
||||||
|
|
||||||
|
snap = E.domainsnapshot()
|
||||||
|
snap.append(E.name(snap_name))
|
||||||
|
|
||||||
|
disks = E.disks()
|
||||||
|
disk = E.disk(name=device)
|
||||||
|
disk.append(E.source(file=snap_path))
|
||||||
|
disks.append(disk)
|
||||||
|
|
||||||
|
snap.append(disks)
|
||||||
|
return snap
|
||||||
|
|
||||||
|
def get_disk_by_name(vm, name):
|
||||||
|
raw_xml = vm.XMLDesc(0)
|
||||||
|
xml = etree.XML(raw_xml)
|
||||||
|
disks = xml.xpath(f'/domain/devices/disk/target[@dev="{name}"]/..')
|
||||||
|
return disks[0]
|
||||||
|
|
||||||
|
def main():
|
||||||
|
# args = parse_args()
|
||||||
|
session = libvirt.open('qemu:///system')
|
||||||
|
tgtDom = 1
|
||||||
|
from time import sleep
|
||||||
|
for i in session.listDefinedDomains():
|
||||||
|
print(i)
|
||||||
|
dom = session.lookupByName(i)
|
||||||
|
|
||||||
|
print("Is active? "+ str(dom.isActive()))
|
||||||
|
dom.create()
|
||||||
|
sleep(15)
|
||||||
|
|
||||||
|
print(session.listDomainsID())
|
||||||
|
print("Active? "+str(dom.isActive()))
|
||||||
|
sleep(15)
|
||||||
|
dom.shutdown()
|
||||||
|
|
||||||
|
sleep(15)
|
||||||
|
print("Active? "+str(dom.isActive()))
|
||||||
|
|
||||||
|
# print(session.getHostname())
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
# try:
|
||||||
|
# vm = session.lookupByName(args.name)
|
||||||
|
# except libvirt.libvirtError:
|
||||||
|
# print(f'Machine with name {args.name} not found!')
|
||||||
|
# session.close()
|
||||||
|
# return
|
||||||
|
# disk = get_disk_by_name(vm, args.device)
|
||||||
|
# snapshot = create_snapshot_node(disk)
|
||||||
|
# vm.snapshotCreateXML(etree.tostring(snapshot, encoding='unicode'),
|
||||||
|
# libvirt.VIR_DOMAIN_SNAPSHOT_CREATE_DISK_ONLY | libvirt.VIR_DOMAIN_SNAPSHOT_CREATE_ATOMIC)
|
||||||
|
session.close()
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
main()
|
@ -1,5 +1,5 @@
|
|||||||
#!/usr/bin/python3
|
#!/usr/bin/python3
|
||||||
|
# Script by Александр Дрожевский from Rebrain
|
||||||
import time
|
import time
|
||||||
import argparse
|
import argparse
|
||||||
import libvirt
|
import libvirt
|
||||||
|
Loading…
Reference in New Issue
Block a user