From d6983b4744eecc4c6467c94b71703ca5e4ef148f Mon Sep 17 00:00:00 2001 From: michael Date: Wed, 30 Oct 2024 01:52:00 -0400 Subject: [PATCH] add syanpse mgmt playbooks --- .../host_vars/localhost/synapse.yaml | 1 + .../playbooks/synapse_delete_empty_rooms.yaml | 44 +++++++++++++++++++ ansible/playbooks/synapse_delete_room.yaml | 28 ++++++++++++ ansible/playbooks/synapse_get_all_rooms.yaml | 19 ++++++++ .../playbooks/synapse_get_large_rooms.yaml | 19 ++++++++ .../playbooks/synapse_get_purge_status.yaml | 44 +++++++++++++++++++ .../playbooks/synapse_get_room_details.yaml | 23 ++++++++++ .../playbooks/synapse_get_room_members.yaml | 23 ++++++++++ 8 files changed, 201 insertions(+) create mode 100644 ansible/inventory/host_vars/localhost/synapse.yaml create mode 100644 ansible/playbooks/synapse_delete_empty_rooms.yaml create mode 100644 ansible/playbooks/synapse_delete_room.yaml create mode 100644 ansible/playbooks/synapse_get_all_rooms.yaml create mode 100644 ansible/playbooks/synapse_get_large_rooms.yaml create mode 100644 ansible/playbooks/synapse_get_purge_status.yaml create mode 100644 ansible/playbooks/synapse_get_room_details.yaml create mode 100644 ansible/playbooks/synapse_get_room_members.yaml diff --git a/ansible/inventory/host_vars/localhost/synapse.yaml b/ansible/inventory/host_vars/localhost/synapse.yaml new file mode 100644 index 0000000..287e7da --- /dev/null +++ b/ansible/inventory/host_vars/localhost/synapse.yaml @@ -0,0 +1 @@ +synapse_host_address: matrix.balsillie.net \ No newline at end of file diff --git a/ansible/playbooks/synapse_delete_empty_rooms.yaml b/ansible/playbooks/synapse_delete_empty_rooms.yaml new file mode 100644 index 0000000..ca1867a --- /dev/null +++ b/ansible/playbooks/synapse_delete_empty_rooms.yaml @@ -0,0 +1,44 @@ +# code: language=ansible + +- name: Clean Synapse + hosts: localhost + connection: local + become: false + gather_facts: false + tasks: + + - name: Get room list + ansible.builtin.uri: + url: "https://{{ synapse_host_address }}/_synapse/admin/v1/rooms?limit=1000" + headers: + Authorization: "Bearer {{ synapse_admin_token }}" + register: room_list + + - name: Set empty_rooms fact + ansible.builtin.set_fact: + empty_rooms: "{{ room_list.json.rooms | selectattr('joined_local_members', '==', 0) | list }}" + + - name: Debug empty room count + ansible.builtin.debug: + msg: "Total empty rooms to delete: {{ empty_rooms | length }}" + + - name: Delete empty rooms + when: empty_rooms | length > 0 + ansible.builtin.uri: + url: "https://{{ synapse_host_address }}/_synapse/admin/v2/rooms/{{ room.room_id }}" + method: DELETE + headers: + Authorization: "Bearer {{ synapse_admin_token }}" + body_format: json + body: {} + loop: "{{ empty_rooms }}" + loop_control: + loop_var: room + label: "{{ room.room_id }}" + register: purge_ids + + - name: Write purge_ids to file + ansible.builtin.copy: + dest: "{{ playbook_dir }}/purge_ids_{{ now(utc=false, fmt='%Y-%m-%d_%H-%M-%S') }}.json" + content: "{{ purge_ids.results | map(attribute='json.delete_id') | list | to_nice_json }}" + mode: "0664" diff --git a/ansible/playbooks/synapse_delete_room.yaml b/ansible/playbooks/synapse_delete_room.yaml new file mode 100644 index 0000000..d18747e --- /dev/null +++ b/ansible/playbooks/synapse_delete_room.yaml @@ -0,0 +1,28 @@ +# code: language=ansible + +- name: Clean Synapse + hosts: localhost + connection: local + become: false + gather_facts: false + vars_prompt: + - name: room_id + prompt: "Enter the room ID to delete" + private: false + tasks: + + - name: Delete room + ansible.builtin.uri: + url: "https://{{ synapse_host_address }}/_synapse/admin/v2/rooms/{{ room_id }}" + method: DELETE + headers: + Authorization: "Bearer {{ synapse_admin_token }}" + body_format: json + body: {} + register: purge_id + + - name: Wait for purge to complete + ansible.builtin.uri: + url: "https://{{ synapse_host_address }}/_synapse/admin/v2/rooms/delete_status/{{ item }}" + headers: + Authorization: "Bearer {{ synapse_admin_token }}" diff --git a/ansible/playbooks/synapse_get_all_rooms.yaml b/ansible/playbooks/synapse_get_all_rooms.yaml new file mode 100644 index 0000000..9da7c4d --- /dev/null +++ b/ansible/playbooks/synapse_get_all_rooms.yaml @@ -0,0 +1,19 @@ +# code: language=ansible + +- name: Clean Synapse + hosts: localhost + connection: local + become: false + gather_facts: false + tasks: + + - name: Get room details + ansible.builtin.uri: + url: "https://{{ synapse_host_address }}/_synapse/admin/v1/rooms?limit=1000" + headers: + Authorization: "Bearer {{ synapse_admin_token }}" + register: result + + - name: Print result + ansible.builtin.debug: + var: result.json.rooms | map(attribute='room_id') | list diff --git a/ansible/playbooks/synapse_get_large_rooms.yaml b/ansible/playbooks/synapse_get_large_rooms.yaml new file mode 100644 index 0000000..c7a76d3 --- /dev/null +++ b/ansible/playbooks/synapse_get_large_rooms.yaml @@ -0,0 +1,19 @@ +# code: language=ansible + +- name: Clean Synapse + hosts: localhost + connection: local + become: false + gather_facts: false + tasks: + + - name: Get large rooms + ansible.builtin.uri: + url: "https://{{ synapse_host_address }}/_synapse/admin/v1/statistics/database/rooms" + headers: + Authorization: "Bearer {{ synapse_admin_token }}" + register: result + + - name: Print result + ansible.builtin.debug: + var: result.json diff --git a/ansible/playbooks/synapse_get_purge_status.yaml b/ansible/playbooks/synapse_get_purge_status.yaml new file mode 100644 index 0000000..d839012 --- /dev/null +++ b/ansible/playbooks/synapse_get_purge_status.yaml @@ -0,0 +1,44 @@ +# code: language=ansible + +- name: Clean Synapse + hosts: localhost + connection: local + become: false + gather_facts: false + vars_prompt: + - name: "purge_ids_file" + prompt: "Enter the file name containing the purge ids" + private: false + tasks: + + - name: Load purge ids + ansible.builtin.slurp: + src: "{{ playbook_dir }}/{{ purge_ids_file }}" + register: purge_ids + + - name: Set purge_ids_list fact + ansible.builtin.set_fact: + purge_ids_list: "{{ purge_ids.content | b64decode | from_json }}" + + - name: Get purge status + ansible.builtin.uri: + url: "https://{{ synapse_host_address }}/_synapse/admin/v2/rooms/delete_status/{{ item }}" + headers: + Authorization: "Bearer {{ synapse_admin_token }}" + loop: "{{ purge_ids_list }}" + register: purge_status + + - name: Set purge_satus_totals + ansible.builtin.set_fact: + purge_status_shutting_down: "{{ purge_status.results | selectattr('json.status', '==', 'shutting_down') | list | length }}" + purge_status_purging: "{{ purge_status.results | selectattr('json.status', '==', 'purging') | list | length }}" + purge_status_complete: "{{ purge_status.results | selectattr('json.status', '==', 'complete') | list | length }}" + purge_status_failed: "{{ purge_status.results | selectattr('json.status', '==', 'failed') | list | length }}" + + - name: Print status + ansible.builtin.debug: + msg: | + Shutting down: {{ purge_status_shutting_down }} + Purging: {{ purge_status_purging }} + Complete: {{ purge_status_complete }} + Failed: {{ purge_status_failed }}" diff --git a/ansible/playbooks/synapse_get_room_details.yaml b/ansible/playbooks/synapse_get_room_details.yaml new file mode 100644 index 0000000..b33076c --- /dev/null +++ b/ansible/playbooks/synapse_get_room_details.yaml @@ -0,0 +1,23 @@ +# code: language=ansible + +- name: Clean Synapse + hosts: localhost + connection: local + become: false + gather_facts: false + vars_prompt: + - name: room_id + prompt: "Enter the room ID to fetch" + private: false + tasks: + + - name: Get room details + ansible.builtin.uri: + url: "https://{{ synapse_host_address }}/_synapse/admin/v1/rooms/{{ room_id }}" + headers: + Authorization: "Bearer {{ synapse_admin_token }}" + register: result + + - name: Print result + ansible.builtin.debug: + var: result.json diff --git a/ansible/playbooks/synapse_get_room_members.yaml b/ansible/playbooks/synapse_get_room_members.yaml new file mode 100644 index 0000000..68ca689 --- /dev/null +++ b/ansible/playbooks/synapse_get_room_members.yaml @@ -0,0 +1,23 @@ +# code: language=ansible + +- name: Room members + hosts: localhost + connection: local + become: false + gather_facts: false + vars_prompt: + - name: room_id + prompt: "Enter the room ID to fetch" + private: false + tasks: + + - name: Get room details + ansible.builtin.uri: + url: "https://{{ synapse_host_address }}/_synapse/admin/v1/rooms/{{ room_id }}/members" + headers: + Authorization: "Bearer {{ synapse_admin_token }}" + register: result + + - name: Print result + ansible.builtin.debug: + var: result.json