Skip to content
Snippets Groups Projects

WS-649: send CARTA URL to workspaces system

Merged Janet Goldstein requested to merge WS-649-send-carta-url-to-ws into main
All threads resolved!
5 files
+ 284
73
Compare changes
  • Side-by-side
  • Inline
Files
5
@@ -2,6 +2,8 @@ import logging
import socket
from typing import Dict
# pylint: disable=C0103, C0301, E0401, E0402, E1101, R0903, R0913, W0105, W1203
import redis
import requests
@@ -26,6 +28,11 @@ class RedisConnect:
@staticmethod
def generate_ids() -> dict:
"""
Generates IDs and system name required by Redis
:return: dict of settings
"""
return {
"front_end_id": generate_random_str(),
"back_end_id": generate_random_str(),
@@ -34,11 +41,18 @@ class RedisConnect:
}
def generate_carta_url(self) -> str:
"""
Generates CARTA URL for Redis
:return: URL of current CARTA session
"""
self.logger.info("Generating CARTA url...")
front_end_id = self.generated_ids["front_end_id"]
back_end_id = self.generated_ids["back_end_id"]
proxy = self.settings["reverse_proxy"]
carta_url = f"https://{proxy}/{front_end_id}/?socketUrl=wss://{proxy}/{back_end_id}/"
carta_url = (
f"https://{proxy}/{front_end_id}/?socketUrl=wss://{proxy}/{back_end_id}/"
)
if self.settings["single_image"]:
carta_url = carta_url + "&file=" + self.settings["image_name"]
@@ -72,6 +86,11 @@ class RedisConnect:
return s.getsockname()[1]
def prepare_redis(self) -> dict:
"""
Gathers Redis server settings
:return: settings as key-value pairs
"""
self.logger.info("Preparing Redis server...")
front_end_port = self.get_available_port()
back_end_port = self.get_available_port()
@@ -162,15 +181,21 @@ class RedisConnect:
f"traefik/http/routers/{carta_wrapper}/middlewares/0": "stripPrefixFE@file",
}
unique_values = self.check_for_duplicate_values(values, front_end_port, back_end_port, wrapper_port)
unique_values = self.check_for_duplicate_values(
values, front_end_port, back_end_port, wrapper_port
)
self.redis_values = unique_values
return unique_values
def check_for_duplicate_values(self, redis_values: dict, front_port: int, back_port: int, wrapper_port: int):
def check_for_duplicate_values(
self, redis_values: dict, front_port: int, back_port: int, wrapper_port: int
):
self.logger.info("Checking for duplicate values on server...")
for key in redis_values:
if self.conn.get(key):
self.logger.warning("WARNING: Redis value collision found. Generating new random IDs.")
self.logger.warning(
"WARNING: Redis value collision found. Generating new random IDs."
)
self.generated_ids = self.generate_ids()
new_values = self.get_redis_values(
self.settings["reverse_proxy"],
@@ -192,7 +217,9 @@ class RedisConnect:
:param redis_values: Dictionary of Redis entries
:param timeout_minutes: Timeout of the valet in minutes
"""
self.logger.info(f"Setting Redis values with a timeout of {timeout_minutes} minutes...")
self.logger.info(
f"Setting Redis values with a timeout of {timeout_minutes} minutes..."
)
for key, val in redis_values.items():
self.conn.setex(key, 60 * timeout_minutes + 60, val)
@@ -218,9 +245,7 @@ class ArchiveConnect:
:param url: URL generated to allow user access to this running CARTA instance
"""
send_archive_msg_url = (
f"{self.settings['workflow_url']}/workflows/carta/requests/{self.settings['wf_request_id']}/send-url-to-aat"
)
send_archive_msg_url = f"{self.settings['workflow_url']}/workflows/carta/requests/{self.settings['wf_request_id']}/send-url-to-aat"
payload = {"carta_url": url}
self.logger.info("Sending REST call to workflow service for AAT messaging.")
requests.post(send_archive_msg_url, json=payload)
@@ -237,11 +262,19 @@ class NotificationConnect:
self.url = settings["notification_url"]
def send_session_ready(self, wrapper_url: str):
"""
Notifies of CARTA session that's been created
:param wrapper_url: CARTA session URL
:return:
"""
if "user_email" not in self.settings:
self.logger.info("Not sending notification because no user email supplied")
return
self.logger.info(f"Sending session ready notification with CARTA wrapper URL {wrapper_url}")
self.logger.info(
f"Sending session ready notification with CARTA wrapper URL {wrapper_url}"
)
requests.post(
f"{self.url}/notify/carta_ready/send",
json={
@@ -251,6 +284,10 @@ class NotificationConnect:
)
def send_session_expired(self):
"""
Notifies of expired CARTA session
:return:
"""
if "user_email" not in self.settings:
self.logger.info("Not sending notification because no user email supplied")
return
@@ -262,3 +299,24 @@ class NotificationConnect:
"destination_email": self.settings["user_email"],
},
)
class WorkflowConnect:
"""For sending CARTA URL to workspaces system"""
def __init__(self, settings):
self.logger = logging.getLogger("carta_envoy")
self.settings = settings
self.url = settings["workflow_url"]
def send_url_message(self):
"""
Makes REST call to send message containing CARTA URL to workspaces system
:return:
"""
send_url_message = f"{self.settings['workflow_url']}/workflows/carta/requests/{self.settings['wf_request_id']}/send-url-to-ws"
payload = {"carta_url": self.url}
self.logger.info(
"Sending REST call to workflow service for WS CARTA messaging."
)
requests.post(send_url_message, json=payload)
Loading