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.

157 lines
5.7 KiB

6 years ago
6 years ago
6 years ago
  1. #!/bin/bash
  2. . /etc/shlib
  3. include parse
  4. include common
  5. include pretty
  6. MIN_DISK_SPACE="${MIN_DISK_SPACE:-300M}"
  7. ## convert human size to bytes using numfmt
  8. ## Check remaining disk space
  9. if [ -n "$MIN_DISK_SPACE" ]; then
  10. min_disk_space_kbytes=$(numfmt --from=iec --to-unit=1024 "$MIN_DISK_SPACE") || {
  11. err "Invalid format for '\$MIN_DISK_SPACE'."
  12. exit 1
  13. }
  14. if ! remaining_kbytes=$(df / | awk 'NR==2 {print $4}'); then
  15. err "Failed to get remaining disk space."
  16. exit 1
  17. fi
  18. if [ "$remaining_kbytes" -lt "$min_disk_space_kbytes" ]; then
  19. err "Not enough disk space."
  20. human_min_dist_space=$(numfmt --to=iec --format="%.2f" --from-unit=1024 "$min_disk_space_kbytes") || {
  21. err "Failed to convert '\$MIN_DISK_SPACE' to human readable format."
  22. exit 1
  23. }
  24. human_remaining_kbytes=$(numfmt --to=iec --format="%.2f" --from-unit=1024 "$remaining_kbytes") || {
  25. err "Failed to convert '\$remaining_kbytes' to human readable format."
  26. exit 1
  27. }
  28. echo " - At least $human_min_dist_space are required." >&2
  29. echo " - Only $human_remaining_kbytes are available." >&2
  30. exit 1
  31. fi
  32. fi
  33. start=$SECONDS
  34. if [ -z "$NO_UPDATE" -a -d "/opt/apps/myc-manage" ]; then
  35. MYC_UPDATE_VERSION="${MYC_UPDATE_VERSION:-master}"
  36. Elt "Checking if myc-manage requires update..."
  37. cd /opt/apps/myc-manage
  38. REMOTE_HEAD="$(git ls-remote origin "refs/heads/${MYC_UPDATE_VERSION}" 2>/dev/null | cut -f 1)"
  39. if [ -z "$REMOTE_HEAD" ]; then
  40. err "Can't find remote branch '$MYC_UPDATE_VERSION'."
  41. echo " - Either this branch is not available on 'origin' remote." >&2
  42. echo " - Either 'origin' remote is not correctly set." >&2
  43. exit 1
  44. fi
  45. HEAD="$(git rev-parse HEAD)"
  46. if [ "$REMOTE_HEAD" != "$HEAD" ]; then
  47. print_info "new version available"
  48. Wrap -d "Update myc-manage" <<EOF || exit 1
  49. if ! [ -d "/opt/apps/myc-manage" ]; then
  50. mkdir -p /opt/apps && cd /opt/apps
  51. git clone https://git.myceliandre.fr/Myceliandre/myc-manage.git -b "$MYC_UPDATE_VERSION"
  52. else
  53. cd /opt/apps/myc-manage &&
  54. git checkout "$MYC_UPDATE_VERSION" &&
  55. git pull -r || exit 1
  56. fi
  57. ln -sfn /opt/apps/myc-manage/bin/* /usr/local/sbin/
  58. find -L /usr/local/sbin -maxdepth 1 -type l -ilname /opt/apps/myc-manage/bin/\* -delete
  59. EOF
  60. Feed || exit 1
  61. export NO_UPDATE=1
  62. exec myc-update
  63. exit 0
  64. else
  65. print_info "up to date"
  66. Feedback noop
  67. fi
  68. fi
  69. Wrap -d "Updating 0k-charms" <<EOF || exit 1
  70. cd /opt/apps/0k-charms
  71. git pull -r
  72. EOF
  73. charm --debug apply docker-host || exit 1
  74. ## there seem to be an error now within compose when trying to download let's encrypt image.
  75. Wrap -d "Updating some docker images" <<EOF || exit 1
  76. docker pull docker.0k.io/letsencrypt
  77. EOF
  78. Wrap -d "Updating cron scripts" <<EOF || exit 1
  79. for d in /etc/cron.{d,daily,hourly,monthly,weekly}; do
  80. ln -sfn "/opt/apps/myc-manage\$d/"* "\$d/" &&
  81. find -L "\$d" -maxdepth 1 -type l -ilname "/opt/apps/myc-manage\$d/"\* -delete
  82. done
  83. EOF
  84. Wrap -d "Updating sysctl scripts" <<EOF || exit 1
  85. for d in /etc/sysctl.d; do
  86. ln -sfn "/opt/apps/myc-manage\$d/"* "\$d/" &&
  87. find -L "\$d" -maxdepth 1 -type l -ilname "/opt/apps/myc-manage\$d/"\* -delete
  88. done
  89. EOF
  90. if [ -f "/root/.bashrc" ]; then
  91. Wrap -d "Enable colors in bash" <<'EOF' || exit 1
  92. sed -ri 's/^# (export LS_OPTIONS=.--color=auto.)/\1/;
  93. s/^# (eval "`dircolors`")/\1/;
  94. s/^# (alias ls='"'ls \\\$LS_OPTIONS'"')/\1/' /root/.bashrc
  95. EOF
  96. fi
  97. Wrap -d "Update authorization to send to ntfy server " <<'EOF' || exit 1
  98. mkdir -p /root/.ssh
  99. umask 077
  100. cat /opt/apps/myc-manage/etc/ssh/ntfy-key | gpg -c --batch --yes --passphrase 'uniquepass' > /root/.ssh/ntfy-key || >&2 echo "Error while copying ntfy key to root"
  101. ## Request token to ntfy server and add to config file
  102. ssh-keyscan -H core-01.0k.io >> /root/.ssh/known_hosts || >&2 echo "Error while adding ntfy server to known_hosts"
  103. cred=$(ssh -i /root/.ssh/ntfy-key ntfy@core-01.0k.io request-token) || >&2 echo "Error while requesting token to ntfy server"
  104. login_ntfy=$(printf "%s" "${cred%$'\n'*}")
  105. password_ntfy=$(printf "%s" "${cred#$'\n'*}")
  106. ## if the config file exist and LOGIN PASSWORD ARE already in we do nothing
  107. if [ -z "$login_ntfy"] || [[ "$login_ntfy" == *$'\n'*]]; then
  108. echo "Error: couldn’t infer credential from ntfy server" >&2;
  109. printf "%s" "$cred" | sed -r 's/^ |/g' >&2;
  110. exit
  111. fi
  112. config_file="/etc/ntfy/ntfy.conf"
  113. mkdir -p "${config_file%/*}"
  114. if [ -f "$config_file" ] || touch $config_file || {
  115. echo "Error: couldn’t create config file $config_file" >&2;
  116. exit 1
  117. }; then
  118. if grep -qE '^LOGIN=' "$config_file"; then
  119. sed -i "s/^LOGIN=.*/LOGIN='$login'/" "$config_file"
  120. else
  121. echo "LOGIN='$login'" >> "$config_file"
  122. fi
  123. if grep -qE '^PASSWORD=' "$config_file"; then
  124. sed -i "s/^PASSWORD=.*/PASSWORD='$password'/" "$config_file"
  125. else
  126. echo "PASSWORD='$password'" >> "$config_file"
  127. fi
  128. fi
  129. EOF
  130. for keyfile in {/root,/home/debian}/.ssh/authorized_keys; do
  131. [ -e "$keyfile" ] || continue
  132. sed -ri 's%^ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDri3GHzDt0Il0jv6zLjwkge48dN9tv11sqVNnKoDeUxzk4kn7Ng5ldd3p6dYL6Pa5NDqJUAhO/d/q08IWuwfEbtj8Yc/EkahcRwVD2imPceUeDgyCaOJhq7WO4c9d9yG8PnRO2\+Zk92a9L5vuELVLr4UHIQOs2/eFRY2/ODV8ebf5L1issGzfLd/IPhX5oJwMwKfqIFOP7KPQ26duHNRq4bYOD9ePW4shfxmyQDk6dSImFat05ErT\+X7703PcPx/PX2AIqqz95zqM6M26BywAohuaD5joxKgkd/mMIJylvT8GEYDlcLMHwnM7LtwtyJ1O9dkVpsibIqGy20KlAOGPf admin@0k$%ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIMV3USt/BLnXnUk7rk8v42mISZaXBZuULbh2vx2Amk7k admin@old0kreplacement%g' "$keyfile"
  133. done
  134. printf "Update finished ${GREEN}successfully${NORMAL} ${GRAY}(in %.2fs)${NORMAL}.\n" "$((SECONDS - start))"