2021-05-10 10:50:10 +00:00
- name : Ensure dateutils and curl is installed in AWX
delegate_to : 127.0 .0 .1
yum :
name : dateutils
state : latest
- name : Ensure dateutils, curl and jq intalled on target machine
apt :
pkg :
- curl
- jq
state : present
- 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
2021-05-25 13:08:00 +00:00
- name : Collect before shrink size of Synapse database
2021-05-10 10:50:10 +00:00
shell : du -sh /matrix/postgres/data
register : db_size_before_stat
2021-05-25 13:08:00 +00:00
when : (purge_mode.find("Perform final shrink") != -1)
2021-05-10 10:50:10 +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-05-25 13:08:00 +00:00
when : (purge_mode.find("No local users [recommended]") != -1) or (purge_mode.find("Number of users [slower]") != -1) or (purge_mode.find("Number of events [slower]") != -1)
2021-05-10 10:50:10 +00:00
register : synapse_container_ip
- name : Collect access token for janitor user
shell : |
curl -X POST -d '{"type":"m.login.password", "user":"janitor", "password":"{{ matrix_awx_janitor_user_password }}"}' "{{ synapse_container_ip.stdout }}:8008/_matrix/client/r0/login" | jq '.access_token'
2021-05-25 13:08:00 +00:00
when : (purge_mode.find("No local users [recommended]") != -1) or (purge_mode.find("Number of users [slower]") != -1) or (purge_mode.find("Number of events [slower]") != -1)
2021-05-10 10:50:10 +00:00
register : janitors_token
no_log : True
2021-05-25 13:08:00 +00:00
- 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'
when : (purge_mode.find("No local users [recommended]") != -1) or (purge_mode.find("Number of users [slower]") != -1) or (purge_mode.find("Number of events [slower]") != -1)
- name : Run build_room_list.py script
2021-05-10 10:50:10 +00:00
shell : |
2021-05-25 13:08:00 +00:00
runuser -u matrix -- python3 /usr/local/bin/matrix_build_room_list.py {{ janitors_token.stdout[1:-1] }} {{ synapse_container_ip.stdout }}
2021-05-10 10:50:10 +00:00
register : rooms_total
2021-05-25 13:08:00 +00:00
when : (purge_mode.find("No local users [recommended]") != -1) or (purge_mode.find("Number of users [slower]") != -1) or (purge_mode.find("Number of events [slower]") != -1)
2021-05-10 10:50:10 +00:00
2021-05-25 13:08:00 +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
when : (purge_mode.find("No local users [recommended]") != -1) or (purge_mode.find("Number of users [slower]") != -1) or (purge_mode.find("Number of events [slower]") != -1)
2021-05-10 10:50:10 +00:00
2021-05-25 13:08:00 +00:00
- name : Remove complete room list from target machine
2021-05-10 10:50:10 +00:00
file :
2021-05-25 13:08:00 +00:00
path : /tmp/room_list_complete.json
state : absent
when : (purge_mode.find("No local users [recommended]") != -1) or (purge_mode.find("Number of users [slower]") != -1) or (purge_mode.find("Number of events [slower]") != -1)
2021-05-10 10:50:10 +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-05-25 13:08:00 +00:00
when : (purge_mode.find("No local users [recommended]") != -1) or (purge_mode.find("Number of users [slower]") != -1) or (purge_mode.find("Number of events [slower]") != -1)
2021-05-10 10:50:10 +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 }'
register : rooms_no_local_total
2021-05-25 13:08:00 +00:00
when : (purge_mode.find("No local users [recommended]") != -1) or (purge_mode.find("Number of users [slower]") != -1) or (purge_mode.find("Number of events [slower]") != -1)
2021-05-10 10:50:10 +00:00
- name : Setting host fact room_list_no_local_users
set_fact :
room_list_no_local_users : "{{ lookup('file', '/tmp/{{ subscription_id }}_room_list_no_local_users.txt') }}"
no_log : True
2021-05-25 13:08:00 +00:00
when : (purge_mode.find("No local users [recommended]") != -1) or (purge_mode.find("Number of users [slower]") != -1) or (purge_mode.find("Number of events [slower]") != -1)
2021-05-10 10:50:10 +00:00
- name : Purge all rooms with no local users
include_tasks : purge_database_no_local.yml
loop : "{{ room_list_no_local_users.splitlines() | flatten(levels=1) }}"
2021-05-25 13:08:00 +00:00
when : (purge_mode.find("No local users [recommended]") != -1) or (purge_mode.find("Number of users [slower]") != -1) or (purge_mode.find("Number of events [slower]") != -1)
2021-05-10 10:50:10 +00:00
- name : Collect epoche time from date
delegate_to : 127.0 .0 .1
shell : |
date -d '{{ purge_date }}' +"%s"
2021-05-25 13:08:00 +00:00
when : (purge_mode.find("Number of users [slower]") != -1) or (purge_mode.find("Number of events [slower]") != -1)
2021-05-10 10:50:10 +00:00
register : purge_epoche_time
- name : Generate list of rooms with more then N users
delegate_to : 127.0 .0 .1
shell : |
jq 'try .rooms[] | select(.joined_members > {{ purge_metric_value }}) | .room_id' < /tmp/{{ subscription_id }}_room_list_complete.json > /tmp/{{ subscription_id }}_room_list_joined_members.txt
2021-05-25 13:08:00 +00:00
when : purge_mode.find("Number of users [slower]") != -1
2021-05-10 10:50:10 +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 }'
register : rooms_join_members_total
2021-05-25 13:08:00 +00:00
when : purge_mode.find("Number of users [slower]") != -1
2021-05-10 10:50:10 +00:00
- name : Setting host fact room_list_joined_members
delegate_to : 127.0 .0 .1
set_fact :
room_list_joined_members : "{{ lookup('file', '/tmp/{{ subscription_id }}_room_list_joined_members.txt') }}"
2021-05-25 13:08:00 +00:00
when : purge_mode.find("Number of users [slower]") != -1
2021-05-10 10:50:10 +00:00
no_log : True
- name : Purge all rooms with more then N users
include_tasks : purge_database_users.yml
loop : "{{ room_list_joined_members.splitlines() | flatten(levels=1) }}"
2021-05-25 13:08:00 +00:00
when : purge_mode.find("Number of users [slower]") != -1
2021-05-10 10:50:10 +00:00
- name : Generate list of rooms with more then N events
delegate_to : 127.0 .0 .1
shell : |
jq 'try .rooms[] | select(.state_events > {{ purge_metric_value }}) | .room_id' < /tmp/{{ subscription_id }}_room_list_complete.json > /tmp/{{ subscription_id }}_room_list_state_events.txt
2021-05-25 13:08:00 +00:00
when : purge_mode.find("Number of events [slower]") != -1
2021-05-10 10:50:10 +00:00
2021-05-25 13:08:00 +00:00
- name : Count number of rooms with more then N events
2021-05-10 10:50:10 +00:00
delegate_to : 127.0 .0 .1
shell : |
wc -l /tmp/{{ subscription_id }}_room_list_state_events.txt | awk '{ print $1 }'
register : rooms_state_events_total
2021-05-25 13:08:00 +00:00
when : purge_mode.find("Number of events [slower]") != -1
2021-05-10 10:50:10 +00:00
- name : Setting host fact room_list_state_events
delegate_to : 127.0 .0 .1
set_fact :
room_list_state_events : "{{ lookup('file', '/tmp/{{ subscription_id }}_room_list_state_events.txt') }}"
2021-05-25 13:08:00 +00:00
when : purge_mode.find("Number of events [slower]") != -1
2021-05-10 10:50:10 +00:00
no_log : True
- name : Purge all rooms with more then N events
include_tasks : purge_database_events.yml
loop : "{{ room_list_state_events.splitlines() | flatten(levels=1) }}"
2021-05-25 13:08:00 +00:00
when : purge_mode.find("Number of events [slower]") != -1
2021-05-10 10:50:10 +00:00
- name : Collect AWX admin token the hard way!
delegate_to : 127.0 .0 .1
shell : |
curl -sku {{ tower_username }}:{{ tower_password }} -H "Content-Type: application/json" -X POST -d '{"description":"Tower CLI", "application":null, "scope":"write"}' https://{{ tower_host }}/api/v2/users/1/personal_tokens/ | jq '.token' | sed -r 's/\"//g'
register : tower_token
no_log : True
2021-05-25 13:08:00 +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://{{ tower_host }}"
tower_oauthtoken : "{{ tower_token.stdout }}"
validate_certs : yes
when : (purge_mode.find("No local users [recommended]") != -1) or (purge_mode.find("Number of users [slower]") != -1) or (purge_mode.find("Number of events [slower]") != -1) or (purge_mode.find("Skip purging rooms [faster]") != -1)
2021-05-10 10:50:10 +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://{{ tower_host }}"
tower_oauthtoken : "{{ tower_token.stdout }}"
validate_certs : yes
2021-05-25 13:08:00 +00:00
when : (purge_mode.find("No local users [recommended]") != -1) or (purge_mode.find("Number of users [slower]") != -1) or (purge_mode.find("Number of events [slower]") != -1) or (purge_mode.find("Skip purging rooms [faster]") != -1)
2021-05-10 10:50:10 +00:00
2021-05-25 13:08:00 +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://{{ tower_host }}"
tower_oauthtoken : "{{ tower_token.stdout }}"
validate_certs : yes
when : (purge_mode.find("No local users [recommended]") != -1) or (purge_mode.find("Number of users [slower]") != -1) or (purge_mode.find("Number of events [slower]") != -1) or (purge_mode.find("Skip purging rooms [faster]") != -1)
- name : Ensure matrix-synapse is stopped
service :
name : matrix-synapse
state : stopped
daemon_reload : yes
when : (purge_mode.find("Perform final shrink") != -1)
2021-05-10 10:50:10 +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-05-25 13:08:00 +00:00
when : (purge_mode.find("Perform final shrink") != -1)
- name : Ensure matrix-synapse is started
service :
name : matrix-synapse
state : started
daemon_reload : yes
when : (purge_mode.find("Perform final shrink") != -1)
- 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://{{ tower_host }}"
tower_oauthtoken : "{{ tower_token.stdout }}"
validate_certs : yes
when : (purge_mode.find("Perform final shrink") != -1)
2021-05-10 10:50:10 +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://{{ tower_host }}"
tower_oauthtoken : "{{ tower_token.stdout }}"
validate_certs : yes
2021-05-25 13:08:00 +00:00
when : (purge_mode.find("Perform final shrink") != -1)
- 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://{{ tower_host }}"
tower_oauthtoken : "{{ tower_token.stdout }}"
validate_certs : yes
when : (purge_mode.find("Perform final shrink") != -1)
2021-05-10 10:50:10 +00:00
- name : Cleanup room_list files
delegate_to : 127.0 .0 .1
shell : |
rm /tmp/{{ subscription_id }}_room_list*
2021-05-25 13:08:00 +00:00
when : (purge_mode.find("No local users [recommended]") != -1) or (purge_mode.find("Number of users [slower]") != -1) or (purge_mode.find("Number of events [slower]") != -1)
2021-05-10 10:50:10 +00:00
ignore_errors : yes
2021-05-25 13:08:00 +00:00
- name : Collect after shrink size of Synapse database
2021-05-10 10:50:10 +00:00
shell : du -sh /matrix/postgres/data
register : db_size_after_stat
2021-05-25 13:08:00 +00:00
when : (purge_mode.find("Perform final shrink") != -1)
2021-05-10 10:50:10 +00:00
no_log : True
- name : Print total number of rooms processed
debug :
msg : '{{ rooms_total.stdout }}'
2021-05-25 13:08:00 +00:00
when : (purge_mode.find("No local users [recommended]") != -1) or (purge_mode.find("Number of users [slower]") != -1) or (purge_mode.find("Number of events [slower]") != -1)
2021-05-10 10:50:10 +00:00
- name : Print the number of rooms purged with no local users
debug :
msg : '{{ rooms_no_local_total.stdout }}'
2021-05-25 13:08:00 +00:00
when : (purge_mode.find("No local users [recommended]") != -1) or (purge_mode.find("Number of users [slower]") != -1) or (purge_mode.find("Number of events [slower]") != -1)
2021-05-10 10:50:10 +00:00
- name : Print the number of rooms purged with more then N users
debug :
msg : '{{ rooms_join_members_total.stdout }}'
2021-05-25 13:08:00 +00:00
when : purge_mode.find("Number of users") != -1
2021-05-10 10:50:10 +00:00
- name : Print the number of rooms purged with more then N events
debug :
msg : '{{ rooms_state_events_total.stdout }}'
2021-05-25 13:08:00 +00:00
when : purge_mode.find("Number of events") != -1
2021-05-10 10:50:10 +00:00
- name : Print before purge size of Synapse database
debug :
msg : "{{ db_size_before_stat.stdout.split('\n') }}"
2021-05-25 13:08:00 +00:00
when : (db_size_before_stat is defined) and (purge_mode.find("Perform final shrink") != -1)
2021-05-10 10:50:10 +00:00
- name : Print after purge size of Synapse database
debug :
msg : "{{ db_size_after_stat.stdout.split('\n') }}"
2021-05-25 13:08:00 +00:00
when : (db_size_after_stat is defined) and (purge_mode.find("Perform final shrink") != -1)
2021-05-10 10:50:10 +00:00
- name : Set boolean value to exit playbook
set_fact :
end_playbook : true
- name : End playbook early if this task is called.
meta : end_play
when : end_playbook is defined and end_playbook|bool