-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
214 lines (191 loc) · 6.82 KB
/
main.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
import flask
from src.db import app, db
from flask import jsonify, Response, request
from src.vm import VM
import json, signal, sys
db.create_all()
def get_vm(uuid):
return VM.query.get(int(uuid))
def add_vm(vm):
global vm_pool
if VM.query.filter(VM.uuid==vm.getUuid()).count() != 0:
return False
else:
db.session.add(vm)
db.session.commit()
return True
def delete_vm(uuid):
if VM.query.filter(VM.uuid==uuid).count() != 0:
obj = VM.query.filter(VM.uuid==uuid).first()
db.session.delete(obj)
db.session.commit()
return True
else:
return False
@app.route('/', methods = ['GET'])
def status():
response = {"status" : "OK"}
return jsonify(response);
@app.route('/status', methods = ['GET'])
def status_vm():
vm_pool = VM.query.all()
num_vm_alive = 0
ram_used = 0
total_vcpu = 0
vcpu_in_use = 0
ram_in_use = 0
uuids = []
for i in vm_pool:
uuids.append(i.getUuid())
ram_used += i.getRAM()
total_vcpu += i.getVCPU()
if(i.isAlive()):
num_vm_alive += 1
vcpu_in_use += i.getVCPU()
ram_in_use += i.getRAM()
response = {"status" : "OK",
"vm_pool_status" :
{
"total_vm" : len(vm_pool),
"VMs_alive" : num_vm_alive,
"total_ram" : ram_used,
"total_vcpu" : total_vcpu,
"in_use_ram" : ram_in_use,
"in_use_vcpu" : vcpu_in_use,
"vm_UUIDs" : uuids
}
}
return jsonify(response);
@app.route('/vm/<uuid>', methods = ['GET', 'POST', 'PUT', 'DELETE'])
def serve_vm(uuid):
if flask.request.method == 'GET':
vm = get_vm(uuid)
if vm != None:
respones = str(vm)
return Response(respones,mimetype="application/json")
else:
response = "Failed VM not found"
return return_status(response,404)
elif flask.request.method == 'PUT':
return register_vm(int(uuid))
elif flask.request.method == 'POST':
vm = get_vm(uuid)
if vm != None:
state={
'ip' : request.form.get('ip'),
'vcpu' : request.form.get('vcpu'),
'nombre' : request.form.get('nombre'),
'mac' : request.form.get('mac'),
'alive' : request.form.get('alive'),
'ram' : request.form.get('ram'),
'vmdsk' : request.form.get('vmdsk')
}
return edit_vm(vm,state)
else:
response = "Failed VM not found"
return return_status(response,404)
elif flask.request.method == 'DELETE':
if delete_vm(uuid):
return return_status('Máquina borrada correctamente',200)
else:
response = "Failed VM not found"
return return_status(response,404)
@app.route('/vm/<uuid>/<resource>', methods = ['GET'])
def serve_vm_resource(uuid, resource):
vm = get_vm(uuid)
if vm != None:
resource = str(resource).lower()
response = {}
if resource == 'ip':
response['ip'] = vm.getIP()
elif resource == 'vcpu':
response['vcpu'] = vm.getVCPU()
elif resource == 'nombre':
response['nombre'] = vm.getNombre()
elif resource == 'mac':
response['mac'] = vm.getMAC()
elif resource == 'isalive':
response['alive'] = vm.isAlive()
elif resource == 'ram':
response['ram'] = vm.getRAM()
elif resource == 'vmdsk':
response['vmdsk'] = vm.getVmdsk()
else:
return return_status('Resource not found',404)
response['success'] = True
return jsonify(response)
else:
response = "Failed VM not found"
return return_status(response,404)
def edit_vm(vm,state):
if vm != None:
response = []
errors = []
if(state.get('ip')):
if vm.setIP(str(state.get('ip'))):
response.append("Successful change of ip")
else:
errors.append("Failed to change ip. Bad Formatted")
elif(state.get('nombre')):
if vm.setNombre(str(state.get('nombre'))):
response.append("Successful change of nombre")
else:
errors.append("Failed to change Nombre. Empty")
elif(state.get('mac')):
if vm.setNombre(str(state.get('mac'))):
response.append("Successful change of MAC")
else:
errors.append("Failed to change MAC. Bad Formatted")
elif(state.get('alive')):
if vm.setAlive(state.get('alive')):
response.append("Successful change of Alive State")
else:
errors.append("Failed to change Alive State. Boolean only")
elif(state.get('vcpu')):
if vm.setVCPU(state.get('vcpu')):
response.append("Successful change of VCPU")
else:
errors.append("Failed to change VCPU. Only integer positive")
elif(state.get('ram')):
if vm.setRAM(state.get('ram')):
response.append("Successful change of RAM")
else:
errors.append("Failed to change RAM. Only integer positive")
elif(state.get('vmdsk')):
if vm.setVmdsk(str(state.get('vmdsk'))):
response.append("Successful change of VMDSK path")
else:
errors.append("Failed to change VMDSK path. VMDSK path not accesible")
else:
errors.append("Bad URL")
db.session.commit()
res = {}
res['success'] = True if len(errors) == 0 else False
res['messages'] = list(response)
if len(errors) != 0:
res['errors'] = list(errors)
return jsonify(res)
else:
response = "Failed to change ip. UUID not found"
return return_status(response,404)
def register_vm(uuid):
uuid = int(uuid)
vm = get_vm(uuid)
if vm == None:
vm = VM()
vm.setUuid(uuid)
add_vm(vm)
return return_status("Máquina registrada", 200)
else:
return return_status("Máquina ya existente", 304)
def return_status(message, code):
if code >= 200 and code < 300:
success = True
else:
success = False
response = {'success' : success,
'code' : code,
'message' : message}
return jsonify(response);
if __name__ == '__main__':
app.run()