-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
executable file
·146 lines (92 loc) · 3.24 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
#!/usr/bin/env python3
import hypervisor_api
from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware
import uvicorn
import os
import json
import sys
from loguru import logger
from dotenv import load_dotenv
load_dotenv()
# Get ENV variables
SERVER_PORT = os.getenv("SERVER_PORT")
HYPERVISOR = os.getenv("HYPERVISOR")
# Get build information
with open("build_info.json", "r") as f:
BUILD_INFO = f.read()
BUILD_INFO = json.loads(BUILD_INFO)
API_VERSION = BUILD_INFO["api_version"]
# Set up API
ironsight_api = FastAPI()
# Set up hypervisor object
hypervisor = hypervisor_api.init_hypervisor(HYPERVISOR)
# Set up API routes
@ironsight_api.get("/")
async def root():
return {"description": "Ironsight API", "api_version": API_VERSION, "hypervisor": hypervisor.get_summary()}
@ironsight_api.get("/health")
async def health():
return {"status": "ok"}
# Set up hypervisor APIs
# Node management APIs
@ironsight_api.get("/nodes")
async def get_nodes():
return hypervisor.get_nodes()
@ironsight_api.get("/nodes/{node_name}")
async def get_node(node_name: str):
return hypervisor.get_node(node_name)
@ironsight_api.get("/nodes/{node_name}/vms")
async def get_vms(node_name: str):
return hypervisor.get_vms_on_node(node_name)
@ironsight_api.get("/nodes/{node_name}/vms/{vm_id}")
async def get_vm(node_name: str, vm_id: int):
return hypervisor.get_vm_by_id(node_name, vm_id)
# Template management APIs
@ironsight_api.get("/templates")
async def get_templates():
return hypervisor.get_templates()
# VM management APIs
@ironsight_api.get("/vms")
async def get_vms():
return hypervisor.get_vms()
@ironsight_api.get("/vms/{vm_name}")
async def get_vm_by_name(vm_name: str):
return hypervisor.get_vm_by_name(vm_name)
@ironsight_api.post("/vms/{vm_name}/start")
async def start_vm(vm_name: str):
return hypervisor.start_vm(vm_name)
@ironsight_api.post("/vms/{vm_name}/stop")
async def stop_vm(vm_name: str):
return hypervisor.stop_vm(vm_name)
@ironsight_api.post("/vms/{vm_name}/toggle_power")
async def power_toggle_vm(vm_name: str):
return hypervisor.power_toggle_vm(vm_name)
@ironsight_api.post("/vms/{vm_name}/reboot")
async def reboot_vm(vm_name: str):
return hypervisor.reboot_vm(vm_name)
@ironsight_api.post("/vms/create")
async def create_vm(vm_name: str, template_name: str):
return hypervisor.create_vm(vm_name, template_name)
@ironsight_api.post("/vms/{vm_name}/vnc")
async def configure_vnc(vm_name: str, port: int):
return hypervisor.configure_vnc(vm_name, port)
@ironsight_api.get("/vms/{vm_name}/config")
async def get_vm_config(vm_name: str):
return hypervisor.get_vm_config(vm_name)
@ironsight_api.get("/usage/nodes")
async def get_usage_graph():
return hypervisor.get_usage_graph()
@ironsight_api.get("/usage/nodes/{node_name}")
async def get_usage_graph(node_name: str):
return hypervisor.get_usage_graph(node_name)
@ironsight_api.get("/usage/vms")
async def get_vm_usage_graph():
return hypervisor.get_vm_usage_graph()
@logger.catch
def main():
uvicorn.run(ironsight_api, host="0.0.0.0", port=int((SERVER_PORT)))
if __name__ == "__main__":
logger.add(
sys.stderr, format="{time} {level} {message}", filter="my_module", level="INFO")
main()