Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
S
shared-libs
Manage
Activity
Members
Code
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Locked files
Deploy
Package registry
Container registry
Model registry
Operate
Terraform modules
Analyze
Contributor analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Pi-Lab
shared-libs
Commits
7e0a938f
Commit
7e0a938f
authored
2 years ago
by
istmxrein
Browse files
Options
Downloads
Patches
Plain Diff
move functionality to reservation.py & bump to 1.5.5
parent
89244434
No related branches found
No related tags found
No related merge requests found
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
pilab/auth.py
+5
-50
5 additions, 50 deletions
pilab/auth.py
pilab/events/reservation.py
+52
-0
52 additions, 0 deletions
pilab/events/reservation.py
setup.py
+1
-1
1 addition, 1 deletion
setup.py
with
58 additions
and
51 deletions
pilab/auth.py
+
5
−
50
View file @
7e0a938f
import
logging
from
collections
import
Counter
from
typing
import
Optional
,
List
,
Dict
,
Callable
from
typing
import
Optional
,
List
from
fastapi
import
Header
,
HTTPException
from
pilab.events
import
reservation
from
datetime
import
datetime
logger
=
logging
.
getLogger
(
__name__
)
ADMIN_GROUPS
=
[
"
/admin
"
]
cube_reservations
:
Dict
[
int
:
reservation
.
Reservation
]
=
{}
res_thread
:
reservation
.
ReservationConsumer
=
None
custom_callback
:
Callable
=
None
def
is_admin
(
x_forwarded_groups
:
str
):
admin
=
False
...
...
@@ -30,22 +26,6 @@ def get_username(usernames: List[str]):
return
None
def
get_active_users
(
cube_id
:
int
):
res
:
reservation
.
Reservation
=
cube_reservations
.
get
(
cube_id
)
if
res
and
res
.
endtime
>
datetime
.
utcnow
():
return
[
*
res
.
extraUsers
,
res
.
owner
]
else
:
return
[]
def
get_active_reservation
(
cube_id
:
int
):
res
:
reservation
.
Reservation
=
cube_reservations
.
get
(
cube_id
)
if
res
and
res
.
endtime
>
datetime
.
utcnow
():
return
res
else
:
return
None
async
def
get_user
(
x_forwarded_user
:
Optional
[
str
]
=
Header
(
None
),
x_forwarded_preferred_username
:
Optional
[
str
]
=
Header
(
None
),
x_forwarded_groups
:
Optional
[
str
]
=
Header
(
None
)):
...
...
@@ -69,7 +49,7 @@ async def verify_user(cube_id: int, x_forwarded_preferred_username: Optional[str
username
=
get_username
([
x_forwarded_preferred_username
,
x_forwarded_user
])
if
admin
:
return
True
if
username
is
not
None
and
username
in
get_active_users
(
cube_id
):
if
username
is
not
None
and
username
in
reservation
.
get_active_users
(
cube_id
):
return
True
raise
HTTPException
(
status_code
=
401
,
detail
=
"
Unauthorized
"
)
...
...
@@ -78,32 +58,7 @@ async def verify_user(cube_id: int, x_forwarded_preferred_username: Optional[str
def
verify_user_pi
(
cube_id
:
int
,
username
:
str
,
admin
:
bool
):
if
admin
:
return
True
if
username
is
not
None
and
username
in
get_active_users
(
cube_id
):
if
username
is
not
None
and
username
in
reservation
.
get_active_users
(
cube_id
):
return
True
return
False
def
restart_reservation_consumer
(
ids
:
List
[
int
],
callback
:
Callable
=
None
):
global
res_thread
global
custom_callback
custom_callback
=
callback
def
res_event_callback
(
res
:
reservation
.
Reservation
):
logger
.
info
(
f
'
Received Reservation for cube
{
res
.
cube_id
}
;
{
res
}
'
)
if
custom_callback
:
custom_callback
(
res
)
cube_reservations
[
res
.
cube_id
]
=
res
if
not
res_thread
:
res_thread
=
reservation
.
ReservationConsumer
(
cubes
=
ids
,
callback
=
res_event_callback
)
res_thread
.
start
()
elif
Counter
(
ids
)
!=
Counter
(
res_thread
.
cubes
):
res_thread
.
stop
()
res_thread
.
join
()
res_thread
=
reservation
.
ReservationConsumer
(
cubes
=
ids
,
callback
=
res_event_callback
)
res_thread
.
start
()
return
res_thread
This diff is collapsed.
Click to expand it.
pilab/events/reservation.py
+
52
−
0
View file @
7e0a938f
...
...
@@ -3,6 +3,7 @@ import json
import
logging
import
threading
import
traceback
from
collections
import
Counter
from
datetime
import
datetime
from
typing
import
List
,
Callable
,
Optional
,
Dict
...
...
@@ -270,3 +271,54 @@ class ReservationConsumer(threading.Thread):
else
:
self
.
_connection
.
ioloop
.
stop
()
logger
.
info
(
'
Stopped
'
)
cube_reservations
:
Dict
[
int
:
Reservation
]
=
{}
res_thread
:
ReservationConsumer
=
None
custom_callback
:
Callable
=
None
def
get_active_users
(
cube_id
:
int
):
res
:
Reservation
=
cube_reservations
.
get
(
cube_id
)
if
not
res_thread
:
raise
RuntimeError
(
"
Reservation consumer is not initialized
"
)
if
res
and
res
.
endtime
>
datetime
.
utcnow
():
return
[
*
res
.
extraUsers
,
res
.
owner
]
else
:
return
[]
def
get_active_reservation
(
cube_id
:
int
):
res
:
Reservation
=
cube_reservations
.
get
(
cube_id
)
if
not
res_thread
:
raise
RuntimeError
(
"
Reservation consumer is not initialized
"
)
if
res
and
res
.
endtime
>
datetime
.
utcnow
():
return
res
else
:
return
None
def
restart_reservation_consumer
(
ids
:
List
[
int
],
callback
:
Callable
=
None
):
global
res_thread
global
custom_callback
custom_callback
=
callback
def
res_event_callback
(
res
:
Reservation
):
logger
.
info
(
f
'
Received Reservation for cube
{
res
.
cube_id
}
;
{
res
}
'
)
if
custom_callback
:
custom_callback
(
res
)
cube_reservations
[
res
.
cube_id
]
=
res
if
not
res_thread
:
res_thread
=
ReservationConsumer
(
cubes
=
ids
,
callback
=
res_event_callback
)
res_thread
.
start
()
elif
Counter
(
ids
)
!=
Counter
(
res_thread
.
cubes
):
res_thread
.
stop
()
res_thread
.
join
()
res_thread
=
ReservationConsumer
(
cubes
=
ids
,
callback
=
res_event_callback
)
res_thread
.
start
()
return
res_thread
This diff is collapsed.
Click to expand it.
setup.py
+
1
−
1
View file @
7e0a938f
...
...
@@ -2,7 +2,7 @@ from setuptools import setup, find_packages
setup
(
name
=
'
pilab
'
,
version
=
'
1.
4
.5
'
,
version
=
'
1.
5
.5
'
,
description
=
'
Shared-Libs for the pi-lab microservices
'
,
url
=
'
https://code.fbi.h-da.de/api/v4/projects/27896/packages/pypi/pilab
'
,
author
=
'
Max Reinheimer
'
,
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment