preinstall 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. #!/bin/bash
  2. ###############################################################################
  3. #
  4. # Title: Pre Install Script for Salt Installation
  5. # Authors: Shane Lee
  6. # Date: December 2015
  7. #
  8. # Description: This script stops the salt minion service before attempting to
  9. # install Salt on macOS. It also removes the /opt/salt/bin
  10. # directory, symlink to salt-config, and salt from paths.d.
  11. #
  12. # Requirements:
  13. # - None
  14. #
  15. # Usage:
  16. # This script is run as a part of the macOS Salt Installation
  17. #
  18. ###############################################################################
  19. ###############################################################################
  20. # Define Variables
  21. ###############################################################################
  22. # Get Minor Version
  23. OSX_VERSION=$(sw_vers | grep ProductVersion | cut -f 2 -d: | tr -d '[:space:]')
  24. MINOR=$(echo ${OSX_VERSION} | cut -f 2 -d.)
  25. # Path Variables
  26. INSTALL_DIR="/opt/salt"
  27. BIN_DIR="$INSTALL_DIR/bin"
  28. CONFIG_DIR="/etc/salt"
  29. TEMP_DIR="/tmp"
  30. SBIN_DIR="/usr/local/sbin"
  31. ###############################################################################
  32. # Set up logging and error handling
  33. ###############################################################################
  34. echo "Preinstall started on:" > "$TEMP_DIR/preinstall.txt"
  35. date "+%Y/%m/%d %H:%m:%S" >> "$TEMP_DIR/preinstall.txt"
  36. trap 'quit_on_error $LINENO $BASH_COMMAND' ERR
  37. quit_on_error() {
  38. echo "$(basename $0) caught error on line : $1 command was: $2" >> "$TEMP_DIR/preinstall.txt"
  39. exit -1
  40. }
  41. OSX_VERSION=$(sw_vers | grep ProductVersion | cut -f 2 -d: | tr -d '[:space:]')
  42. MINOR=$(echo ${OSX_VERSION} | cut -f 2 -d.)
  43. ###############################################################################
  44. # Stop the service
  45. ###############################################################################
  46. stop_service_maverick() {
  47. echo "Service: Using old (< 10.10) launchctl interface" >> "$TEMP_DIR/preinstall.txt"
  48. if /bin/launchctl list "com.saltstack.salt.minion" &> /dev/null; then
  49. echo "Service: Unloading minion..." >> "$TEMP_DIR/preinstall.txt"
  50. launchctl unload -w /Library/LaunchDaemons/com.saltstack.salt.minion.plist
  51. echo "Service: Unloaded Successfully" >> "$TEMP_DIR/preinstall.txt"
  52. fi
  53. if /bin/launchctl list "com.saltstack.salt.master" &> /dev/null; then
  54. echo "Service: Unloading master..." >> "$TEMP_DIR/preinstall.txt"
  55. launchctl unload -w /Library/LaunchDaemons/com.saltstack.salt.master.plist
  56. echo "Service: Unloaded Successfully" >> "$TEMP_DIR/preinstall.txt"
  57. fi
  58. if /bin/launchctl list "com.saltstack.salt.syndic" &> /dev/null; then
  59. echo "Service: Unloading syndic..." >> "$TEMP_DIR/preinstall.txt"
  60. launchctl unload -w /Library/LaunchDaemons/com.saltstack.salt.syndic.plist
  61. echo "Service: Unloaded Successfully" >> "$TEMP_DIR/preinstall.txt"
  62. fi
  63. if /bin/launchctl list "com.saltstack.salt.api" &> /dev/null; then
  64. echo "Service: Unloading api..." >> "$TEMP_DIR/preinstall.txt"
  65. launchctl unload -w /Library/LaunchDaemons/com.saltstack.salt.api.plist
  66. echo "Service: Unloaded Successfully" >> "$TEMP_DIR/preinstall.txt"
  67. fi
  68. }
  69. stop_service_yosemite_and_later() {
  70. echo "Service: Using new (>= 10.10) launchctl interface" >> "$TEMP_DIR/preinstall.txt"
  71. if /bin/launchctl list "com.saltstack.salt.minion" &> /dev/null; then
  72. echo "Service: Stopping minion..." >> "$TEMP_DIR/preinstall.txt"
  73. launchctl disable system/com.saltstack.salt.minion
  74. launchctl bootout system /Library/LaunchDaemons/com.saltstack.salt.minion.plist
  75. echo "Service: Stopped Successfully" >> "$TEMP_DIR/preinstall.txt"
  76. fi
  77. if /bin/launchctl list "com.saltstack.salt.master" &> /dev/null; then
  78. echo "Service: Stopping master..." >> "$TEMP_DIR/preinstall.txt"
  79. launchctl disable system/com.saltstack.salt.master
  80. launchctl bootout system /Library/LaunchDaemons/com.saltstack.salt.master.plist
  81. echo "Service: Stopped Successfully" >> "$TEMP_DIR/preinstall.txt"
  82. fi
  83. if /bin/launchctl list "com.saltstack.salt.syndic" &> /dev/null; then
  84. echo "Service: Stopping syndic..." >> "$TEMP_DIR/preinstall.txt"
  85. launchctl disable system/com.saltstack.salt.syndic
  86. launchctl bootout system /Library/LaunchDaemons/com.saltstack.salt.syndic.plist
  87. echo "Service: Stopped Successfully" >> "$TEMP_DIR/preinstall.txt"
  88. fi
  89. if /bin/launchctl list "com.saltstack.salt.api" &> /dev/null; then
  90. echo "Service: Stopping api..." >> "$TEMP_DIR/preinstall.txt"
  91. launchctl disable system/com.saltstack.salt.api
  92. launchctl bootout system /Library/LaunchDaemons/com.saltstack.salt.api.plist
  93. echo "Service: Stopped Successfully" >> "$TEMP_DIR/preinstall.txt"
  94. fi
  95. }
  96. echo "Service: Configuring..." >> "$TEMP_DIR/preinstall.txt"
  97. case $MINOR in
  98. 9 )
  99. stop_service_maverick;
  100. ;;
  101. * )
  102. stop_service_yosemite_and_later;
  103. ;;
  104. esac
  105. echo "Service: Configured Successfully" >> "$TEMP_DIR/preinstall.txt"
  106. ###############################################################################
  107. # Remove the Symlink to salt-config.sh
  108. ###############################################################################
  109. if [ -L "$SBIN_DIR/salt-config" ]; then
  110. echo "Cleanup: Removing Symlink $BIN_DIR/salt-config" >> "$TEMP_DIR/preinstall.txt"
  111. rm "$SBIN_DIR/salt-config"
  112. echo "Cleanup: Removed Successfully" >> "$TEMP_DIR/preinstall.txt"
  113. fi
  114. ###############################################################################
  115. # Remove the $INSTALL_DIR directory
  116. ###############################################################################
  117. if [ -d "$INSTALL_DIR" ]; then
  118. echo "Cleanup: Removing $INSTALL_DIR" >> "$TEMP_DIR/preinstall.txt"
  119. rm -rf "$INSTALL_DIR"
  120. echo "Cleanup: Removed Successfully" >> "$TEMP_DIR/preinstall.txt"
  121. fi
  122. ###############################################################################
  123. # Remove the salt from the paths.d
  124. ###############################################################################
  125. if [ -f "/etc/paths.d/salt" ]; then
  126. echo "Path: Removing salt from the path..." >> "$TEMP_DIR/preinstall.txt"
  127. rm "/etc/paths.d/salt"
  128. echo "Path: Removed Successfully" >> "$TEMP_DIR/preinstall.txt"
  129. fi
  130. echo "Preinstall Completed Successfully on:" >> "$TEMP_DIR/preinstall.txt"
  131. date "+%Y/%m/%d %H:%m:%S" >> "$TEMP_DIR/preinstall.txt"
  132. exit 0