-
Notifications
You must be signed in to change notification settings - Fork 0
/
ipxe_app.py
80 lines (56 loc) · 2.06 KB
/
ipxe_app.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
#!/usr/bin/python
import datetime
import time
from flask import Flask, jsonify, render_template, request, url_for
from ipxe_logic import db_connection, db_transaction, ServerForm
app = Flask(__name__)
INSERT_SQL = """
INSERT INTO server
(mac, hostname, state, profile, created, updated)
values(?, ?, ?, ?, ?, ?)
"""
SELECT_SQL = """SELECT mac, hostname, state, profile, created, updated FROM server"""
UPDATE_SQL = """
UPDATE server SET
hostname = ?,
state = ?,
profile = ?,
updated = ?
WHERE mac = ? and updated = ?
"""
VALID_PROFILES = []
VALID_STATES = []
def create_server(): pass
@app.route('/servers/<mac>', methods=['GET'])
def get_server(mac):
with db_connection('./ipxe.sqlite3') as conn:
with db_transaction(conn) as cursor:
sql = "%s WHERE mac=?"
server = cursor.execute(sql % SELECT_SQL, (mac,)).fetchone()
if server is None: return jsonify({}), 404
server_dict = dict(mac=server[0],
hostname=server[1],
state=server[2],
profile=server[3],
created=server[4]*1000,
updated=server[5]*1000)
return jsonify(server_dict)
def update_server(mac): pass
@app.route('/servers', methods=['GET'])
def servers():
with db_connection('./ipxe.sqlite3') as conn:
with db_transaction(conn) as cursor:
servers = cursor.execute(SELECT_SQL).fetchall()
servers_list = []
for server in servers:
server_dict = dict(mac=server[0],
hostname=server[1],
state=server[2],
profile=server[3],
created=server[4]*1000,
updated=server[5]*1000)
servers_list.append(server_dict)
return jsonify(servers=servers_list)
port = 5000
if __name__ == '__main__':
app.run(host='0.0.0.0', port=int(port), debug=True)