Automated SaltStack Master/Minion testrun on Ubuntu Linux
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.

233 lines
6.3 KiB

6 years ago
6 years ago
  1. #!/bin/bash
  2. # Salt Master/Minion preconfiguration script
  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
  21. ###########################################################
  22. # Check for command dependencies
  23. function checkCommands() {
  24. if [[ $(which --help 2>/dev/null) ]] && [[ $(echo --help 2>/dev/null) ]]; then
  25. COMMANDS=(grep mkdir systemctl id wget cat tee awk sed chmod)
  26. a=0
  27. for command in ${COMMANDS[@]}; do
  28. if [[ ! $(which $command 2>/dev/null) ]]; then
  29. COMMANDS_NOTFOUND[$a]=$command
  30. let a++
  31. fi
  32. done
  33. if [[ -v COMMANDS_NOTFOUND ]]; then
  34. echo -e "\n${bash_red}Error:${bash_color_default} The following commands could not be found: ${COMMANDS_NOTFOUND[*]}\nAborting\n"
  35. exit 1
  36. fi
  37. else
  38. exit 1
  39. fi
  40. }
  41. checkCommands
  42. ###########################################################
  43. # Check root
  44. function checkRoot() {
  45. if [ $(id -u) -ne 0 ]; then
  46. echo "Run the script with root permissions (sudo or root)."
  47. exit 1
  48. fi
  49. }
  50. checkRoot
  51. ###########################################################
  52. # Check network connection
  53. function checkInternet() {
  54. if [[ $(echo $(wget --delete-after -q -T 5 github.com -o -)$?) -ne 0 ]]; then
  55. echo -e "\nInternet connection failed (GitHub). Please check your connection and try again.\n"
  56. exit 1
  57. fi
  58. }
  59. checkInternet
  60. ###########################################################
  61. # Welcome message
  62. function welcomeMessage() {
  63. read -r -p "This script will install Salt Master/Minion test environment. Continue? [y/N] " answer
  64. if [[ ! $(echo $answer | sed 's/ //g') =~ ^([yY][eE][sS]|[yY])$ ]]; then
  65. echo -e "Aborting.\n"
  66. exit 1
  67. fi
  68. unset answer
  69. }
  70. welcomeMessage
  71. ###########################################################
  72. # Run package database updates?
  73. function packageUpdateQ() {
  74. read -r -p "Refresh package databases before Salt installation? [y/N] " answer
  75. if [[ $(echo $answer | sed 's/ //g') =~ ^([yY][eE][sS]|[yY])$ ]]; then
  76. UPDATES=""
  77. fi
  78. unset answer
  79. }
  80. packageUpdateQ
  81. ###########################################################
  82. if [[ -f /etc/os-release ]]; then
  83. DISTRO=$(grep ^PRETTY_NAME /etc/os-release | grep -oP '(?<=")[^\s]*')
  84. function installPackages() {
  85. case "${DISTRO}" in
  86. #Arch*)
  87. # pkgmgr_cmd() {
  88. # if [[ -z UPDATES ]]; then
  89. # pacman -Suy && pacman -Fy
  90. # fi
  91. # pacman -S $1
  92. # }
  93. # PKGS=(salt openssh vagrant virtualbox)
  94. # ;;
  95. Ubuntu*)
  96. pkgmgr_cmd() {
  97. if [[ -v UPDATES ]]; then
  98. apt-get update
  99. fi
  100. apt-get -y install $1
  101. }
  102. PKGS=(salt-master salt-minion)
  103. ;;
  104. default)
  105. echo -e "Can't recognize your Linux distribution. Aborting.\n"
  106. exit 1
  107. esac
  108. pkgmgr_cmd "${PKGS[*]}"
  109. systemctl enable salt-master.service &> /dev/null
  110. systemctl restart salt-master.service &> /dev/null
  111. }
  112. installPackages
  113. unset UPDATES
  114. else
  115. echo -e "Can't recognize your Linux distribution. Aborting.\n"
  116. exit 1
  117. fi
  118. ###########################################################
  119. function saltEnvironment() {
  120. if [[ ! -d /srv/salt/apache/{data,scripts} ]]; then
  121. mkdir -p /srv/salt/apache/{data,scripts}
  122. fi
  123. function defaultMinionConf() {
  124. MINION_NAME="defaultMinion"
  125. if [[ -d /etc/salt ]]; then
  126. echo -e "\nWriting default Salt minion configuration '${MINION_NAME}' to /etc/salt/minion\n"
  127. echo -e "master: localhost\nid: ${MINION_NAME}" > /etc/salt/minion
  128. systemctl enable salt-minion.service &> /dev/null
  129. systemctl restart salt-minion.service &> /dev/null
  130. salt-key -y -a ${MINION_NAME} > /dev/null
  131. # Adding this sleep timer may reduce chance to failed connection tests in some cases
  132. sleep 5
  133. echo -e "Testing default Salt minion connection with the Salt master\n"
  134. if [[ $(echo $(salt "${MINION_NAME}" test.ping &> /dev/null)$?) -ne 0 ]]; then
  135. echo -e "Salt master can't connect to the default Salt minion. Aborting.\n"
  136. exit 1
  137. else
  138. echo -e "Connection OK!\n"
  139. fi
  140. else
  141. echo -e "Missing Salt configuration directory /etc/salt. Aborting.\n"
  142. exit 1
  143. fi
  144. }
  145. defaultMinionConf
  146. }
  147. if [ $? -eq 0 ]; then
  148. saltEnvironment
  149. else
  150. echo -e "Unknown error. Aborting.\n"
  151. exit 1
  152. fi
  153. ###########################################################
  154. function getFiles() {
  155. echo -e "Downloading additional files\n"
  156. wget -q https://raw.githubusercontent.com/Fincer/salt_testrun/master/scripts/salt_pillar_apache_sample.sh \
  157. -O /srv/salt/apache/scripts/salt_pillar_apache_sample.sh
  158. wget -q https://raw.githubusercontent.com/Fincer/salt_testrun/master/data/sampleindex.html \
  159. -O /srv/salt/apache/data/sampleindex.html
  160. wget -q https://raw.githubusercontent.com/Fincer/salt_testrun/master/data/sampleindex_functions.js \
  161. -O /srv/salt/apache/data/sampleindex_functions.js
  162. chmod -R u+rw,go+r /srv/salt/apache
  163. }
  164. getFiles
  165. cd /srv/salt/apache/scripts
  166. bash ./salt_pillar_apache_sample.sh
  167. sudo -u \#1000 xdg-open $(echo "http://${DISTRO}.${MINION_NAME}.com" | awk '{print tolower($0)}') &