Created
October 14, 2021 13:44
-
-
Save linw1995/3358973fb41938ba00b386e929510044 to your computer and use it in GitHub Desktop.
Use sshfs to mount directory of MacOS into podman machine.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
https://github.com/containers/podman/issues/8016#issuecomment-920015800 | |
""" | |
import getpass | |
import os | |
import re | |
import shlex | |
import subprocess | |
from pathlib import Path | |
SCRIPT_LAUNCH_PODMAN_MACHINE = """ | |
podman machine init | |
podman machine start | |
""" | |
def invoke(script: str): | |
for line in script.splitlines(): | |
if not line: | |
continue | |
return_code = subprocess.call(shlex.split(line)) | |
def get_podman_machine_ssh_args() -> str: | |
output = subprocess.check_output( | |
shlex.split("podman machine --log-level=debug ssh -- exit 2>&1"), | |
stderr=subprocess.STDOUT, | |
encoding="utf-8", | |
) | |
ssh_command = [line for line in output.splitlines() if "Executing" in line][0] | |
m = re.match(r".+ ssh \[(.+localhost) -o.+\]", ssh_command) | |
return m.group(1) | |
def get_hostname() -> str: | |
return subprocess.check_output(["hostname"], encoding="utf-8").strip() | |
def get_username() -> str: | |
return os.environ.get("USER") | |
def podman_machine_invoke(hostname, ssh_args, cmd, stdin): | |
p = subprocess.Popen( | |
shlex.split(f"ssh -T -R 10000:{hostname}:22 {ssh_args} {cmd}"), | |
stdin=subprocess.PIPE, | |
stdout=subprocess.PIPE, | |
) | |
stdout, _ = p.communicate(stdin.lstrip().encode()) | |
try: | |
p.wait() | |
except KeyboardInterrupt: | |
p.terminate() | |
stdout = stdout.decode() | |
print(stdout) | |
return stdout | |
def main(): | |
invoke(SCRIPT_LAUNCH_PODMAN_MACHINE) | |
ssh_args = get_podman_machine_ssh_args() | |
hostname = get_hostname() | |
username = get_username() | |
podman_machine_invoke(hostname, ssh_args, "ssh-keygen -t rsa", "\n\n\n") | |
stdout = podman_machine_invoke( | |
hostname, ssh_args, "cat /var/home/core/.ssh/id_rsa.pub", "" | |
) | |
with Path("~/.ssh/authorized_keys").expanduser().open("w+") as f: | |
for line in f: | |
if line == stdout: | |
break | |
else: | |
f.write(stdout) | |
podman_machine_invoke( | |
hostname, | |
ssh_args, | |
"", | |
f""" | |
sudo mkdir -p /mnt/Users/{username}/Documents/workspace | |
sudo chown core:core /mnt/Users/{username}/Documents/workspace | |
""", | |
) | |
podman_machine_invoke( | |
hostname, | |
ssh_args, | |
"", | |
f"sshfs -p 10000 \ | |
{username}@127.0.0.1:/Users/{username}/Documents/workspace \ | |
/mnt/Users/{username}/Documents/workspace", | |
) | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment