forked from jroivas/klapi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
klapi.py
executable file
·441 lines (364 loc) · 12.3 KB
/
klapi.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
#!flask/bin/python
from flask import Flask, jsonify
from flask import abort
from flask import make_response
from flask import request
from flask import url_for
from flask.ext.httpauth import HTTPBasicAuth
import settings
from db import db
from images import images
from infra import infra
from actions import actions
import utils
import os
import setup
import json
app = Flask(__name__)
auth = HTTPBasicAuth()
api_version = 'v0.1'
api_prefix = 'klapi'
api_url = '/%s/%s' % (api_prefix, api_version)
_db = None
@auth.error_handler
def unauthorized():
return make_response(jsonify({'error': 'Unauthorized access'}), 401)
@app.before_request
def before_request():
setup.create_db()
_db = db.connect(settings.settings())
res = db.select(_db, 'users', ['pass'], 'name=\'%s\'' % ('admin'))
if res is None or not res:
admin_pass = utils.generatePassword(20)
db.insert(_db, 'users',
['admin',
admin_pass,
utils.generateApiKey()])
print ('Password for admin: "%s", keep this in safe place!\n' % (admin_pass))
@app.teardown_request
def teardown_request(exception):
if _db is not None:
_db.close()
@auth.get_password
def get_password(username):
_db = db.connect(settings.settings())
res = db.select(_db, 'users', ['pass'], 'name=\'%s\'' % (username))
for item in res:
return str(item[0])
return None
def abort_msg(code, msg):
abort(make_response(jsonify({'error': msg}), code))
@app.errorhandler(400)
def not_found400(error):
return make_response(jsonify({'error': 'Item does not exist'}), 400)
@app.errorhandler(404)
def not_found404(error):
return make_response(jsonify({'error': 'Not found'}), 404)
@app.route('/' + api_prefix, methods=['GET'])
def klapi():
return jsonify({api_prefix: api_version})
@app.route(api_url, methods=['GET'])
def klapi_version():
return jsonify({api_prefix + '/' + api_version: ['ids']})
@app.route(api_url + '/user', methods=['GET'])
@auth.login_required
def user():
data = {
'user': auth.username()
}
_db = db.connect(settings.settings())
res = db.select(_db, 'users', items=['apikey'], where='name=\'%s\'' % auth.username())
if res:
res = res[0]
data['api_key'] = res[0]
return jsonify(data)
@app.route(api_url + '/user', methods=['POST'])
@auth.login_required
def post_user():
if auth.username() != 'admin':
abort_msg(400, 'Access denied, admin needed!')
if not request.json:
abort_msg(400, 'Expected JSON parameters')
if 'user' not in request.json:
abort_msg(400, 'Value for "user" not defined')
user = request.json['user']
passwd = ''
if 'password' in request.json:
passwd = request.json['password']
if user == 'admin':
abort_msg(400, 'Can\'t create admin')
_db = db.connect(settings.settings())
res = db.select(_db, 'users', where='name=\'%s\'' % user)
if res:
abort_msg(400, 'User \"%s\" already exists' % user)
if not passwd:
passwd = utils.generatePassword(20)
db.insert(_db, 'users',
[user, passwd, utils.generateApiKey()])
data = {
'user': request.json['user'],
'password': passwd
}
return jsonify(data)
@app.route(api_url + '/user', methods=['DELETE'])
@auth.login_required
def delete_user():
if auth.username() != 'admin':
abort_msg(400, 'Access denied, admin needed!')
if not request.json:
abort_msg(400, 'Expected JSON parameters')
if 'user' not in request.json:
abort_msg(400, 'Value for "user" not defined')
user = request.json['user']
_db = db.connect(settings.settings())
res = db.select(_db, 'users', where='name=\'%s\'' % user)
if not res:
abort_msg(400, 'User \"%s\" does not exist' % user)
res = db.delete(_db, 'users', where='name=\'%s\'' % user)
data = {
'deleted': request.json['user'],
}
return jsonify(data)
@app.route(api_url + '/user', methods=['PUT'])
@auth.login_required
def put_user():
if not request.json:
abort_msg(400, 'Expected JSON parameters')
if 'password' in request.json:
passwd = request.json['password']
if not passwd:
passwd = utils.generatePassword(20)
_db = db.connect(settings.settings())
db.update(_db, 'users', 'pass="%s"' % (passwd), where='name="%s"' % auth.username())
data = {
'user': auth.username(),
'password': passwd
}
return jsonify(data)
if 'api_key' in request.json:
apikey = utils.generateApiKey()
_db = db.connect(settings.settings())
db.update(_db, 'users', 'apikey="%s"' % (apikey), where='name="%s"' % auth.username())
data = {
'user': auth.username(),
'api_key': apikey
}
return jsonify(data)
abort_msg(400, 'Expected "password" or "api_key"')
@app.route(api_url + '/machine', methods=['GET'])
@auth.login_required
def machine():
_db = db.connect(settings.settings())
user = auth.username()
if user == 'admin':
res = db.select(_db, 'machines')
else:
res = db.select(_db, 'machines', where='owner=\'%s\'' % user)
items = [x[0] for x in res]
return jsonify(
{
'machines': [
{
'uri': url_for('machine_id', machine_id=x, _external=True),
'id': x
}
for x in items]
})
@app.route(api_url + '/machine/<string:machine_id>', methods=['GET'])
@auth.login_required
def machine_id(machine_id):
user = auth.username()
_db = db.connect(settings.settings())
if user == 'admin':
res = db.select(_db, 'machines', where='id=\'%s\'' % machine_id)
else:
res = db.select(_db, 'machines', where='id=\'%s\' AND owner=\'%s\'' % (machine_id, user))
if not res:
abort_msg(400, 'Machine "%s" not found' % (machine_id))
res = res[0]
inf = infra.provider(settings.settings())
dom = inf.getDomain(res[0])
if not dom or dom is None:
abort(400)
data = {
'id': res[0],
'base': res[1],
'address': res[2],
'config': json.loads(res[4]),
'active': dom.isActive(),
'max-memory': dom.maxMemory(),
#'max-cpus': dom.maxVcpus(),
#'memory-stats': dom.memoryStats(),
#'info': dom.info(),
#'cpus': dom.vcpus(),
#'state': '%s' % dom.state(),
#'state': '%s' % dir(dom),
'state': inf.domState(dom),
'owner': res[3]
}
#data['vols'] = inf.deviceItems(dom, 'disk')
if dom.isActive():
data['max-cpus'] = dom.maxVcpus()
data['memory-stats'] = dom.memoryStats()
return jsonify(data)
@app.route(api_url + '/machine/<string:machine_id>', methods=['DELETE'])
@auth.login_required
def machine_del(machine_id):
user = auth.username()
_db = db.connect(settings.settings())
if user == 'admin':
res = db.select(_db, 'machines', where='id=\'%s\'' % machine_id)
else:
res = db.select(_db, 'machines', where='id=\'%s\' AND owner=\'%s\'' % (machine_id, user))
if not res:
abort_msg(400, 'Machine "%s" not found' % (machine_id))
res = res[0]
inf = infra.provider(settings.settings())
dom = inf.getDomain(res[0])
if not dom or dom is None:
abort(400)
if dom.isActive():
dom.destroy()
flags = 0
if dom.hasManagedSaveImage():
flags |= libvirt.VIR_DOMAIN_UNDEFINE_MANAGED_SAVE
if dom.snapshotNum() > 0:
flags |= VIR_DOMAIN_UNDEFINE_SNAPSHOTS_METADATA
vol_provider = images.volume_provider(settings.settings())
vols = inf.deviceItems(dom, 'disk')
error_msg = ''
try:
dom.undefineFlags(flags)
except:
error_msg = 'ERROR: Undefining domain: %s' % macine_id
print (error_msg)
error_msg += vol_provider.removeVolumes(vols)
_db = db.connect(settings.settings())
db.delete(_db, 'machines', where='id=\'%s\'' % machine_id)
data = {'removed': machine_id}
if error_msg:
data['error'] = error_msg
return jsonify(data)
@app.route(api_url + '/image', methods=['GET'])
@auth.login_required
def image():
img = images.provider(settings.settings())
return jsonify({'images': img.list()})
@app.route(api_url + '/image/<string:img_id>', methods=['GET'])
@auth.login_required
def image_id(img_id):
img = images.provider(settings.settings())
loc = img.get(img_id)
if loc is None:
abort(404)
res = {
'image': img_id,
'location': loc,
'uri': url_for('get_image', img_id=img_id, _external=True),
}
if loc.lower().endswith('.img'):
res['type'] = 'img'
elif loc.lower().endswith('.iso'):
res['type'] = 'iso'
elif loc.lower().endswith('.ext2'):
res['type'] = 'ext2'
return jsonify(res)
@app.route(api_url + '/image/get/<string:img_id>', methods=['GET'])
@auth.login_required
def get_image(img_id):
img = images.provider(settings.settings())
loc = img.get(img_id)
if loc is None:
abort(404)
with open(loc, 'r') as fd:
return fd.read()
@app.route(api_url + '/machine/<string:machine_id>', methods=['PUT'])
@auth.login_required
def put_machine(machine_id):
if not request.json:
abort_msg(400, 'Expected JSON input')
user = auth.username()
_db = db.connect(settings.settings())
if user == 'admin':
res = db.select(_db, 'machines', where='id=\'%s\'' % machine_id)
else:
res = db.select(_db, 'machines', where='id=\'%s\' AND owner=\'%s\'' % (machine_id, user))
if not res:
abort_msg(400, 'Machine "%s" not found' % (machine_id))
res = res[0]
inf = infra.provider(settings.settings())
dom = inf.getDomain(res[0])
if not dom or dom is None:
abort_msg(400, 'Machine "%s" not in backedn' % (machine_id))
if 'operation' not in request.json:
abort_msg(400, 'No operation defined')
data = {}
if request.json['operation'] == 'reboot':
data['result'] = dom.reboot()
elif request.json['operation'] == 'reset':
data['result'] = dom.reset()
elif request.json['operation'] == 'shutdown':
data['result'] = dom.shutdown()
elif request.json['operation'] == 'start':
st = dom.state()[0]
if st in [0,4,5,6]:
data['result'] = dom.create()
else:
abort_msg(400, 'Can\'t start, invalid state: %s' % (inf.domState(dom)))
elif request.json['operation'] == 'forceoff':
data['result'] = dom.destroy()
elif request.json['operation'] == 'suspend':
data['result'] = dom.suspend()
elif request.json['operation'] == 'resume':
data['result'] = dom.resume()
else:
abort_msg(400, 'Unknown operation: %s' % (request.json['operation']))
data[request.json['operation']] = machine_id
return jsonify(data)
@app.route(api_url + '/machine', methods=['POST'])
@auth.login_required
def post_machine():
if not request.json:
abort_msg(400, 'Expected JSON input')
# TODO: quota per user
res = {
'memory': 256 * 1024, # FIXME some reasonable default
'cpus': 1,
'name': utils.generateID(),
'net': '',
'image': '',
'size': '',
'cdrom': '',
}
if 'mem' in request.json:
res['memory'] = request.json['mem']
if 'memory' in request.json:
res['memory'] = request.json['memory']
if 'size' in request.json:
res['size'] = request.json['size']
if 'cpus' in request.json:
try:
res['cpus'] = int(request.json['cpus'])
except:
pass
if 'image' in request.json:
res['image'] = request.json['image']
if 'cdrom' in request.json:
res['cdrom'] = request.json['cdrom']
if 'name' in request.json:
res['name'] = request.json['name']
_db = db.connect(settings.settings())
db.insert(_db, 'machines', [res['name'], '', '', auth.username(), ''])
act = actions.provider(settings.settings())
act.sendActionNow({'createMachine': res}, None)
data = {
'uri': url_for('machine_id', machine_id=res['name'], _external=True),
'id': res['name']
}
return jsonify(data)
if __name__ == '__main__':
port = 5050
sets = settings.settings()
if 'server_port' in sets:
port = int(sets['server_port'])
app.run(host='0.0.0.0', port=port, debug=True)