upgrade.sh 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. #!/bin/bash
  2. # This script will prepare NetBox to run after the code has been upgraded to
  3. # its most recent release.
  4. #
  5. # Once the script completes, remember to restart the WSGI service (e.g.
  6. # gunicorn or uWSGI).
  7. # Determine which version of Python/pip to use. Default to v3 (if available)
  8. # but allow the user to force v2.
  9. PYTHON="python3"
  10. PIP="pip3"
  11. type $PYTHON >/dev/null 2>&1 && type $PIP >/dev/null 2>&1 || PYTHON="python" PIP="pip"
  12. while getopts ":2" opt; do
  13. case $opt in
  14. 2)
  15. PYTHON="python"
  16. PIP="pip"
  17. echo "Forcing Python/pip v2"
  18. ;;
  19. \?)
  20. echo "Invalid option: -$OPTARG" >&2
  21. exit
  22. ;;
  23. esac
  24. done
  25. # Optionally use sudo if not already root, and always prompt for password
  26. # before running the command
  27. PREFIX="sudo -k "
  28. if [ "$(whoami)" = "root" ]; then
  29. # When running upgrade as root, ask user to confirm if they wish to
  30. # continue
  31. read -n1 -rsp $'Running NetBox upgrade as root, press any key to continue or ^C to cancel\n'
  32. PREFIX=""
  33. fi
  34. # Delete stale bytecode
  35. COMMAND="${PREFIX}find . -name \"*.pyc\" -delete"
  36. echo "Cleaning up stale Python bytecode ($COMMAND)..."
  37. eval $COMMAND
  38. # Uninstall any Python packages which are no longer needed
  39. COMMAND="${PREFIX}${PIP} uninstall -r old_requirements.txt -y"
  40. echo "Removing old Python packages ($COMMAND)..."
  41. eval $COMMAND
  42. # Install any new Python packages
  43. COMMAND="${PREFIX}${PIP} install -r requirements.txt --upgrade"
  44. echo "Updating required Python packages ($COMMAND)..."
  45. eval $COMMAND
  46. # Apply any database migrations
  47. COMMAND="${PYTHON} netbox/manage.py migrate"
  48. echo "Applying database migrations ($COMMAND)..."
  49. eval $COMMAND
  50. # Collect static files
  51. COMMAND="${PYTHON} netbox/manage.py collectstatic --no-input"
  52. echo "Collecting static files ($COMMAND)..."
  53. eval $COMMAND