manage-isos.sh 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. #!/bin/bash
  2. set -ueE
  3. usage()
  4. {
  5. echo "----------------------------------------------------------------------------------------"
  6. echo "- This script is for internal use at Vates to transfer and manage ISOs generated automatically by our Jenkins server and to store them on our PXE server and ISO SR."
  7. echo "- Usage: $0 <XCP-ng version> <Repository> <PXE Server> <Path on the PXE server> <ISO SR Server> <Path on the ISO SR Server> <Path on the PXE Server for the installer>"
  8. echo "- All options are mandatory."
  9. echo "- Repository: updates, ci, testing..."
  10. echo "- Version of XCP-ng: 8.2 or 8.3"
  11. echo "- Example: $0 8.3 updates myuser@my_pxe /my/path/pxe/ iso_user@srv-sr-iso /my/path/on/sriso/ /my/path/pxe/installers/"
  12. echo "----------------------------------------------------------------------------------------"
  13. }
  14. if [ $# -lt 7 ]; then
  15. echo "Missing parameter(s)."
  16. echo
  17. usage
  18. exit 1
  19. fi
  20. VERSION="$1"
  21. REPO="$2"
  22. PXESERVER="$3"
  23. PXEPATH="${4%/}/" # ensure there's a trailing slash
  24. ISOSR="$5"
  25. ISOSRPATH="${6%/}/"
  26. PXEPATHINST="${7%/}/"
  27. if [ "$VERSION" != "8.3" ] && [ "$VERSION" != "8.2" ]; then
  28. echo "Unsupported version. Please choose between 8.2 and 8.3."
  29. echo
  30. usage
  31. exit 1
  32. fi
  33. # get file names
  34. ISO_FILE_NAME_NI=$(ls *.iso | grep netinstall)
  35. ISO_FILE_NAME=$(ls *.iso | grep -v netinstall)
  36. # defined early so that the cleanup function doesn't fail due to undefined variable
  37. LOCAL_DIRECTORY_NAME="/tmp/${ISO_FILE_NAME%.*}"
  38. function cleanup_files () {
  39. #clean files
  40. echo "We clean the temp directory, if it exists."
  41. if [ -d "${LOCAL_DIRECTORY_NAME}" ]; then
  42. rm -Rf ${LOCAL_DIRECTORY_NAME}
  43. fi
  44. echo "We clean the img and iso files."
  45. rm -f *.img *.iso
  46. }
  47. trap cleanup_files EXIT INT
  48. set -x
  49. #scp to the pxe server
  50. echo "We're doing the scp to the pxe server for the two ISOs."
  51. scp *.iso "${PXESERVER}":"${PXEPATH}"
  52. echo "Creating or changing the '-latest' link on the PXE server."
  53. ssh "${PXESERVER}" "ln -sf '${PXEPATH}${ISO_FILE_NAME}' '${PXEPATH}xcp-ng-${VERSION}-${REPO}-latest'"
  54. echo "Cleaning ISOs older than 10 days on the pxe."
  55. ssh "${PXESERVER}" "find '${PXEPATH}' -mtime +9 -name *nightly*.iso -delete"
  56. # For ci and updates repos, uncompress to an internal netinstall repo on PXE server
  57. if [ "$REPO" == "ci" ] || [ "$REPO" == "updates" ]; then
  58. echo "Repo: ${REPO}: copy the iso content (${ISO_FILE_NAME}) into a directory of the pxe server."
  59. DISTANT_DIRECTORY_NAME="${PXEPATHINST}${VERSION}-${REPO}/"
  60. mkdir "${LOCAL_DIRECTORY_NAME}"
  61. 7z x -o"${LOCAL_DIRECTORY_NAME}/" "${ISO_FILE_NAME}"
  62. if [ ! -z "${DISTANT_DIRECTORY_NAME}" ]; then
  63. rsync -av --delete "${LOCAL_DIRECTORY_NAME}/" "${PXESERVER}":"${DISTANT_DIRECTORY_NAME}"
  64. ssh "${PXESERVER}" "chmod -R 755 '${DISTANT_DIRECTORY_NAME}'"
  65. fi
  66. fi
  67. # Create a hardlink for a monthly ISO each month. We keep it three months.
  68. MONTH=$(date +%Y%m)
  69. THEDATE=$(date +%Y%m%d)
  70. MONTHLY_ISO_NAME="${ISO_FILE_NAME/nightly/monthly}"
  71. MONTHLY_ISO_NAME="${MONTHLY_ISO_NAME/${THEDATE}/${MONTH}}"
  72. if ssh -q "${PXESERVER}" [[ ! -f "${PXEPATH}${MONTHLY_ISO_NAME}" ]]; then
  73. ssh "${PXESERVER}" "ln '${PXEPATH}${ISO_FILE_NAME}' '${PXEPATH}${MONTHLY_ISO_NAME}'"
  74. ssh "${PXESERVER}" "find '${PXEPATH}' -type f -mtime +182 -name '*monthly*.iso' -delete"
  75. fi
  76. # scp to the ISO SR
  77. echo "Copy the ISO files to the ISO SR."
  78. scp *.iso "${ISOSR}":"${ISOSRPATH}"
  79. ssh "${ISOSR}" "find '${ISOSRPATH}' -mtime +2 -name '*nightly*.iso' -delete"