Skip to content

Commit

Permalink
added CARTA 2.x support
Browse files Browse the repository at this point in the history
  • Loading branch information
o-smirnov committed Jun 2, 2021
1 parent e102b44 commit 98b5858
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 16 deletions.
13 changes: 13 additions & 0 deletions iglesia/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@

LOCALHOST_URL = None # http://localhost or https://localhost, depending on SSL settings

CARTA_VERSION = None

HOSTNAME = "localhost"

SNOOP_MODE = False # true if browsing someone else's files
Expand Down Expand Up @@ -179,3 +181,14 @@ def set_userside_ports(userside_ports):
"""Sets the relevant userside port variables"""
global JUPYTER_PORT, JS9HELPER_PORT, HTTPSERVER_PORT, CARTA_PORT, CARTA_WS_PORT, WETTY_PORT
JUPYTER_PORT, JS9HELPER_PORT, HTTPSERVER_PORT, CARTA_PORT, CARTA_WS_PORT, WETTY_PORT = userside_ports

def get_carta_url(*args):
url = f"http://localhost:{CARTA_PORT}"

if CARTA_VERSION < "2":
args = [f"socketUrl=ws://localhost:{CARTA_WS_PORT}"] + args

if args:
return f"{url}?{'&'.join(args)}"
else:
return url
37 changes: 28 additions & 9 deletions iglesia/helpers.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import os, sys, subprocess, atexit, traceback, getpass, tempfile, psutil, stat
from radiopadre_client.config import RADIOPADRE_VENV

import iglesia
from iglesia import PadreError
Expand Down Expand Up @@ -145,9 +146,9 @@ def init_helpers(radiopadre_base, verbose=False, run_http=True, interactive=True

if interactive:
if 'RADIOPADRE_CARTA_PID' not in os.environ:
# start CARTA backend
# find CARTA backend or CARTA app
for carta_exec in os.environ.get('RADIOPADRE_CARTA_EXEC'), f"{sys.prefix}/carta/carta", \
find_which('carta'):
find_which('carta_backend'), find_which('carta'):
# if carta_exec:
# subprocess.call(f"ls -l /.radiopadre/venv", shell=True)
# message("{}: {} {}".format(carta_exec, os.path.exists(carta_exec), os.access(carta_exec, os.X_OK)))
Expand All @@ -157,16 +158,34 @@ def init_helpers(radiopadre_base, verbose=False, run_http=True, interactive=True
carta_exec = None

if not carta_exec or not os.path.exists(carta_exec):
warning(f"CARTA backend not found, omitting ({sys.prefix}/carta/carta)")
warning(f"CARTA backend not found, omitting")
else:
# check version, assume 1.x if not found
carta_version = f"{sys.prefix}/carta_version"
if os.path.exists(carta_version):
iglesia.CARTA_VERSION = open(carta_version, "rt").read()
message(f"Detected CARTA version {iglesia.CARTA_VERSION}")
else:
iglesia.CARTA_VERSION = "2.x" if carta_exec.endswith("backend") else "1.x"
message(f"Assuming CARTA version {iglesia.CARTA_VERSION}, as none was detected")

carta_dir = os.environ.get('RADIOPADRE_CARTA_DIR') or os.path.dirname(os.path.dirname(carta_exec))
message(f"Running CARTA backend {carta_exec} (in dir {carta_dir})")
message(f"Running CARTA {iglesia.CARTA_VERSION} backend {carta_exec} (in dir {carta_dir})")

if iglesia.CARTA_VERSION >= "2":
carta_dir = iglesia.ABSROOTDIR
cmdline = [carta_exec, f"--port={carta_port}", "--no_browser", "--debug_no_auth",
f"--top_level_folder={iglesia.ABSROOTDIR}", f"--frontend_folder=/usr/share/carta/frontend" ]
carta_stdout, carta_stderr = sys.stdout, sys.stderr
else:
cmdline = [carta_exec, "--remote",
f"--root={iglesia.ABSROOTDIR}", f"--folder={iglesia.ABSROOTDIR}",
f"--port={carta_ws_port}", f"--fport={carta_port}"]
carta_stdout, carta_stderr = stdout, stderr

message(f"$ {' '.join(cmdline)}")
with chdir(carta_dir):
_child_processes.append(
subprocess.Popen([carta_exec, "--remote",
f"--root={iglesia.ABSROOTDIR}", f"--folder={iglesia.ABSROOTDIR}",
f"--port={carta_ws_port}", f"--fport={carta_port}"],
stdin=subprocess.PIPE, stdout=stdout, stderr=stderr))
_child_processes.append(subprocess.Popen(cmdline, stdin=subprocess.PIPE, stdout=carta_stdout, stderr=carta_stderr, shell=False))
os.environ['RADIOPADRE_CARTA_PID'] = str(_child_processes[-1].pid)
## doesn't exit cleanly, let it be eaten rather
# atexit.register(_exit_carta, _child_processes[-1])
Expand Down
4 changes: 4 additions & 0 deletions radiopadre_client/backends/venv.py
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,10 @@ def start_session(container_name, selected_ports, userside_ports, notebook_path,
iglesia.init_helpers(radiopadre_base, verbose=config.VERBOSE > 0,
interactive=not config.NBCONVERT, certificate=config.SERVER_PEM)

# add CARTA UR, if configured
if config.CARTA_BROWSER and iglesia.CARTA_VERSION:
browser_urls.append(iglesia.get_carta_url())

## start jupyter process
jupyter_path = config.RADIOPADRE_VENV + "/bin/jupyter"
message("Starting: {} {} in {}".format(jupyter_path, " ".join(JUPYTER_OPTS), os.getcwd()))
Expand Down
4 changes: 2 additions & 2 deletions radiopadre_client/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -177,10 +177,10 @@ def add_to_parser(parser):
# else check for opposite-value switch
else:
if type(DefaultConfig[key]) is bool and "--" + optname not in NON_PERSISTING_OPTIONS:
if default_cmdline is 0:
if default_cmdline == 0:
parser.add_argument("--no-" + optname, action="store_false", dest=lkey,
help=f"opposite of --{optname}.")
elif default_cmdline is 1:
elif default_cmdline == 1:
parser.add_argument("--" + optname, action="store_true",
help=f"opposite of --no-{optname}.")
_CMDLINE_DEFAULTS[key] = default_cmdline
Expand Down
10 changes: 9 additions & 1 deletion radiopadre_client/remote.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ def check_remote_command(command):

for backend in config.BACKEND:
remote_config["BACKEND"] = backend
if backend == "venv" and check_remote_command("virtualenv") and check_remote_command("pip"):
if backend == "venv" and check_remote_command("virtualenv"):
USE_VENV = True
break
elif backend == "docker":
Expand Down Expand Up @@ -416,6 +416,14 @@ def check_remote_command(command):
container_name = match.group(1)
continue

# check for CARTA version
match = re.match(".*Running CARTA (.+) backend", line)
if match:
iglesia.CARTA_VERSION = match.group(1)
if config.CARTA_BROWSER:
urls.append(iglesia.get_carta_url())
continue

if "jupyter notebook server is running" in line:
remote_running = True
time.sleep(1)
Expand Down
8 changes: 4 additions & 4 deletions radiopadre_client/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -347,10 +347,10 @@ def run_radiopadre_server(command, arguments, notebook_path, workdir=None):
if not config.NBCONVERT:
for url in urls[::-1]:
message(f"Browse to URL: {url}", color="GREEN")
if config.CARTA_BROWSER:
url = f"http://localhost:{iglesia.CARTA_PORT}/?socketUrl=ws://localhost:{iglesia.CARTA_WS_PORT}"
message(f"Browse to URL: {url} (CARTA file browser)", color="GREEN")
urls.append(url)
# if config.CARTA_BROWSER:
# message(f"Browse to URL: {iglesia.get_carta_url()} (CARTA file browser)", color="GREEN")
# urls.append(url)


# now we're ready to start the session
Expand Down

0 comments on commit 98b5858

Please sign in to comment.