From 2078fab902701cdf4551fc6f0abdcee24b9a4f84 Mon Sep 17 00:00:00 2001 From: Raphael Dannecker Date: Mon, 4 Sep 2023 11:37:59 +0200 Subject: [PATCH] solve conflicts with multiple parallel ctorrents --- roles/lmn_vm/files/sync-vm.sh | 35 ++++++++++++++++++++++++++++++----- 1 file changed, 30 insertions(+), 5 deletions(-) diff --git a/roles/lmn_vm/files/sync-vm.sh b/roles/lmn_vm/files/sync-vm.sh index e0ee196..195ab92 100755 --- a/roles/lmn_vm/files/sync-vm.sh +++ b/roles/lmn_vm/files/sync-vm.sh @@ -23,14 +23,39 @@ download_image() { } torrent_image() { - if [[ -f "/lmn/vm/${VM_NAME}.qcow2.torrent" ]]; then - cd /lmn/vm - ctorrent -e 0 "${VM_NAME}.qcow2.torrent" - /usr/local/bin/vmimage-torrent restart "${VM_NAME}.qcow2" - else + if [[ ! -f "/lmn/vm/${VM_NAME}.qcow2.torrent" ]]; then echo "No torrent-File found" exit 1 fi + lockfile="/tmp/sync-vm-${VM_NAME}.lock" + if ! flock -n "$lockfile" echo "try to acquire lock"; then + echo torrent seems to be in process. + echo waiting for completion ... + flock -w 3600 "$lockfile" echo "...completed" + sleep 5 + else + ( + if ! flock -n 200; then + echo "failed to acquire lock" + echo "Bitte noch einmal starten." + echo "Beliebige Taste zum Beenden." + read -n 1 + exit 1 + fi + torrent="${VM_NAME}.qcow2.torrent" + session="${torrent//./_}" + if vmimage-torrent status | grep -qw ^"$session"; then + vmimage-torrent stop "${VM_NAME}.qcow2" + fi + cd /lmn/vm + ctorrent -e 0 "${VM_NAME}.qcow2.torrent" + /usr/local/bin/vmimage-torrent restart "${VM_NAME}.qcow2" + if ! flock -u 200; then + echo failed to drop lock + exit 1 + fi + ) 200>"$lockfile" + fi } sync_all_images() {