Automatically update the SmartOS platform served for netbooting
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

60 lines
1.8 KiB

  1. #!/usr/bin/env bash
  2. # SmartOS platform image updater for netboot environments | tamas@gerczei.eu
  3. ME=$(basename ${0%.sh})
  4. DESTDIR="/var/tmp/"
  5. LOCATION="us-east"
  6. FILENAME="platform-latest.tgz"
  7. TFTPROOT="/var/lib/tftpboot"
  8. URL="https://${LOCATION}.manta.joyent.com/Joyent_Dev/public/SmartOS/${FILENAME}"
  9. REPLY=$(curl ${URL} -z ${DESTDIR:-/var/tmp}/${FILENAME} -o ${DESTDIR:-/var/tmp}/${FILENAME} -s -L -w %{http_code} -C - 2>/dev/null)
  10. case $REPLY in
  11. 200)
  12. # OK, new image downloaded
  13. # extract it
  14. tar xf ${DESTDIR:-/var/tmp}/${FILENAME} -C ${TFTPROOT}/smartos --transform 's!^platform-!!' 2>/dev/null
  15. if [ $? -ne 0 ]
  16. then
  17. # failed to extract, force the process to repeat next time and bail out
  18. rm ${DESTDIR:-/var/tmp}/${FILENAME}
  19. exit 1
  20. fi
  21. # determine which one it is
  22. shopt -s extglob
  23. LASTDIR="$(ls -dt ${TFTPROOT}/smartos/+([0-9])T+([0-9])Z | head -1)"
  24. # re-organize it slightly
  25. mkdir ${LASTDIR}/platform
  26. mv ${LASTDIR}/i86pc ${LASTDIR}/platform
  27. # set ownership
  28. chown -R tftp:tftp ${LASTDIR}
  29. # generate iPXE configuration
  30. TODAY="$(date '+%d%m%y')"
  31. if [ -f ${TFTPROOT}/smartos.ipxe ]
  32. then
  33. # secure a copy of the previous configuration file
  34. cp ${TFTPROOT}/smartos.ipxe ${TFTPROOT}/smartos.ipxe.${TODAY}
  35. fi
  36. sed -e "s/\$release/$(basename ${LASTDIR})/g" < ${TFTPROOT}/smartos.ipxe.tpl > ${TFTPROOT}/smartos.ipxe
  37. # housekeeping and logging
  38. logger -t $ME -p user.debug created ${LASTDIR} and ${TFTPROOT}/smartos.ipxe
  39. logger -t $ME -p user.debug removed $(find ${TFTPROOT}/smartos -maxdepth 1 -type d -mtime +${KEEPDAYS:-28} -printf '%f ' -exec rm -r {} \;)
  40. ;;
  41. 304)
  42. # NOT MODIFIED, no update
  43. ;;
  44. *)
  45. # WTF
  46. echo "server returned $REPLY, I cannot handle that"
  47. exit 1
  48. ;;
  49. esac
  50. exit 0