SUPER! Support for variables, might be a little bit buggy for now!

This commit is contained in:
Daan
2024-11-28 09:41:49 +01:00
parent 7fb2d21acf
commit f05ab15d4b
7 changed files with 71 additions and 38 deletions

18
examples/apt_upgrade.yaml Normal file
View File

@@ -0,0 +1,18 @@
---
name: Refresh the apt cache
company: Temp-Agents
variables:
- name: package_manager
value: "apt"
tasks:
- name: refresh the cache
command: "{{ package_manager }} update"
- name: display available upgrades
command: "{{ package_manager }} list --upgradable"
- name: apply upgrades
command: "{{ package_manager }} upgrade -y"
- name: cleanup remaining packages
command: "{{ package_manager }} autoremove -y"

View File

@@ -0,0 +1,12 @@
---
name: Refresh the apt cache
company: Temp-Agents
variables:
- name: package_manager
value: "apt"
tasks:
- name: refresh the cache
command: "{{ package_manager }} update"
- name: display available upgrades
command: "{{ package_manager }} list --upgradable"

View File

@@ -0,0 +1,18 @@
---
name: Ping Multiple Points
company: Temp-Agents
variables:
- name: host1
value: "1.1.1.1"
- name: host2
value: "ns.systemec.nl"
- name: command1
value: "ping"
- name: cmd_arguments
value: "-c 4"
tasks:
- name: Ping Cloudflare
command: "{{ command1 }} {{ host1 }} {{ cmd_arguments }}"
- name: Ping Google
command: "{{ command1 }} {{ host2 }} {{ cmd_arguments }}"

View File

@@ -1,16 +0,0 @@
---
name: Refresh the apt cache
company: Temp-Agents
#device: MSI
tasks:
- name: refresh the cache
command: "apt update"
- name: display available upgrades
command: "apt list --upgradable"
- name: apply upgrades
command: "apt upgrade -y"
- name: cleanup remaining packages
command: "apt autoremove -y"

View File

@@ -1,9 +0,0 @@
---
name: Ping Multiple Points
company: Temp-Agents
tasks:
- name: Ping Cloudflare
command: "ping 1.1.1.1 -c 4"
- name: Ping Google
command: "ping 8.8.8.8 -c 4"

View File

@@ -1,9 +0,0 @@
---
name: Refresh the apt cache
company: Temp-Agents
tasks:
- name: refresh the cache
command: "apt update"
- name: display available upgrades
command: "apt list --upgradable"

View File

@@ -72,6 +72,20 @@ class MeshbookUtilities:
"""Read a YAML file and return its content as a dictionary.""" """Read a YAML file and return its content as a dictionary."""
with open(file_path, 'r') as file: with open(file_path, 'r') as file:
return yaml.safe_load(file) return yaml.safe_load(file)
@staticmethod
def replace_placeholders(playbook) -> dict:
# Convert 'variables' to a dictionary for quick lookup
variables = {var["name"]: var["value"] for var in playbook.get("variables", [])}
# Traverse 'tasks' to replace placeholders
for task in playbook.get("tasks", []):
command = task.get("command", "")
for var_name, var_value in variables.items():
placeholder = f"{{{{ {var_name} }}}}" # Create the placeholder string like "{{ host1 }}"
command = command.replace(placeholder, var_value) # Update the command string
task["command"] = command # Save the updated command string
return playbook
@staticmethod @staticmethod
def translate_nodeids(batches_dict, global_list) -> dict: def translate_nodeids(batches_dict, global_list) -> dict:
@@ -220,6 +234,7 @@ class MeshbookProcessor:
"""Processes messages received from the WebSocket.""" """Processes messages received from the WebSocket."""
global response_counter global response_counter
temp_responses_list = [] temp_responses_list = []
while True: while True:
message = await python_client.received_response_queue.get() message = await python_client.received_response_queue.get()
action_type = message.get('action') action_type = message.get('action')
@@ -250,13 +265,12 @@ class MeshcallerActions:
"""Processes playbook actions.""" """Processes playbook actions."""
@staticmethod @staticmethod
async def process_arguments(python_client: MeshbookWebsocket, playbook_path: str): async def process_arguments(python_client: MeshbookWebsocket, playbook_yaml: dict):
"""Executes tasks defined in the playbook.""" """Executes tasks defined in the playbook."""
global response_counter, expected_responses, target_ids global response_counter, expected_responses, target_ids
await basic_ready_state.wait() # Wait for the basic data to be ready await basic_ready_state.wait() # Wait for the basic data to be ready
playbook_yaml = MeshbookUtilities.read_yaml(playbook_path)
target_ids = MeshbookUtilities.get_target_ids( target_ids = MeshbookUtilities.get_target_ids(
company=playbook_yaml.get('company'), company=playbook_yaml.get('company'),
device=playbook_yaml.get('device') device=playbook_yaml.get('device')
@@ -308,9 +322,10 @@ class MeshcallerActions:
async def main(): async def main():
parser = argparse.ArgumentParser(description="Process command-line arguments") parser = argparse.ArgumentParser(description="Process command-line arguments")
parser.add_argument("-pb", "--playbook", type=str, help="Path to the playbook file.", required=True)
parser.add_argument("--conf", type=str, help="Path for the API configuration file (default: ./api.conf).") parser.add_argument("--conf", type=str, help="Path for the API configuration file (default: ./api.conf).")
parser.add_argument("--nojson", action="store_true", help="Makes the program not output the JSON response data.") parser.add_argument("--nojson", action="store_true", help="Makes the program not output the JSON response data.")
parser.add_argument("-pb", "--playbook", type=str, help="Path to the playbook file.", required=True)
parser.add_argument("-s", "--silent", action="store_true", help="Suppress terminal output.") parser.add_argument("-s", "--silent", action="store_true", help="Suppress terminal output.")
parser.add_argument("-i", "--information", action="store_true", help="Add the calculations and other informational data to the output.") parser.add_argument("-i", "--information", action="store_true", help="Add the calculations and other informational data to the output.")
@@ -328,7 +343,11 @@ async def main():
credentials['password'] credentials['password']
)) ))
processor_task = asyncio.create_task(processor.receive_processor(python_client)) processor_task = asyncio.create_task(processor.receive_processor(python_client))
await MeshcallerActions.process_arguments(python_client, args.playbook)
playbook_yaml = MeshbookUtilities.read_yaml(args.playbook)
translated_playbook = MeshbookUtilities.replace_placeholders(playbook_yaml)
await MeshcallerActions.process_arguments(python_client, translated_playbook)
await asyncio.gather(websocket_task, processor_task) await asyncio.gather(websocket_task, processor_task)
except ScriptEndTrigger as e: except ScriptEndTrigger as e: