Skip to content

Commit

Permalink
split config utils
Browse files Browse the repository at this point in the history
  • Loading branch information
rekup committed Sep 19, 2023
1 parent 25a85cd commit e8b300a
Show file tree
Hide file tree
Showing 3 changed files with 103 additions and 90 deletions.
89 changes: 1 addition & 88 deletions plugins/module_utils/opnsense_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
from typing import List
import subprocess

def _run_function(php_requirements: List[str], configure_function: str, configure_params: List = []) -> str:
def run_function(php_requirements: List[str], configure_function: str, configure_params: List = []) -> str:
"""
Execute a php function optional with parameters
Expand Down Expand Up @@ -42,90 +42,3 @@ def _run_function(php_requirements: List[str], configure_function: str, configur
)
return cmd_result.stdout


def system_settings_general() -> List[str]:
"""
Execute the required php function to apply settings in the System -> Settings -> General (system_general.php) view.
https://github.com/opnsense/core/blob/cbaf7cee1f0a6fabd1ec4c752a5d169c402976dc/src/www/system_general.php#L227
:return: Returns a list os strings containing the stdout of all the commands executed
"""

# requirements to execute the various functions can be found in the respective php file
# https://github.com/opnsense/core/blob/cbaf7cee1f0a6fabd1ec4c752a5d169c402976dc/src/www/system_general.php#L30
php_requirements = [
"/usr/local/etc/inc/config.inc",
"/usr/local/etc/inc/util.inc",
"/usr/local/etc/inc/system.inc",
"/usr/local/etc/inc/interfaces.lib.inc",
"/usr/local/etc/inc/interfaces.inc",
"/usr/local/etc/inc/filter.inc",
]

cmd_output = []
# the order of commands executed is relevant
# https://github.com/opnsense/core/blob/cbaf7cee1f0a6fabd1ec4c752a5d169c402976dc/src/www/system_general.php#L227

# https://github.com/opnsense/core/blob/cbaf7cee1f0a6fabd1ec4c752a5d169c402976dc/src/etc/inc/system.inc#L935
cmd_output.append(
_run_function(
php_requirements=php_requirements,
configure_function="system_timezone_configure",
configure_params=["true"], # first param: verbose
)
)

# https://github.com/opnsense/core/blob/cbaf7cee1f0a6fabd1ec4c752a5d169c402976dc/src/etc/inc/system.inc#L864
cmd_output.append(
_run_function(
php_requirements=php_requirements,
configure_function="system_trust_configure",
configure_params=["true"], # first param: verbose
)
)

# https://github.com/opnsense/core/blob/cbaf7cee1f0a6fabd1ec4c752a5d169c402976dc/src/etc/inc/system.inc#L864
cmd_output.append(
_run_function(
php_requirements=php_requirements,
configure_function="system_hostname_configure",
configure_params=["true"], # first param: verbose
)
)

# https://github.com/opnsense/core/blob/cbaf7cee1f0a6fabd1ec4c752a5d169c402976dc/src/etc/inc/system.inc#L506
cmd_output.append(
_run_function(
php_requirements=php_requirements,
configure_function="system_resolver_configure",
configure_params=["true"], # first param: verbose
)
)

# https://github.com/opnsense/core/blob/cbaf7cee1f0a6fabd1ec4c752a5d169c402976dc/src/etc/inc/plugins.inc#L251
cmd_output.append(
_run_function(
php_requirements=php_requirements,
configure_function="plugins_configure",
configure_params=["'dns'", "true"], # first param: hook, second param: verbose
)
)

# https://github.com/opnsense/core/blob/cbaf7cee1f0a6fabd1ec4c752a5d169c402976dc/src/etc/inc/plugins.inc#L251
cmd_output.append(
_run_function(
php_requirements=php_requirements,
configure_function="plugins_configure",
configure_params=["'dhcp'", "true"], # first param: hook, second param: verbose
)
)

# https://github.com/opnsense/core/blob/cbaf7cee1f0a6fabd1ec4c752a5d169c402976dc/src/etc/inc/filter.inc#L125
cmd_output.append(
_run_function(
php_requirements=php_requirements,
configure_function="filter_configure",
configure_params=["true"], # first param: verbose
)
)
return cmd_output
100 changes: 100 additions & 0 deletions plugins/module_utils/system_settings_general_utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@

# Copyright: (c) 2023, Reto Kupferschmid <[email protected]>, Puzzle ITC
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)

"""Utilities used to apply OPNsense System -> Settings -> General config changes"""

from __future__ import (absolute_import, division, print_function)

__metaclass__ = type

from typing import List

from ansible_collections.puzzle.opnsense.plugins.module_utils import opnsense_utils

def apply() -> List[str]:
"""
Execute the required php function to apply settings in the System -> Settings -> General (system_general.php) view.
https://github.com/opnsense/core/blob/cbaf7cee1f0a6fabd1ec4c752a5d169c402976dc/src/www/system_general.php#L227
:return: Returns a list os strings containing the stdout of all the commands executed
"""

# requirements to execute the various functions can be found in the respective php file
# https://github.com/opnsense/core/blob/cbaf7cee1f0a6fabd1ec4c752a5d169c402976dc/src/www/system_general.php#L30
php_requirements = [
"/usr/local/etc/inc/config.inc",
"/usr/local/etc/inc/util.inc",
"/usr/local/etc/inc/system.inc",
"/usr/local/etc/inc/interfaces.lib.inc",
"/usr/local/etc/inc/interfaces.inc",
"/usr/local/etc/inc/filter.inc",
]

cmd_output = []
# the order of commands executed is relevant
# https://github.com/opnsense/core/blob/cbaf7cee1f0a6fabd1ec4c752a5d169c402976dc/src/www/system_general.php#L227

# https://github.com/opnsense/core/blob/cbaf7cee1f0a6fabd1ec4c752a5d169c402976dc/src/etc/inc/system.inc#L935
cmd_output.append(
opnsense_utils.run_function(
php_requirements=php_requirements,
configure_function="system_timezone_configure",
configure_params=["true"], # first param: verbose
)
)

# https://github.com/opnsense/core/blob/cbaf7cee1f0a6fabd1ec4c752a5d169c402976dc/src/etc/inc/system.inc#L864
cmd_output.append(
opnsense_utils.run_function(
php_requirements=php_requirements,
configure_function="system_trust_configure",
configure_params=["true"], # first param: verbose
)
)

# https://github.com/opnsense/core/blob/cbaf7cee1f0a6fabd1ec4c752a5d169c402976dc/src/etc/inc/system.inc#L864
cmd_output.append(
opnsense_utils.run_function(
php_requirements=php_requirements,
configure_function="system_hostname_configure",
configure_params=["true"], # first param: verbose
)
)

# https://github.com/opnsense/core/blob/cbaf7cee1f0a6fabd1ec4c752a5d169c402976dc/src/etc/inc/system.inc#L506
cmd_output.append(
opnsense_utils.run_function(
php_requirements=php_requirements,
configure_function="system_resolver_configure",
configure_params=["true"], # first param: verbose
)
)

# https://github.com/opnsense/core/blob/cbaf7cee1f0a6fabd1ec4c752a5d169c402976dc/src/etc/inc/plugins.inc#L251
cmd_output.append(
opnsense_utils.run_function(
php_requirements=php_requirements,
configure_function="plugins_configure",
configure_params=["'dns'", "true"], # first param: hook, second param: verbose
)
)

# https://github.com/opnsense/core/blob/cbaf7cee1f0a6fabd1ec4c752a5d169c402976dc/src/etc/inc/plugins.inc#L251
cmd_output.append(
opnsense_utils.run_function(
php_requirements=php_requirements,
configure_function="plugins_configure",
configure_params=["'dhcp'", "true"], # first param: hook, second param: verbose
)
)

# https://github.com/opnsense/core/blob/cbaf7cee1f0a6fabd1ec4c752a5d169c402976dc/src/etc/inc/filter.inc#L125
cmd_output.append(
opnsense_utils.run_function(
php_requirements=php_requirements,
configure_function="filter_configure",
configure_params=["true"], # first param: verbose
)
)
return cmd_output
4 changes: 2 additions & 2 deletions plugins/modules/system_settings_general.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@
from ansible.module_utils.basic import AnsibleModule
from ansible_collections.puzzle.opnsense.plugins.module_utils import (
config_utils,
opnsense_utils,
system_settings_general_utils,
)

HOSTNAME_INDEX = 1
Expand Down Expand Up @@ -145,7 +145,7 @@ def main():
config_mgr.save()
result[
"opnsense_configure_output"
] = opnsense_utils.system_settings_general()
] = system_settings_general_utils.apply()

# Return results
module.exit_json(**result)
Expand Down

0 comments on commit e8b300a

Please sign in to comment.