Skip to content

Commit

Permalink
Adds support for specifying a network interface (#19)
Browse files Browse the repository at this point in the history
* Adding networks support on VM creation

---------

Co-authored-by: Jakub Suchy
Co-authored-by: theko2fi
  • Loading branch information
jakubsuchy authored Dec 20, 2024
1 parent 436b537 commit 7778488
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 2 deletions.
16 changes: 14 additions & 2 deletions plugins/module_utils/multipass.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,14 +120,26 @@ class MultipassClient:
def __init__(self, multipass_cmd="multipass"):
self.cmd = multipass_cmd

def launch(self, vm_name=None, cpu=1, disk="5G", mem="1G", image=None, cloud_init=None):
def launch(self, vm_name=None, cpu=1, disk="5G", mem="1G", networks=[], image=None, cloud_init=None):
if(not vm_name):
# similar to Multipass's VM name generator
vm_name = Haikunator().haikunate(token_length=0)
cmd = [self.cmd, "launch", "-c", str(cpu), "-d", disk, "-n", vm_name, "-m", mem]
if(cloud_init):
cmd.append("--cloud-init")
cmd.append(cloud_init)
for network in networks:
network_str = f"name={network['name']}"

# Add optional parameters if they exist
if 'mode' in network and network['mode']:
network_str += f",mode={network['mode']}"

if 'mac' in network and network['mac']:
network_str += f",mac={network['mac']}"

cmd.append("--network")
cmd.append(network_str)
if(image and not image == "ubuntu-lts"):
cmd.append(image)
try:
Expand Down Expand Up @@ -230,4 +242,4 @@ def get(self, key):
if(exitcode != 0):
raise Exception("Multipass get command failed: {0}".format(stderr.decode(encoding="utf-8")))
# remove trailing "\r\n" when returning the stdout
return stdout.rstrip()
return stdout.rstrip()
50 changes: 50 additions & 0 deletions plugins/modules/multipass_vm.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,12 @@ def main():
state = dict(required=False, type=str, default='present'),
recreate = dict(required=False, type=bool, default=False),
purge = dict(required=False, type=bool, default=False),
networks = dict(type='list', elements='dict', suboptions=dict(
name=dict(type='str', required=True),
mode=dict(type='str', choices=['auto', 'manual'], default='auto'),
mac=dict(type='str'),
)
),
mounts = dict(type='list', elements='dict', suboptions=dict(
target=dict(type='str'),
source=dict(type='str', required=True),
Expand All @@ -105,6 +111,7 @@ def main():
vm_name = module.params.get('name')
image = module.params.get('image')
cpus = module.params.get('cpus')
networks = module.params.get('networks')
state = module.params.get('state')
memory = module.params.get('memory')
disk = module.params.get('disk')
Expand All @@ -123,6 +130,7 @@ def main():
image=image,
cpu=cpus,
mem=memory,
networks=networks,
disk=disk,
cloud_init=cloud_init
)
Expand Down Expand Up @@ -153,6 +161,7 @@ def main():
image=image,
cpu=cpus,
mem=memory,
networks=networks,
disk=disk,
cloud_init=cloud_init
)
Expand Down Expand Up @@ -263,6 +272,32 @@ def main():
description: The number of CPUs of the VM.
required: false
type: int
networks:
description: A list of networks to be used in the VM specification.
required: false
elements: dict
type: list
suboptions:
name:
type: str
description:
- Name of the host network to connect the instance's device to
required: true
mode:
type: str
description:
- Network mode
- If it's C('auto') the VM will attempt to configure the network automatically.
required: false
default: auto
choices:
- auto
- manual
mac:
type: str
description:
- Custom MAC address to use for the device.
required: false
memory:
description: The amount of RAM to allocate to the VM.
required: false
Expand Down Expand Up @@ -372,6 +407,21 @@ def main():
cpus: 2
memory: 2G
disk: 5G
networks:
- name: en0
- name: Create a VM with multiple networks
theko2fi.multipass.multipass_vm:
name: foo
cpus: 2
memory: 2G
disk: 5G
networks:
- name: en0
- name: bridge0
mode: manual
mac: '50:b3:41:6c:83:cd'
- name: Stop a VM
theko2fi.multipass.multipass_vm:
Expand Down

0 comments on commit 7778488

Please sign in to comment.