postinstall 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. #!/bin/bash
  2. ###############################################################################
  3. #
  4. # Title: Post Script for Salt Installation
  5. # Authors: Shane Lee
  6. # Date: December 2015
  7. #
  8. # Description: This script copies the minion config file and starts the salt
  9. # service
  10. #
  11. # Requirements:
  12. # - None
  13. #
  14. # Usage:
  15. # This script is run as a part of the macOS Salt Installation
  16. #
  17. ###############################################################################
  18. ###############################################################################
  19. # Define Variables
  20. ###############################################################################
  21. # Get Minor Version
  22. OSX_VERSION=$(sw_vers | grep ProductVersion | cut -f 2 -d: | tr -d '[:space:]')
  23. MINOR=$(echo ${OSX_VERSION} | cut -f 2 -d.)
  24. # Path Variables
  25. INSTALL_DIR="/opt/salt"
  26. BIN_DIR="$INSTALL_DIR/bin"
  27. CONFIG_DIR="/etc/salt"
  28. TEMP_DIR="/tmp"
  29. SBIN_DIR="/usr/local/sbin"
  30. ###############################################################################
  31. # Set up logging and error handling
  32. ###############################################################################
  33. echo "Post install script started on:" > "$TEMP_DIR/postinstall.txt"
  34. date "+%Y/%m/%d %H:%m:%S" >> "$TEMP_DIR/postinstall.txt"
  35. trap 'quit_on_error $LINENO $BASH_COMMAND' ERR
  36. quit_on_error() {
  37. echo "$(basename $0) caught error on line : $1 command was: $2" >> "$TEMP_DIR/postinstall.txt"
  38. exit -1
  39. }
  40. ###############################################################################
  41. # Check for existing minion config, copy if it doesn't exist
  42. ###############################################################################
  43. if [ ! -f "$CONFIG_DIR/minion" ]; then
  44. echo "Config: Copy Started..." >> "$TEMP_DIR/postinstall.txt"
  45. cp "$CONFIG_DIR/minion.dist" "$CONFIG_DIR/minion"
  46. echo "Config: Copied Successfully" >> "$TEMP_DIR/postinstall.txt"
  47. fi
  48. ###############################################################################
  49. # Create symlink to salt-config.sh
  50. ###############################################################################
  51. if [ ! -d "$SBIN_DIR" ]; then
  52. echo "Symlink: Creating $SBIN_DIR..." >> "$TEMP_DIR/postinstall.txt"
  53. mkdir "$SBIN_DIR"
  54. echo "Symlink: Created Successfully" >> "$TEMP_DIR/postinstall.txt"
  55. fi
  56. echo "Symlink: Creating symlink for salt-config..." >> "$TEMP_DIR/postinstall.txt"
  57. ln -sf "$BIN_DIR/salt-config.sh" "$SBIN_DIR/salt-config"
  58. echo "Symlink: Created Successfully" >> "$TEMP_DIR/postinstall.txt"
  59. ###############################################################################
  60. # Add salt to paths.d
  61. ###############################################################################
  62. if [ ! -d "/etc/paths.d" ]; then
  63. echo "Path: Creating paths.d directory..." >> "$TEMP_DIR/postinstall.txt"
  64. mkdir /etc/paths.d
  65. echo "Path: Created Successfully" >> "$TEMP_DIR/postinstall.txt"
  66. fi
  67. echo "Path: Adding salt to the path..." >> "$TEMP_DIR/postinstall.txt"
  68. sh -c "echo \"$BIN_DIR\" > /etc/paths.d/salt"
  69. sh -c "echo \"$SBIN_DIR\" >> /etc/paths.d/salt"
  70. echo "Path: Added Successfully" >> "$TEMP_DIR/postinstall.txt"
  71. ###############################################################################
  72. # Register Salt as a service
  73. ###############################################################################
  74. setup_services_maverick() {
  75. echo "Service: Using old (< 10.10) launchctl interface" >> "$TEMP_DIR/postinstall.txt"
  76. if /bin/launchctl list "com.saltstack.salt.minion" &> /dev/null; then
  77. echo "Service: Stopping salt-minion..." >> "$TEMP_DIR/postinstall.txt"
  78. launchctl unload -w /Library/LaunchDaemons/com.saltstack.salt.minion.plist
  79. echo "Service: Stopped Successfully" >> "$TEMP_DIR/postinstall.txt"
  80. fi;
  81. echo "Service: Starting salt-minion..." >> "$TEMP_DIR/postinstall.txt"
  82. launchctl load -w /Library/LaunchDaemons/com.saltstack.salt.minion.plist || return 1
  83. echo "Service: Started Successfully" >> "$TEMP_DIR/postinstall.txt"
  84. echo "Service: Disabling Master, Syndic, and API services..." >> "$TEMP_DIR/postinstall.txt"
  85. launchctl unload -w /Library/LaunchDaemons/com.saltstack.salt.api.plist
  86. launchctl unload -w /Library/LaunchDaemons/com.saltstack.salt.master.plist
  87. launchctl unload -w /Library/LaunchDaemons/com.saltstack.salt.syndic.plist
  88. echo "Service: Disabled Successfully" >> "$TEMP_DIR/postinstall.txt"
  89. return 0
  90. }
  91. setup_services_yosemite_and_later() {
  92. echo "Service: Using new (>= 10.10) launchctl interface" >> "$TEMP_DIR/postinstall.txt"
  93. echo "Service: Enabling salt-minion..." >> "$TEMP_DIR/postinstall.txt"
  94. launchctl enable system/com.saltstack.salt.minion
  95. echo "Service: Enabled Successfully" >> "$TEMP_DIR/postinstall.txt"
  96. echo "Service: Bootstrapping salt-minion..." >> "$TEMP_DIR/postinstall.txt"
  97. launchctl bootstrap system /Library/LaunchDaemons/com.saltstack.salt.minion.plist
  98. echo "Service: Bootstrapped Successfully" >> "$TEMP_DIR/postinstall.txt"
  99. if /bin/launchctl list "com.saltstack.salt.minion" &> /dev/null; then
  100. echo "Service: Service Running" >> "$TEMP_DIR/postinstall.txt"
  101. else
  102. echo "Service: Kickstarting Service..." >> "$TEMP_DIR/postinstall.txt"
  103. launchctl kickstart -kp system/com.saltstack.salt.minion
  104. echo "Service: Kickstarted Successfully" >> "$TEMP_DIR/postinstall.txt"
  105. fi
  106. echo "Service: Started Successfully" >> "$TEMP_DIR/postinstall.txt"
  107. echo "Service: Disabling Master, Syndic, and API services" >> "$TEMP_DIR/postinstall.txt"
  108. launchctl disable system/com.saltstack.salt.master
  109. launchctl disable system/com.saltstack.salt.syndic
  110. launchctl disable system/com.saltstack.salt.api
  111. echo "Service: Disabled Successfully" >> "$TEMP_DIR/postinstall.txt"
  112. return 0
  113. }
  114. echo "Service: Configuring..." >> "$TEMP_DIR/postinstall.txt"
  115. case $MINOR in
  116. 9 )
  117. setup_services_maverick;
  118. ;;
  119. * )
  120. setup_services_yosemite_and_later;
  121. ;;
  122. esac
  123. echo "Service: Configured Successfully" >> "$TEMP_DIR/postinstall.txt"
  124. echo "Post install completed successfully on:" >> "$TEMP_DIR/postinstall.txt"
  125. date "+%Y/%m/%d %H:%m:%S" >> "$TEMP_DIR/postinstall.txt"
  126. exit 0