From bf62cba8e76e87cbfe51c36d9a119efa7d0e377f Mon Sep 17 00:00:00 2001 From: erebion Date: Fri, 16 Aug 2024 22:43:13 +0200 Subject: [PATCH] added a playbook for cleaning the database used by Synapse --- clean-synapse-db.yml | 65 ++++++++++++++++++ clean_synapse_db.sh | 2 + host_vars/unhb4/unhb4_vault.yml | 113 ++++++++++++++++++-------------- 3 files changed, 131 insertions(+), 49 deletions(-) create mode 100644 clean-synapse-db.yml create mode 100755 clean_synapse_db.sh diff --git a/clean-synapse-db.yml b/clean-synapse-db.yml new file mode 100644 index 0000000..5e40571 --- /dev/null +++ b/clean-synapse-db.yml @@ -0,0 +1,65 @@ + +- name: "Clean up the Synapse DB" + hosts: unhb4 + remote_user: root + + tasks: + - name: "Install jq" + ansible.builtin.apt: + pkg: + - jq + - python3-psycopg2 + + - name: "Get IP of DB container for the database synapse uses" + ansible.builtin.shell: | + docker inspect {{ synapse_db_clean_container_db }} | jq -r '.[].NetworkSettings.Networks[].IPAddress' + register: docker_network_inspect_synapse_db + + - ansible.builtin.set_fact: + postgres_ip_synapse_db: "{{ docker_network_inspect_synapse_db.stdout }}" + + - name: "Ensure Synapse is disabled before accessing the database" + ansible.builtin.shell: + cmd: "docker compose stop {{ synapse_db_clean_container_synapse }}" + chdir: "{{ synapse_db_clean_compose_file_dir }}" + tags: molecule-notest + + - name: "Delete orphan state_groups_state" + postgresql_query: + db: "{{ synapse_db_clean_db }}" + login_host: "{{ postgres_ip_synapse_db }}" + login_user: "{{ synapse_db_clean_user }}" + login_password: "{{ synapse_db_clean_password }}" + query: DELETE FROM state_groups_state WHERE room_id IN (SELECT DISTINCT(state_groups.room_id) AS room_id_gone FROM state_groups LEFT JOIN events USING(room_id) WHERE events.room_id IS NULL GROUP BY room_id_gone) + + - name: "Delete orphan state_groups_state" + postgresql_query: + db: "{{ synapse_db_clean_db }}" + login_host: "{{ postgres_ip_synapse_db }}" + login_user: "{{ synapse_db_clean_user }}" + login_password: "{{ synapse_db_clean_password }}" + query: DELETE FROM state_groups WHERE room_id IN (SELECT DISTINCT(state_groups.room_id) AS room_id_gone FROM state_groups LEFT JOIN events USING(room_id) WHERE events.room_id IS NULL GROUP BY room_id_gone) + + - name: "Reindex DB" + postgresql_query: + db: "{{ synapse_db_clean_db }}" + login_host: "{{ postgres_ip_synapse_db }}" + login_user: "{{ synapse_db_clean_user }}" + login_password: "{{ synapse_db_clean_password }}" + autocommit: yes + query: "REINDEX (VERBOSE) DATABASE {{ synapse_db_clean_db }}" + + - name: "Vacuum DB" + postgresql_query: + db: "{{ synapse_db_clean_db }}" + login_host: "{{ postgres_ip_synapse_db }}" + login_user: "{{ synapse_db_clean_user }}" + login_password: "{{ synapse_db_clean_password }}" + autocommit: yes + query: VACUUM FULL VERBOSE + + - name: "Ensure Synapse is re-enabled" + ansible.builtin.shell: + cmd: "docker compose up -d {{ synapse_db_clean_container_synapse }}" + chdir: "{{ synapse_db_clean_compose_file_dir }}" + tags: molecule-notest diff --git a/clean_synapse_db.sh b/clean_synapse_db.sh new file mode 100755 index 0000000..3fed576 --- /dev/null +++ b/clean_synapse_db.sh @@ -0,0 +1,2 @@ +#!/bin/bash +ansible-playbook clean-synapse-db.yml -i inventory.ini diff --git a/host_vars/unhb4/unhb4_vault.yml b/host_vars/unhb4/unhb4_vault.yml index 7c5ac44..c8c7638 100644 --- a/host_vars/unhb4/unhb4_vault.yml +++ b/host_vars/unhb4/unhb4_vault.yml @@ -1,50 +1,65 @@ $ANSIBLE_VAULT;1.1;AES256 -39663531646261623063306461623531383931653762613831656461313064376561336138663733 -6434313637646163656637323234613335323031363933330a373661353061373033333063336331 -31646266353135336237386163623861306463613733633964656462366634306361646365313566 -6239323762343662630a626336326137346261306363643862376636323266393362353437623161 -33356635303231346363613537396631643130313033646332323565666434373066613662353161 -37346366666663336639663664616134366264383061643336356135343631626332656466373638 -38666363336139343739353931313062613430653735376532613866313062663032613432346330 -34653139316539373131393363303736386433363734616432326430346636653762363935363032 -63633861333663626566323539366362633665666537653765313361353963343133326638643538 -62643035383936666637333135616662343436303864373665613463363834623363623366613164 -61633536613438636433653935333437653432656232633936346438323439353662373165626630 -37356333646166303837373738643465353635376531623231623937666633613165643434356535 -39353833366435396639646331666433353431353137393531653539303538313064313534376330 -32376131386530373032373235323765663836663962663262363532303866323331663130623864 -66313631646234323639613362653230363363326564633033623136643439653461373439376331 -64303162613235333639613663373833643632376238316130333764373334616531303761393861 -63303464653436393062343164306631646161333064303230366264313332303365326235383931 -34666466626266333761373837373730633030356163333538313434666238623432623965383262 -64626661323464323465643761356332343365316535343964353836346339313132333937333939 -31656338613739363633373838363862343763613061373463306161643131653264646664323966 -62653638646438336531633534336663326631303461613661383830353564363862373966343862 -61373366326366366137333765376566663638366366303264363366363838646136306330313361 -39306430333234363664383039373764636431303934323664646430303863383131623237356433 -66316434323161653764363361383563643734366635356539386561316135663736303831353264 -61346131633831613365313966626363393765376639326263643237346333363033313763376533 -32396132666164396338653439626436396661366131623234646564356232353764303931313337 -64636365313739343864346333396235623963303061396262653230353533613564636163656262 -33376663663231343266336131613634623233633733343130636133316336363433666134393166 -36633831373762373464376633613430633839333061303531376462626432333264333232356238 -30663461653461626236633865346639363536666264663036653961623737343434306133663735 -30356464343831623463393262646131336538376232323366316666386462653939336365613663 -63613264623861386332663534313562636134373736396263653336393365323665666161336634 -31656536393161653866363933356266323566666438353237656531356162636234323865386437 -62653333623738323264313331303565646533663037633837366632303136633330643132613039 -35393933613161363431643565353234393861636535643463636362306533353961656636383366 -61323366663765373339646363383435306537376336633833393238633238623338303161336332 -39656463303134623461383761613431653439343038623661363765333166363239316435646631 -37346237346466313439653263326262616130613664613332343065636130353661633662393666 -66306234346661303334383363333831323763336663383030633164623230343530383365316565 -64323732653165653535643838376264313930376632373835623866356462626630333833663366 -32623335333435333437653063393633323732636531353334613333353233633536373533326636 -33646264363136343238626133663231653238396539326462326362633839333031383432656465 -38353735623133386564626133663261643030626365303139613832306130613337663062613834 -33653962636431356534653036326434386466616438666131363834353235343762643365323330 -62383933623461383437633631633761616633653430326636393635666533373339336266383338 -64316361656330306233343330613335373164656138353964646535323762323835333265636435 -37376338653238333863383664373064323331363334353765636265663334363739363363636463 -30656331366463353639383065363234653661346330346131353838626362373634353661326163 -63623762383639633264353466323837383434653536343931653231656535343064 +36666365343065316630613363313965396532623165336164353162316430623436363830366664 +3133313530303132353661336534616338663666306632620a663461346230353038646635303031 +66333830666564316262636239643035353836313137393063376131333730343365313061333831 +3435363164666463350a373839336464666133346561333332386439393938336365636336343464 +66623136323930303133333931623738333235313130663935383639373839366265633133633735 +31316331613864633662346662346262346136633161333566623332666530393861306464393834 +32306265656562326462306232633236313335646231306662323966363265393535633039656335 +62313936653463643134363961376464373765313838363631366364633832396437353132386361 +32613563316438383033333735643634356530336264356139356230366165626364353233373061 +63326462383862373232663030326462346537353837643164663937306335653538343031343733 +37303339393337663235663362646164643135643437626466303630633732636366366538333665 +64646436343362643139323131393466653835623331393735346361653230663533353132306132 +31316435353666393537373231306361353137343236383132653163313336353332346339386630 +31393238353039323032376265366331346235346430623766663566663762356130363232336437 +32356664353330623334343130393532646465666137346162353437396162353062636335333166 +61633939663336346335383339653761343962336465666161306166646465316238666632336532 +38316532376339666539636335636230373430643464663663383939303865376665366663656263 +66633864373132653337656531623839313132303332303737623633623731613336363432616234 +37343738373461623038363535313666373032613132326163646433326164636230656563393532 +35663864323239363765373162303933363238353831363037306431656666643136313731643238 +34323635633464633266623232356364303239633933613261323864336165316538323161383037 +64333161613461333863333035626130346431663764666165383864346331323561366263363964 +63633333643162643664356365366466366233643233353434633732313335363361363564333533 +35323164633739346333623864646464623539363139396239316663333334356363373631343331 +34653730363466326632333337373531646332386438383732613832333935323833313565373139 +61323763646636373437333839623935626632663934303666306131383738633139663635643562 +65643039313637306663616266623435383733333336623261643761363030613234343030363162 +61646337346362303866336464316438316565666166666630336539663137326630383532333930 +37393662653334616263623566653836343034323962346132333834666331316139303637646534 +66353564353961343963393164643135383833666663336562353966323033356265323730343931 +39663932613261323465373837656430323265373936613561323136663263636261346638383735 +33636133396237303034373739663039663465376265356263313633386234333465316135306537 +62363865383764613062613762616339336632643938663333636661666236303138626633623637 +30363235323532383336633633626538386439663437323231353661353665346437613237643432 +37333563616139663935613863313937303865383732313835386635623036636630303562393862 +35636666393339333537373434306161366435373537353338356161633266386539326564643562 +38616332363638343338623232643461633339656164383130303037653038326365373864373166 +31303039663038396333306634653534383336626630613163373037316333373263376334383037 +64306536626333353566346261393033336430363062623530643261353065313462616166383230 +33633933643763613364653131306437653065663833663932326334613639613664333336613265 +66313539303761343336303363646463366666333465303432383031653766613935343362343536 +33306261646532643766626339333738613932616661376562383435623434613566336366306438 +37373138396538353465393536643566333135623237303663343932313962383865353233323966 +62306333383638383362666463343238333064303633623037333138646362643763623536383334 +31646334356164353731353366393039663663383939643537383663663938613735303533613235 +39333666663861373730323061313032656161323034376434363630393164353037383338346435 +61636366323562646264643663346463303535636234366130336431363538656630653534346431 +38656639633035323764623563663031333139336139353063663266343265393466373565306236 +36366337393164653264303066363533346532313030313766666637663933363036666632303663 +64346134316138616239396334623466323663623933656662626138363462653730373639643261 +39323331386164326365313365333836396636336334663865666431343736643161323561623433 +32326233653365653734326136303066323966643263313164306466303930666436383564323936 +36363261363963353938666532373264616532623733393033366230323965613432346134613462 +65646664383838376332653662393031313739343438666538376164666332653962333134306135 +65656634663766316365386366363639336430663666303935303033343039326164656166373837 +38663337373132316662663334383737643732653338373435383064316266396434623463623565 +36363134363039626232643936353631343866633931353530656234616562626330636638303537 +65363633366230633635366132656437623962396538363836316235633465383262623030353533 +61353631336465313265353363313239363538653231383336343738656635383837363665336338 +38646162393461313066666235376434303861366437353433313464323030636463396363333364 +34646333366564633034343833643562373366643263316263626363363838653563633065396633 +63616239343665326432643735313765306461333032663934393639303263303662393064353664 +32383537383338376661393638383831313263346236343635333638333666333430373936303964 +63653664356435306266