This commit is contained in:
Ludwig Behm 2024-07-17 22:55:20 +02:00
parent 92236209d1
commit e56bea4b0f
Signed by: l.behm
GPG key ID: D344835D63B89384
4 changed files with 102 additions and 0 deletions

39
cleanup.d/delete_devices Executable file
View file

@ -0,0 +1,39 @@
#!/bin/bash
token_path="$1"
base_url="$2"
verbose="true"
warn() {
echo "WARN: $@" >&2
}
log() {
if [[ "$verbose" == "true" ]]; then
echo "LOG: $@" >&2
fi
}
invoke_req() {
log "invoke curl $@"
curl -s --header "@${token_path}" --max-time 600 --connect-timeout 600 $@
return $?
}
ec=0
log "starting device cleanup - api url: ${base_url}"
for user_id in `invoke_req -X GET "${base_url}v2/users" | jq -r '.users[]|.name'`; do
log "processing user ${user_id}"
for device in `invoke_req -X GET "${base_url}v2/users/${user_id}/devices" | jq -r '.devices|sort_by(.last_seen_ts)[0:-5][]|.device_id'`; do
warn "deleting device ${user_id}/${device}"
#resp=$(invoke_req -X DELETE "${base_url}v2/users/${user_id}/devices/${device}")
#rc=$?
#if [[ "$resp" != "{}" ]]; then
# ec=$rc
# echo "Deletion of device '${user_id}/devices/${device}' failed:" >&2
# echo "$resp" | jq '.' >&2
#fi
done
done
exit $ec

32
cleanup.d/purge_media_cache Executable file
View file

@ -0,0 +1,32 @@
#!/bin/bash
token_path="$1"
base_url="$2"
verbose="true"
warn() {
echo "WARN: $@" >&2
}
log() {
if [[ "$verbose" == "true" ]]; then
echo "LOG: $@" >&2
fi
}
invoke_req() {
log "invoke curl $@"
curl -s --header "@${token_path}" --max-time 600 --connect-timeout 600 $@
return $?
}
log "starting media purge - api url: ${base_url}"
cutoff_ts="$(date -d '-9 months' '+%s')000"
response="$(invoke_req -X POST --data-raw '{}' "${base_url}v1/purge_media_cache?before_ts=${cutoff_ts}")"
rc=$?
if [[ "$(echo "$response" | jq -r '.deleted')" == "null" ]]; then
warn "purge failed: $response"
fi
exit $rc

8
install.sh Executable file
View file

@ -0,0 +1,8 @@
#!/bin/bash
BASEDIR=$(dirname $0)
install --owner=0 --group=0 --mode=444 $BASEDIR/matrix-synapse-maintenance.service /etc/systemd/system/
mkdir -p /etc/matrix-synapse/cleanup.d
install --owner=0 --group=0 --mode=555 $BASEDIR/cleanup.d/* /etc/matrix-synapse/cleanup.d/

View file

@ -0,0 +1,23 @@
[Unit]
Description=cleanup tasks for the matrix synapse service
After=matrix-synapse.service
BindsTo=matrix-synapse.service
Requisite=matrix-synapse.service
# Please create a token file
ConditionPathExists=/etc/matrix-synapse/cleanup.token
[Service]
Type=simple
LoadCredential=token_header:/etc/matrix-synapse/cleanup.token
ExecStart=/usr/bin/run-parts --verbose --arg=%d/token_header --arg=http://127.0.0.1:8008/_synapse/admin/ -- /etc/matrix-synapse/cleanup.d
# Matrix user isn't needed for the current scripts
# User=matrix-synapse
# Group=matrix-synapse
DynamicUser=yes
ProtectSystem=strict
ConfigurationDirectory=matrix-synapse/cleanup.d