Summary
Refactor the handling of CHALLENGES_ROOT_SUBNET and related variables in the codebase to use the Python ipaddress module, improving clarity and correctness.
Details
Replace this code in launch_challenge.py, stop_challenge.py, and teardown_challenge.py:
CHALLENGES_ROOT_SUBNET = os.getenv("CHALLENGES_ROOT_SUBNET", "10.128.0.0")
CHALLENGES_ROOT_SUBNET_MASK = os.getenv("CHALLENGES_ROOT_SUBNET_MASK", "255.128.0.0")
CHALLENGES_ROOT_SUBNET_MASK_INT = sum(bin(int(x)).count('1') for x in CHALLENGES_ROOT_SUBNET_MASK.split('.'))
CHALLENGES_ROOT_SUBNET_CIDR = f"{CHALLENGES_ROOT_SUBNET}/{CHALLENGES_ROOT_SUBNET_MASK_INT}"
with an approach, leveraging the Python ipaddress module.
Also, update subnet_calculations.py to take advantage of ipaddress for subnet and IP calculations, if possible, to simplify and improve reliability.
Rationale
- More robust and readable handling of subnets using a well-maintained standard library.
- Simplifies conversion between mask formats and avoids manual calculations.
Additional Context
- Ensure import of
ipaddress is added where needed.
- Refactored code should maintain backward compatibility with current environment variables in
.env.
Summary
Refactor the handling of
CHALLENGES_ROOT_SUBNETand related variables in the codebase to use the Pythonipaddressmodule, improving clarity and correctness.Details
Replace this code in
launch_challenge.py,stop_challenge.py, andteardown_challenge.py:with an approach, leveraging the Python ipaddress module.
Also, update
subnet_calculations.pyto take advantage ofipaddressfor subnet and IP calculations, if possible, to simplify and improve reliability.Rationale
Additional Context
ipaddressis added where needed..env.