libvirt-webctl/main.py
2021-04-15 18:04:33 +00:00

59 lines
1.7 KiB
Python

#!/usr/bin/python3
from bottle import run, route, request, template,get ,post
import libvirt, json
import urllib.request, json
session = libvirt.open('qemu:///system')
url_path = "http://localhost:5000/list"
def listVDS():
VdsListVar = list()
for domain in session.listAllDomains(0):
VdsListVar.append(dict(name=domain.name(),state=domain.isActive()))
return json.dumps(VdsListVar)
# @route('/list')
# def index():
# return str(listVDS())
@route('/')
def index():
# domainList = json.loads(urllib.request.urlopen(url_path).read().decode())
return template('index', domainList=json.loads(listVDS()))
@post('/action') # or @route('/login', method='POST')
def do_action():
act = request.forms.get('act')
dom = session.lookupByName(request.forms.get('name'))
if (act == "start"):
dom.create()
return dict(name=request.forms.get('name'),state="started")
elif (act == "stop"):
dom.shutdown()
return dict(name=request.forms.get('name'),state="shutdown")
elif (act == "destroy"):
dom.destroy()
return dict(name=request.forms.get('name'),state="destroyed")
else:
return dict(state="error")
@get('/action') # or @route('/login')
def login():
return '''
<form action="/action" method="post">
VDS Name: <input name="name" 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>
'''
if __name__ == '__main__':
run(host='0.0.0.0', port=5000, debug=True)