-
Notifications
You must be signed in to change notification settings - Fork 2
/
lxcweb.py
63 lines (51 loc) · 1.58 KB
/
lxcweb.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
from flask import Flask, redirect, url_for, \
render_template
import lxc
# create our little application :)
app = Flask(__name__)
app.config.from_object(__name__)
app.config.from_envvar('FLASKR_SETTINGS', silent=True)
def machinesorter(m):
order = ['RUNNING', 'FROZEN', 'PAUSED']
if m.state in order:
nr = order.index(m.state)
else:
nr = 9
return "%s%s" % (nr, m.name)
@app.route('/')
def show_container():
classmap = {'RUNNING': 'text-success',
'STOPPED': 'text-error',
}
names = lxc.list_containers()
machines = list()
for name in names:
machine = lxc.Container(name)
machine.klass = classmap.get(machine.state, 'text-warning')
machines.append(machine)
return render_template('overview.html', machines=sorted(machines, key=machinesorter))
@app.route('/<name>/')
def info(name):
return render_template('details.html', machine=lxc.Container(name))
@app.route('/<name>/<action>')
def action(name, action):
m = lxc.Container(name)
result = getattr(m, action)()
if action != "info":
return redirect(url_for('show_container'))
else:
return result
@app.route('/<name>/delete')
def deleteMachine(name):
m = lxc.Container(name)
if m.running:
m.stop()
m.destroy()
return redirect(url_for('show_container'))
@app.route('/<name>/clone/<newname>')
def clone(name, newname):
m = lxc.Container(name)
m.clone(newname)
return redirect(url_for('show_container'))
if __name__ == '__main__':
app.run(debug=True, host="0.0.0.0")