Commit b22c95bd by Karsa Zoltán István

pool utils

parent ab22253e
from fastapi.responses import ORJSONResponse
from sredis.sredis import get_all_datacenter, get_datacenter_token, rr_get
from sredis.sredis import (
get_all_datacenter,
get_datacenter_token,
pop_vm,
rr_get,
save_rpvm,
)
from fastapi import HTTPException
import requests
import json
......@@ -81,16 +87,56 @@ def get_stat_from_dc(datacenter):
def create_rp(username: str, rp: ResourcePoolSchema):
if rp.manual:
id = 0
errors = 0
for dc, cnt in rp.manual.items():
for _ in range(cnt):
create_vm_from_template(
dc,
username,
name=f"{rp.rpname} #{id}",
template_name=rp.from_template,
)
id += 1
try:
create_vm_from_template(
dc,
username,
name=f"{rp.rpname} #{id}",
template_name=rp.from_template,
)
save_rpvm(rp.rpname, username, dc, id, rp.key)
id += 1
except Exception:
errors += 1
return id, errors
else:
datacenters = get_all_datacenter()
stats = {}
for dc in datacenters:
get_stat_from_dc(dc)
stats[dc] = get_stat_from_dc(dc)
def update_vm_owner(datacenter, vm_id, username):
pass
def get_vm_from_rp(username, rpname, key):
vm = pop_vm(rpname, key)
if vm is None:
raise HTTPException(status_code=400, detail="Bad resource pool or key")
datacenter, id = vm
update_vm_owner(datacenter, id, username)
token = get_datacenter_token(username, datacenter)
try:
t_resp = requests.request(
method="GET",
url=f"{datacenter}dashboard/acpi/vm/{id}",
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")
from typing import Dict
from typing import Dict, Optional
from pydantic import BaseModel, EmailStr, Field
......@@ -32,18 +32,20 @@ class ResourcePoolSchema(BaseModel):
rpname: str = Field(min_length=3, max_length=50)
from_template: str = Field(default="")
num_vms: int = Field(default=1)
manual: Dict = Field(
manual: Optional[Dict] = Field(
default={
"https://kappa1.fured.cloud.bme.hu": 0,
"https://kappa2.fured.cloud.bme.hu": 0,
"https://kappa3.fured.cloud.bme.hu": 0,
}
},
nullable=True,
)
key: str = Field(min_length=10, max_length=100)
class VMFromRP(BaseModel):
class VMFromRPSchema(BaseModel):
rpname: str = Field(min_length=3, max_length=50)
username: str = Field()
key: str = Field(min_length=10, max_length=100)
class ResourceStat(BaseModel):
......
from fastapi import FastAPI, Response, Body, Depends
import os
from typing import List
from balancer.util import proxy_datacenters, create_rp
from balancer.util import get_vm_from_rp, proxy_datacenters, create_rp
from sredis.sredis import (
check_user,
create_puser,
......@@ -24,6 +24,7 @@ from core.models import (
DataCenterSchema,
ResourcePoolSchema,
ResourceStat,
VMFromRPSchema,
)
from core.auth import signJWT
from core.bearer import get_current_user, admin_user
......@@ -91,13 +92,20 @@ def proxy_post_ff(
return proxy_datacenters(server_path, username, body=body, method="POST")
@app.post("/rp/")
@app.post("/createrp/")
def create_resource_pool(
username=Depends(get_current_user), rp: ResourcePoolSchema = Body(...)
):
create_rp(username, rp)
@app.post("/rp/")
def get_vm_from_pool(
username=Depends(get_current_user), vm: VMFromRPSchema = Body(...)
):
return get_vm_from_rp(username, vm.rpname, vm.key)
@app.post("/add_datacenter/")
def create_datacenter(dc: DataCenterSchema = None, username=Depends(admin_user)):
add_datacenter(dc.url)
......
......@@ -20,6 +20,7 @@ class ResourcePool(HashModel):
owner: str = Field(default="")
datacenter: str = Field(default="")
id: int = Field()
token: str = Field(default="")
class DataCenterResource(HashModel):
......
import redis
import requests
from core.models import UserLoginSchema, ResourceStat
from .models import PUser, DataCenterResource
from .models import PUser, DataCenterResource, ResourcePool
from passlib.hash import pbkdf2_sha256
from core.models import User, DataCenterSchema
from core.auth import hash_pass
......@@ -146,3 +146,19 @@ def update_stats(stat: ResourceStat):
else:
dc.update(**stat)
dc.save()
def save_rpvm(rpname, username, datacenter, id, token):
rpvm = ResourcePool(
rpname=rpname, owner=username, datacenter=datacenter, id=id, token=token
)
rpvm.save()
def pop_vm(rpname, key):
rpvm = ResourcePool.find(ResourcePool.rpname == rpname).first()
if rpvm.token == key:
vm = rpvm.datacenter, rpvm.id
rpvm.delete()
return vm
return None
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