matrix-docker-ansible-deploy/roles/matrix-awx/tasks/purge_database_main.yml

321 lines
15 KiB
YAML
Raw Normal View History

2021-10-10 05:23:49 +00:00
---
2021-08-13 08:05:57 +00:00
- name: Ensure dateutils and curl is installed in AWX
delegate_to: 127.0.0.1
yum:
name: dateutils
state: latest
- name: Include vars in matrix_vars.yml
include_vars:
file: '/var/lib/awx/projects/clients/{{ member_id }}/{{ subscription_id }}/matrix_vars.yml'
no_log: True
- name: Ensure curl and jq intalled on target machine
2021-08-13 08:05:57 +00:00
apt:
pkg:
- curl
- jq
state: present
- name: Collect before shrink size of Synapse database
shell: du -sh /matrix/postgres/data
2021-10-10 05:23:49 +00:00
register: awx_db_size_before_stat
when: (awx_purge_mode.find("Perform final shrink") != -1)
2021-08-13 08:05:57 +00:00
no_log: True
- name: Collect the internal IP of the matrix-synapse container
shell: "/usr/bin/docker inspect --format '{''{range.NetworkSettings.Networks}''}{''{.IPAddress}''}{''{end}''}' matrix-synapse"
2021-10-10 05:23:49 +00:00
when: (awx_purge_mode.find("No local users [recommended]") != -1) or (awx_purge_mode.find("Number of users [slower]") != -1) or (awx_purge_mode.find("Number of events [slower]") != -1)
register: awx_synapse_container_ip
2021-08-13 08:05:57 +00:00
2021-12-06 03:54:31 +00:00
- name: Collect access token for @admin-janitor user
2021-08-13 08:05:57 +00:00
shell: |
2021-12-06 03:54:31 +00:00
curl -X POST -d '{"type":"m.login.password", "user":"admin-janitor", "password":"{{ awx_janitor_user_password }}"}' "{{ awx_synapse_container_ip.stdout }}:{{ matrix_synapse_container_client_api_port }}/_matrix/client/r0/login" | jq '.access_token'
2021-10-10 05:23:49 +00:00
when: (awx_purge_mode.find("No local users [recommended]") != -1) or (awx_purge_mode.find("Number of users [slower]") != -1) or (awx_purge_mode.find("Number of events [slower]") != -1)
register: awx_janitors_token
2021-08-13 08:05:57 +00:00
no_log: True
- name: Copy build_room_list.py script to target machine
copy:
src: ./roles/matrix-awx/scripts/matrix_build_room_list.py
dest: /usr/local/bin/matrix_build_room_list.py
owner: matrix
group: matrix
mode: '0755'
2021-10-10 05:23:49 +00:00
when: (awx_purge_mode.find("No local users [recommended]") != -1) or (awx_purge_mode.find("Number of users [slower]") != -1) or (awx_purge_mode.find("Number of events [slower]") != -1)
2021-08-13 08:05:57 +00:00
- name: Run build_room_list.py script
shell: |
2021-11-15 20:43:05 +00:00
runuser -u matrix -- python3 /usr/local/bin/matrix_build_room_list.py {{ awx_janitors_token.stdout[1:-1] }} {{ awx_synapse_container_ip.stdout }} {{ matrix_synapse_container_client_api_port.stdout }}
2021-10-10 05:23:49 +00:00
register: awx_rooms_total
when: (awx_purge_mode.find("No local users [recommended]") != -1) or (awx_purge_mode.find("Number of users [slower]") != -1) or (awx_purge_mode.find("Number of events [slower]") != -1)
2021-08-13 08:05:57 +00:00
- name: Fetch complete room list from target machine
fetch:
src: /tmp/room_list_complete.json
dest: "/tmp/{{ subscription_id }}_room_list_complete.json"
flat: yes
2021-10-10 05:23:49 +00:00
when: (awx_purge_mode.find("No local users [recommended]") != -1) or (awx_purge_mode.find("Number of users [slower]") != -1) or (awx_purge_mode.find("Number of events [slower]") != -1)
2021-08-13 08:05:57 +00:00
- name: Remove complete room list from target machine
file:
path: /tmp/room_list_complete.json
state: absent
2021-10-10 05:23:49 +00:00
when: (awx_purge_mode.find("No local users [recommended]") != -1) or (awx_purge_mode.find("Number of users [slower]") != -1) or (awx_purge_mode.find("Number of events [slower]") != -1)
2021-08-13 08:05:57 +00:00
- name: Generate list of rooms with no local users
delegate_to: 127.0.0.1
shell: |
jq 'try .rooms[] | select(.joined_local_members == 0) | .room_id' < /tmp/{{ subscription_id }}_room_list_complete.json > /tmp/{{ subscription_id }}_room_list_no_local_users.txt
2021-10-10 05:23:49 +00:00
when: (awx_purge_mode.find("No local users [recommended]") != -1) or (awx_purge_mode.find("Number of users [slower]") != -1) or (awx_purge_mode.find("Number of events [slower]") != -1)
2021-11-15 20:43:05 +00:00
2021-08-13 08:05:57 +00:00
- name: Count number of rooms with no local users
delegate_to: 127.0.0.1
shell: |
wc -l /tmp/{{ subscription_id }}_room_list_no_local_users.txt | awk '{ print $1 }'
2021-10-10 05:23:49 +00:00
register: awx_rooms_no_local_total
when: (awx_purge_mode.find("No local users [recommended]") != -1) or (awx_purge_mode.find("Number of users [slower]") != -1) or (awx_purge_mode.find("Number of events [slower]") != -1)
2021-08-13 08:05:57 +00:00
2021-10-10 05:23:49 +00:00
- name: Setting host fact awx_room_list_no_local_users
2021-08-13 08:05:57 +00:00
set_fact:
2021-10-10 05:23:49 +00:00
awx_room_list_no_local_users: "{{ lookup('file', '/tmp/{{ subscription_id }}_room_list_no_local_users.txt') }}"
2021-08-13 08:05:57 +00:00
no_log: True
2021-10-10 05:23:49 +00:00
when: (awx_purge_mode.find("No local users [recommended]") != -1) or (awx_purge_mode.find("Number of users [slower]") != -1) or (awx_purge_mode.find("Number of events [slower]") != -1)
2021-08-13 08:05:57 +00:00
- name: Purge all rooms with no local users
2021-11-15 20:43:05 +00:00
include_tasks: purge_database_no_local.yml
2021-10-10 05:23:49 +00:00
loop: "{{ awx_room_list_no_local_users.splitlines() | flatten(levels=1) }}"
when: (awx_purge_mode.find("No local users [recommended]") != -1) or (awx_purge_mode.find("Number of users [slower]") != -1) or (awx_purge_mode.find("Number of events [slower]") != -1)
2021-08-13 08:05:57 +00:00
- name: Collect epoche time from date
delegate_to: 127.0.0.1
shell: |
2021-10-10 05:23:49 +00:00
date -d '{{ awx_purge_date }}' +"%s"
when: (awx_purge_mode.find("Number of users [slower]") != -1) or (awx_purge_mode.find("Number of events [slower]") != -1)
register: awx_purge_epoche_time
2021-08-13 08:05:57 +00:00
- name: Generate list of rooms with more then N users
delegate_to: 127.0.0.1
shell: |
2021-10-10 05:23:49 +00:00
jq 'try .rooms[] | select(.joined_members > {{ awx_purge_metric_value }}) | .room_id' < /tmp/{{ subscription_id }}_room_list_complete.json > /tmp/{{ subscription_id }}_room_list_joined_members.txt
when: awx_purge_mode.find("Number of users [slower]") != -1
2021-08-13 08:05:57 +00:00
- name: Count number of rooms with more then N users
delegate_to: 127.0.0.1
shell: |
wc -l /tmp/{{ subscription_id }}_room_list_joined_members.txt | awk '{ print $1 }'
2021-10-10 05:23:49 +00:00
register: awx_rooms_join_members_total
when: awx_purge_mode.find("Number of users [slower]") != -1
2021-08-13 08:05:57 +00:00
2021-10-10 05:23:49 +00:00
- name: Setting host fact awx_room_list_joined_members
2021-08-13 08:05:57 +00:00
delegate_to: 127.0.0.1
set_fact:
2021-10-10 05:23:49 +00:00
awx_room_list_joined_members: "{{ lookup('file', '/tmp/{{ subscription_id }}_room_list_joined_members.txt') }}"
when: awx_purge_mode.find("Number of users [slower]") != -1
2021-08-13 08:05:57 +00:00
no_log: True
- name: Purge all rooms with more then N users
2021-11-15 20:43:05 +00:00
include_tasks: purge_database_users.yml
2021-10-10 05:23:49 +00:00
loop: "{{ awx_room_list_joined_members.splitlines() | flatten(levels=1) }}"
when: awx_purge_mode.find("Number of users [slower]") != -1
2021-08-13 08:05:57 +00:00
- name: Generate list of rooms with more then N events
delegate_to: 127.0.0.1
shell: |
2021-10-10 05:23:49 +00:00
jq 'try .rooms[] | select(.state_events > {{ awx_purge_metric_value }}) | .room_id' < /tmp/{{ subscription_id }}_room_list_complete.json > /tmp/{{ subscription_id }}_room_list_state_events.txt
when: awx_purge_mode.find("Number of events [slower]") != -1
2021-08-13 08:05:57 +00:00
- name: Count number of rooms with more then N events
delegate_to: 127.0.0.1
shell: |
wc -l /tmp/{{ subscription_id }}_room_list_state_events.txt | awk '{ print $1 }'
2021-10-10 05:23:49 +00:00
register: awx_rooms_state_events_total
when: awx_purge_mode.find("Number of events [slower]") != -1
2021-08-13 08:05:57 +00:00
2021-10-10 05:23:49 +00:00
- name: Setting host fact awx_room_list_state_events
2021-08-13 08:05:57 +00:00
delegate_to: 127.0.0.1
set_fact:
2021-10-10 05:23:49 +00:00
awx_room_list_state_events: "{{ lookup('file', '/tmp/{{ subscription_id }}_room_list_state_events.txt') }}"
when: awx_purge_mode.find("Number of events [slower]") != -1
2021-08-13 08:05:57 +00:00
no_log: True
- name: Purge all rooms with more then N events
2021-11-15 20:43:05 +00:00
include_tasks: purge_database_events.yml
2021-10-10 05:23:49 +00:00
loop: "{{ awx_room_list_state_events.splitlines() | flatten(levels=1) }}"
when: awx_purge_mode.find("Number of events [slower]") != -1
2021-08-13 08:05:57 +00:00
- name: Adjust 'Deploy/Update a Server' job template
delegate_to: 127.0.0.1
awx.awx.tower_job_template:
name: "{{ matrix_domain }} - 0 - Deploy/Update a Server"
description: "Creates a new matrix service with Spantaleev's playbooks"
extra_vars: "{{ lookup('file', '/var/lib/awx/projects/clients/{{ member_id }}/{{ subscription_id }}/extra_vars.json') }}"
job_type: run
job_tags: "rust-synapse-compress-state"
inventory: "{{ member_id }}"
project: "{{ member_id }} - Matrix Docker Ansible Deploy"
playbook: setup.yml
credential: "{{ member_id }} - AWX SSH Key"
state: present
verbosity: 1
tower_host: "https://{{ awx_host }}"
tower_oauthtoken: "{{ awx_session_token.ansible_facts.tower_token.token }}"
2021-08-13 08:05:57 +00:00
validate_certs: yes
2021-10-10 05:23:49 +00:00
when: (awx_purge_mode.find("No local users [recommended]") != -1) or (awx_purge_mode.find("Number of users [slower]") != -1) or (awx_purge_mode.find("Number of events [slower]") != -1) or (awx_purge_mode.find("Skip purging rooms [faster]") != -1)
2021-08-13 08:05:57 +00:00
- name: Execute rust-synapse-compress-state job template
delegate_to: 127.0.0.1
awx.awx.tower_job_launch:
job_template: "{{ matrix_domain }} - 0 - Deploy/Update a Server"
wait: yes
tower_host: "https://{{ awx_host }}"
tower_oauthtoken: "{{ awx_session_token.ansible_facts.tower_token.token }}"
2021-11-15 20:43:05 +00:00
validate_certs: yes
2021-10-10 05:23:49 +00:00
when: (awx_purge_mode.find("No local users [recommended]") != -1) or (awx_purge_mode.find("Number of users [slower]") != -1) or (awx_purge_mode.find("Number of events [slower]") != -1) or (awx_purge_mode.find("Skip purging rooms [faster]") != -1)
2021-08-13 08:05:57 +00:00
- name: Revert 'Deploy/Update a Server' job template
delegate_to: 127.0.0.1
awx.awx.tower_job_template:
name: "{{ matrix_domain }} - 0 - Deploy/Update a Server"
description: "Creates a new matrix service with Spantaleev's playbooks"
extra_vars: "{{ lookup('file', '/var/lib/awx/projects/clients/{{ member_id }}/{{ subscription_id }}/extra_vars.json') }}"
job_type: run
job_tags: "setup-all,start"
inventory: "{{ member_id }}"
project: "{{ member_id }} - Matrix Docker Ansible Deploy"
playbook: setup.yml
credential: "{{ member_id }} - AWX SSH Key"
state: present
verbosity: 1
tower_host: "https://{{ awx_host }}"
tower_oauthtoken: "{{ awx_session_token.ansible_facts.tower_token.token }}"
2021-08-13 08:05:57 +00:00
validate_certs: yes
2021-10-10 05:23:49 +00:00
when: (awx_purge_mode.find("No local users [recommended]") != -1) or (awx_purge_mode.find("Number of users [slower]") != -1) or (awx_purge_mode.find("Number of events [slower]") != -1) or (awx_purge_mode.find("Skip purging rooms [faster]") != -1)
2021-08-13 08:05:57 +00:00
- name: Ensure matrix-synapse is stopped
service:
name: matrix-synapse
state: stopped
daemon_reload: yes
2021-10-10 05:23:49 +00:00
when: (awx_purge_mode.find("Perform final shrink") != -1)
2021-08-13 08:05:57 +00:00
- name: Re-index Synapse database
shell: docker exec -i matrix-postgres psql "host=127.0.0.1 port=5432 dbname=synapse user=synapse password={{ matrix_synapse_connection_password }}" -c 'REINDEX (VERBOSE) DATABASE synapse'
2021-10-10 05:23:49 +00:00
when: (awx_purge_mode.find("Perform final shrink") != -1)
2021-08-13 08:05:57 +00:00
- name: Ensure matrix-synapse is started
service:
name: matrix-synapse
state: started
daemon_reload: yes
2021-10-10 05:23:49 +00:00
when: (awx_purge_mode.find("Perform final shrink") != -1)
2021-08-13 08:05:57 +00:00
- name: Adjust 'Deploy/Update a Server' job template
delegate_to: 127.0.0.1
awx.awx.tower_job_template:
name: "{{ matrix_domain }} - 0 - Deploy/Update a Server"
description: "Creates a new matrix service with Spantaleev's playbooks"
extra_vars: "{{ lookup('file', '/var/lib/awx/projects/clients/{{ member_id }}/{{ subscription_id }}/extra_vars.json') }}"
job_type: run
job_tags: "run-postgres-vacuum,start"
inventory: "{{ member_id }}"
project: "{{ member_id }} - Matrix Docker Ansible Deploy"
playbook: setup.yml
credential: "{{ member_id }} - AWX SSH Key"
state: present
verbosity: 1
tower_host: "https://{{ awx_host }}"
tower_oauthtoken: "{{ awx_session_token.ansible_facts.tower_token.token }}"
2021-08-13 08:05:57 +00:00
validate_certs: yes
2021-10-10 05:23:49 +00:00
when: (awx_purge_mode.find("Perform final shrink") != -1)
2021-08-13 08:05:57 +00:00
- name: Execute run-postgres-vacuum job template
delegate_to: 127.0.0.1
awx.awx.tower_job_launch:
job_template: "{{ matrix_domain }} - 0 - Deploy/Update a Server"
wait: yes
tower_host: "https://{{ awx_host }}"
tower_oauthtoken: "{{ awx_session_token.ansible_facts.tower_token.token }}"
2021-11-15 20:43:05 +00:00
validate_certs: yes
2021-10-10 05:23:49 +00:00
when: (awx_purge_mode.find("Perform final shrink") != -1)
2021-08-13 08:05:57 +00:00
- name: Revert 'Deploy/Update a Server' job template
delegate_to: 127.0.0.1
awx.awx.tower_job_template:
name: "{{ matrix_domain }} - 0 - Deploy/Update a Server"
description: "Creates a new matrix service with Spantaleev's playbooks"
extra_vars: "{{ lookup('file', '/var/lib/awx/projects/clients/{{ member_id }}/{{ subscription_id }}/extra_vars.json') }}"
job_type: run
job_tags: "setup-all,start"
inventory: "{{ member_id }}"
project: "{{ member_id }} - Matrix Docker Ansible Deploy"
playbook: setup.yml
credential: "{{ member_id }} - AWX SSH Key"
state: present
verbosity: 1
tower_host: "https://{{ awx_host }}"
tower_oauthtoken: "{{ awx_session_token.ansible_facts.tower_token.token }}"
2021-08-13 08:05:57 +00:00
validate_certs: yes
2021-10-10 05:23:49 +00:00
when: (awx_purge_mode.find("Perform final shrink") != -1)
2021-08-13 08:05:57 +00:00
- name: Cleanup room_list files
delegate_to: 127.0.0.1
shell: |
rm /tmp/{{ subscription_id }}_room_list*
2021-10-10 05:23:49 +00:00
when: (awx_purge_mode.find("No local users [recommended]") != -1) or (awx_purge_mode.find("Number of users [slower]") != -1) or (awx_purge_mode.find("Number of events [slower]") != -1)
2021-08-13 08:05:57 +00:00
ignore_errors: yes
- name: Collect after shrink size of Synapse database
shell: du -sh /matrix/postgres/data
2021-10-10 05:23:49 +00:00
register: awx_db_size_after_stat
when: (awx_purge_mode.find("Perform final shrink") != -1)
2021-08-13 08:05:57 +00:00
no_log: True
2021-11-15 20:43:05 +00:00
- name: Print total number of rooms processed
2021-08-13 08:05:57 +00:00
debug:
2021-10-10 05:23:49 +00:00
msg: '{{ awx_rooms_total.stdout }}'
when: (awx_purge_mode.find("No local users [recommended]") != -1) or (awx_purge_mode.find("Number of users [slower]") != -1) or (awx_purge_mode.find("Number of events [slower]") != -1)
2021-08-13 08:05:57 +00:00
- name: Print the number of rooms purged with no local users
debug:
2021-10-10 05:23:49 +00:00
msg: '{{ awx_rooms_no_local_total.stdout }}'
when: (awx_purge_mode.find("No local users [recommended]") != -1) or (awx_purge_mode.find("Number of users [slower]") != -1) or (awx_purge_mode.find("Number of events [slower]") != -1)
2021-08-13 08:05:57 +00:00
- name: Print the number of rooms purged with more then N users
debug:
2021-10-10 05:23:49 +00:00
msg: '{{ awx_rooms_join_members_total.stdout }}'
when: awx_purge_mode.find("Number of users") != -1
2021-08-13 08:05:57 +00:00
- name: Print the number of rooms purged with more then N events
debug:
2021-10-10 05:23:49 +00:00
msg: '{{ awx_rooms_state_events_total.stdout }}'
when: awx_purge_mode.find("Number of events") != -1
2021-08-13 08:05:57 +00:00
- name: Print before purge size of Synapse database
debug:
2021-10-10 05:23:49 +00:00
msg: "{{ awx_db_size_before_stat.stdout.split('\n') }}"
when: ( awx_db_size_before_stat is defined ) and ( awx_purge_mode.find("Perform final shrink" ) != -1 )
2021-08-13 08:05:57 +00:00
- name: Print after purge size of Synapse database
debug:
2021-10-10 05:23:49 +00:00
msg: "{{ awx_db_size_after_stat.stdout.split('\n') }}"
when: (awx_db_size_after_stat is defined) and (awx_purge_mode.find("Perform final shrink") != -1)
2021-08-13 08:05:57 +00:00
- name: Delete the AWX session token for executing modules
awx.awx.tower_token:
description: 'AWX Session Token'
scope: "write"
state: absent
existing_token_id: "{{ awx_session_token.ansible_facts.tower_token.id }}"
tower_host: "https://{{ awx_host }}"
tower_oauthtoken: "{{ awx_session_token.ansible_facts.tower_token.token }}"
2021-08-13 08:05:57 +00:00
- name: Set boolean value to exit playbook
set_fact:
2021-10-10 05:23:49 +00:00
awx_end_playbook: true
2021-08-13 08:05:57 +00:00
- name: End playbook early if this task is called.
meta: end_play
2021-10-10 05:23:49 +00:00
when: awx_end_playbook is defined and awx_end_playbook|bool