Skip to content

Commit

Permalink
fix: improved remote execution (worked only locally before) - require…
Browse files Browse the repository at this point in the history
…s (currently unreleased) edwh>=0.43
  • Loading branch information
robinvandernoord committed Jul 11, 2024
1 parent cccbf11 commit 0eab701
Showing 1 changed file with 13 additions and 9 deletions.
22 changes: 13 additions & 9 deletions src/edwh_multipass_plugin/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,25 +7,28 @@
import edwh
import yaml
from edwh import confirm
from invoke import Context, task
from fabric import Connection, task

T = typing.TypeVar("T")

# abs path required for remote connections
MULTIPASS = "/snap/bin/multipass"


@task(name="install", pre=[edwh.tasks.require_sudo])
def install_multipass(c: Context):
def install_multipass(c: Connection):
"""
Install multipass on this host.
"""
if not c.run("multipass --version", warn=True, hide=True).ok:
if not c.run(f"{MULTIPASS} --version", warn=True, hide=True).ok:
print(" [ ] Multipass not found. installing...")
c.sudo("snap install multipass")
print(" [x] Multipass installed")
else:
print(" [x] Multipass already installed")


def generate_key(c, comment: str, filename: str):
def generate_key(c: Connection, comment: str, filename: str):
"""
Create an SSH pub-priv keypair.
"""
Expand All @@ -42,7 +45,7 @@ def uniq(lst: list[T]) -> list[T]:


@task(name="fix-host", aliases=["fix-dns"], iterable=["hostname"], pre=[edwh.tasks.require_sudo])
def fix_hosts_for_multipass_machine(c: Context, machine_name: str, hostname: typing.Collection[str] = ()):
def fix_hosts_for_multipass_machine(c: Connection, machine_name: str, hostname: typing.Collection[str] = ()):
"""
Update your hosts file to connect fake hostnames to your multipass IP.
Expand All @@ -54,7 +57,7 @@ def fix_hosts_for_multipass_machine(c: Context, machine_name: str, hostname: typ
print("Machine name required. Use -m or --machine-name", file=sys.stderr)
exit(1)

output = c.run("multipass list --format yaml", hide=True).stdout.strip()
output = c.run(f"{MULTIPASS} list --format yaml", hide=True).stdout.strip()
machines = yaml.load(output, yaml.SafeLoader)
if machine_name not in machines:
print(
Expand Down Expand Up @@ -120,19 +123,20 @@ def fix_hosts_for_multipass_machine(c: Context, machine_name: str, hostname: typ


@task(name="list")
def list_machines(c: Context, quiet=False):
def list_machines(c: Connection, quiet=False):
"""
List multipass machines.
"""
output = c.run("multipass list --format json", hide=True).stdout

output = c.run(f"{MULTIPASS} list --format json", hide=True).stdout
if quiet:
return json.loads(output)["list"]
else:
print(output)


@task(pre=[install_multipass], name="prepare")
def prepare_multipass(c: Context, machine_name: str):
def prepare_multipass(c: Connection, machine_name: str):
"""
Setup ssh access to a multipass machine.
"""
Expand Down

0 comments on commit 0eab701

Please sign in to comment.