Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

17-dashboard-keeps-the-login-session-when-id-token-is-expired #18

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 61 additions & 3 deletions MAIA/dashboard_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -777,6 +777,59 @@ def get_user_table(settings):

return table, users_to_register_in_group, users_to_register_in_keycloak, maia_group_dict

def register_cluster_for_project_in_db(settings, namespace, cluster):
"""
Registers a cluster for a project in the database.
Depending on the DEBUG setting, this function connects to either a local SQLite database or a remote MySQL database.
It updates the cluster information for a given namespace in the `authentication_maiaproject` table.

Parameters
----------
settings : object
An object containing configuration settings. Must have `DEBUG` and `LOCAL_DB_PATH` attributes.
namespace : str
The namespace of the project to update.
cluster : str
The cluster information to register for the project.

Returns
-------
None
"""

if settings.DEBUG:
cnx = sqlite3.connect(os.path.join(settings.LOCAL_DB_PATH,"db.sqlite3"))
else:
db_host = os.environ["DB_HOST"]
db_user = os.environ["DB_USERNAME"]
dp_password = os.environ["DB_PASS"]

#try:
engine = create_engine(f"mysql+pymysql://{db_user}:{dp_password}@{db_host}:3306/mysql")
cnx = engine.raw_connection()

authentication_maiaproject = pd.read_sql_query("SELECT * FROM authentication_maiaproject", con=cnx)

try:
id = authentication_maiaproject[authentication_maiaproject["namespace"] == namespace ]["id"].values[0]
authentication_maiaproject.loc[authentication_maiaproject["id"] == id, "cluster"] = cluster
except:
id = 0 if pd.isna(authentication_maiaproject["id"].max()) else authentication_maiaproject["id"].max() + 1
authentication_maiaproject = authentication_maiaproject.append({"id": id, "namespace": namespace, "cluster": cluster, "memory_limit": "2 Gi", "cpu_limit": "2"}, ignore_index=True)



cnx.close()


if settings.DEBUG:
cnx = sqlite3.connect(os.path.join(settings.LOCAL_DB_PATH,"db.sqlite3"))
authentication_maiaproject.to_sql("authentication_maiaproject", con=cnx, if_exists="replace", index=False)

else:
engine.dispose()
engine_2 = create_engine(f"mysql+pymysql://{db_user}:{dp_password}@{db_host}:3306/mysql")
authentication_maiaproject.to_sql("authentication_maiaproject", con=engine_2, if_exists="replace", index=False)


def update_user_table(form, settings):
Expand Down Expand Up @@ -1122,7 +1175,7 @@ def get_project(group_id, settings, is_namespace_style=False):
cluster_id = None
return namespace_form, cluster_id

return None
return None, None


def register_user_in_keycloak(email, settings):
Expand Down Expand Up @@ -1394,7 +1447,7 @@ def get_argocd_project_status(argocd_namespace, project_id):

def get_namespace_details(settings, id_token, namespace, user_id, is_admin=False):
"""
Retrieve details about the namespace including workspace applications, remote desktops, SSH ports, MONAI models, and Orthanc instances.
Retrieve details about the namespace including workspace applications, remote desktops, SSH ports, MONAI models, Orthanc instances and deployed clusters.

Parameters
----------
Expand All @@ -1418,12 +1471,14 @@ def get_namespace_details(settings, id_token, namespace, user_id, is_admin=False
- ssh_ports (dict): Dictionary of SSH ports for users.
- monai_models (dict): Dictionary of MONAI models.
- orthanc_list (dict): Dictionary of Orthanc instances.
- deployed_clusters (list): List of clusters where the namespace is deployed.
"""
maia_workspace_apps = {}
remote_desktop_dict = {}
orthanc_list = []
monai_models = {}
ssh_ports = {}
deployed_clusters = []

for API_URL in settings.API_URL:
if API_URL in settings.PRIVATE_CLUSTERS:
Expand Down Expand Up @@ -1454,6 +1509,9 @@ def get_namespace_details(settings, id_token, namespace, user_id, is_admin=False
if services['code'] == 403:
...

if len(ingresses['items']) > 0 or len(services['items']) > 0:
deployed_clusters.append(settings.CLUSTER_NAMES[API_URL])

for ingress in ingresses['items']:
for rule in ingress['spec']['rules']:
if 'host' not in rule:
Expand Down Expand Up @@ -1510,7 +1568,7 @@ def get_namespace_details(settings, id_token, namespace, user_id, is_admin=False
if "xnat" not in maia_workspace_apps:
maia_workspace_apps["xnat"] = "N/A"

return maia_workspace_apps, remote_desktop_dict, ssh_ports, monai_models, orthanc_list
return maia_workspace_apps, remote_desktop_dict, ssh_ports, monai_models, orthanc_list, deployed_clusters


def get_allocation_date_for_project(settings, group_id, is_namespace_style=False):
Expand Down
13 changes: 9 additions & 4 deletions dashboard/apps/namespaces/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
from django.http import HttpResponse, HttpResponseRedirect
from .utils import get_svc_for_namespace
# Create your views here.
from MAIA.dashboard_utils import get_allocation_date_for_project, get_namespace_details, get_project
from MAIA.dashboard_utils import get_allocation_date_for_project, get_namespace_details, get_project, register_cluster_for_project_in_db
import datetime
from django.conf import settings

Expand Down Expand Up @@ -82,7 +82,7 @@ def namespace_view(request,namespace_id):
is_admin = False
if request.user.is_superuser:
is_admin = True
maia_workspace_apps, remote_desktop_dict, ssh_ports, monai_models, orthanc_list = get_namespace_details(settings,id_token, namespace_id, user_id, is_admin=is_admin)
maia_workspace_apps, remote_desktop_dict, ssh_ports, monai_models, orthanc_list, deployed_clusters = get_namespace_details(settings, id_token, namespace_id, user_id, is_admin=is_admin)

groups = request.user.groups.all()

Expand All @@ -100,9 +100,14 @@ def namespace_view(request,namespace_id):
_, cluster_id = get_project(namespace_id, settings=settings, is_namespace_style=True)

cluster_config_path = os.environ["CLUSTER_CONFIG_PATH"]
cluster_config_dict = yaml.safe_load(Path(cluster_config_path).joinpath(cluster_id+".yaml").read_text())

if cluster_id is not None:
cluster_config_dict = yaml.safe_load(Path(cluster_config_path).joinpath(cluster_id+".yaml").read_text())
else:
register_cluster_for_project_in_db(settings, namespace_id, deployed_clusters[0])
cluster_config_dict = yaml.safe_load(Path(cluster_config_path).joinpath(deployed_clusters[0]+".yaml").read_text())

context = { "maia_workspace_ingress":maia_workspace_apps,"namespace":namespace_id,
context = { "maia_workspace_ingress": maia_workspace_apps,"namespace":namespace_id,
#"pods":pods, "nodes": nodes,
"remote_desktop_dict": remote_desktop_dict,
"allocation_date": allocation_date,
Expand Down
11 changes: 6 additions & 5 deletions dashboard/core/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
'mozilla_django_oidc.middleware.SessionRefresh',
]

ROOT_URLCONF = 'core.urls'
Expand Down Expand Up @@ -280,8 +281,8 @@

if v_file["maia_dashboard"]["token"] != "":
PRIVATE_CLUSTERS[v_file["api"]] = v_file["maia_dashboard"]["token"]

API_URL = list(set(API_URL))
with open(os.path.join(MOUNT_DIR, 'cluster_config.json')) as v_file:
CLUSTER_CONFIG = json.load(v_file)
GPU_LIST.extend(CLUSTER_CONFIG["GPU_LIST"])
if file.endswith(".yaml"):
with open(os.path.join(root, file)) as v_file:
v_file = yaml.safe_load(v_file)
if "gpu_list" in v_file:
GPU_LIST.extend(v_file["gpu_list"])