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
1b1e1bf3
authored
Jul 07, 2023
by
Karsa Zoltán István
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
resource pool setup
parent
c18a7a0b
Show whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
78 additions
and
3 deletions
+78
-3
balancer/util.py
+33
-0
core/models.py
+11
-0
main.py
+16
-2
sredis/models.py
+7
-0
sredis/sredis.py
+11
-1
No files found.
balancer/util.py
View file @
1b1e1bf3
...
@@ -3,6 +3,7 @@ from sredis.sredis import get_datacenter_token, rr_get
...
@@ -3,6 +3,7 @@ from sredis.sredis import get_datacenter_token, rr_get
from
fastapi
import
HTTPException
from
fastapi
import
HTTPException
import
requests
import
requests
import
json
import
json
from
core.models
import
ResourcePoolSchema
def
proxy_datacenters
(
def
proxy_datacenters
(
...
@@ -21,6 +22,7 @@ def proxy_datacenters(
...
@@ -21,6 +22,7 @@ def proxy_datacenters(
server
=
balancer_fun
()
server
=
balancer_fun
()
token
=
get_datacenter_token
(
username
,
server
)
token
=
get_datacenter_token
(
username
,
server
)
url
=
f
"{server}/{serverpath}"
url
=
f
"{server}/{serverpath}"
try
:
t_resp
=
requests
.
request
(
t_resp
=
requests
.
request
(
json
=
body
,
json
=
body
,
method
=
method
,
method
=
method
,
...
@@ -43,3 +45,34 @@ def proxy_datacenters(
...
@@ -43,3 +45,34 @@ def proxy_datacenters(
obj
[
"datacenter"
]
=
server
obj
[
"datacenter"
]
=
server
response
=
ORJSONResponse
(
obj
,
status_code
=
t_resp
.
status_code
)
response
=
ORJSONResponse
(
obj
,
status_code
=
t_resp
.
status_code
)
return
response
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
core/models.py
View file @
1b1e1bf3
...
@@ -25,3 +25,14 @@ class DataCenterSchema(BaseModel):
...
@@ -25,3 +25,14 @@ class DataCenterSchema(BaseModel):
url
:
str
url
:
str
active
:
bool
=
False
active
:
bool
=
False
weight
:
float
=
1.0
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
()
main.py
View file @
1b1e1bf3
from
fastapi
import
FastAPI
,
Response
,
Body
,
Depends
from
fastapi
import
FastAPI
,
Response
,
Body
,
Depends
import
os
import
os
from
typing
import
List
from
typing
import
List
from
balancer.util
import
proxy_datacenters
from
balancer.util
import
proxy_datacenters
,
create_rp
from
sredis.sredis
import
(
from
sredis.sredis
import
(
check_user
,
check_user
,
create_puser
,
create_puser
,
...
@@ -15,7 +15,13 @@ from sredis.sredis import (
...
@@ -15,7 +15,13 @@ from sredis.sredis import (
import
logging
import
logging
import
requests
import
requests
from
fastapi_utils.tasks
import
repeat_every
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.auth
import
signJWT
from
core.bearer
import
get_current_user
,
admin_user
from
core.bearer
import
get_current_user
,
admin_user
from
redis_om
import
Migrator
from
redis_om
import
Migrator
...
@@ -31,6 +37,7 @@ app = FastAPI()
...
@@ -31,6 +37,7 @@ app = FastAPI()
add_datacenter
(
"https://kappa1.fured.cloud.bme.hu"
)
add_datacenter
(
"https://kappa1.fured.cloud.bme.hu"
)
add_datacenter
(
"https://kappa2.fured.cloud.bme.hu"
)
add_datacenter
(
"https://kappa2.fured.cloud.bme.hu"
)
add_datacenter
(
"https://kappa3.fured.cloud.bme.hu"
)
add_datacenter
(
"https://kappa3.fured.cloud.bme.hu"
)
create_superpuser
(
create_superpuser
(
User
(
username
=
"admin"
,
email
=
"example@domain.com"
,
password
=
os
.
getenv
(
"ADMIN_PASS"
))
User
(
username
=
"admin"
,
email
=
"example@domain.com"
,
password
=
os
.
getenv
(
"ADMIN_PASS"
))
)
)
...
@@ -81,6 +88,13 @@ def proxy_post_ff(
...
@@ -81,6 +88,13 @@ def proxy_post_ff(
return
proxy_datacenters
(
server_path
,
username
,
body
=
body
,
method
=
"POST"
)
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/"
)
@app.post
(
"/add_datacenter/"
)
def
create_datacenter
(
dc
:
DataCenterSchema
=
None
,
username
=
Depends
(
admin_user
)):
def
create_datacenter
(
dc
:
DataCenterSchema
=
None
,
username
=
Depends
(
admin_user
)):
add_datacenter
(
dc
.
url
)
add_datacenter
(
dc
.
url
)
...
...
sredis/models.py
View file @
1b1e1bf3
...
@@ -13,3 +13,10 @@ class DataCenter(HashModel):
...
@@ -13,3 +13,10 @@ class DataCenter(HashModel):
name
:
str
=
Field
(
index
=
True
)
name
:
str
=
Field
(
index
=
True
)
url
:
str
=
Field
(
index
=
True
)
url
:
str
=
Field
(
index
=
True
)
active
:
bool
=
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
()
sredis/sredis.py
View file @
1b1e1bf3
...
@@ -7,10 +7,17 @@ from core.models import User, DataCenterSchema
...
@@ -7,10 +7,17 @@ from core.models import User, DataCenterSchema
from
core.auth
import
hash_pass
from
core.auth
import
hash_pass
from
fastapi
import
HTTPException
from
fastapi
import
HTTPException
import
random
import
random
import
logging
logger
=
logging
.
getLogger
(
__name__
)
def
get_status
(
datacenter_url
:
str
):
def
get_status
(
datacenter_url
:
str
):
logger
.
info
(
f
"Health check datacenter '{datacenter_url}'"
)
try
:
resp
=
requests
.
request
(
method
=
"GET"
,
url
=
datacenter_url
,
verify
=
False
)
resp
=
requests
.
request
(
method
=
"GET"
,
url
=
datacenter_url
,
verify
=
False
)
except
Exception
:
return
False
return
int
(
resp
.
status_code
/
100
)
==
2
return
int
(
resp
.
status_code
/
100
)
==
2
...
@@ -18,7 +25,7 @@ r = redis.Redis(host="localhost", port=6379, db=0, decode_responses=True)
...
@@ -18,7 +25,7 @@ r = redis.Redis(host="localhost", port=6379, db=0, decode_responses=True)
def
reset
():
def
reset
():
print
(
"Reset OK
"
)
logging
.
info
(
"Clean settings
"
)
r
.
set
(
"datacenters_cnt"
,
1
)
r
.
set
(
"datacenters_cnt"
,
1
)
all_keys
=
r
.
hgetall
(
"datacenters_hash"
)
all_keys
=
r
.
hgetall
(
"datacenters_hash"
)
if
all_keys
.
keys
():
if
all_keys
.
keys
():
...
@@ -33,10 +40,13 @@ def reset():
...
@@ -33,10 +40,13 @@ def reset():
def
update_status
(
datacenter
):
def
update_status
(
datacenter
):
status
=
"OK"
if
get_status
(
datacenter_url
=
datacenter
)
is
True
else
"Wrong"
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
)
r
.
hset
(
"datacenter_status"
,
datacenter
,
status
)
def
update_datacenter_status
():
def
update_datacenter_status
():
logging
.
info
(
"Update datacenters status"
)
all_keys
=
r
.
hgetall
(
"datacenters_hash"
)
all_keys
=
r
.
hgetall
(
"datacenters_hash"
)
for
dc
in
all_keys
.
values
():
for
dc
in
all_keys
.
values
():
update_status
(
datacenter
=
dc
)
update_status
(
datacenter
=
dc
)
...
...
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