Commit 1b1e1bf3 by Karsa Zoltán István

resource pool setup

parent c18a7a0b
......@@ -3,6 +3,7 @@ from sredis.sredis import get_datacenter_token, rr_get
from fastapi import HTTPException
import requests
import json
from core.models import ResourcePoolSchema
def proxy_datacenters(
......@@ -21,25 +22,57 @@ def proxy_datacenters(
server = balancer_fun()
token = get_datacenter_token(username, server)
url = f"{server}/{serverpath}"
t_resp = requests.request(
json=body,
method=method,
url=url,
allow_redirects=True,
verify=False,
headers={"Authorization": token},
)
if int(t_resp.status_code / 100) != 2:
raise HTTPException(
status_code=t_resp.status_code, detail="Remote server error"
try:
t_resp = requests.request(
json=body,
method=method,
url=url,
allow_redirects=True,
verify=False,
headers={"Authorization": token},
)
if not t_resp.content:
return ORJSONResponse({"status": "ok"}, status_code=t_resp.status_code)
obj = json.loads(t_resp.content)
if type(obj) is list:
for o in obj:
o["datacenter"] = server
else:
obj["datacenter"] = server
response = ORJSONResponse(obj, status_code=t_resp.status_code)
return response
if int(t_resp.status_code / 100) != 2:
raise HTTPException(
status_code=t_resp.status_code, detail="Remote server error"
)
if not t_resp.content:
return ORJSONResponse({"status": "ok"}, status_code=t_resp.status_code)
obj = json.loads(t_resp.content)
if type(obj) is list:
for o in obj:
o["datacenter"] = server
else:
obj["datacenter"] = server
response = ORJSONResponse(obj, status_code=t_resp.status_code)
return response
except Exception:
raise HTTPException(status_code=500, detail="Remote server error")
def create_vm_from_template(
datacenter: str, username: str, name: str, template_name: str
):
token = get_datacenter_token(username, datacenter)
body = {"template_name": template_name, "username": username, "name": name}
try:
t_resp = requests.request(
json=body,
method="POST",
url=f"{datacenter}dashboard/acpi/bvm/",
allow_redirects=True,
verify=False,
headers={"Authorization": token},
)
if int(t_resp.status_code / 100) != 2:
raise HTTPException(
status_code=t_resp.status_code, detail="Remote server error"
)
obj = json.loads(t_resp.content)
obj["datacenter"] = datacenter
return obj
except Exception:
raise HTTPException(status_code=500, detail="Remote server error")
def create_rp(username: str, rp: ResourcePoolSchema):
pass
......@@ -25,3 +25,14 @@ class DataCenterSchema(BaseModel):
url: str
active: bool = False
weight: float = 1.0
class ResourcePoolSchema(BaseModel):
rpname: str = Field(min_length=3, max_length=50)
from_template: str = Field(default="")
num_vms: int = Field(default=1)
class VMFromRP(BaseModel):
rpname: str = Field(min_length=3, max_length=50)
username: str = Field()
from fastapi import FastAPI, Response, Body, Depends
import os
from typing import List
from balancer.util import proxy_datacenters
from balancer.util import proxy_datacenters, create_rp
from sredis.sredis import (
check_user,
create_puser,
......@@ -15,7 +15,13 @@ from sredis.sredis import (
import logging
import requests
from fastapi_utils.tasks import repeat_every
from core.models import User, Token, UserLoginSchema, DataCenterSchema
from core.models import (
User,
Token,
UserLoginSchema,
DataCenterSchema,
ResourcePoolSchema,
)
from core.auth import signJWT
from core.bearer import get_current_user, admin_user
from redis_om import Migrator
......@@ -31,6 +37,7 @@ app = FastAPI()
add_datacenter("https://kappa1.fured.cloud.bme.hu")
add_datacenter("https://kappa2.fured.cloud.bme.hu")
add_datacenter("https://kappa3.fured.cloud.bme.hu")
create_superpuser(
User(username="admin", email="example@domain.com", password=os.getenv("ADMIN_PASS"))
)
......@@ -81,6 +88,13 @@ def proxy_post_ff(
return proxy_datacenters(server_path, username, body=body, method="POST")
@app.post("/rp/")
def create_resource_pool(
username=Depends(get_current_user), rp: ResourcePoolSchema = Body(...)
):
create_rp(username, rp)
@app.post("/add_datacenter/")
def create_datacenter(dc: DataCenterSchema = None, username=Depends(admin_user)):
add_datacenter(dc.url)
......
......@@ -13,3 +13,10 @@ class DataCenter(HashModel):
name: str = Field(index=True)
url: str = Field(index=True)
active: bool = Field(index=True)
class ResourcePool(HashModel):
rpname: str = Field(index=True)
owner: str = Field(default="")
datacenter: str = Field(default="")
id: int = Field()
......@@ -7,10 +7,17 @@ from core.models import User, DataCenterSchema
from core.auth import hash_pass
from fastapi import HTTPException
import random
import logging
logger = logging.getLogger(__name__)
def get_status(datacenter_url: str):
resp = requests.request(method="GET", url=datacenter_url, verify=False)
logger.info(f"Health check datacenter '{datacenter_url}'")
try:
resp = requests.request(method="GET", url=datacenter_url, verify=False)
except Exception:
return False
return int(resp.status_code / 100) == 2
......@@ -18,7 +25,7 @@ r = redis.Redis(host="localhost", port=6379, db=0, decode_responses=True)
def reset():
print("Reset OK")
logging.info("Clean settings")
r.set("datacenters_cnt", 1)
all_keys = r.hgetall("datacenters_hash")
if all_keys.keys():
......@@ -33,10 +40,13 @@ def reset():
def update_status(datacenter):
status = "OK" if get_status(datacenter_url=datacenter) is True else "Wrong"
if status == "Wrong":
logging.warning(f"{datacenter} is inactive status")
r.hset("datacenter_status", datacenter, status)
def update_datacenter_status():
logging.info("Update datacenters status")
all_keys = r.hgetall("datacenters_hash")
for dc in all_keys.values():
update_status(datacenter=dc)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment