freedombone-upgrade 5.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. #!/bin/bash
  2. #
  3. # .---. . .
  4. # | | |
  5. # |--- .--. .-. .-. .-.| .-. .--.--. |.-. .-. .--. .-.
  6. # | | (.-' (.-' ( | ( )| | | | )( )| | (.-'
  7. # ' ' --' --' -' - -' ' ' -' -' -' ' - --'
  8. #
  9. # Freedom in the Cloud
  10. #
  11. # Command to upgrade the system
  12. # License
  13. # =======
  14. #
  15. # Copyright (C) 2015-2016 Bob Mottram <bob@robotics.uk.to>
  16. #
  17. # This program is free software: you can redistribute it and/or modify
  18. # it under the terms of the GNU Affero General Public License as published by
  19. # the Free Software Foundation, either version 3 of the License, or
  20. # (at your option) any later version.
  21. #
  22. # This program is distributed in the hope that it will be useful,
  23. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  24. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  25. # GNU Affero General Public License for more details.
  26. #
  27. # You should have received a copy of the GNU Affero General Public License
  28. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  29. PROJECT_NAME='freedombone'
  30. PROJECT_DIR="/root/${PROJECT_NAME}"
  31. # An optional configuration file which overrides some of these variables
  32. CONFIGURATION_FILE="/root/${PROJECT_NAME}.cfg"
  33. PROJECT_REPO="https://github.com/bashrc/${PROJECT_NAME}"
  34. FRIENDS_MIRRORS_SERVER=
  35. FRIENDS_MIRRORS_SSH_PORT=2222
  36. FRIENDS_MIRRORS_PASSWORD=
  37. MY_MIRRORS_PASSWORD=
  38. function read_repo_servers {
  39. if [ -f $CONFIGURATION_FILE ]; then
  40. if grep -q "FRIENDS_MIRRORS_SERVER" $CONFIGURATION_FILE; then
  41. FRIENDS_MIRRORS_SERVER=$(grep "FRIENDS_MIRRORS_SERVER" $CONFIGURATION_FILE | awk -F '=' '{print $2}')
  42. fi
  43. if grep -q "FRIENDS_MIRRORS_SSH_PORT" $CONFIGURATION_FILE; then
  44. FRIENDS_MIRRORS_SSH_PORT=$(grep "FRIENDS_MIRRORS_SSH_PORT" $CONFIGURATION_FILE | awk -F '=' '{print $2}')
  45. fi
  46. if grep -q "MY_MIRRORS_PASSWORD" $CONFIGURATION_FILE; then
  47. MY_MIRRORS_PASSWORD=$(grep "MY_MIRRORS_PASSWORD" $CONFIGURATION_FILE | awk -F '=' '{print $2}')
  48. fi
  49. if grep -q "FRIENDS_MIRRORS_PASSWORD" $CONFIGURATION_FILE; then
  50. FRIENDS_MIRRORS_PASSWORD=$(grep "FRIENDS_MIRRORS_PASSWORD" $CONFIGURATION_FILE | awk -F '=' '{print $2}')
  51. fi
  52. fi
  53. if [ ! $FRIENDS_MIRRORS_SERVER ]; then
  54. return
  55. fi
  56. if [ ${#FRIENDS_MIRRORS_SERVER} -lt 2 ]; then
  57. return
  58. fi
  59. MAIN_COMMAND=/usr/local/bin/${PROJECT_NAME}
  60. if [ ! -f $MAIN_COMMAND ]; then
  61. MAIN_COMMAND=/usr/bin/${PROJECT_NAME}
  62. fi
  63. REPOS=($(cat ${MAIN_COMMAND} | grep "_REPO=\"" | uniq -u | sed 's|${PROJECT_NAME}|'"${PROJECT_NAME}"'|g'))
  64. for line in "${REPOS[@]}"
  65. do
  66. repo_name=$(echo "$line" | awk -F '=' '{print $1}')
  67. mirrors_name=$(echo "$repo_name" | sed "s|_REPO||g" | awk '{print tolower($0)}')
  68. friends_repo_url="ssh://mirrors@${FRIENDS_MIRRORS_SERVER}:${FRIENDS_MIRRORS_SSH_PORT}/home/mirrors/${mirrors_name}"
  69. ${repo_name}="${friends_repo_url}"
  70. done
  71. }
  72. function git_clone {
  73. repo_url="$1"
  74. destination_dir="$2"
  75. if [[ "$repo_url" == "ssh:"* ]]; then
  76. if [ "${FRIENDS_MIRRORS_SERVER}" ]; then
  77. if [ ${#FRIENDS_MIRRORS_SERVER} -gt 2 ]; then
  78. if [ "$FRIENDS_MIRRORS_PASSWORD" ]; then
  79. if [ ${#FRIENDS_MIRRORS_PASSWORD} -gt 2 ]; then
  80. sshpass -p "$FRIENDS_MIRRORS_PASSWORD" git clone "$repo_url" "$destination_dir"
  81. return
  82. fi
  83. fi
  84. fi
  85. fi
  86. fi
  87. git clone "$repo_url" "$destination_dir"
  88. }
  89. function git_pull {
  90. if [ ! $1 ]; then
  91. echo $'git_pull no repo specified'
  92. fi
  93. git stash
  94. git remote set-url origin $1
  95. git checkout master
  96. if [ "${FRIENDS_MIRRORS_SERVER}" ]; then
  97. if [ ${#FRIENDS_MIRRORS_SERVER} -gt 2 ]; then
  98. if [ "$FRIENDS_MIRRORS_PASSWORD" ]; then
  99. if [ ${#FRIENDS_MIRRORS_PASSWORD} -gt 2 ]; then
  100. sshpass -p "$FRIENDS_MIRRORS_PASSWORD" git pull
  101. if [ $2 ]; then
  102. git checkout $2 -b $2
  103. fi
  104. return
  105. fi
  106. fi
  107. fi
  108. fi
  109. git pull
  110. if [ $2 ]; then
  111. git checkout $2 -b $2
  112. fi
  113. }
  114. if [ -f $CONFIGURATION_FILE ]; then
  115. # read the location of the main project repo
  116. if grep -q "PROJECT_REPO" $CONFIGURATION_FILE; then
  117. PROJECT_REPO=$(grep "PROJECT_REPO" $CONFIGURATION_FILE | awk -F '=' '{print $2}')
  118. fi
  119. fi
  120. update-ca-certificates
  121. read_repo_servers
  122. ${PROJECT_NAME}-mirrors
  123. if [ ! -d $PROJECT_DIR ]; then
  124. git_clone $PROJECT_REPO $PROJECT_DIR
  125. fi
  126. if [ -d $PROJECT_DIR ]; then
  127. if [ -f $CONFIGURATION_FILE ]; then
  128. cd $PROJECT_DIR
  129. rm -rf $PROJECT_DIR/locale/*
  130. git_pull $PROJECT_REPO
  131. make install
  132. ${PROJECT_NAME} -c $CONFIGURATION_FILE
  133. fi
  134. fi
  135. # remove the original sipwitch daemon if it exists
  136. if [ -f /etc/init.d/sipwitch ]; then
  137. rm -f /etc/init.d/sipwitch
  138. fi
  139. # update blog avatar
  140. ${PROJECT_NAME}-blog
  141. echo '
  142. ' | reset-tripwire
  143. # deliberately there is no 'exit 0' here