MAIA.dashboard_utils module#
- MAIA.dashboard_utils.create_namespace(request, settings, namespace_id, cluster_id)[source]#
Creates a Kubernetes namespace using the provided request, settings, namespace ID, and cluster ID.
- Parameters:
request (HttpRequest) – The HTTP request object containing session and user information.
settings (Settings) – The settings object containing configuration details.
namespace_id (str) – The ID of the namespace to be created.
cluster_id (str) – The ID of the Kubernetes cluster where the namespace will be created.
- Returns:
None
- Raises:
ApiException – If an error occurs while creating the namespace using the Kubernetes API.
- MAIA.dashboard_utils.generate_kubeconfig(id_token, user_id, namespace, cluster_id, settings)[source]#
Generates a Kubernetes configuration dictionary for a given user and cluster.
- Parameters:
id_token (str) – The ID token for the user.
user_id (str) – The user ID.
namespace (str) – The Kubernetes namespace.
cluster_id (str) – The cluster ID.
settings (object) – An object containing various settings, including: - CLUSTER_NAMES (dict): A dictionary mapping cluster names to their IDs. - PRIVATE_CLUSTERS (dict): A dictionary of private clusters with their tokens. - OIDC_ISSUER_URL (str): The OIDC issuer URL. - OIDC_RP_CLIENT_ID (str): The OIDC client ID. - OIDC_RP_CLIENT_SECRET (str): The OIDC client secret.
- Returns:
dict – A dictionary representing the Kubernetes configuration.
- MAIA.dashboard_utils.get_allocation_date_for_project(settings, group_id, is_namespace_style=False)[source]#
Retrieves the allocation date for a project based on the provided group ID.
- Parameters:
settings (object) – The settings object containing configuration details.
group_id (str) – The group ID or namespace to search for.
is_namespace_style (bool, optional) – Flag to indicate if the group ID should be treated as a namespace. Defaults to False.
- Returns:
datetime or None – The allocation date of the project if found, otherwise None.
- MAIA.dashboard_utils.get_available_resources(id_token, api_urls, cluster_names, private_clusters=[])[source]#
Retrieves available GPU, CPU, and RAM resources from multiple Kubernetes clusters.
- Parameters:
id_token (str) – The ID token for authentication.
api_urls (list) – List of API URLs for the Kubernetes clusters.
cluster_names (dict) – Dictionary mapping API URLs to cluster names.
private_clusters (list, optional) – List of private clusters with their tokens. Defaults to [].
- Returns:
tuple –
- A tuple containing:
gpu_dict (dict): Dictionary with GPU availability information for each node.
cpu_dict (dict): Dictionary with CPU availability information for each node.
ram_dict (dict): Dictionary with RAM availability information for each node.
gpu_allocations (dict): Dictionary with GPU allocation details for each pod.
- MAIA.dashboard_utils.get_cluster_status(id_token, api_urls, cluster_names, private_clusters=[])[source]#
Retrieve the status of clusters and their nodes.
- Parameters:
id_token (str) – The ID token for authentication.
api_urls (list) – A list of API URLs for the clusters.
cluster_names (dict) – A dictionary mapping API URLs to cluster names.
private_clusters (dict, optional) – A dictionary mapping private cluster API URLs to their tokens. Defaults to [].
- Returns:
tuple –
- A tuple containing:
node_status_dict (dict): A dictionary mapping node names to their status and schedulability.
cluster_dict (dict): A dictionary mapping cluster names to their node names.
- MAIA.dashboard_utils.get_filtered_available_nodes(gpu_dict, cpu_dict, ram_dict, gpu_request, cpu_request, memory_request)[source]#
Filters and returns nodes that meet the specified GPU, CPU, and memory requirements.
- Parameters:
gpu_dict (dict) – A dictionary where keys are node names and values are lists containing GPU information.
cpu_dict (dict) – A dictionary where keys are node names and values are lists containing CPU information.
ram_dict (dict) – A dictionary where keys are node names and values are lists containing RAM information.
gpu_request (int) – The minimum number of GPUs required.
cpu_request (float) – The minimum amount of CPU required.
memory_request (float) – The minimum amount of memory required.
- Returns:
tuple – Three dictionaries containing the filtered nodes and their respective GPU, CPU, and RAM information.
- MAIA.dashboard_utils.get_groups_in_keycloak(settings)[source]#
Retrieve groups from Keycloak that start with “MAIA:” and return them in a dictionary.
- Parameters:
settings (object) –
An object containing the Keycloak connection settings. It should have the following attributes: - OIDC_SERVER_URL : str
The URL of the Keycloak server.
- OIDC_USERNAMEstr
The username for Keycloak authentication.
- OIDC_REALM_NAMEstr
The name of the Keycloak realm.
- OIDC_RP_CLIENT_IDstr
The client ID for Keycloak.
- OIDC_RP_CLIENT_SECRETstr
The client secret for Keycloak.
- Returns:
dict – A dictionary where the keys are group IDs and the values are group names (with the “MAIA:” prefix removed) for groups that start with “MAIA:”.
- MAIA.dashboard_utils.get_list_of_groups_requesting_a_user(email, settings)[source]#
Retrieves a list of groups (namespaces) that have requested a specific user based on their email.
- Parameters:
email (str) – The email address of the user to search for.
settings (module) – The settings module containing configuration such as DEBUG and LOCAL_DB_PATH.
- Returns:
list – A list of namespaces that have requested the user. Returns an empty list if no groups are found.
- Raises:
KeyError – If environment variables ‘DB_HOST’, ‘DB_USERNAME’, or ‘DB_PASS’ are not set in non-debug mode.
Exception – If there is an issue connecting to the database or executing the SQL queries.
- MAIA.dashboard_utils.get_list_of_users_requesting_a_group(group_id, settings)[source]#
Retrieves a list of email addresses of users who have requested access to a specific group.
- Parameters:
group_id (str) – The ID of the group to check for user requests.
settings (object) – A settings object that contains configuration parameters, including DEBUG and LOCAL_DB_PATH.
- Returns:
list – A list of email addresses of users who have requested access to the specified group.
- Raises:
KeyError – If environment variables for database connection are not set when DEBUG is False.
Exception – If there is an issue with database connection or query execution.
Notes
When settings.DEBUG is True, a local SQLite database is used. When settings.DEBUG is False, a MySQL database is used with connection parameters from environment variables.
- MAIA.dashboard_utils.get_namespace_details(settings, id_token, namespace, user_id, is_admin=False)[source]#
Retrieve details about the namespace including workspace applications, remote desktops, SSH ports, MONAI models, and Orthanc instances.
- Parameters:
settings (object) – Configuration settings containing API URLs and private cluster tokens.
id_token (str) – Identity token for authentication.
namespace (str) – The namespace to retrieve details for.
user_id (str) – The user ID to filter resources.
is_admin (bool, optional) – Flag indicating if the user has admin privileges. Defaults to False.
- Returns:
tuple – A tuple containing: - maia_workspace_apps (dict): Dictionary of workspace applications with their URLs. - remote_desktop_dict (dict): Dictionary of remote desktop URLs for users. - ssh_ports (dict): Dictionary of SSH ports for users. - monai_models (dict): Dictionary of MONAI models. - orthanc_list (dict): Dictionary of Orthanc instances.
- MAIA.dashboard_utils.get_namespaces(id_token, api_urls, private_clusters=[])[source]#
Retrieves a list of unique namespaces from multiple API URLs.
- Parameters:
id_token (str) – The ID token used for authorization when accessing public clusters.
api_urls (list) – A list of API URLs to query for namespaces.
private_clusters (dict, optional) – A dictionary where keys are API URLs of private clusters and values are their respective tokens. Defaults to an empty list.
- Returns:
list – A list of unique namespace names retrieved from the provided API URLs.
- MAIA.dashboard_utils.get_pending_projects(settings)[source]#
Retrieve a list of pending projects that are not in active groups.
- Parameters:
settings (object) – A settings object that contains configuration parameters.
- Returns:
list – A list of namespaces of pending projects.
- MAIA.dashboard_utils.get_project(group_id, settings, is_namespace_style=False)[source]#
Retrieve project information based on the provided group ID.
- Parameters:
group_id (str) – The group ID or namespace to search for.
settings (module) – A settings module containing configuration values.
is_namespace_style (bool, optional) – Flag to determine if the group ID should be treated as a namespace. Defaults to False.
- Returns:
tuple – A tuple containing the namespace form (dict) and the cluster ID (str or None). Returns None if no matching project is found.
- Raises:
KeyError – If required environment variables are not set.
Exception – If there is an error connecting to the database or querying the data.
- MAIA.dashboard_utils.get_project_argo_status_and_user_table(request, settings)[source]#
Retrieves the Argo CD project status and user table information.
- Parameters:
request (HttpRequest) – The HTTP request object containing session and user information.
settings (Settings) – The settings object containing configuration values.
- Returns:
tuple – A tuple containing: - user_table (dict): The user table information. - to_register_in_groups (list): List of users to register in groups. - to_register_in_keycloak (list): List of users to register in Keycloak. - maia_groups_dict (dict): Dictionary of MAIA groups. - project_argo_status (dict): Dictionary containing the Argo CD project status for each project.
- MAIA.dashboard_utils.get_user_table(settings)[source]#
Retrieves and processes user data from various sources including a local SQLite database or a remote MySQL database, Keycloak, and Minio. Combines and returns user information along with group and project details.
- Parameters:
settings (object) – A settings object containing configuration parameters such as database paths, Keycloak server details, and Minio credentials.
- Returns:
tuple – A tuple containing: - table (pd.DataFrame): A DataFrame containing merged user data from the auth_user and authentication_maiauser tables. - users_to_register_in_group (dict): A dictionary mapping user emails to the groups they need to be registered in. - users_to_register_in_keycloak (list): A list of user emails that need to be registered in Keycloak. - maia_group_dict (dict): A dictionary containing detailed information about each MAIA group and pending projects.
- MAIA.dashboard_utils.register_group_in_keycloak(group_id, settings)[source]#
Registers a group in Keycloak with the specified group ID and settings.
- Parameters:
group_id (str) – The ID of the group to be registered.
settings (object) –
An object containing the Keycloak server settings, including: - OIDC_SERVER_URL : str
The URL of the Keycloak server.
- OIDC_USERNAMEstr
The username for Keycloak authentication.
- OIDC_REALM_NAMEstr
The name of the Keycloak realm.
- OIDC_RP_CLIENT_IDstr
The client ID for Keycloak.
- OIDC_RP_CLIENT_SECRETstr
The client secret for Keycloak.
- Returns:
None
- MAIA.dashboard_utils.register_user_in_keycloak(email, settings)[source]#
Registers a user in Keycloak and sends an approved registration email.
- Parameters:
email (str) – The email address of the user to be registered.
settings (object) – An object containing the necessary settings for Keycloak connection and email sending.
Attributes (Settings)
-------------------
OIDC_SERVER_URL (str) – The URL of the Keycloak server.
OIDC_USERNAME (str) – The username for Keycloak authentication.
OIDC_REALM_NAME (str) – The name of the Keycloak realm.
OIDC_RP_CLIENT_ID (str) – The client ID for Keycloak.
OIDC_RP_CLIENT_SECRET (str) – The client secret for Keycloak.
HOSTNAME (str) – The hostname for generating the MAIA login URL.
- Returns:
None
- MAIA.dashboard_utils.register_users_in_group_in_keycloak(emails, group_id, settings)[source]#
Registers users in a specified Keycloak group.
- Parameters:
emails (list) – A list of email addresses of users to be added to the group.
group_id (str) – The ID of the group to which users should be added.
settings (object) –
An object containing Keycloak server settings, including: - OIDC_SERVER_URL : str
The URL of the Keycloak server.
- OIDC_USERNAMEstr
The username for Keycloak authentication.
- OIDC_REALM_NAMEstr
The realm name in Keycloak.
- OIDC_RP_CLIENT_IDstr
The client ID for Keycloak.
- OIDC_RP_CLIENT_SECRETstr
The client secret for Keycloak.
- Returns:
None
- MAIA.dashboard_utils.send_approved_registration_email(receiver_email, login_url, temp_password)[source]#
Sends an email to notify the user that their MAIA account registration has been approved.
- Parameters:
receiver_email (str) – The email address of the recipient.
login_url (str) – The URL where the user can log in to MAIA.
temp_password (str) – The temporary password assigned to the user.
- Raises:
KeyError – If the environment variables ‘email_account’ or ‘email_password’ are not set.
smtplib.SMTPException – If there is an error sending the email.
- MAIA.dashboard_utils.send_discord_message(username, namespace, url)[source]#
Sends a message to a Discord webhook to request a MAIA account.
- Parameters:
username (str) – The username of the person requesting the account.
namespace (str) – The project namespace for which the account is being requested.
url (str) – The Discord webhook URL to which the message will be sent.
- Raises:
requests.exceptions.HTTPError – If the HTTP request returned an unsuccessful status code.
Prints –
------ –
str – Success message with the HTTP status code if the payload is delivered successfully.
str – Error message if the HTTP request fails.
- MAIA.dashboard_utils.update_user_table(form, settings)[source]#
Updates the user and project tables based on the provided form data.
- Parameters:
form (dict) – A dictionary containing form data with keys indicating the type of data (e.g., “namespace”, “memory_limit”, etc.) and values being the corresponding data.
settings (object) – An object containing configuration settings. It should have a DEBUG attribute to determine the environment and a LOCAL_DB_PATH attribute for the local database path.
- Returns:
None
The function performs the following steps
1. Connects to the appropriate database (SQLite for debug mode, MySQL for production).
Reads the current data from the auth_user, authentication_maiauser, and authentication_maiaproject tables.
Iterates over the form data and updates the authentication_maiauser and authentication_maiaproject tables accordingly.
4. Closes the database connection.
5. Writes the updated data back to the database.
- MAIA.dashboard_utils.verify_minio_availability(settings)[source]#
Verifies the availability of a MinIO server.
- Parameters:
settings (object) –
An object containing the MinIO configuration settings. - settings.MINIO_URL : str
The URL of the MinIO server.
- settings.MINIO_ACCESS_KEYstr
The access key for the MinIO server.
- settings.MINIO_SECRET_KEYstr
The secret key for the MinIO server.
- settings.BUCKET_NAMEstr
The name of the bucket to check for existence.
- Returns:
bool – True if the MinIO server is available and the bucket exists, False otherwise.