freedombone-utils-time 3.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. #!/bin/bash
  2. #
  3. # .---. . .
  4. # | | |
  5. # |--- .--. .-. .-. .-.| .-. .--.--. |.-. .-. .--. .-.
  6. # | | (.-' (.-' ( | ( )| | | | )( )| | (.-'
  7. # ' ' --' --' -' - -' ' ' -' -' -' ' - --'
  8. #
  9. # Freedom in the Cloud
  10. #
  11. # Time functions
  12. #
  13. # License
  14. # =======
  15. #
  16. # Copyright (C) 2014-2016 Bob Mottram <bob@robotics.uk.to>
  17. #
  18. # This program is free software: you can redistribute it and/or modify
  19. # it under the terms of the GNU Affero General Public License as published by
  20. # the Free Software Foundation, either version 3 of the License, or
  21. # (at your option) any later version.
  22. #
  23. # This program is distributed in the hope that it will be useful,
  24. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  25. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  26. # GNU Affero General Public License for more details.
  27. #
  28. # You should have received a copy of the GNU Affero General Public License
  29. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  30. # Why use Google as a time source?
  31. # The thinking here is that it's likely to be reliable and fast.
  32. # The ping doesn't reveal any information other than that the server
  33. # is running, and if anyone maliciously alters the time on Google's
  34. # servers then that would certainly be newsworthy and they'd be
  35. # likely to do something about it quickly.
  36. # If you have better time sources then change them here.
  37. TLS_TIME_SOURCE1="google.com"
  38. TLS_TIME_SOURCE2="www.ptb.de"
  39. # Time synchronisation
  40. TLSDATE_REPO="https://github.com/bashrc/tlsdate"
  41. TLSDATE_COMMIT='505e31540eebde8074e7dc93b29be0d848def06a'
  42. function check_date {
  43. curr_date=$(date)
  44. if [[ $curr_date == *"1970"* ]]; then
  45. apt-get -y install ntp
  46. fi
  47. }
  48. function time_synchronisation {
  49. # mesh peers typically don't sync over the internet
  50. if [[ $SYSTEM_TYPE == "$VARIANT_MESH" ]]; then
  51. return
  52. fi
  53. if [ -f /usr/local/bin/${PROJECT_NAME}-update-date ]; then
  54. cp /usr/local/bin/${PROJECT_NAME}-update-date /usr/bin/updatedate
  55. else
  56. cp /usr/bin/${PROJECT_NAME}-update-date /usr/bin/updatedate
  57. fi
  58. chmod +x /usr/bin/updatedate
  59. if grep -Fxq "time_synchronisation" $COMPLETION_FILE; then
  60. return
  61. fi
  62. apt-get -y install tlsdate
  63. apt-get -y remove ntpdate
  64. function_check cron_add_mins
  65. cron_add_mins 15 '/usr/bin/updatedate'
  66. systemctl restart cron
  67. echo 'time_synchronisation' >> $COMPLETION_FILE
  68. }
  69. function time_synchronisation_tlsdate {
  70. # mesh peers typically don't sync over the internet
  71. if [[ $SYSTEM_TYPE == "$VARIANT_MESH" ]]; then
  72. return
  73. fi
  74. if [ -f /usr/local/bin/${PROJECT_NAME}-update-date ]; then
  75. cp /usr/local/bin/${PROJECT_NAME}-update-date /usr/bin/updatedate
  76. else
  77. cp /usr/bin/${PROJECT_NAME}-update-date /usr/bin/updatedate
  78. fi
  79. chmod +x /usr/bin/updatedate
  80. if [ ! -d $INSTALL_DIR ]; then
  81. mkdir -p $INSTALL_DIR
  82. fi
  83. set_repo_commit $INSTALL_DIR/tlsdate "tlsdate commit" "$TLSDATE_COMMIT" $TLSDATE_REPO
  84. if grep -Fxq "time_synchronisation_tlsdate" $COMPLETION_FILE; then
  85. return
  86. fi
  87. apt-get -y remove tlsdate ntpdate
  88. apt-get -y install build-essential autoconf libevent-dev
  89. apt-get -y install pkg-config libtool libssl-dev
  90. cd $INSTALL_DIR
  91. function_check git_clone
  92. git_clone $TLSDATE_REPO $INSTALL_DIR/tlsdate
  93. cd $INSTALL_DIR/tlsdate
  94. git checkout $TLSDATE_COMMIT -b $TLSDATE_COMMIT
  95. ./autogen.sh
  96. ./configure
  97. if [ ! "$?" = "0" ]; then
  98. echo $'Unable to configure tlsdate'
  99. exit 6825277
  100. fi
  101. make
  102. if [ ! "$?" = "0" ]; then
  103. echo $'Unable to build tlsdate'
  104. exit 3792726
  105. fi
  106. make install
  107. function_check cron_add_mins
  108. cron_add_mins 15 '/usr/bin/updatedate'
  109. echo 'time_synchronisation_tlsdate' >> $COMPLETION_FILE
  110. }
  111. # NOTE: deliberately no exit 0