Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Setup Commands #14

Merged
14 commits merged into from
Oct 27, 2023
Merged
Show file tree
Hide file tree
Changes from 13 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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: 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 < " + self.IPV4_DIR, "netfilter-persistent reload", "systemctl enable iptables", "systemctl start iptables", "sysctl -p"]
HenrithicusGreenson marked this conversation as resolved.
Show resolved Hide resolved
HenrithicusGreenson marked this conversation as resolved.
Show resolved Hide resolved

# 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