From e8b300a7780df06fd7764d000004a0d487b2ec6d Mon Sep 17 00:00:00 2001 From: Reto Kupferschmid Date: Tue, 19 Sep 2023 18:48:52 +0200 Subject: [PATCH] split config utils --- plugins/module_utils/opnsense_utils.py | 89 +--------------- .../system_settings_general_utils.py | 100 ++++++++++++++++++ plugins/modules/system_settings_general.py | 4 +- 3 files changed, 103 insertions(+), 90 deletions(-) create mode 100644 plugins/module_utils/system_settings_general_utils.py diff --git a/plugins/module_utils/opnsense_utils.py b/plugins/module_utils/opnsense_utils.py index 0b65d1bb..524e9b21 100644 --- a/plugins/module_utils/opnsense_utils.py +++ b/plugins/module_utils/opnsense_utils.py @@ -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 @@ -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 diff --git a/plugins/module_utils/system_settings_general_utils.py b/plugins/module_utils/system_settings_general_utils.py new file mode 100644 index 00000000..15f01961 --- /dev/null +++ b/plugins/module_utils/system_settings_general_utils.py @@ -0,0 +1,100 @@ + +# Copyright: (c) 2023, Reto Kupferschmid , 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 diff --git a/plugins/modules/system_settings_general.py b/plugins/modules/system_settings_general.py index 4e54275e..9b361306 100644 --- a/plugins/modules/system_settings_general.py +++ b/plugins/modules/system_settings_general.py @@ -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 @@ -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)