From 406b79bec4620e340a96acf2cdc3a48c0f192582 Mon Sep 17 00:00:00 2001
From: Raphael Dannecker <rdannecker@gmail.com>
Date: Mon, 17 Jul 2023 16:27:28 +0200
Subject: [PATCH] default sync by torrent, rsync with option -d

---
 roles/lmn_vm/files/sync-vm.sh | 61 +++++++++++++++++++++++++----------
 1 file changed, 44 insertions(+), 17 deletions(-)

diff --git a/roles/lmn_vm/files/sync-vm.sh b/roles/lmn_vm/files/sync-vm.sh
index b2ab12d..61d1d26 100755
--- a/roles/lmn_vm/files/sync-vm.sh
+++ b/roles/lmn_vm/files/sync-vm.sh
@@ -4,26 +4,35 @@ set -eu
 
 show_help() {
     cat << EOF >&2
-Usage: $(basename "$0") [-d vmname] [-a] [-t]"
-The images from images.list and xml-directory will be synced from server.
+Usage: $(basename "$0") [-d] [-a] [-t] [vmnames]"
+Images from vmnames-List will be synced from server. Default by torrent.
+Using flag -d VMs will be synced by rsync
+Using flag -a images from images.list and xml-directory will be synced from server.
 Using flag -t all torrents and xml-VM-Definitions will be synced
 EOF
 }
 
-VM_DIR="/tmp/${SUDO_UID}/vmimages"
-
 download_image() {
+    rsync -av --password-file=/etc/rsync.secret \
+       "rsync://vmuser@server:/vmimages-download/${VM_NAME}.qcow2" \
+       /var/lib/libvirt/images/
+    rsync -av --password-file=/etc/rsync.secret \
+       "rsync://vmuser@server:/vmimages-download/xml/${VM_NAME}.xml" \
+       /var/lib/libvirt/images/xml/
+    rsync -av --password-file=/etc/rsync.secret \
+       "rsync://vmuser@server:/vmimages-download/${VM_NAME}.qcow2.torrent" \
+       /var/lib/libvirt/images/
+    /usr/local/bin/vmimage-torrent restart "${VM_NAME}.qcow2"
+}
+
+torrent_image() {
     if [[ -f "/var/lib/libvirt/images/${VM_NAME}.qcow2.torrent" ]]; then
       cd /var/lib/libvirt/images
       ctorrent -e 0 "${VM_NAME}.qcow2.torrent"
       /usr/local/bin/vmimage-torrent restart "${VM_NAME}.qcow2"
-    else 
-      rsync -av --password-file=/etc/rsync.secret \
-	  "rsync://vmuser@server:/vmimages-download/${VM_NAME}.qcow2" \
-	  /var/lib/libvirt/images/
-      rsync -av --password-file=/etc/rsync.secret \
-	  "rsync://vmuser@server:/vmimages-download/xml/${VM_NAME}.xml" \
-	  /var/lib/libvirt/images/xml/
+    else
+      echo "No torrent-File found"
+      exit 1
     fi
 }
 
@@ -41,21 +50,39 @@ sync_all_torrents() {
 	  /var/lib/libvirt/images/
 }
 
-while getopts ':d:at' OPTION; do
+while getopts ':dat' OPTION; do
     case "$OPTION" in
         d)
-            VM_NAME=$OPTARG
-            download_image
+            DOWNLOAD=1
             ;;
         a)
             sync_all_images
+            exit 1
             ;;
         t)
             sync_all_torrents
             ;;
         ?)
-        show_help
-        exit 1
-        ;;
+            show_help
+            exit 1
+            ;;
     esac
 done
+
+shift "$((OPTIND -1))"
+
+# if less than one arguments supplied, display usage
+if [[  $# -lt 1 ]]; then
+    show_help
+    exit 1
+fi
+
+for VM_NAME in "$@"; do
+  if [[ -v "DOWNLOAD" ]]; then
+    echo "Downloading $VM_NAME"
+    download_image
+  else
+    echo "Torrenting $VM_NAME"
+    torrent_image
+  fi
+done