-
Notifications
You must be signed in to change notification settings - Fork 0
/
fake-infra.py
90 lines (81 loc) · 3.15 KB
/
fake-infra.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
#!/usr/bin/env python3
from http.server import BaseHTTPRequestHandler, HTTPServer
from urllib.parse import urlparse
import json
import threading
ssh_pub_key = ''
class ServiceServer(BaseHTTPRequestHandler):
def do_GET(self):
mach = {
'uuid': '00000000-0000-0000-0000-000000000001',
'name': 'qemu',
'installerAddress': 'localhost',
'sshPorts': {
'installer': '5555',
},
'managedBy': 'http://localhost:7707/',
}
p = urlparse(self.path)
if p.path.endswith('/v1/machines'):
self.send_response(200)
self.send_header('Content-type', 'application/json')
self.end_headers()
res = [mach]
self.wfile.write(json.dumps(res).encode())
elif p.path.startswith('/v1/machines/00000000-0000-0000-0000-000000000001'):
self.send_response(200)
self.send_header('Content-type', 'application/json')
self.end_headers()
self.wfile.write(json.dumps(mach).encode())
else:
self.send_response(404)
self.end_headers()
self.wfile.write(b'404 not found')
def do_POST(self):
global ssh_pub_key
if self.path.endswith('installer-callback'):
if not ssh_pub_key:
self.send_response(204)
self.send_header('Content-type', 'application/json')
self.end_headers()
self.wfile.write('{}'.encode())
else:
self.send_response(200)
self.send_header('Content-type', 'application/json')
self.end_headers()
res = {'ssh_pub_key': ssh_pub_key}
self.wfile.write(json.dumps(res).encode())
class ManagerServer(BaseHTTPRequestHandler):
def do_POST(self):
global ssh_pub_key
if self.path.endswith('boot-installer'):
print(' > Boot installation request')
length = int(self.headers['content-length'])
reqd = self.rfile.read(length)
d = json.loads(reqd)
ssh_pub_key = d['ssh_pub_key']
print(' > Updated SSH public key:', ssh_pub_key)
self.send_response(200)
self.send_header('Content-type', 'text/plain')
self.end_headers()
self.wfile.write(b'')
else:
self.send_response(404)
self.end_headers()
self.wfile.write(b'404 not found')
def do_GET(self):
if self.path.startswith('/v1/machines'):
self.send_response(200)
self.send_header('Content-type', 'application/json')
self.end_headers()
res = [{
'uuid': '00000000-0000-0000-0000-000000000001',
'name': 'qemu',
}]
self.wfile.write(json.dumps(res).encode())
if __name__ == '__main__':
h = HTTPServer(('127.0.0.1', 7708), ServiceServer)
threading.Thread(target=h.serve_forever, daemon=True).start()
m = HTTPServer(('127.0.0.1', 7707), ManagerServer)
print('Tateru Development Service started')
m.serve_forever()