Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

copy id_rsa wsl to windows #135

Draft
wants to merge 5 commits into
base: master
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 21 additions & 17 deletions milatools/cli/init_command.py
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,25 @@ def setup_ssh_config(
print(f"Wrote {ssh_config_path}")
return ssh_config

def _maybe_copy_keys():
if not running_inside_WSL():
return None
# Copy the SSH key to the windows folder so that passwordless SSH also works on
# Windows.
# TODO: This will need to change if we support using a non-default location at some
# point.
windows_home = get_windows_home_path_in_wsl()
linux_private_key_file = Path.home() / ".ssh/id_rsa"
windows_private_key_file = windows_home / ".ssh/id_rsa"

for linux_key_file, windows_key_file in [
(linux_private_key_file, windows_private_key_file),
(
linux_private_key_file.with_suffix(".pub"),
windows_private_key_file.with_suffix(".pub"),
),
]:
_copy_if_needed(linux_key_file, windows_key_file)

def setup_windows_ssh_config_from_wsl(linux_ssh_config: SSHConfig):
"""Setup the Windows SSH configuration and public key from within WSL.
Expand Down Expand Up @@ -211,22 +230,6 @@ def setup_windows_ssh_config_from_wsl(linux_ssh_config: SSHConfig):
print(f"Did not change ssh config at path {windows_ssh_config.path}")
return # also skip copying the SSH keys.

# Copy the SSH key to the windows folder so that passwordless SSH also works on
# Windows.
# TODO: This will need to change if we support using a non-default location at some
# point.
linux_private_key_file = Path.home() / ".ssh/id_rsa"
windows_private_key_file = windows_home / ".ssh/id_rsa"

for linux_key_file, windows_key_file in [
(linux_private_key_file, windows_private_key_file),
(
linux_private_key_file.with_suffix(".pub"),
windows_private_key_file.with_suffix(".pub"),
),
]:
_copy_if_needed(linux_key_file, windows_key_file)


def setup_passwordless_ssh_access(ssh_config: SSHConfig) -> bool:
"""Sets up passwordless ssh access to the Mila and optionally also to DRAC.
Expand Down Expand Up @@ -254,6 +257,7 @@ def setup_passwordless_ssh_access(ssh_config: SSHConfig) -> bool:
# TODO: This uses the public key set in the SSH config file, which may (or may not)
# be the random id*.pub file that was just checked for above.
success = setup_passwordless_ssh_access_to_cluster("mila")
_maybe_copy_keys() # if running inside WSL, copy the keys to the Windows folder.
if not success:
return False
setup_keys_on_login_node("mila")
Expand Down Expand Up @@ -434,7 +438,7 @@ def _copy_if_needed(linux_key_file: Path, windows_key_file: Path):
)
shutil.copy2(src=linux_key_file, dst=windows_key_file)


@functools.lru_cache()
def get_windows_home_path_in_wsl() -> Path:
assert running_inside_WSL()
windows_username = subprocess.getoutput("powershell.exe '$env:UserName'").strip()
Expand Down
Loading