Skip to content

Commit

Permalink
Setup Commands (#14)
Browse files Browse the repository at this point in the history
* 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 <[email protected]>
Co-authored-by: Bobzemob <[email protected]>
  • Loading branch information
3 people authored Oct 27, 2023
1 parent ef755d5 commit 40166c1
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 9 deletions.
Empty file added genisys/__init__.py
Empty file.
15 changes: 13 additions & 2 deletions genisys/modules/base.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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
8 changes: 4 additions & 4 deletions genisys/modules/kernelparameter.py
Original file line number Diff line number Diff line change
@@ -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 '''
Expand All @@ -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


8 changes: 7 additions & 1 deletion genisys/modules/nat.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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
9 changes: 7 additions & 2 deletions genisys/modules/netplan.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -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")
Expand Down Expand Up @@ -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

0 comments on commit 40166c1

Please sign in to comment.