Skip to content

Commit

Permalink
Fix computers management logic and links. (#244)
Browse files Browse the repository at this point in the history
  • Loading branch information
yakutovicha committed Sep 23, 2021
1 parent 1f55895 commit cc513d4
Showing 1 changed file with 20 additions and 10 deletions.
30 changes: 20 additions & 10 deletions aiidalab_widgets_base/computers.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import os
from copy import copy
from os import path
from subprocess import call, check_output
from subprocess import CalledProcessError, call, check_output

import ipywidgets as ipw
import pexpect
Expand Down Expand Up @@ -41,7 +41,7 @@ class SshComputerSetup(ipw.VBox):

def __init__(self, **kwargs):
computer_image = ipw.HTML(
'<img width="200px" src="./miscellaneous/images/computer.png">'
'<img width="200px" src="../miscellaneous/images/computer.png">'
)

# Username.
Expand Down Expand Up @@ -204,11 +204,16 @@ def _make_host_known(self, hostname, proxycmd=None):
proxycmd = [] if proxycmd is None else proxycmd
fname = path.expanduser("~/.ssh/known_hosts")
print(f"Adding keys from {hostname} to {fname}")
hashes = check_output(
proxycmd + ["ssh-keyscan", "-p", str(self.port), "-H", hostname]
)
try:
hashes = check_output(
proxycmd + ["ssh-keyscan", "-p", str(self.port), "-H", hostname]
)
except CalledProcessError:
print(f"Couldn't add keys from {hostname} to {fname}. Aborting.")
return False
with open(fname, "a") as fobj:
fobj.write(hashes.decode("utf-8"))
return True

def can_login(self, silent=False):
"""Check if it is possible to login into the remote host."""
Expand Down Expand Up @@ -369,13 +374,17 @@ def _configure_proxy(self, password, proxy_password):

# Make proxy server known.
if not self.is_host_known(self.proxy_hostname):
self._make_host_known(self.proxy_hostname)
success = self._make_host_known(self.proxy_hostname)
if not success:
return False, ""

# Finally trying to connect.
if self._send_pubkey(self.proxy_hostname, proxy_username, proxy_password):
return True, proxy_username + "@" + self.proxy_hostname

print(f"Could not send public key to {self.proxy_hostname} (proxy server).")
print(
f"Could not send public key to {self.proxy_hostname} (proxy server), sorry :-("
)
return False, ""

# If proxy is NOT required.
Expand Down Expand Up @@ -447,14 +456,16 @@ def _on_setup_ssh(

# make host known by ssh on the proxy server
if not self.is_host_known():
self._make_host_known(
success = self._make_host_known(
self.hostname, ["ssh"] + [proxycmd] if proxycmd else []
)
if not success:
return

if mode == "password":
# sending public key to the main host
if not self._send_pubkey(self.hostname, self.username, password, proxycmd):
print("Could not send public key to {self.hostname}")
print("Could not send public key to {self.hostname}, sorry :-(")
return

# modify the ssh config file if necessary
Expand Down Expand Up @@ -834,7 +845,6 @@ def __init__(self, description="Select computer:", path_to_root="../", **kwargs)
"""

self.output = ipw.HTML()

self._dropdown = ipw.Dropdown(
options={},
value=None,
Expand Down

0 comments on commit cc513d4

Please sign in to comment.