22
22
get_master_ip_and_username ,
23
23
get_stack ,
24
24
get_stack_name ,
25
+ retry ,
25
26
)
26
27
27
28
LOGGER = logging .getLogger (__name__ )
28
29
29
30
31
+ class DCVConnectionError (Exception ):
32
+ """Error raised with DCV connection fails."""
33
+
34
+ pass
35
+
36
+
30
37
def _check_command_output (cmd ):
31
38
return sub .check_output (cmd , shell = True , universal_newlines = True , stderr = sub .STDOUT ).strip ()
32
39
@@ -52,13 +59,35 @@ def dcv_connect(args):
52
59
DCV_SHARED_DIR = shared_dir ,
53
60
)
54
61
55
- # Connect by ssh to the master instance and prepare DCV session
56
62
try :
57
- LOGGER .debug ("SSH command: {0}" .format (cmd ))
58
- output = _check_command_output (cmd )
63
+ url = retry (_retrieve_dcv_session_url , func_args = [cmd , args .cluster_name , master_ip ], attempts = 4 )
64
+ url_message = "Please use the following one-time URL in your browser within 30 seconds:\n {0}" .format (url )
65
+ except DCVConnectionError as e :
66
+ error (
67
+ "Something went wrong during DCV connection.\n {0}"
68
+ "Please check the logs in the /var/log/parallelcluster/ folder "
69
+ "of the master instance and submit an issue {1}\n " .format (e , PCLUSTER_ISSUES_LINK )
70
+ )
71
+
72
+ if args .show_url :
73
+ LOGGER .info (url_message )
74
+ return
75
+
76
+ try :
77
+ if not webbrowser .open_new (url ):
78
+ raise webbrowser .Error ("Unable to open the Web browser." )
79
+ except webbrowser .Error as e :
80
+ LOGGER .info ("{0}\n {1}" .format (e , url_message ))
81
+
82
+
83
+ def _retrieve_dcv_session_url (ssh_cmd , cluster_name , master_ip ):
84
+ """Connect by ssh to the master instance, prepare DCV session and return the DCV session URL."""
85
+ try :
86
+ LOGGER .debug ("SSH command: {0}" .format (ssh_cmd ))
87
+ output = _check_command_output (ssh_cmd )
59
88
# At first ssh connection, the ssh command alerts it is adding the host to the known hosts list
60
89
if re .search ("Permanently added .* to the list of known hosts." , output ):
61
- output = _check_command_output (cmd )
90
+ output = _check_command_output (ssh_cmd )
62
91
63
92
dcv_parameters = re .search (
64
93
r"PclusterDcvServerPort=([\d]+) PclusterDcvSessionId=([\w]+) PclusterDcvSessionToken=([\w-]+)" , output
@@ -71,30 +100,18 @@ def dcv_connect(args):
71
100
error (
72
101
"Something went wrong during DCV connection. Please manually execute the command:\n {0}\n "
73
102
"If the problem persists, please check the logs in the /var/log/parallelcluster/ folder "
74
- "of the master instance and submit an issue {1}. " .format (cmd , PCLUSTER_ISSUES_LINK )
103
+ "of the master instance and submit an issue {1}" .format (ssh_cmd , PCLUSTER_ISSUES_LINK )
75
104
)
76
105
77
106
except sub .CalledProcessError as e :
78
107
if "{0}: No such file or directory" .format (DCV_CONNECT_SCRIPT ) in e .output :
79
108
error (
80
109
"The cluster {0} has been created with an old version of ParallelCluster "
81
- "without the DCV support." .format (args . cluster_name )
110
+ "without the DCV support." .format (cluster_name )
82
111
)
83
112
else :
84
- error ( "Something went wrong during DCV connection. \n {0}" . format (e .output ) )
113
+ raise DCVConnectionError (e .output )
85
114
86
- # DCV URL
87
- url = "https://{IP}:{PORT}?authToken={TOKEN}#{SESSION_ID}" .format (
115
+ return "https://{IP}:{PORT}?authToken={TOKEN}#{SESSION_ID}" .format (
88
116
IP = master_ip , PORT = dcv_server_port , TOKEN = dcv_session_token , SESSION_ID = dcv_session_id
89
117
)
90
- url_message = "Please use the following one-time URL in your browser within 30 seconds:\n {0}" .format (url )
91
-
92
- if args .show_url :
93
- LOGGER .info (url_message )
94
- return
95
-
96
- try :
97
- if not webbrowser .open_new (url ):
98
- raise webbrowser .Error ("Unable to open the Web browser." )
99
- except webbrowser .Error as e :
100
- LOGGER .info ("{0}\n {1}" .format (e , url_message ))
0 commit comments