Skip to content

Commit

Permalink
initial commit of the system_settings_general module (#21)
Browse files Browse the repository at this point in the history
  • Loading branch information
rekup authored Dec 29, 2023
1 parent b66fabb commit 167aef7
Show file tree
Hide file tree
Showing 10 changed files with 395 additions and 31 deletions.
1 change: 1 addition & 0 deletions .pylintrc
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ disable=
import-error,
no-name-in-module,
wrong-import-position,
too-many-branches,

[BASIC]

Expand Down
41 changes: 41 additions & 0 deletions molecule/system_settings_general/converge.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
---
- name: converge
hosts: all
become: true
tasks:
- name: Converge - Set hostname
puzzle.opnsense.system_settings_general:
hostname: opnsense01

- name: Converge - Set domain
puzzle.opnsense.system_settings_general:
domain: example.org

- name: Converge - Set timezone
puzzle.opnsense.system_settings_general:
timezone: Europe/Zurich

- name: Get hostname
ansible.builtin.command: hostname
register: hostname_value
changed_when: false

- name: Get current tz
ansible.builtin.stat:
path: /etc/localtime
register: current_tz_stat
changed_when: false

- name: Get desired tz
ansible.builtin.stat:
path: /usr/share/zoneinfo/Europe/Zurich
register: desired_tz_stat
changed_when: false

- name: Compare hostname
ansible.builtin.assert:
that: hostname_value.stdout == "opnsense01.example.org"

- name: Compare tz
ansible.builtin.assert:
that: desired_tz_stat.stat.checksum == current_tz_stat.stat.checksum
57 changes: 57 additions & 0 deletions molecule/system_settings_general/molecule.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
---
scenario:
name: system_settings_general
test_sequence:
# - dependency not relevant uless we have requirements
- destroy
- syntax
- create
- converge
- idempotence
- verify
- destroy

driver:
name: vagrant
parallel: true

platforms:
- name: "22.7"
hostname: false
box: puzzle/opnsense
box_version: "22.7"
memory: 1024
cpus: 2
instance_raw_config_args:
- 'vm.guest = :freebsd'
- 'ssh.sudo_command = "%c"'
- 'ssh.shell = "/bin/sh"'
- name: "23.1"
box: puzzle/opnsense
hostname: false
box_version: "23.1"
memory: 1024
cpus: 2
instance_raw_config_args:
- 'vm.guest = :freebsd'
- 'ssh.sudo_command = "%c"'
- 'ssh.shell = "/bin/sh"'
- name: "23.7"
box: puzzle/opnsense
hostname: false
box_version: "23.7"
memory: 1024
cpus: 2
instance_raw_config_args:
- 'vm.guest = :freebsd'
- 'ssh.sudo_command = "%c"'
- 'ssh.shell = "/bin/sh"'

provisioner:
name: ansible
env:
ANSIBLE_VERBOSITY: 3
verifier:
name: ansible
options:
become: true
6 changes: 6 additions & 0 deletions molecule/system_settings_general/verify.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
- name: Verify connectivity to server
hosts: all
tasks:
- name: Ping the server
ansible.builtin.ping:
23 changes: 10 additions & 13 deletions plugins/module_utils/config_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,7 @@ def save(self) -> bool:
Returns:
- bool: True if changes were saved, False if no changes were detected.
"""

if self.changed:
tree: ElementTree.ElementTree = ElementTree.ElementTree(
self._config_xml_tree
Expand Down Expand Up @@ -367,36 +368,32 @@ def set(self, value: str, setting: str) -> None:
_setting.text = value

@property
def diff(self) -> Optional[Dict[str, str]]:
def diff(self) -> [Dict[dict, dict]]:
"""
Compares the in-memory configuration with the configuration on the file path
and returns a dictionary of differences.
Returns:
- Optional[Dict[str, str]]: A dictionary containing the differences between
the in-memory configuration and the file-based configuration.
- Dict[dict, dict]: A dictionary containing the before and
after values (the in-memory configuration and the file-based configuration).
Example:
- diff might return {'setting1': 'new_value', 'setting2': 'changed_value'}.
- diff might return {'before': {"foo": "bar"}, 'after': {"foo": "baz"}}.
"""
file_config_tree = ElementTree.parse(self._config_path)
file_config = file_config_tree.getroot()

# Create a dictionary to store the differences
config_diff = {}
config_diff_before = {}
config_diff_after = {}

for setting_name, xpath in self._config_map.items():
if setting_name in ["php_requirements", "configure_functions"]:
continue

# Find the setting in the file-based configuration
file_setting = file_config.find(xpath)

config_diff_before.update({xpath: file_config.find(xpath).text})
# Find the setting in the in-memory configuration
in_memory_setting = self._config_xml_tree.find(xpath)

# Compare the values
if in_memory_setting.text != file_setting.text:
config_diff[setting_name] = in_memory_setting.text
config_diff_after.update({xpath: self._config_xml_tree.find(xpath).text})

return config_diff
return {"before": config_diff_before, "after": config_diff_after}
49 changes: 49 additions & 0 deletions plugins/module_utils/module_index.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,4 +127,53 @@
},
},
},
"OPNsense 23.7": {
"system_settings_general": {
"hostname": "system/hostname",
"domain": "system/domain",
"timezone": "system/timezone",
# Add other mappings here
"php_requirements": [
"/usr/local/etc/inc/config.inc",
"/usr/local/etc/inc/util.inc",
"/usr/local/etc/inc/filter.inc",
"/usr/local/etc/inc/system.inc",
"/usr/local/etc/inc/interfaces.inc",
],
"configure_functions": {
"system_timezone_configure": {
"name": "system_timezone_configure",
"configure_params": ["true"],
},
"system_trust_configure": {
"name": "system_trust_configure",
"configure_params": ["true"],
},
"system_hostname_configure": {
"name": "system_hostname_configure",
"configure_params": ["true"],
},
"system_hosts_generate": {
"name": "system_hosts_generate",
"configure_params": ["true"],
},
"system_resolvconf_generate": {
"name": "system_resolvconf_generate",
"configure_params": ["true"],
},
"plugins_configure_dns": {
"name": "plugins_configure",
"configure_params": ["'dns'", "true"],
},
"plugins_configure_dhcp": {
"name": "plugins_configure",
"configure_params": ["'dhcp'", "true"],
},
"filter_configure": {
"name": "filter_configure",
"configure_params": ["true"],
},
},
},
},
}
27 changes: 12 additions & 15 deletions plugins/module_utils/opnsense_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,22 +36,19 @@ def run_function(

# assemble php command
php_cmd = f"{requirements_string} {configure_function}({params_string});"
cmd_result = {"stdout": "", "stderr": "", "returncode": 2}
# run command
try:
cmd_result = subprocess.run(
[
"php",
"-r",
php_cmd,
],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
check=True, # raise exception if program fails
)
# handle subprocess process error in module using stderr
except subprocess.CalledProcessError:
pass
cmd_result = subprocess.run(
[
"php",
"-r",
php_cmd,
],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
# do not raise exception if program fails
# handle subprocess process error in module using stderr
check=False,
)

return {
"stdout": cmd_result.stdout.decode().strip(),
Expand Down
Loading

0 comments on commit 167aef7

Please sign in to comment.