Skip to content

Commit

Permalink
Added note on how to access index.html on remote server (#2276)
Browse files Browse the repository at this point in the history
  • Loading branch information
schlunma authored Apr 3, 2024
1 parent dbfdea8 commit 6429a41
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 1 deletion.
34 changes: 34 additions & 0 deletions esmvalcore/experimental/recipe_output.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
"""API for handing recipe output."""
import base64
import getpass
import logging
import os.path
import sys
from collections.abc import Mapping, Sequence
from pathlib import Path
from typing import Optional, Tuple, Type
Expand Down Expand Up @@ -228,6 +230,37 @@ def from_core_recipe_output(cls, recipe_output: dict):

return cls(task_output, session=session, info=info)

def _log_ssh_html_info(self):
"""Log information about accessing index.html on an SSH server."""
if 'SSH_CONNECTION' not in os.environ:
return
server_ip = os.environ['SSH_CONNECTION'].split()[2]
server_ip_env = '${server}'
server = f'{getpass.getuser()}@{server_ip_env}'
port = '31415'
port_env = '${port}'
command = (
f'server={server_ip} && port={port} && '
f'ssh -t -L {port_env}:localhost:{port_env} {server} '
f'{sys.executable} -m http.server {port_env} -d '
f'{self.session.session_dir}'
)
logger.info(
"It looks like you are connected to a remote machine via SSH. To "
"show the output html file, you can try the following command:"
"\n%s\nThen visit http://localhost:%s in your browser",
command,
port,
)
logger.info(
"If the port %s is already in use, you can replace it with any "
"other free one (e.g., 12789). If you are connected through a "
"jump host, replace the server IP address %s with your SSH server "
"name",
port,
server_ip,
)

def write_html(self):
"""Write output summary to html document.
Expand All @@ -242,6 +275,7 @@ def write_html(self):
file.write(html_dump)

logger.info("Wrote recipe output to:\nfile://%s", filename)
self._log_ssh_html_info()

def render(self, template=None):
"""Render output as html.
Expand Down
18 changes: 17 additions & 1 deletion tests/sample_data/experimental/test_run_recipe.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
Runs recipes using :meth:`esmvalcore.experimental.Recipe.run`.
"""

import logging
import os
from contextlib import contextmanager
from pathlib import Path

Expand Down Expand Up @@ -55,12 +57,20 @@ def recipe():


@pytest.mark.use_sample_data
@pytest.mark.parametrize('ssh', (True, False))
@pytest.mark.parametrize('task', (None, 'example/ta'))
def test_run_recipe(monkeypatch, task, recipe, tmp_path):
def test_run_recipe(monkeypatch, task, ssh, recipe, tmp_path, caplog):
"""Test running a basic recipe using sample data.
Recipe contains no provenance and no diagnostics.
"""
caplog.set_level(logging.INFO)
caplog.clear()
if ssh:
monkeypatch.setitem(os.environ, 'SSH_CONNECTION', '0.0 0 1.1 1')
else:
monkeypatch.delitem(os.environ, 'SSH_CONNECTION', raising=False)

TAGS.set_tag_values(AUTHOR_TAGS)

assert isinstance(recipe, Recipe)
Expand Down Expand Up @@ -97,6 +107,12 @@ def test_run_recipe(monkeypatch, task, recipe, tmp_path):
cube = data_file.load_iris()
assert isinstance(cube, iris.cube.CubeList)

msg = "It looks like you are connected to a remote machine via SSH."
if ssh:
assert msg in caplog.text
else:
assert msg not in caplog.text


@pytest.mark.use_sample_data
def test_run_recipe_diagnostic_failing(monkeypatch, recipe, tmp_path):
Expand Down

0 comments on commit 6429a41

Please sign in to comment.