-
Notifications
You must be signed in to change notification settings - Fork 8
run: add --ip-address option
#261
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
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,128 @@ | ||
| # SPDX-FileCopyrightText: The vmnet-helper authors | ||
| # SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| import ipaddress | ||
|
|
||
|
|
||
| def ip_list(s): | ||
| return [str(ipaddress.IPv4Address(x)) for x in s.split(",")] | ||
|
|
||
|
|
||
| def cpus(s): | ||
| n = int(s) | ||
| if n < 1: | ||
| raise ValueError(f"Invalid number of cpus: '{s}'") | ||
| return n | ||
|
|
||
|
|
||
| def subnet_mask(s): | ||
| """ | ||
| Raises NetmaskValueError if 's' is not a valid netmask. | ||
|
|
||
| Returns the unmodified string expected by ipaddress.IPv4Network. | ||
|
nirs marked this conversation as resolved.
|
||
| """ | ||
| ipaddress.IPv4Network(f"0.0.0.0/{s}") | ||
| return s | ||
|
|
||
|
|
||
| def _one_dhcp_option_set(args): | ||
| """ | ||
| Returns True if one or more DHCP option is set. | ||
| """ | ||
| return args.start_address or args.end_address or args.subnet_mask | ||
|
|
||
|
|
||
| def _all_dhcp_options_set(args): | ||
| """ | ||
| Returns True if all DHCP options are set. | ||
| """ | ||
| return args.start_address and args.end_address and args.subnet_mask | ||
|
|
||
|
|
||
| def _dhcp_options(p, args): | ||
| if _one_dhcp_option_set(args) and not _all_dhcp_options_set(args): | ||
| p.error( | ||
| "--start-address, --end-address, --subnet-mask must all be set or all omitted" | ||
| ) | ||
|
|
||
|
|
||
| def _bridged_mode(p, args): | ||
| if not args.shared_interface: | ||
| p.error("--shared-interface required for --operation-mode=bridged") | ||
| if args.enable_isolation: | ||
| p.error("--enable-isolation not compatible with --operation-mode=bridged") | ||
|
|
||
|
|
||
| def _shared_mode(p, args): | ||
| _dhcp_options(p, args) | ||
|
|
||
|
|
||
| def _host_mode(p, args): | ||
| _dhcp_options(p, args) | ||
|
|
||
|
|
||
| def operation_mode(p, args): | ||
| if args.operation_mode == "shared" or not args.operation_mode: | ||
| _shared_mode(p, args) | ||
| elif args.operation_mode == "host": | ||
| _host_mode(p, args) | ||
| elif args.operation_mode == "bridged": | ||
| _bridged_mode(p, args) | ||
|
|
||
|
|
||
| def network_options(p, args): | ||
| """ | ||
| Validate the network options passed to run. | ||
| """ | ||
| if args.network_name: | ||
| if args.operation_mode: | ||
| p.error("--network cannot be used with --operation-mode") | ||
| if args.start_address: | ||
| p.error("--network cannot be used with --start-address") | ||
| if args.end_address: | ||
| p.error("--network cannot be used with --end-address") | ||
| if args.subnet_mask: | ||
| p.error("--network cannot be used with --subnet-mask") | ||
|
tofugarden marked this conversation as resolved.
|
||
| if args.ip_address: | ||
| p.error("--network cannot be used with --ip-address") | ||
|
|
||
| if args.ip_address and not _all_dhcp_options_set(args): | ||
| p.error("--ip-address requires --start-address, --end-address, --subnet-mask") | ||
|
|
||
| if _all_dhcp_options_set(args): | ||
|
nirs marked this conversation as resolved.
|
||
| # vmnet does not enforce the order of --start-address and --end-address. | ||
| network = ipaddress.IPv4Interface( | ||
| (args.start_address, args.subnet_mask) | ||
| ).network | ||
| if args.end_address not in network: | ||
| p.error("--start-address and --end-address must be in the same subnet") | ||
| # --ip-address inside the DHCP range may cause conflicts, but works. | ||
| if args.ip_address: | ||
| if args.ip_address not in network: | ||
| p.error( | ||
| "--ip-address, --start-address and --end-address " | ||
| "must be in the same subnet", | ||
| ) | ||
| # Only reserve --start-address, since it gets assigned to the host. | ||
| if args.ip_address == args.start_address: | ||
| p.error("--ip-address must be different from --start-address") | ||
|
|
||
|
|
||
| _RFC1918_NETWORKS = [ | ||
| ipaddress.IPv4Network("10.0.0.0/8"), | ||
| ipaddress.IPv4Network("172.16.0.0/12"), | ||
| ipaddress.IPv4Network("192.168.0.0/16"), | ||
| ] | ||
|
|
||
|
tofugarden marked this conversation as resolved.
|
||
|
|
||
| def private_ipv4_address(ip): | ||
| """ | ||
| Validates that "ip" is in the RFC 1918 private range. | ||
|
|
||
| Returns an ipaddress.IPv4Address object, or raises ValueError if validation fails. | ||
| """ | ||
| address = ipaddress.IPv4Address(ip) | ||
| for network in _RFC1918_NETWORKS: | ||
| if address in network: | ||
| return address | ||
| raise ValueError(f"{ip} is not a valid RFC 1918 IP address") | ||
This file contains hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.