multipass

Multipass VM management for local Linux testing

CLI wrapper

callmultipass() mirrors calldocker() — runs the multipass CLI and returns stdout. Multipass uses the same kwargs-to-flags convention as Docker.


source

Multipass


def Multipass(
    args:VAR_POSITIONAL, kwargs:VAR_KEYWORD
):

Wrap multipass CLI: getattr dispatches subcommands, kwargs become flags


source

callmultipass


def callmultipass(
    args:VAR_POSITIONAL
):

Run a multipass CLI command, return stdout.

cloud_init_yaml

Generates a #cloud-config YAML string for Multipass --cloud-init. When docker=True (default) it installs Docker via get.docker.com.


source

cloud_init_yaml


def cloud_init_yaml(
    docker:bool=True, packages:NoneType=None, cmds:NoneType=None
)->str:

Generate cloud-init YAML for a Multipass VM

init = cloud_init_yaml()
assert '#cloud-config' in init
assert 'get.docker.com' in init
assert 'usermod' in init
print('cloud_init_yaml() default OK')

init2 = cloud_init_yaml(docker=False, packages=['git', 'vim'], cmds=['echo hello'])
assert 'get.docker.com' not in init2
assert '  - git' in init2
assert 'echo hello' in init2
print('cloud_init_yaml() custom OK')

print(init)

VM helpers


source

launch


def launch(
    name, image:str='22.04', cpus:int=1, memory:str='1G', disk:str='10G', cloud_init:NoneType=None,
    mounts:NoneType=None
):

Launch a Multipass VM. cloud_init can be YAML string or path to existing file.


source

transfer


def transfer(
    src, dst
)->None:

Transfer files to/from a Multipass VM. Use “vmname:/path” for VM paths.


source

delete


def delete(
    name, purge:bool=True
)->None:

Delete a Multipass VM.


source

exec_


def exec_(
    name, cmd:VAR_POSITIONAL
)->str:

Run a command in a Multipass VM.


source

vm_ip


def vm_ip(
    name
)->str:

Get the IPv4 address of a Multipass VM.


source

vms


def vms(
    running:bool=False
)->list:

List Multipass VM names. running=True filters to Running state.

# Test vms() - runs without error
try:
    result = vms()
    assert isinstance(result, list)
    print(f'vms() OK: {result}')
except Exception as e:
    print(f'multipass not available: {e}')

# Test vm_ip() error path on a non-existent VM
try: vm_ip('nonexistent-vm-dockr-test')
except subprocess.CalledProcessError: print('vm_ip() error path OK')
except Exception as e: print(f'Other error (multipass not installed?): {e}')

source

launch_docker_vm


def launch_docker_vm(
    name, image:str='22.04', cpus:int=2, memory:str='2G', disk:str='20G', packages:NoneType=None,
    mounts:NoneType=None
)->str:

Launch a Multipass VM with Docker pre-installed. Convenience wrapper for cloud_init_yaml + launch.