upgrade.sh 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. #!/bin/bash
  2. # This script will prepare NetBox to run after the code has been upgraded to
  3. # its most recent release.
  4. # This script will invoke Python with the value of the PYTHON environment
  5. # variable (if set), or fall back to "python3". Note that NetBox v3.0+ requires
  6. # Python 3.7 or later.
  7. cd "$(dirname "$0")"
  8. VIRTUALENV="$(pwd -P)/venv"
  9. PYTHON="${PYTHON:-python3}"
  10. # Remove the existing virtual environment (if any)
  11. if [ -d "$VIRTUALENV" ]; then
  12. COMMAND="rm -rf ${VIRTUALENV}"
  13. echo "Removing old virtual environment..."
  14. eval $COMMAND
  15. else
  16. WARN_MISSING_VENV=1
  17. fi
  18. # Create a new virtual environment
  19. COMMAND="${PYTHON} -m venv ${VIRTUALENV}"
  20. echo "Creating a new virtual environment at ${VIRTUALENV}..."
  21. eval $COMMAND || {
  22. echo "--------------------------------------------------------------------"
  23. echo "ERROR: Failed to create the virtual environment. Check that you have"
  24. echo "the required system packages installed and the following path is"
  25. echo "writable: ${VIRTUALENV}"
  26. echo "--------------------------------------------------------------------"
  27. exit 1
  28. }
  29. # Activate the virtual environment
  30. source "${VIRTUALENV}/bin/activate"
  31. # Upgrade pip
  32. COMMAND="pip install --upgrade pip"
  33. echo "Updating pip ($COMMAND)..."
  34. eval $COMMAND || exit 1
  35. pip -V
  36. # Install necessary system packages
  37. COMMAND="pip install wheel"
  38. echo "Installing Python system packages ($COMMAND)..."
  39. eval $COMMAND || exit 1
  40. # Install required Python packages
  41. COMMAND="pip install -r requirements.txt"
  42. echo "Installing core dependencies ($COMMAND)..."
  43. eval $COMMAND || exit 1
  44. # Install optional packages (if any)
  45. if [ -s "local_requirements.txt" ]; then
  46. COMMAND="pip install -r local_requirements.txt"
  47. echo "Installing local dependencies ($COMMAND)..."
  48. eval $COMMAND || exit 1
  49. elif [ -f "local_requirements.txt" ]; then
  50. echo "Skipping local dependencies (local_requirements.txt is empty)"
  51. else
  52. echo "Skipping local dependencies (local_requirements.txt not found)"
  53. fi
  54. # Test schema migrations integrity
  55. COMMAND="python3 netbox/manage.py showmigrations"
  56. eval $COMMAND > /dev/null 2>&1 || {
  57. echo "--------------------------------------------------------------------"
  58. echo "ERROR: Database schema migrations are out of synchronization. (No"
  59. echo "data has been lost.) If attempting to upgrade to NetBox v3.0 or"
  60. echo "later, first upgrade to a v2.11 release to ensure schema migrations"
  61. echo "have been correctly prepared. For further detail on the exact error,"
  62. echo "run the following commands:"
  63. echo ""
  64. echo " source ${VIRTUALENV}/bin/activate"
  65. echo " ${COMMAND}"
  66. echo "--------------------------------------------------------------------"
  67. exit 1
  68. }
  69. # Apply any database migrations
  70. COMMAND="python3 netbox/manage.py migrate"
  71. echo "Applying database migrations ($COMMAND)..."
  72. eval $COMMAND || exit 1
  73. # Trace any missing cable paths (not typically needed)
  74. COMMAND="python3 netbox/manage.py trace_paths --no-input"
  75. echo "Checking for missing cable paths ($COMMAND)..."
  76. eval $COMMAND || exit 1
  77. # Build the local documentation
  78. COMMAND="mkdocs build"
  79. echo "Building documentation ($COMMAND)..."
  80. eval $COMMAND || exit 1
  81. # Collect static files
  82. COMMAND="python3 netbox/manage.py collectstatic --no-input"
  83. echo "Collecting static files ($COMMAND)..."
  84. eval $COMMAND || exit 1
  85. # Delete any stale content types
  86. COMMAND="python3 netbox/manage.py remove_stale_contenttypes --no-input"
  87. echo "Removing stale content types ($COMMAND)..."
  88. eval $COMMAND || exit 1
  89. # Delete any expired user sessions
  90. COMMAND="python3 netbox/manage.py clearsessions"
  91. echo "Removing expired user sessions ($COMMAND)..."
  92. eval $COMMAND || exit 1
  93. if [ -v WARN_MISSING_VENV ]; then
  94. echo "--------------------------------------------------------------------"
  95. echo "WARNING: No existing virtual environment was detected. A new one has"
  96. echo "been created. Update your systemd service files to reflect the new"
  97. echo "Python and gunicorn executables. (If this is a new installation,"
  98. echo "this warning can be ignored.)"
  99. echo ""
  100. echo "netbox.service ExecStart:"
  101. echo " ${VIRTUALENV}/bin/gunicorn"
  102. echo ""
  103. echo "netbox-rq.service ExecStart:"
  104. echo " ${VIRTUALENV}/bin/python"
  105. echo ""
  106. echo "After modifying these files, reload the systemctl daemon:"
  107. echo " > systemctl daemon-reload"
  108. echo "--------------------------------------------------------------------"
  109. fi
  110. echo "Upgrade complete! Don't forget to restart the NetBox services:"
  111. echo " > sudo systemctl restart netbox netbox-rq"