Skip to content

Commit

Permalink
Merge pull request #31 from rekup/feature/opnsense-utils
Browse files Browse the repository at this point in the history
add opnsense utils (run_function function used to apply settings)
  • Loading branch information
rekup authored Nov 9, 2023
2 parents ea1b8dd + a6ddfca commit 51b8ef4
Showing 1 changed file with 46 additions and 0 deletions.
46 changes: 46 additions & 0 deletions plugins/module_utils/opnsense_utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
# 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 config changes"""

from __future__ import (absolute_import, division, print_function)

__metaclass__ = type

from typing import List
import subprocess


def run_function(php_requirements: List[str], configure_function: str, configure_params: List = None) -> str:
"""
Execute a php function optional with parameters
:param php_requirements: A list os strings containing the location of php files which must be included to execute the function.
:param configure_function: The php function to call.
:param configure_params: An optional list of parameters to pass to the function.
:return: Returns the stdout generated by the command
"""
if configure_params is None:
configure_params = []

# assemble the php require statements
requirements_string = " ".join(
["require '" + req + "';" for req in php_requirements]
)
params_string = ",".join(configure_params)

# assemble php command
php_cmd = f"{requirements_string} {configure_function}({params_string});"

# run command
cmd_result = subprocess.run(
[
"php",
"-r",
php_cmd,
],
stdout=subprocess.PIPE,
check=True, # raise exception if program fails
)
return cmd_result.stdout

0 comments on commit 51b8ef4

Please sign in to comment.