Skip to content
Toggle navigation
P
Projects
G
Groups
S
Snippets
Help
CIRCLE3
/
balancer
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
0
Merge Requests
0
Pipelines
Wiki
Snippets
Members
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Commit
b22c95bd
authored
Sep 27, 2023
by
Karsa Zoltán István
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
pool utils
parent
ab22253e
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
90 additions
and
17 deletions
+90
-17
balancer/util.py
+55
-9
core/models.py
+7
-5
main.py
+10
-2
sredis/models.py
+1
-0
sredis/sredis.py
+17
-1
No files found.
balancer/util.py
View file @
b22c95bd
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"
)
core/models.py
View file @
b22c95bd
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
VMFromRP
Schema
(
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
):
...
...
main.py
View file @
b22c95bd
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
(
"/
create
rp/"
)
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
)
...
...
sredis/models.py
View file @
b22c95bd
...
...
@@ -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
):
...
...
sredis/sredis.py
View file @
b22c95bd
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
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment