From 40166c111736e7c79b34b9a276c235ebd2f9cc58 Mon Sep 17 00:00:00 2001 From: gingrm02 <89487245+gingrm02@users.noreply.github.com> Date: Fri, 27 Oct 2023 11:29:22 -0400 Subject: [PATCH] Setup Commands (#14) * added setup_commands function to base Module * added setup commands to netplan and fixed linting issues * changed | to union for backward compatibility * fixed list -> List for typing compatibility * forgot the imports * removed walrus and added python 3.7 to test matrix * typoed it * loosened python version constraint * nevermind, typing-extensions and pylint both require >=3.8 * Added netfilter commands * Added run commands to nat module * Updated correct command to load the commands from the system file to the running configuration * Moved sysctl -p to kernelparameter.py, fixed nat.py commands to use -f instead of the redirect operator --------- Co-authored-by: unknown Co-authored-by: Bobzemob --- genisys/__init__.py | 0 genisys/modules/base.py | 15 +++++++++++++-- genisys/modules/kernelparameter.py | 8 ++++---- genisys/modules/nat.py | 8 +++++++- genisys/modules/netplan.py | 9 +++++++-- 5 files changed, 31 insertions(+), 9 deletions(-) create mode 100644 genisys/__init__.py diff --git a/genisys/__init__.py b/genisys/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/genisys/modules/base.py b/genisys/modules/base.py index 9bbf791c..df822abd 100644 --- a/genisys/modules/base.py +++ b/genisys/modules/base.py @@ -1,14 +1,17 @@ from pathlib import Path -from typing_extensions import Self +from abc import ABCMeta, abstractmethod +from typing_extensions import Self, Union, List -class Module: +class Module(metaclass=ABCMeta): """Base class all module should inherit from""" + @abstractmethod def generate(self: Self) -> str: """Generates the content of the configuration file.""" raise NotImplementedError #end generate + @abstractmethod def install_location(self: Self) -> Path: """Returns the location that the config file should be installed to. This path should always be absolute. Relative paths will be assumed @@ -38,4 +41,12 @@ def validate(self: Self) -> bool: except: return False #end validate + + def setup_commands(self: Self) -> Union[List[str], List[List[str]]]: + """Returns commands which are should be ran before the module's configuration output is + completed. Should return a List such that each item can be passed to the subprocess.run() + function. + """ + return [] + # end setup #end class Module diff --git a/genisys/modules/kernelparameter.py b/genisys/modules/kernelparameter.py index 1a3257de..72bf6d4a 100644 --- a/genisys/modules/kernelparameter.py +++ b/genisys/modules/kernelparameter.py @@ -1,9 +1,6 @@ -from typing_extensions import Self +from typing_extensions import Self, Union, List from pathlib import Path - from genisys.modules import base -from typing import Self -from pathlib import Path class KernelParameter(base.module): ''' 99 prefix guarantees that this rule will overwrite sysctl.conf parameter assignment, this file will need to be created beforehand ''' @@ -22,6 +19,9 @@ def install_location(self: Self): def generate(self: Self) -> str: return "net.ipv4.ip_forward=1" + + def setup_commands(self: Self) -> Union[List[str], List[List[str]]]: + return ["sysctl -p"] # end generate diff --git a/genisys/modules/nat.py b/genisys/modules/nat.py index 925c0f93..599255e8 100644 --- a/genisys/modules/nat.py +++ b/genisys/modules/nat.py @@ -1,4 +1,4 @@ -from typing_extensions import Self +from typing_extensions import Self, Union, List from pathlib import Path from genisys.modules import base from jinja2 import Template @@ -67,4 +67,10 @@ def install_location(self: Self) -> Path: # end install_location + def setup_commands(self: Self) -> Union[List[str], List[List[str]]]: + + return ["iptables-restore -f " + self.IPV4_DIR, "netfilter-persistent reload", "systemctl enable iptables", "systemctl start iptables"] + + # end setup_commands + # end nat class \ No newline at end of file diff --git a/genisys/modules/netplan.py b/genisys/modules/netplan.py index 3832c83a..d9adc031 100644 --- a/genisys/modules/netplan.py +++ b/genisys/modules/netplan.py @@ -2,7 +2,7 @@ from pathlib import Path import yaml -from typing_extensions import Self +from typing_extensions import Self, Union, List from genisys.modules.base import Module NETPLAN_DIR = '/etc/netplan' @@ -33,7 +33,8 @@ def generate(self: Self) -> str: prefix_len = None # parse the subnet option if it uses CIDR notation - if (cidr_start := self.config['subnet'].find('/')) != -1: + cidr_start = self.config['subnet'].find('/') + if cidr_start != -1: cidr_pfx_len = int(self.config['subnet'][cidr_start+1:]) if prefix_len is not None and prefix_len != cidr_pfx_len: raise ValueError("Subnet mask does not match CIDR prefix length") @@ -64,4 +65,8 @@ def generate(self: Self) -> str: # return the yaml return yaml.dump(netplan) # end generate + + def setup_commands(self: Self) -> Union[List[str], List[List[str]]]: + return [ "netplan apply" ] + # end setup_commands # end class Interface