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

Add Custom Network Interface at mech up #24

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
21 changes: 11 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ Usage: mech up [OPTIONS] [INSTANCE]

If no instance is specified, all instances will be started.

The options ('memsize', 'numvcpus', and 'no-nat') will only be applied
The options ('memsize', 'numvcpus', add-custom-interface, and 'no-nat') will only be applied
upon first run of the 'up' command.

The 'no-nat' option will only be applied if there is no network interface
Expand All @@ -110,15 +110,16 @@ Usage: mech up [OPTIONS] [INSTANCE]
root cannot ssh, or change the root password.

Options:
--disable-provisioning Do not provision.
--disable-shared-folders Do not share folders.
--gui Start GUI, otherwise starts headless.
--memsize MEMORY Specify memory size in MB.
--no-cache Do not save the downloaded box.
--no-nat Do not use NAT networking (i.e., use bridged).
--numvcpus VCPUS Specify number of vcpus.
-r, --remove-vagrant Remove vagrant user.
-h, --help Show this message and exit.
--disable-provisioning Do not provision.
--disable-shared-folders Do not share folders.
--gui Start GUI, otherwise starts headless.
--memsize MEMORY Specify memory size in MB.
--no-cache Do not save the downloaded box.
--no-nat Do not use NAT networking (i.e., use bridged).
--numvcpus VCPUS Specify number of vcpus.
--add-custom-interface VMNET Add a custom netowrk interface (i.e., 3 for VMnet3)
-r, --remove-vagrant Remove vagrant user.
-h, --help Show this message and exit.
```

# Example using mech
Expand Down
8 changes: 5 additions & 3 deletions mech/mech.py
Original file line number Diff line number Diff line change
Expand Up @@ -734,18 +734,19 @@ def global_status(ctx, purge):
@click.option('--no-nat', is_flag=True, default=False,
help='Do not use NAT networking (i.e., use bridged).')
@click.option('--numvcpus', metavar='VCPUS', help='Specify number of vcpus.')
@click.option('--add-custom-interface', metavar='VMNET', help='Add a custom netowrk interface (i.e., 3 for VMnet3)')
@click.option('-r', '--remove-vagrant', is_flag=True, default=False, help='Remove vagrant user.')
@click.pass_context
def up(ctx, instance, disable_provisioning, disable_shared_folders, gui, memsize, no_cache,
no_nat, numvcpus, remove_vagrant):
no_nat, numvcpus, add_custom_interface, remove_vagrant):
'''
Starts and provisions instance(s).

Notes:

If no instance is specified, all instances will be started.

The options ('memsize', 'numvcpus', and 'no-nat') will only be applied
The options ('memsize', 'numvcpus', 'add-custom-interface', and 'no-nat') will only be applied
upon first run of the 'up' command.

The 'no-nat' option will only be applied if there is no network
Expand All @@ -768,7 +769,7 @@ def up(ctx, instance, disable_provisioning, disable_shared_folders, gui, memsize
LOGGER.debug('cloud_name:%s instance:%s disable_provisioning:%s disable_shared_folders:%s '
'gui:%s memsize:%s no_cache:%s no_nat:%s numvcpus:%s remove_vagrant:%s',
cloud_name, instance, disable_provisioning, disable_shared_folders,
gui, memsize, no_cache, no_nat, numvcpus, remove_vagrant)
gui, memsize, no_cache, no_nat, numvcpus, add_custom_interface, remove_vagrant)

if cloud_name:
utils.cloud_run(cloud_name, ['up', 'start'])
Expand Down Expand Up @@ -808,6 +809,7 @@ def up(ctx, instance, disable_provisioning, disable_shared_folders, gui, memsize
instance_path=inst.path,
save=not no_cache,
numvcpus=numvcpus,
add_custom_interface = add_custom_interface,
memsize=memsize,
no_nat=no_nat,
windows=inst.windows,
Expand Down
16 changes: 13 additions & 3 deletions mech/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,7 @@ def parse_vmx(path):
return vmx


def update_vmx(path, numvcpus=None, memsize=None, no_nat=False):
def update_vmx(path, numvcpus=None, memsize=None, no_nat=False, add_custom_interface=None):
"""Update the virtual machine configuration (.vmx)
file with desired settings.
"""
Expand Down Expand Up @@ -278,6 +278,16 @@ def update_vmx(path, numvcpus=None, memsize=None, no_nat=False):
vmx["memsize"] = '"{}"'.format(memsize)
updated = True

if add_custom_interface is not None:
vmx["ethernet1.connectionType"] = "custom"
vmx["ethernet1.addressType"] = "generated"
vmx["ethernet1.vnet"] = "VMnet" + '{}'.format(add_custom_interface)
vmx["ethernet1.displayname"] = "VMnet" + '{}'.format(add_custom_interface)
vmx["ethernet1.virtualDev"] = "e1000e"
vmx["ethernet1.present"] = "TRUE"
click.secho("Added custom network interface to vmx file", fg="yellow")
updated = True

if updated:
with open(path, 'w') as new_vmx:
for key in vmx:
Expand Down Expand Up @@ -442,7 +452,7 @@ def tar_cmd(*args, **kwargs):


def init_box(name, box=None, box_version=None, location=None, force=False, save=True,
instance_path=None, numvcpus=None, memsize=None, no_nat=False, provider=None,
instance_path=None, numvcpus=None, add_custom_interface=None, memsize=None, no_nat=False, provider=None,
windows=None):
"""Initialize the box. This includes uncompressing the files
from the box file and updating the vmx file with
Expand Down Expand Up @@ -515,7 +525,7 @@ def init_box(name, box=None, box_version=None, location=None, force=False, save=
vmx_path = locate(instance_path, '*.vmx')
if not vmx_path:
sys.exit(click.style("Cannot locate a VMX file", fg="red"))
update_vmx(vmx_path, numvcpus=numvcpus, memsize=memsize, no_nat=no_nat)
update_vmx(vmx_path, numvcpus=numvcpus, memsize=memsize, no_nat=no_nat, add_custom_interface=add_custom_interface)
return vmx_path
else:
ovf_path = locate(instance_path, '*.ovf')
Expand Down