generated from ansible-collections/collection_template
-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #31 from rekup/feature/opnsense-utils
add opnsense utils (run_function function used to apply settings)
- Loading branch information
Showing
1 changed file
with
46 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |