Page Menu
Home
DevCentral
Search
Configure Global Search
Log In
Files
F58935805
No One
Temporary
Actions
View File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Flag For Later
Award Token
Size
7 KB
Referenced Files
None
Subscribers
None
View Options
diff --git a/.arclint b/.arclint
index 7c20df9..f18a3be 100644
--- a/.arclint
+++ b/.arclint
@@ -1,34 +1,35 @@
{
"linters": {
"chmod": {
"type": "chmod"
},
"shell": {
"type": "shellcheck",
"include": [
"(\\.sh$)"
]
},
"filename": {
"type": "filename"
},
"json": {
"type": "json",
"include": [
"(^\\.arcconfig$)",
"(^\\.arclint$)",
"(\\.json$)"
]
},
"python": {
"type": "flake8",
"severity": {
"E203": "disabled",
+ "E731": "disabled",
"F821": "advice"
},
"include": [
"(\\.py$)"
]
}
}
}
diff --git a/.flake8 b/.flake8
index 8dd399a..e5d3fe7 100644
--- 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
index 0000000..25160f6
--- /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
index 0000000..88786d1
--- /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
index 30bb73a..4633f7f 100644
--- a/requirements.txt
+++ b/requirements.txt
@@ -1,12 +1,13 @@
# Linters
flake8>=3.9.2,<4.0
autopep8>=1.5.7,<2.0
pycodestyle>=2.7.0,<3.0
# Tests and utilities
PyYAML>=5.4.1,<6.0
# Tests
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
index 2a56387..307b020 100644
--- a/roles/core/network/files/RedHat/ifcfg-private
+++ b/roles/core/network/files/RedHat/ifcfg-private
@@ -1,32 +1,32 @@
# -------------------------------------------------------------
# Network configuration
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# Project: Nasqueron
# License: Trivial work, not eligible to copyright
# Source file: roles/core/network/files/RedHat/ifcfg-private
# -------------------------------------------------------------
#
# <auto-generated>
# This file is managed by our rOPS SaltStack repository.
#
# Changes to this file may cause incorrect behavior
# and will be lost if the state is redeployed.
# </auto-generated>
TYPE=Ethernet
PROXY_METHOD=none
BROWSER_ONLY=no
BOOTPROTO=none
DEFROUTE=yes
IPV4_FAILURE_FATAL=no
IPV6INIT=yes
IPV6_AUTOCONF=yes
IPV6_DEFROUTE=yes
IPV6_FAILURE_FATAL=no
IPV6_ADDR_GEN_MODE=stable-privacy
NAME={{ interface.device }}
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
index 6e48336..4c075a8 100644
--- a/roles/core/network/private.sls
+++ b/roles/core/network/private.sls
@@ -1,40 +1,41 @@
# -------------------------------------------------------------
# Salt — Network
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# Project: Nasqueron
# Created: 2020-09-24
# License: Trivial work, not eligible to copyright
# -------------------------------------------------------------
{% set network = salt['node.get']('network') %}
# -------------------------------------------------------------
# Interface
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
{% if salt['node.has']('network:private_interface') %}
{% set interface = network['private_interface'] %}
{% if grains['os'] == 'FreeBSD' %}
/etc/rc.conf.d/netif/ipv4_{{ network['private_interface'] }}:
file.managed:
- source: salt://roles/core/network/files/FreeBSD/netif_ipv4.rc
- makedirs: True
- template: jinja
- context:
interface: {{ interface['device'] }}
ipv4_address: {{ interface['address'] }}
ipv4_netmask: {{ interface['netmask'] | default('255.255.255.0') }}
ipv4_aliases: {{ salt['node.get_list']('network:private_interface:aliases') }}
{% endif %}
{% if grains['os_family'] == 'RedHat' %}
/etc/sysconfig/network-scripts/ifcfg-{{ interface['device'] }}:
file.managed:
- source: salt://roles/core/network/files/RedHat/ifcfg-private
- template: jinja
- context:
interface: {{ interface }}
+ prefix: {{ salt['network_utils.netmask_to_cidr_prefix'](interface['netmask']) }}
{% endif %}
{% endif %}
File Metadata
Details
Attached
Mime Type
text/x-diff
Expires
Sun, Dec 6, 06:29 (1 d, 17 h)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
4299029
Default Alt Text
(7 KB)
Attached To
Mode
rOPS Nasqueron Operations
Attached
Detach File
Event Timeline
Log In to Comment