update-packages.sh 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. #! /bin/bash
  2. ######################################################################
  3. # This script sets up docker environments for supported Python versions
  4. # for updating packages in each of them.
  5. #
  6. # To upgrade, add "upgrade" as parameter.
  7. #
  8. # To execute this script, cd into the `ci` directory, then call from there.
  9. ######################################################################
  10. set -e
  11. set -x
  12. PYTHON_VERSIONS=(3.8 3.9 3.10 3.11 3.12)
  13. # check if we will upgrade or just freeze
  14. UPDATE_CMD=freeze-deps
  15. if [ "$1" == "upgrade" ]; then
  16. UPDATE_CMD=upgrade-deps
  17. echo "Going to upgrade dependencies with make $UPDATE_CMD ..."
  18. else
  19. echo "Going to freeze dependencies with make $UPDATE_CMD..."
  20. fi
  21. # Check if docker is installed
  22. if ! [ -x "$(command -v docker)" ]; then
  23. echo "Docker is not installed. Please install docker and try again."
  24. exit 1
  25. fi
  26. # Check if we can run docker without sudo (check is not needed for Macos system)
  27. if ! docker ps > /dev/null 2>&1 && [[ "$(uname)" != "Darwin" ]]; then
  28. echo "Docker is not running without sudo. Please add your user to the docker group and try again."
  29. echo "You may use the following command to do so:"
  30. echo "sudo usermod -aG docker $USER"
  31. echo "You will need to log out and log back in for this to take effect."
  32. exit 1
  33. fi
  34. SOURCE_DIR=$(pwd)/../
  35. TEMP_DIR=$(mktemp -d)
  36. # Copy the build files to the temp directory
  37. cp -r ../ci $TEMP_DIR/ci
  38. cp -r ../requirements $TEMP_DIR/requirements
  39. cp -r ../Makefile $TEMP_DIR
  40. cd $TEMP_DIR
  41. for PYTHON_VERSION in "${PYTHON_VERSIONS[@]}"
  42. do
  43. echo "Working on dependencies for Python $PYTHON_VERSION ..."
  44. # Check if container exists and remove it
  45. docker container inspect flexmeasures-update-packages-$PYTHON_VERSION > /dev/null 2>&1 && docker rm --force flexmeasures-update-packages-$PYTHON_VERSION
  46. # Build the docker image
  47. docker build --build-arg=PYTHON_VERSION=$PYTHON_VERSION -t flexmeasures-update-packages:$PYTHON_VERSION . -f ci/Dockerfile.update
  48. # Build flexmeasures
  49. # We are disabling running tests here, because we only want to update the packages. Running tests would require us to setup a postgres database,
  50. # which is not necessary for updating packages.
  51. docker run --name flexmeasures-update-packages-$PYTHON_VERSION -it flexmeasures-update-packages:$PYTHON_VERSION make $UPDATE_CMD skip-test=yes
  52. # Copy the requirements to the source directory
  53. docker cp flexmeasures-update-packages-$PYTHON_VERSION:/app/requirements/$PYTHON_VERSION $SOURCE_DIR/requirements/
  54. # Remove the container
  55. docker rm flexmeasures-update-packages-$PYTHON_VERSION
  56. # Remove the image
  57. docker rmi flexmeasures-update-packages:$PYTHON_VERSION
  58. done
  59. # Clean up docker builder cache
  60. echo "You can clean up the docker builder cache with the following command:"
  61. echo "docker builder prune --all -f"
  62. # Remove the temp directory
  63. rm -rf $TEMP_DIR
  64. # Return to the ci directory (in case you want to rerun this script)
  65. cd $SOURCE_DIR
  66. cd ci