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.

171 lines
7.2 KiB

  1. #!/bin/bash
  2. set -eux
  3. ## Certificate DST_Root_CA-X3 expired, it needs to be removed
  4. ## from list of available certificates. Debian <10 have the issue.
  5. ##
  6. ## Fixing: https://www.reddit.com/r/sysadmin/comments/pzags0/lets_encrypts_dst_root_ca_x3_expired_yesterday/
  7. ## see also: https://techcrunch.com/2021/09/21/lets-encrypt-root-expiry/?guccounter=1
  8. modified_certificate=
  9. mkdir -p /usr/local/share/ca-certificates/custom
  10. for certfile_name in isrgrootx1:ISRG_Root_X1 isrg-root-x2 lets-encrypt-r3; do
  11. certfile=${certfile_name%%:*}
  12. name=${certfile_name#*:}
  13. echo "Checking $certfile for $name"
  14. if ! [ -e "/usr/local/share/ca-certificates/custom/$certfile".crt ] &&
  15. ! [ -e "/etc/ssl/certs/$name.pem" ]; then
  16. wget --no-check-certificate https://letsencrypt.org/certs/"$certfile".pem \
  17. -O "/usr/local/share/ca-certificates/custom/$certfile".crt
  18. modified_certificate=1
  19. fi
  20. done
  21. if grep "^mozilla/DST_Root_CA_X3.crt" /etc/ca-certificates.conf 2>/dev/null 2>&1; then
  22. sed -ri 's%^(mozilla/DST_Root_CA_X3.crt)%!\1%g' /etc/ca-certificates.conf
  23. fi
  24. if [ -n "$modified_certificate" ]; then
  25. update-ca-certificates
  26. fi
  27. ## We can now do the ``apt-get update`` safely...
  28. apt:update() {
  29. tried=()
  30. backup=
  31. while ! out=$(apt-get update 2>&1); do
  32. echo "Failed to 'apt-get update', looking for fixes..."
  33. old_tried_length="${#tried[@]}"
  34. failed_fetch=$(printf "%s" "$out" | egrep "^E: Failed to fetch .*404\s+Not Found")
  35. failed_release=$(printf "%s" "$out" | egrep "^[EW]: The repository '.*' (does (no longer|not) have|no longer has) a Release file.$")
  36. changed_release=$(printf "%s" "$out" | egrep "^[EW]: Repository '.*' changed its 'Suite' value from .* to .*$")
  37. if [[ " ${tried[*]} " != *" stretch-updates-fix "* ]] &&
  38. [[ "$failed_fetch" == *" http://archive.debian.org/dists/stretch/updates/"* ]]; then
  39. tried+=("stretch-updates-fix")
  40. [ -z "$backup" ] && {
  41. backup=1
  42. echo "Backup old /etc/apt/sources.list"
  43. cp -v /etc/apt/sources.list{,.myc-update}
  44. }
  45. echo "Applying stretch-updates-fix"
  46. sed -ri 's%^(\s*deb(-src)? http://archive.debian.org/? stretch/updates .*)$%# \1%g' /etc/apt/sources.list
  47. fi
  48. for distrib in stretch buster; do
  49. for variant in "$distrib"{,-{updates,backports},/updates}; do
  50. if [[ " ${tried[*]} " != *" ${variant}-archive-fix "* ]] &&
  51. [[ "$failed_release" == *"'http://deb.debian.org/debian $variant "* ]]; then
  52. tried+=("${variant}-archive-fix")
  53. [ -z "$backup" ] && {
  54. backup=1
  55. echo "Backup old /etc/apt/sources.list"
  56. cp -v /etc/apt/sources.list{,.myc-update}
  57. }
  58. echo "Applying ${variant}-archive-fix"
  59. sed -ri 's,http://(deb|security).debian.org/debian '"$variant"',http://archive.debian.org/debian '"$variant"',g' \
  60. /etc/apt/sources.list
  61. #s,^(deb(-src)? http://deb.debian.org/debian '"$variant"'),#\1,' \
  62. fi
  63. if [[ " ${tried[*]} " != *" ${variant}-archive-comment-out "* ]] &&
  64. [[ "$failed_fetch" == *" http://archive.debian.org/debian/dists/$variant/"* ]]; then
  65. tried+=("${variant}-archive-comment-out")
  66. [ -z "$backup" ] && {
  67. backup=1
  68. echo "Backup old /etc/apt/sources.list"
  69. cp -v /etc/apt/sources.list{,.myc-update}
  70. }
  71. echo "Applying ${variant}-archive-comment-out"
  72. sed -ri 's,^(deb(-src)? http://archive.debian.org/debian '"$variant"'),#\1,g' \
  73. /etc/apt/sources.list
  74. fi
  75. done
  76. if [[ " ${tried[*]} " != *" ${variant}-security-comment-out "* ]] &&
  77. [[ "$failed_release" == *"'http://security.debian.org $distrib/updates "* ]]; then
  78. tried+=("${distrib}-comment-out")
  79. [ -z "$backup" ] && {
  80. backup=1
  81. echo "Backup old /etc/apt/sources.list"
  82. cp -v /etc/apt/sources.list{,.myc-update}
  83. }
  84. echo "Applying $distrib}-comment-out"
  85. sed -ri 's,^(deb(-src)? http://security.debian.org/ '"$distrib/updates"'),#\1,g' \
  86. /etc/apt/sources.list
  87. fi
  88. done
  89. if [[ " ${tried[*]} " != *" change-release-fix "* ]] &&
  90. [[ "$changed_release" == *"'http://deb.debian.org/debian "* ]]; then
  91. tried+=("change-release-fix")
  92. echo "Applying change-release-fix"
  93. apt-get update --allow-releaseinfo-change </dev/null || true
  94. fi
  95. if [[ "$old_tried_length" == "${#tried[@]}" ]]; then
  96. echo "Failing 'apt-get update'. Couldn't fix it automatically. Stopping."
  97. if [ -n "$backup" ]; then
  98. mv -v /etc/apt/sources.list{,.myc-update-fix}
  99. mv -v /etc/apt/sources.list{.myc-update,}
  100. fi
  101. printf "%s\n" "$out" | sed -r 's/^/ | /g'
  102. return 1
  103. fi
  104. done
  105. echo "Successful 'apt-get update'."
  106. }
  107. apt:update || exit 1
  108. apt-get -y install bash-completion wget bzip2 git-core \
  109. less tmux mosh \
  110. sudo git vim file gawk </dev/null
  111. if ! apt-get -y python-software-properties </dev/null; then
  112. if ! apt-get -y software-properties-common </dev/null; then
  113. echo "Couldn't install package, but you probably don't need it."
  114. fi
  115. fi
  116. type -p lsb_release >/dev/null 2>&1 ||
  117. apt-get install -y lsb-release </dev/null
  118. case $(lsb_release -is) in
  119. Ubuntu)
  120. apt-get install -y language-pack-en </dev/null
  121. ;;
  122. Debian)
  123. if ! type -p locale-gen >/dev/null && [ -x /usr/sbin/locale-gen ]; then
  124. echo "Your shell is incorrectly set as your PATH doesn't contain '/usr/sbin'." >&2
  125. echo "This probably happens because you've incorrectly entered root environment" >&2
  126. echo "Please use 'sudo -i' or 'su -' to enter a root shell from another user." >&2
  127. echo " ref: https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=918754"
  128. exit 1
  129. fi
  130. sed -ri 's/^\s*#\s*(en_US\.UTF-?8.*)\s*$/\1/g' /etc/locale.gen
  131. locale-gen
  132. ## Debian11 doesn't have a 'python' executable but only a
  133. ## 'python3' executable some python script don't care about
  134. ## the version, they just want a 'python' executable.
  135. if ! type -p python >/dev/null 2>&1; then
  136. if py3=$(type -p python3); then
  137. echo "No 'python' available in \$PATH, but 'python3' found, using it." >&2
  138. ln -svf "$py3" /usr/local/bin/python
  139. fi
  140. fi
  141. ;;
  142. esac
  143. YQ_VERSION=4.35.2
  144. if ! type -p "yq" 2>/dev/null ||
  145. ! version_line=$(yq --version) ||
  146. [[ "${version_line}" != *"${YQ_VERSION}" ]]; then
  147. wget "https://github.com/mikefarah/yq/releases/download/v${YQ_VERSION}/yq_linux_amd64" \
  148. -O /usr/local/bin/yq &&
  149. chmod +x /usr/local/bin/yq
  150. fi