#!/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 '''
VDS Name:
Action (start/stop):
Start
Stop
destroy
''' if __name__ == '__main__': run(host='0.0.0.0', port=5000, debug=True)