Page Menu
Home
DevCentral
Search
Configure Global Search
Log In
Files
F4217393
D2568.id6501.diff
No One
Temporary
Actions
View File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Award Token
Flag For Later
Size
4 KB
Referenced Files
None
Subscribers
None
D2568.id6501.diff
View Options
diff --git a/.arclint b/.arclint
--- a/.arclint
+++ b/.arclint
@@ -24,6 +24,7 @@
"type": "flake8",
"severity": {
"E203": "disabled",
+ "E731": "disabled",
"F821": "advice"
},
"include": [
diff --git a/.flake8 b/.flake8
--- a/.flake8
+++ b/.flake8
@@ -1,3 +1,5 @@
[flake8]
max-line-length = 88
-extend-ignore = E203
+extend-ignore =
+ E203,
+ E731,
diff --git a/_modules/network_utils.py b/_modules/network_utils.py
new file mode 100644
--- /dev/null
+++ b/_modules/network_utils.py
@@ -0,0 +1,46 @@
+# -*- coding: utf-8 -*-
+
+# -------------------------------------------------------------
+# Salt — Network execution module
+# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
+# Project: Nasqueron
+# License: BSD-2-Clause
+# -------------------------------------------------------------
+
+
+import re
+
+
+# -------------------------------------------------------------
+# CIDR netmask and prefixes
+# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
+
+
+def is_valid_netmask(netmask):
+ # "255.255.255.240" → "11111111111111111111111111110000"
+ bits = ''.join([format(int(octet), 'b') for octet in netmask.split(".")])
+
+ # A netmask is valid if the suite of bits:
+ # - starts by contiguous 1, e.g. here 1111111111111111111111111111
+ # - ends by contiguous 0, e.g. here 0000
+ #
+ # Also, as 0.0.0.0 is invalid, netmask must starts by 1.
+ return re.compile("^1+0*$").match(bits) is not None
+
+
+def netmask_to_cidr_prefix(netmask):
+ """
+ Convert a netmask like 255.255.255.240 into a CIDR prefix like 24.
+
+ This can be useful for RHEL network scripts requiring PREFIX information.
+ """
+
+ if not is_valid_netmask(netmask):
+ raise ValueError("Netmask is invalid.")
+
+ # The CIDR prefix is the count of 1 bits in each octect.
+ # e.g. 255.255.255.240 can be split in octets [255, 255, 255, 240],
+ # then becomes ['0b11111111', '0b11111111', '0b11111111', '0b11110000'].
+ #
+ # There is 24 "1" in this expression, that's 24 is our CIDR prefix.
+ return sum([bin(int(octet)).count("1") for octet in netmask.split(".")])
diff --git a/_tests/modules/test_network.py b/_tests/modules/test_network.py
new file mode 100644
--- /dev/null
+++ b/_tests/modules/test_network.py
@@ -0,0 +1,52 @@
+from importlib.machinery import SourceFileLoader
+from unittest_data_provider import data_provider
+import unittest
+
+
+salt_test_case = SourceFileLoader("salt_test_case", "salt_test_case.py").load_module()
+network = SourceFileLoader("network", "../_modules/network_utils.py").load_module()
+
+
+class Testinstance(unittest.TestCase, salt_test_case.SaltTestCase):
+ cidr_prefixes = lambda: (
+ ("255.255.255.255", 32),
+ ("255.255.255.254", 31),
+ ("255.255.255.252", 30),
+ ("255.255.255.240", 28),
+ ("255.255.255.224", 27),
+ ("255.255.255.0", 24),
+ ("255.252.0.0", 14),
+ )
+
+ valid_netmasks = lambda: (
+ ("255.255.255.255",),
+ ("255.255.255.254",),
+ ("255.255.255.252",),
+ ("255.255.255.240",),
+ )
+
+ invalid_netmasks = lambda: (
+ # In binary, it's not a suite of 1 then a suite of 0
+ ("255.255.255.209",),
+
+ # By definition, netmask MUST be strictly greater than 0
+ ("0.0.0.0",),
+ )
+
+ @data_provider(cidr_prefixes)
+ def test_netmask_to_cidr_prefix(self, netmask, expected_prefix):
+ actual_prefix = network.netmask_to_cidr_prefix(netmask)
+
+ self.assertTrue(actual_prefix == expected_prefix)
+
+ @data_provider(valid_netmasks)
+ def test_is_valid_netmask(self, netmask):
+ self.assertTrue(network.is_valid_netmask(netmask))
+
+ @data_provider(invalid_netmasks)
+ def test_is_valid_netmask_when_it_is_not(self, netmask):
+ self.assertFalse(network.is_valid_netmask(netmask))
+
+
+if __name__ == "__main__":
+ unittest.main()
diff --git a/requirements.txt b/requirements.txt
--- a/requirements.txt
+++ b/requirements.txt
@@ -10,3 +10,4 @@
mock>=4.0.3,<5.0
json==3003
jsondiff==1.3.0
+unittest-data-provider>=1.0.1,<2.0
diff --git a/roles/core/network/files/RedHat/ifcfg-private b/roles/core/network/files/RedHat/ifcfg-private
--- a/roles/core/network/files/RedHat/ifcfg-private
+++ b/roles/core/network/files/RedHat/ifcfg-private
@@ -28,5 +28,5 @@
UUID={{ interface.uuid }}
DEVICE={{ interface.device }}
ONBOOT=yes
-PREFIX=24
+PREFIX={{ prefix }}
IPADDR={{ interface.address }}
diff --git a/roles/core/network/private.sls b/roles/core/network/private.sls
--- a/roles/core/network/private.sls
+++ b/roles/core/network/private.sls
@@ -36,6 +36,7 @@
- template: jinja
- context:
interface: {{ interface }}
+ prefix: {{ salt['network.netmask_to_cidr_prefix'](interface['netmask']) }}
{% endif %}
{% endif %}
File Metadata
Details
Attached
Mime Type
text/plain
Expires
Wed, Feb 12, 02:51 (14 h, 9 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
2411387
Default Alt Text
D2568.id6501.diff (4 KB)
Attached To
Mode
D2568: Compute CIDR prefix from netmask for RHEL network scripts
Attached
Detach File
Event Timeline
Log In to Comment