diff --git a/examples/apt_upgrade.yaml b/examples/apt_upgrade.yaml new file mode 100644 index 0000000..3a0e4b4 --- /dev/null +++ b/examples/apt_upgrade.yaml @@ -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" \ No newline at end of file diff --git a/examples/refresh_aptcache.yaml b/examples/refresh_aptcache.yaml new file mode 100644 index 0000000..196ebb5 --- /dev/null +++ b/examples/refresh_aptcache.yaml @@ -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" \ No newline at end of file diff --git a/examples/variable_example.yaml b/examples/variable_example.yaml new file mode 100644 index 0000000..4f92d9f --- /dev/null +++ b/examples/variable_example.yaml @@ -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 }}" \ No newline at end of file diff --git a/meshbook/examples/apt_upgrade.yaml b/meshbook/examples/apt_upgrade.yaml deleted file mode 100644 index f77ea5a..0000000 --- a/meshbook/examples/apt_upgrade.yaml +++ /dev/null @@ -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" \ No newline at end of file diff --git a/meshbook/examples/ping.yaml b/meshbook/examples/ping.yaml deleted file mode 100644 index 0a39d9a..0000000 --- a/meshbook/examples/ping.yaml +++ /dev/null @@ -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" \ No newline at end of file diff --git a/meshbook/examples/refresh_aptcache.yaml b/meshbook/examples/refresh_aptcache.yaml deleted file mode 100644 index 0f7eb8d..0000000 --- a/meshbook/examples/refresh_aptcache.yaml +++ /dev/null @@ -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" \ No newline at end of file diff --git a/meshbook/meshbook.py b/meshbook/meshbook.py index f838a62..7f86b6b 100644 --- a/meshbook/meshbook.py +++ b/meshbook/meshbook.py @@ -72,6 +72,20 @@ class MeshbookUtilities: """Read a YAML file and return its content as a dictionary.""" with open(file_path, 'r') as 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 def translate_nodeids(batches_dict, global_list) -> dict: @@ -220,6 +234,7 @@ class MeshbookProcessor: """Processes messages received from the WebSocket.""" global response_counter temp_responses_list = [] + while True: message = await python_client.received_response_queue.get() action_type = message.get('action') @@ -250,13 +265,12 @@ class MeshcallerActions: """Processes playbook actions.""" @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.""" global response_counter, expected_responses, target_ids 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( company=playbook_yaml.get('company'), device=playbook_yaml.get('device') @@ -308,9 +322,10 @@ class MeshcallerActions: async def main(): 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("--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("-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'] )) 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) except ScriptEndTrigger as e: