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

add opnsense utils #31

Merged
merged 2 commits into from
Nov 9, 2023
Merged
Changes from all 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
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