qvm-template-repo-query 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. #!/usr/bin/bash
  2. OPTS=()
  3. SPEC=
  4. while IFS= read -r line; do
  5. if [ "$line" = "---" ]; then
  6. break
  7. fi
  8. case "$line" in
  9. --enablerepo=*|\
  10. --disablerepo=*|\
  11. --repoid=*|\
  12. --releasever=*|\
  13. --refresh)
  14. OPTS+=("$line")
  15. ;;
  16. *)
  17. SPEC="$line"
  18. ;;
  19. esac
  20. done
  21. repodir=$(mktemp -d)
  22. cat > "$repodir/template.repo"
  23. OPTS+=("--setopt=reposdir=${repodir}")
  24. OPTS+=("--quiet")
  25. # This creates the hashfile if it doesn't exist, and keep the ctime and mtime
  26. # unchanged otherwise.
  27. # We then copy the {c,m}time to the repo config.
  28. # This allows DNF caching to work properly.
  29. hashfile="/tmp/qvm-template-$(b2sum "$repodir/template.repo" | cut -f1 -d' ')"
  30. touch -a "$hashfile"
  31. touch -r "$hashfile" "$repodir/template.repo"
  32. RET=0
  33. if [ "$1" = "query" ]; then
  34. dnf repoquery "${OPTS[@]}" --qf='%{name}|%{epoch}|%{version}|%{release}|%{reponame}|%{downloadsize}|%{buildtime}|%{license}|%{url}|%{summary}|%{description}|' "$SPEC"
  35. RET="$?"
  36. elif [ "$1" = "download" ]; then
  37. url="$(dnf download "${OPTS[@]}" --url "$SPEC" | shuf -n 1)"
  38. curl --silent -L "$url" -o -
  39. RET="$?"
  40. fi
  41. rm -r "$repodir"
  42. exit "$RET"