20
20
import webbrowser
21
21
from datetime import datetime
22
22
from pathlib import Path
23
- from typing import TYPE_CHECKING , Any , Dict , Iterable , List , Optional , Tuple , Union , cast
23
+ from typing import TYPE_CHECKING , Any , Dict , Iterator , List , Optional , Tuple , Union , cast
24
24
from uuid import uuid4
25
25
26
26
import docker
29
29
from renku .core import errors
30
30
from renku .core .config import get_value
31
31
from renku .core .constant import ProviderPriority
32
+ from renku .core .login import read_renku_token
32
33
from renku .core .plugin import hookimpl
34
+ from renku .core .session .utils import get_renku_url
33
35
from renku .core .util import communication
36
+ from renku .core .util .jwt import is_token_expired
34
37
from renku .domain_model .project_context import project_context
35
38
from renku .domain_model .session import ISessionProvider , Session , SessionStopStatus
36
39
@@ -67,11 +70,14 @@ def docker_client(self) -> docker.client.DockerClient:
67
70
return self ._docker_client
68
71
69
72
@staticmethod
70
- def _get_jupyter_urls (ports : Dict [str , Any ], auth_token : str , jupyter_port : int = 8888 ) -> Iterable [str ]:
73
+ def _get_jupyter_urls (ports : Dict [str , Any ], auth_token : str , jupyter_port : int = 8888 ) -> Iterator [str ]:
71
74
port_key = f"{ jupyter_port } /tcp"
72
75
if port_key not in ports :
73
- return list ()
74
- return map (lambda x : f"http://{ x ['HostIp' ]} :{ x ['HostPort' ]} /?token={ auth_token } " , ports [port_key ])
76
+ return list () # type: ignore
77
+ default_url = get_value ("interactive" , "default_url" )
78
+ if not default_url :
79
+ default_url = "/lab"
80
+ return map (lambda x : f"http://{ x ['HostIp' ]} :{ x ['HostPort' ]} { default_url } ?token={ auth_token } " , ports [port_key ])
75
81
76
82
def _get_docker_containers (self , project_name : str ) -> List [docker .models .containers .Container ]:
77
83
return self .docker_client ().containers .list (filters = {"label" : f"renku_project={ project_name } " })
@@ -92,9 +98,22 @@ def build_image(self, image_descriptor: Path, image_name: str, config: Optional[
92
98
def find_image (self , image_name : str , config : Optional [Dict [str , Any ]]) -> bool :
93
99
"""Find the given container image."""
94
100
with communication .busy (msg = f"Checking for image { image_name } " ):
101
+ renku_url = get_renku_url ()
102
+
103
+ # only search remote image if a user is logged in
104
+ find_remote = True
105
+ if renku_url is None :
106
+ find_remote = False
107
+ else :
108
+ token = read_renku_token (endpoint = renku_url )
109
+ if not token or is_token_expired (token ):
110
+ find_remote = False
111
+
95
112
try :
96
113
self .docker_client ().images .get (image_name )
97
114
except docker .errors .ImageNotFound :
115
+ if not find_remote :
116
+ return False
98
117
try :
99
118
self .docker_client ().images .get_registry_data (image_name )
100
119
except docker .errors .APIError :
@@ -454,13 +473,23 @@ def session_open(self, project_name: str, session_name: Optional[str], **kwargs)
454
473
def session_url (self , session_name : Optional [str ]) -> Optional [str ]:
455
474
"""Get the URL of the interactive session."""
456
475
sessions = self .docker_client ().containers .list ()
476
+ default_url = get_value ("interactive" , "default_url" )
477
+ if not default_url :
478
+ default_url = "/lab"
457
479
458
480
for c in sessions :
459
481
if (
460
482
c .short_id == session_name or (not session_name and len (sessions ) == 1 )
461
483
) and f"{ DockerSessionProvider .JUPYTER_PORT } /tcp" in c .ports :
462
- host = c .ports [f"{ DockerSessionProvider .JUPYTER_PORT } /tcp" ][0 ]
463
- return f'http://{ host ["HostIp" ]} :{ host ["HostPort" ]} /?token={ c .labels ["jupyter_token" ]} '
484
+ url = next (
485
+ DockerSessionProvider ._get_jupyter_urls (
486
+ c .ports , c .labels ["jupyter_token" ], DockerSessionProvider .JUPYTER_PORT
487
+ ),
488
+ None ,
489
+ )
490
+ if not url :
491
+ continue
492
+ return url
464
493
return None
465
494
466
495
def force_build_image (self , force_build : bool = False , ** kwargs ) -> bool :
0 commit comments