libvirt-webctl/main.py

59 lines
1.7 KiB
Python
Raw Permalink Normal View History

2021-03-24 22:05:34 +03:00
#!/usr/bin/python3
2021-03-24 23:27:57 +03:00
from bottle import run, route, request, template,get ,post
2021-03-26 00:04:24 +03:00
import libvirt, json
2021-04-15 21:04:33 +03:00
import urllib.request, json
2021-03-24 23:27:57 +03:00
session = libvirt.open('qemu:///system')
2021-04-15 21:04:33 +03:00
url_path = "http://localhost:5000/list"
2021-03-24 23:27:57 +03:00
def listVDS():
VdsListVar = list()
2021-03-25 23:30:11 +03:00
for domain in session.listAllDomains(0):
VdsListVar.append(dict(name=domain.name(),state=domain.isActive()))
2021-03-26 00:04:24 +03:00
return json.dumps(VdsListVar)
2021-03-24 23:27:57 +03:00
2021-04-15 21:04:33 +03:00
# @route('/list')
# def index():
# return str(listVDS())
2021-03-24 23:27:57 +03:00
@route('/')
def index():
2021-04-15 21:04:33 +03:00
# domainList = json.loads(urllib.request.urlopen(url_path).read().decode())
return template('index', domainList=json.loads(listVDS()))
2021-03-24 23:27:57 +03:00
@post('/action') # or @route('/login', method='POST')
def do_action():
act = request.forms.get('act')
2021-03-25 20:37:24 +03:00
dom = session.lookupByName(request.forms.get('name'))
2021-03-24 23:27:57 +03:00
if (act == "start"):
2021-03-24 22:05:34 +03:00
dom.create()
2021-03-25 20:37:24 +03:00
return dict(name=request.forms.get('name'),state="started")
2021-03-24 23:27:57 +03:00
elif (act == "stop"):
2021-03-24 22:05:34 +03:00
dom.shutdown()
2021-03-25 20:37:24 +03:00
return dict(name=request.forms.get('name'),state="shutdown")
2021-03-24 23:27:57 +03:00
elif (act == "destroy"):
dom.destroy()
2021-03-25 20:37:24 +03:00
return dict(name=request.forms.get('name'),state="destroyed")
2021-03-24 23:27:57 +03:00
else:
2021-03-25 20:37:24 +03:00
return dict(state="error")
2021-03-24 22:05:34 +03:00
2021-03-26 00:03:44 +03:00
@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>
'''
2021-03-24 22:05:34 +03:00
if __name__ == '__main__':
2021-03-24 23:27:57 +03:00
run(host='0.0.0.0', port=5000, debug=True)