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.

141 lines
4.4 KiB

6 years ago
  1. #!/bin/bash
  2. # Salt default Master/Minion preconfiguration script for a single computer
  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. # This script sets up default environment for basic Salt Master & Minion configuration
  19. # for one computer
  20. # Supported distributions: Ubuntu 18.04 LTS or variants
  21. # Use alternative Saltstack official repository, not the one provided by default Ubuntu repositories?
  22. # Usually Saltstack repository provides a newer Salt master/minion versions
  23. #
  24. # NOTE:Please make sure that Salt master & minion versions correspond each other!
  25. #
  26. USE_SALTREPO="true"
  27. ###########################################################
  28. # Run package database updates?
  29. function packageUpdateQ() {
  30. read -r -p "Refresh package databases before installation (recommended)? [y/N] " answer
  31. if [[ $(echo $answer | sed 's/ //g') =~ ^([yY][eE][sS]|[yY])$ ]]; then
  32. UPDATES=""
  33. fi
  34. unset answer
  35. }
  36. packageUpdateQ
  37. ###########################################################
  38. if [[ -f /etc/os-release ]]; then
  39. DISTRO=$(grep ^PRETTY_NAME /etc/os-release | grep -oP '(?<=")[^\s]*')
  40. function installPackages() {
  41. case "${DISTRO}" in
  42. Ubuntu*)
  43. pkgmgr_cmd() {
  44. if [[ -v UPDATES ]]; then
  45. # Update Salt to the latest version - 2018.3
  46. # Yes, we use 16.04 until 18.04 will officially be available
  47. if [[ $USE_SALTREPO == "true" ]]; then
  48. wget -O - https://repo.saltstack.com/py3/ubuntu/16.04/amd64/latest/SALTSTACK-GPG-KEY.pub | apt-key add -
  49. echo "deb http://repo.saltstack.com/py3/ubuntu/16.04/amd64/latest xenial main" > /etc/apt/sources.list.d/saltstack.list
  50. elif [[ $USE_ALTREPO != "true" ]] && [[ -f /etc/apt/sources.list.d/saltstack.list ]]; then
  51. rm /etc/apt/sources.list.d/saltstack.list
  52. fi
  53. apt-get update
  54. fi
  55. apt-get -y install $1
  56. }
  57. PKGS=(salt-master salt-minion)
  58. ;;
  59. default)
  60. echo -e "Can't recognize your Linux distribution. Aborting.\n"
  61. exit 1
  62. esac
  63. pkgmgr_cmd "${PKGS[*]}"
  64. systemctl enable salt-master.service &> /dev/null
  65. systemctl restart salt-master.service &> /dev/null
  66. }
  67. installPackages
  68. unset UPDATES
  69. else
  70. echo -e "Can't recognize your Linux distribution. Aborting.\n"
  71. exit 1
  72. fi
  73. ###########################################################
  74. function saltEnvironment() {
  75. function defaultMinionConf() {
  76. MINION_NAME="defaultMinion"
  77. if [[ -d /etc/salt ]]; then
  78. echo -e "\nWriting default Salt minion configuration '${MINION_NAME}' to /etc/salt/minion\n"
  79. echo -e "master: localhost\nid: ${MINION_NAME}" > /etc/salt/minion
  80. systemctl enable salt-minion.service &> /dev/null
  81. systemctl restart salt-minion.service &> /dev/null
  82. salt-key -y -a ${MINION_NAME} > /dev/null
  83. echo -e "Testing default Salt minion connection with the Salt master\n"
  84. if [[ $(echo $(salt "${MINION_NAME}" test.ping &> /dev/null)$?) -ne 0 ]]; then
  85. echo -e "Salt master can't connect to the default Salt minion. Aborting.\n"
  86. exit 1
  87. else
  88. echo -e "Connection OK!\n"
  89. fi
  90. else
  91. echo -e "Missing Salt configuration directory /etc/salt. Aborting.\n"
  92. exit 1
  93. fi
  94. }
  95. defaultMinionConf
  96. }
  97. if [ $? -eq 0 ]; then
  98. saltEnvironment
  99. else
  100. echo -e "Unknown error. Aborting.\n"
  101. exit 1
  102. fi