diff options
author | Christian Segundo | 2023-10-06 19:34:59 +0200 |
---|---|---|
committer | Christian Segundo | 2023-10-06 19:34:59 +0200 |
commit | 66cba1261e4e734cd0571351c0bbba6a305099d8 (patch) | |
tree | 7a726e978e9f8869e8bca79ebf37397a830b5e5c | |
parent | 02b189653a1f4a3b98fc200e2280c86aa31eda5c (diff) | |
parent | 3c0cd71bc9b7ec681a2ff373c1385cca53a9a3bf (diff) | |
download | transmission-hacks-66cba1261e4e734cd0571351c0bbba6a305099d8.tar.gz |
Merge remote-tracking branch 'origin/feat/delete-if-unlinked'
-rw-r--r-- | Jenkinsfile | 8 | ||||
-rw-r--r-- | scripts/cron.sh | 48 | ||||
-rw-r--r-- | scripts/delete-if-unlinked.py | 53 | ||||
-rw-r--r-- | scripts/entrypoint.sh | 14 |
4 files changed, 107 insertions, 16 deletions
diff --git a/Jenkinsfile b/Jenkinsfile index 564d41f..055bef6 100644 --- a/Jenkinsfile +++ b/Jenkinsfile @@ -20,6 +20,7 @@ pipeline { env.TAG_NAME = sh( returnStdout: true, script: 'git name-rev --name-only --tags HEAD | sed \'s/^undefined$//\'').trim() + env.BRANCH_DOCKER_TAG = "${env.GIT_BRANCH}".replace("/", "-") } } } @@ -47,7 +48,6 @@ pipeline { } } stage('docker.io login') { - when { branch 'master' } steps { withCredentials([string( credentialsId: 'dockerhub-personal', @@ -62,6 +62,12 @@ pipeline { } } } + stage('Push branch') { + when { not { branch 'master' } } + steps { + sh "buildah manifest push --all $PROJECT docker://$IMAGE_NAME:$BRANCH_DOCKER_TAG" + } + } stage('Push latest') { when { branch 'master' } steps { sh "buildah manifest push --all $PROJECT docker://$IMAGE_NAME:latest" } diff --git a/scripts/cron.sh b/scripts/cron.sh index 3cde994..d57db7e 100644 --- a/scripts/cron.sh +++ b/scripts/cron.sh @@ -4,25 +4,43 @@ set -euo pipefail declare -A tag_age i=0 while true; do - key="DELETE_TAG_${i}" - val="DELETE_AGE_${i}" - if [ -z "${!key:-}" ]; then - break - fi - tag_age["${!key}"]="${!val}" - i=$((i+1)) + key="DELETE_TAG_${i}" + val="DELETE_AGE_${i}" + if [ -z "${!key:-}" ]; then + break + fi + tag_age["${!key}"]="${!val}" + i=$((i+1)) +done + +declare -A unlinked_tag_age +i=0 +while true; do + key="DELETE_UNLINKED_TAG_${i}" + val="DELETE_UNLINKED_AGE_${i}" + if [ -z "${!key:-}" ]; then + break + fi + unlinked_tag_age["${!key}"]="${!val}" + i=$((i+1)) done ${PYTHON_PATH} "${SCRIPT_PREFIX}"/tag-based-on-dir.py \ - --host "${TRANSMISSION_HOST}" + --host "${TRANSMISSION_HOST}" ${PYTHON_PATH} "${SCRIPT_PREFIX}"/ask-tracker-for-more-peers.py \ - --host "${TRANSMISSION_HOST}" + --host "${TRANSMISSION_HOST}" + +for key in "${!tag_age[@]}"; do + ${PYTHON_PATH} "${SCRIPT_PREFIX}"/delete-based-on-tag.py \ + --host "${TRANSMISSION_HOST}" \ + --tag "${key}" \ + --age "${tag_age[$key]}" +done -for key in "${!tag_age[@]}" -do - ${PYTHON_PATH} "${SCRIPT_PREFIX}"/delete-based-on-tag.py \ - --host "${TRANSMISSION_HOST}" \ - --tag "${key}" \ - --age "${tag_age[$key]}" +for key in "${!unlinked_tag_age[@]}"; do + ${PYTHON_PATH} "${SCRIPT_PREFIX}"/delete-if-unlinked.py \ + --host "${TRANSMISSION_HOST}" \ + --tag "${key}" \ + --min-age "${unlinked_tag_age[$key]}" done diff --git a/scripts/delete-if-unlinked.py b/scripts/delete-if-unlinked.py new file mode 100644 index 0000000..54df8ff --- /dev/null +++ b/scripts/delete-if-unlinked.py @@ -0,0 +1,53 @@ +#!/usr/bin/env python3 +import click +import os +from transmission_rpc import Client +from datetime import timedelta, datetime + + +def convert_to_seconds(s): + units = {"s": "seconds", "m": "minutes", + "h": "hours", "d": "days", "w": "weeks"} + count = int(s[:-1]) + unit = units[s[-1]] + td = timedelta(**{unit: count}) + return td.seconds + 60 * 60 * 24 * td.days + +def has_links(torrent): + for file in torrent.get_files(): + file_path = torrent.download_dir + '/' + file.name + if not os.path.exists(file_path): + continue + if os.stat(file_path).st_nlink > 1: + return True + return False + + +@click.command() +@click.option('--port', default=9091) +@click.option('--host', default="localhost") +@click.option('--tag', required=True) +@click.option('--min-age', default='1w') +def main(host, port, tag, min_age): + """ Deletes torrents older than the specified age if no file has a link. """ + + c = Client(host=host, port=port) + torrents = c.get_torrents() + for torrent in torrents: + if tag not in torrent.labels: + continue + + specified_age = convert_to_seconds(min_age) + age_in_seconds = int((datetime.today().timestamp() - + torrent.done_date.timestamp())) + + if age_in_seconds < specified_age: + continue + + if not has_links(torrent): + print(f"Deleting {torrent.name}") + c.remove_torrent(torrent.id, delete_data=True) + + +if __name__ == '__main__': + main() diff --git a/scripts/entrypoint.sh b/scripts/entrypoint.sh index 4d89be5..e40b462 100644 --- a/scripts/entrypoint.sh +++ b/scripts/entrypoint.sh @@ -22,6 +22,20 @@ EOF i=$((i+1)) done +i=0 +while true; do + key="DELETE_UNLINKED_${i}_NAME" + val="DELETE_UNLINKED_${i}_AGE" + if [ -z "${!key:-}" ]; then + break + fi + cat <<EOF >> /etc/cron.d/crontab +DELETE_UNLINKED_TAG_${i}=${!key} +DELETE_UNLINKED_AGE_${i}=${!val} +EOF + i=$((i+1)) +done + echo "${CRON_EXPRESSION:-* * * * *} /bin/bash /scripts/cron.sh >/proc/1/fd/1 2>/proc/1/fd/2" >> /etc/cron.d/crontab cat /etc/cron.d/crontab crontab /etc/cron.d/crontab |