Set up GIS software on multiple computers (Windows & Linux) simultaneosly using SaltStack
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.

350 lines
10 KiB

6 years ago
  1. #!/bin/bash
  2. # Salt module: GIS workstation environment for multiple computers
  3. # Copyright (C) 2018 Pekka Helenius
  4. #
  5. # This program is free software: you can redistribute it and/or modify
  6. # it under the terms of the GNU General Public License as published by
  7. # the Free Software Foundation, either version 3 of the License, or
  8. # (at your option) any later version.
  9. #
  10. # This program is distributed in the hope that it will be useful,
  11. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. # GNU General Public License for more details.
  14. #
  15. # You should have received a copy of the GNU General Public License
  16. # along with this program. If not, see <https://www.gnu.org/licenses/>.
  17. ###########################################################
  18. # Salt master requirement: Ubuntu 18.04 LTS or variants
  19. # Salt minion requirement: Microsoft Windows and/or Ubuntu 18.04 LTS or variants
  20. ###########################################################
  21. # Test connection to minion computers and ask for confirmation to continue.
  22. # May be useful in some situations but consider disabling the check
  23. # in large automated cluster environments.
  24. #
  25. # If accepted minion computer can't be reached and the value is true,
  26. # user is asked for input
  27. #
  28. # Default value: true
  29. #
  30. TEST_MINION_PING="true"
  31. # Automatically accept all minion computers?
  32. # If not true, user is asked for input for each unaccepted minion ID
  33. #
  34. # Default value: true
  35. #
  36. AUTOACCEPT_MINIONS="true"
  37. # Use more verbose debug output for Salt master?
  38. #
  39. # Default value: true
  40. #
  41. SALT_DEBUG="true"
  42. # Run salt only locally (I.E. test localhost minion)?
  43. #
  44. # Another implementation would be using 'salt-call' command
  45. #
  46. # Default value: true
  47. #
  48. SALT_LOCALONLY="false"
  49. ###########################################################
  50. # Check root
  51. function checkRoot() {
  52. if [ $(id -u) -ne 0 ]; then
  53. echo "Run the script with root permissions (sudo or root)."
  54. exit 1
  55. fi
  56. }
  57. checkRoot
  58. ###########################################################
  59. # Check for command dependencies
  60. function checkCommands() {
  61. if [[ $(which --help 2>/dev/null) ]] && [[ $(echo --help 2>/dev/null) ]]; then
  62. COMMANDS=(grep mkdir systemctl id wget cat tee awk sed tr chmod cp rm mv touch dpkg apt-cache apt-get)
  63. a=0
  64. for command in ${COMMANDS[@]}; do
  65. if [[ ! $(which $command 2>/dev/null) ]]; then
  66. COMMANDS_NOTFOUND[$a]=$command
  67. let a++
  68. fi
  69. done
  70. if [[ -n $COMMANDS_NOTFOUND ]]; then
  71. echo -e "\n${bash_red}Error:${bash_color_default} The following commands could not be found: ${COMMANDS_NOTFOUND[*]}\nAborting\n"
  72. exit 1
  73. fi
  74. else
  75. exit 1
  76. fi
  77. }
  78. checkCommands
  79. ###########################################################
  80. # Check Salt master Linux distribution
  81. if [[ -f /etc/os-release ]]; then
  82. MASTER_DISTRO=$(grep ^VERSION_ID /etc/os-release | grep -oP '(?<=")[^"]*')
  83. if [[ $MASTER_DISTRO != "18.04" ]]; then
  84. echo -e "This script is supported only on Ubuntu 18.04 LTS. Aborting.\n"
  85. exit 1
  86. fi
  87. else
  88. echo -e "Can't recognize your Linux distribution. Aborting.\n"
  89. exit 1
  90. fi
  91. ###########################################################
  92. # Welcome message
  93. function welcomeMessage() {
  94. echo -e "This script will install GIS workstation environment for multiple Ubuntu 18.04 LTS & MS Windows computers.\n\nSoftware:\n\n -QGIS\n -CloudCompare\n -LASTools\n -QuickRoute\n -Merkaartor\n -etc.\n"
  95. read -r -p "Continue? [y/N] " answer
  96. if [[ ! $(echo $answer | sed 's/ //g') =~ ^([yY][eE][sS]|[yY])$ ]]; then
  97. echo -e "Aborting.\n"
  98. exit 1
  99. fi
  100. unset answer
  101. }
  102. welcomeMessage
  103. ###########################################################
  104. # Check network connection
  105. function checkInternet() {
  106. if [[ $(echo $(wget --delete-after -q -T 5 github.com -o -)$?) -ne 0 ]]; then
  107. echo -e "\nInternet connection test failed (GitHub). Please check your connection and try again.\n"
  108. exit 1
  109. fi
  110. }
  111. ###########################################################
  112. if [[ ! -d /srv/{salt,pillar} ]]; then
  113. mkdir -p /srv/{pillar,salt/win/repo-ng/installers}
  114. fi
  115. cp -R srv_salt/* /srv/salt/
  116. cp -R srv_salt_winrepo/* /srv/salt/win/repo-ng/
  117. cp -R srv_pillar/* /srv/pillar
  118. ###########################################################
  119. if [[ ! -d compiled ]]; then
  120. mkdir compiled
  121. fi
  122. ###########################################################
  123. # Run scripts
  124. checkInternet
  125. if [[ $(systemctl is-active salt-master) != "active" ]]; then
  126. bash ./saltscripts/1-setup-salt-env.sh
  127. fi
  128. function minionAcceptPolicy() {
  129. echo -e "\nApplying minion ID key policy\n"
  130. if [[ $AUTOACCEPT_MINIONS == "true" ]]; then
  131. salt-key -y -A | sed '/^The key glob/d'
  132. else
  133. for accept_minion in $(salt-key | sed '/Unaccepted Keys/,/Rejected Keys/!d;//d'); do
  134. salt-key -a $accept_minion
  135. done
  136. fi
  137. echo -e "Current policy:\n$(salt-key)"
  138. }
  139. if [[ $SALT_LOCALONLY != "true" ]]; then
  140. minionAcceptPolicy
  141. else
  142. salt-key -y -D &> /dev/null
  143. systemctl restart salt-minion.service
  144. if [[ $? -eq 0 ]]; then
  145. sleep 5
  146. salt-key -y -a defaultMinion
  147. if [[ $(salt-key | sed '/Accepted Keys/,/Unaccepted Keys/!d;//d' | grep defaultMinion | wc -w) -eq 0 ]]; then
  148. exit 1
  149. fi
  150. fi
  151. fi
  152. function checkWinMinions() {
  153. local IFS=$'\n'
  154. WINMINIONS=""
  155. if [[ $SALT_LOCALONLY != "true" ]]; then
  156. echo -e "Checking for accepted Windows minions. Please wait.\n"
  157. for win_minion in $(salt-key | sed '/Accepted Keys/,/Unaccepted Keys/!d;//d'); do
  158. if [[ $(salt $win_minion grains.item os 2> /dev/null | sed '$!d; s/^\s*//; /^No minions/d') == "Windows" ]]; then
  159. WINMINIONS="true"
  160. fi
  161. done
  162. fi
  163. }
  164. echo -e "\nSalt master - version: $(salt-call grains.item saltversion | sed -n '$p' | sed -e 's/\s//g')"
  165. echo -e "Salt master - reported IP addresses: $(salt-call network.ip_addrs | sed '1d; s/^.*\-\s//' | sed ':a;N;$!ba;s/\n/, /g')\n"
  166. checkWinMinions
  167. bash ./saltscripts/2-get-programs-on-master.sh ${SALT_LOCALONLY} ${WINMINIONS}
  168. ###########################################################
  169. # Fix permissions
  170. for dir in /srv/salt /srv/pillar; do
  171. find $dir -type d -exec chmod u=rwx,go=rx {} +
  172. find $dir -type f -exec chmod u=rw,go=r {} +
  173. done
  174. ###########################################################
  175. # Test minion connectivity
  176. function testMinions() {
  177. local IFS=$'\n'
  178. MASTER_VERSION=$(dpkg -s salt-master | grep '^Version:' | awk '{print $2}')
  179. ACCEPTED_MINIONS=$(salt-key | sed '/Accepted Keys/,/Denied Keys/!d;//d')
  180. i=0
  181. m=1
  182. for check_minion in $ACCEPTED_MINIONS; do
  183. echo -e "Testing minion '$check_minion' ($m / $(echo $ACCEPTED_MINIONS | wc -w))"
  184. if [[ $(echo $(salt $check_minion test.ping) | awk '{print $NF}') != "True" ]]; then
  185. echo -e "Can't connect to Salt minion '\e[91m$check_minion\e[0m'. Make sure the minion computer is connected and\nSalt minion program version matches with the Salt master version ($MASTER_VERSION)\nIf they do match, delete old master public key from the minion computer, restart Salt minion service and try again."
  186. echo -e "\nThe master key is stored at the following locations:\n\n -Windows: C:\salt\\\\conf\pki\minion\minion_master.pub\n -Linux: /etc/salt/minion/minion_master.pub\n\nSystem administration rights are required to access it.\n"
  187. read -r -p "Continue? [y/N] " answer
  188. if [[ $(echo $answer | sed 's/ //g') =~ ^([nN][oO]|[nN])$ ]]; then
  189. echo -e "Aborting.\n"
  190. exit 1
  191. fi
  192. else
  193. echo -e "\tSalt minion '$check_minion' - version: $(salt $check_minion grains.item saltversion | sed -n '$p' | sed -e 's/\s//g')"
  194. OK_MINIONS[$i]=$check_minion
  195. let i++
  196. fi
  197. let m++
  198. done
  199. }
  200. if [[ $TEST_MINION_PING == "true" ]]; then
  201. echo -e "\nTesting connections to the accepted minion computers. This takes a while.\n"
  202. testMinions
  203. fi
  204. ###########################################################
  205. # Install & set up software
  206. TIMEOUT="600" # 10 minutes
  207. # Reason for increasing the timeout from 5 to 300:
  208. # Installing CloudCompare & QGIS programs take very long time
  209. # During the installation, Windows minions do not return anything
  210. # to the master computer, thus the master computer
  211. # thinks that the minions have timed out, although, in reality,
  212. # they are likely installing the applications
  213. #
  214. # Extra long timeout especially required by Windows QGIS installation!!
  215. #
  216. # Proper fix for this would be needed
  217. # but this behavior may be linked with options and output
  218. # by Windows NSIS (Nullsoft) installers
  219. # NOTE: Windows minions return "Failed" (retcode 2) status
  220. # for installed programs, although they return
  221. # alphabetical "install status: success" as well.
  222. # This is likely a retcode bug in Saltstack.
  223. # Salt is known to have these issues in the past.
  224. # Failed minions (failed)
  225. f=0
  226. # Succeeded minions (succeeded)
  227. s=0
  228. # All minions (total)
  229. t=0
  230. for minion in ${OK_MINIONS[*]}; do
  231. minion_ips=$(salt $minion network.ip_addrs | sed '1d; s/^.*\-\s//' | sed ':a;N;$!ba;s/\n/, /g')
  232. echo -e "\n(Minion $(( $t + 1 )) / ${#OK_MINIONS[@]}) Refreshing Salt grains & pillars for minion computer '\e[95m$minion\e[0m' (IPs: $minion_ips).\n"
  233. salt $minion saltutil.refresh_grains
  234. salt $minion saltutil.refresh_pillar
  235. # Update Salt package databases, especially for Windows minions
  236. echo -e "\n(Minion $(( $t + 1 )) / ${#OK_MINIONS[@]}) Updating Salt minion package databases for minion computer '\e[95m$minion\e[0m' (IPs: $minion_ips).\n"
  237. salt $minion pkg.refresh_db
  238. if [[ $SALT_DEBUG == "true" ]]; then
  239. saltdbg='-l debug'
  240. else
  241. saltdbg=''
  242. fi
  243. echo -e "\n(Minion $(( $t + 1 )) / ${#OK_MINIONS[@]}) Applying Salt state updates to the minion computer '\e[95m$minion\e[0m' (IPs: $minion_ips).\n"
  244. salt $saltdbg -t $TIMEOUT $minion state.highstate --state-output terse
  245. if [[ $? -ne 0 ]]; then
  246. let f++
  247. FAILED_MINIONS[$f]=$(echo "$minion (IPs: $minion_ips)")
  248. else
  249. let s++
  250. fi
  251. let t++
  252. done
  253. echo -e "\nSucceeded minions: $s of $t\nFailed minions: $f of $t\n"
  254. if [[ $f -ne 0 ]]; then
  255. echo -e "The following minions returned failed status:\n\n \e[91m$(echo ${FAILED_MINIONS[@]})\e[0m\n"
  256. fi
  257. unset OK_MINIONS