fork 0k-charms
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.

113 lines
4.5 KiB

  1. #!/bin/bash
  2. ## Set NO_DOCKER_RESTART to prevent restarting docker after upgrade.
  3. ## Set ALLOW_DOCKER_CHANGE to allow change your docker version.
  4. ## Set TARGET_DOCKER_VERSION to force a version (use cautiously!)
  5. distro=$(lsb_release -is)
  6. release=$(lsb_release -rs)
  7. if [ -z "$TARGET_DOCKER_VERSION" ]; then
  8. case "$distro" in
  9. Debian)
  10. case "$release" in
  11. 10|11|12) target_version=24;;
  12. *) target_version=19;;
  13. esac
  14. ;;
  15. Ubuntu)
  16. case "$release" in
  17. 22.04) target_version=24;;
  18. *) target_version=19;;
  19. esac
  20. ;;
  21. *) target_version=17;;
  22. esac
  23. else
  24. target_version="$TARGET_DOCKER_VERSION"
  25. fi
  26. just_installed=
  27. if ! type -p docker >/dev/null; then
  28. echo "Installing docker..."
  29. type -p curl >/dev/null ||
  30. apt-get install -y curl </dev/null
  31. curl -sSL https://get.docker.io | sh || exit 1
  32. just_installed=1
  33. fi
  34. read docker_client_version docker_server_version < <(
  35. docker version --format '{{.Client.Version}} {{.Server.Version}}'
  36. )
  37. mismatch_client_server=
  38. ## issue a warning if client and server versions are different
  39. if [ "$docker_client_version" != "$docker_server_version" ]; then
  40. echo "WARNING: docker client and server versions differ!" >&2
  41. echo " client: $docker_client_version" >&2
  42. echo " server: $docker_server_version" >&2
  43. mismatch_client_server=1
  44. fi
  45. distrib_available_version="$(apt-cache madison docker-ce |
  46. cut -f 2 -d \| |
  47. grep "$target_version" |
  48. head -n 1 | xargs echo)"
  49. cleaned_distrib_available_version="${distrib_available_version%%~*}"
  50. cleaned_distrib_available_version="${distrib_available_version%-*}"
  51. cleaned_distrib_available_version="${cleaned_distrib_available_version#*:}"
  52. if [ -z "$mismatch_client_server" ] &&
  53. [[ "$docker_client_version" == "$cleaned_distrib_available_version" ]]; then
  54. ## current and target versions match, no upgrade needed
  55. echo "INFO: recommended target docker version $docker_client_version already installed" >&2
  56. echo " To set a different target version, set TARGET_DOCKER_VERSION" >&2
  57. exit 0
  58. fi
  59. ## Should we skip upgrade?
  60. if [ -z "$just_installed" ] && [ -z "$ALLOW_DOCKER_CHANGE" ]; then
  61. if [ -n "$distrib_available_version" ]; then
  62. if [ -z "$mismatch_client_server" ]; then
  63. ## get current version for docker-ce debian package installed
  64. current_debian_package_version="$(dpkg -s docker-ce | grep '^Version:' | cut -f 2 -d ' ')"
  65. echo "INFO: docker recommended version available (current: $current_debian_package_version => recommended: $distrib_available_version)" >&2
  66. else
  67. current_docker_ce_package_version="$(dpkg -s docker-ce | grep '^Version:' | cut -f 2 -d ' ')"
  68. current_docker_ce_cli_package_version="$(dpkg -s docker-ce-cli | grep '^Version:' | cut -f 2 -d ' ')"
  69. echo "WARNING: docker recommended version available (recommended: $distrib_available_version)" >&2
  70. echo " And your client ($current_docker_ce_cli_package_version) and server ($current_docker_ce_package_version) versions differ." >&2
  71. fi
  72. echo " To change version, run this script with ALLOW_DOCKER_CHANGE=1" >&2
  73. else
  74. echo "WARNING: your docker version does match target recommended version" >&2
  75. echo " And your distribution does not package target version." >&2
  76. echo " - Your distribution: $distro $release" >&2
  77. echo " - Current docker version: $docker_client_version" >&2
  78. echo " - Target recommended version: ${target_version}*" >&2
  79. fi
  80. exit 0
  81. fi
  82. if [ -z "$distrib_available_version" ]; then
  83. echo "ERROR: no docker version matching '${target_version}*' available for your distribution" >&2
  84. echo " Your distribution: $distro $release" >&2
  85. echo " Available versions:" >&2
  86. apt-cache madison docker-ce | cut -f 2 -d \| | sed -r 's/^/ -/g' >&2
  87. echo
  88. echo " 1) You may need to contact your system administrator to upgrade this script." >&2
  89. echo " 2) Meanwhile, if you know what you are doing, you can set yourself a target version" >&2
  90. echo " by setting TARGET_DOCKER_VERSION." >&2
  91. exit 1
  92. fi
  93. apt-get install -y --allow-downgrades \
  94. docker-ce="$distrib_available_version" \
  95. docker-ce-cli="$distrib_available_version" </dev/null &&
  96. if [ -z "$NO_DOCKER_RESTART" ]; then
  97. systemctl daemon-reload &&
  98. service docker restart
  99. fi