salt-minion 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  1. #!/bin/sh
  2. #
  3. # Salt minion
  4. ###################################
  5. # LSB header
  6. ### BEGIN INIT INFO
  7. # Provides: salt-minion
  8. # Required-Start: $all
  9. # Required-Stop:
  10. # Default-Start: 2 3 4 5
  11. # Default-Stop: 0 1 6
  12. # Short-Description: Salt minion daemon
  13. # Description: This is the Salt minion daemon that can be controlled by the
  14. # Salt master.
  15. ### END INIT INFO
  16. # chkconfig header
  17. # chkconfig: 345 97 04
  18. # description: This is the Salt minion daemon that can be controlled by the Salt master.
  19. #
  20. # processname: /usr/bin/salt-minion
  21. # Allow these to be overridden for tests
  22. : "${SALTMINION_BINDIR:=/usr/bin}"
  23. : "${SALTMINION_SYSCONFDIR:=/etc}"
  24. # Default values (can be overridden in settings file)
  25. : "${USER:=$(id -nu)}"
  26. SALTMINION="${SALTMINION_BINDIR}/salt-minion"
  27. SALTCALL="${SALTMINION_BINDIR}/salt-call"
  28. # SALTMINION_CONFIGS are newline-separated entries of: MINION_USER CONFIG_DIR
  29. : "${SALTMINION_CONFIGS:="
  30. $USER ${SALTMINION_SYSCONFDIR}/salt
  31. "}"
  32. SALTMINION_ARGS=""
  33. SALTMINION_TIMEOUT=30
  34. SALTMINION_TICK=1
  35. SERVICE="salt-minion"
  36. # Read in settings file
  37. if [ -f "${SALTMINION_SYSCONFDIR}/default/salt" ]; then
  38. . "${SALTMINION_SYSCONFDIR}/default/salt"
  39. elif [ -f "${SALTMINION_SYSCONFDIR}/sysconfig/salt" ]; then
  40. . "${SALTMINION_SYSCONFDIR}/sysconfig/salt"
  41. fi
  42. RETVAL=0
  43. NS_NOTRIM="--notrim"
  44. ERROR_TO_DEVNULL="/dev/null"
  45. _su_cmd() {
  46. local user="$1"
  47. shift
  48. if [ "X$USER" = "X$user" ]; then
  49. eval $1
  50. else
  51. su -l -c "$1" "$user"
  52. fi
  53. }
  54. _get_pid() {
  55. cat $PID_FILE 2>/dev/null
  56. }
  57. _is_running() {
  58. [ -n "$(_get_pid)" ] && ps wwwaxu | grep '[s]alt-minion' | awk '{print $2}' | grep -qi "\b$(_get_pid)\b"
  59. }
  60. _get_salt_config_value() {
  61. _su_cmd \
  62. "$MINION_USER" \
  63. "\
  64. \"$SALTCALL\" \
  65. -c \"$CONFIG_DIR\" \
  66. --no-color \
  67. --skip-grains \
  68. --local config.get \
  69. \"$1\" \
  70. " \
  71. 2>$ERROR_TO_DEVNULL \
  72. | sed -r -e '2!d; s/^\s*//;'
  73. }
  74. _make_id_hash() {
  75. # $1 - minion_id
  76. local hasher=''
  77. case "$(_get_salt_config_value hash_type)" in
  78. (md5) hasher="md5sum";;
  79. (sha1) hasher="sha1sum";;
  80. (sha224) hasher="sha224sum";;
  81. (sha256) hasher="sha256sum";;
  82. (sha384) hasher="sha384sum";;
  83. (sha512) hasher="sha512sum";;
  84. (*) echo "ERROR: No salt hash_type specified";;
  85. esac
  86. if [ -n "$hasher" ]; then
  87. printf "$1" | "$hasher" | cut -c 1-10
  88. fi
  89. }
  90. start() {
  91. # $1 - config dir
  92. local retval=0
  93. if _is_running; then
  94. echo "Service $SERVICE:$MINION_USER:$MINION_ID already running"
  95. return 0
  96. fi
  97. echo -n "Starting $SERVICE:$MINION_USER:$MINION_ID daemon: "
  98. _su_cmd \
  99. "$MINION_USER" \
  100. "\
  101. \"$SALTMINION\" \
  102. -c \"$CONFIG_DIR\" \
  103. -d $SALTMINION_ARGS \
  104. ${SALTMINION_DEBUG:+-l debug} \
  105. " \
  106. 2>$ERROR_TO_DEVNULL \
  107. || retval=$?
  108. if [ 0 -eq "$retval" ]; then
  109. local endtime=$(($(date '+%s')+$SALTMINION_TIMEOUT))
  110. while ! _is_running; do
  111. if [ "$endtime" -lt "$(date '+%s')" ]; then
  112. echo -n "TIMEOUT "
  113. retval=1
  114. break
  115. fi
  116. sleep $SALTMINION_TICK
  117. done
  118. fi
  119. if [ 0 -eq "$retval" ]; then
  120. echo -n "OK"
  121. else
  122. echo -n "FAIL"
  123. if [ -n "$SALTMINION_DEBUG" ]; then
  124. printf "\nPROCESSES:\n" >&2
  125. ps wwwaxu | grep '[s]alt-minion' >&2
  126. printf "\nSOCKETS:\n" >&2
  127. netstat -n $NS_NOTRIM -ap --protocol=unix | grep 'salt.*minion' >&2
  128. printf "\nLOG_FILE:\n" >&2
  129. tail -n 20 "$LOG_FILE" >&2
  130. printf "\nENVIRONMENT:\n" >&2
  131. env >&2
  132. fi
  133. fi
  134. echo
  135. return $retval
  136. }
  137. stop() {
  138. # $1 - config dir
  139. local retval=0
  140. if ! _is_running; then
  141. echo "Service $SERVICE:$MINION_USER:$MINION_ID is not running"
  142. return 0
  143. fi
  144. echo -n "Stopping $SERVICE:$MINION_USER:$MINION_ID daemon: "
  145. local pid="$(_get_pid)"
  146. # pid below is intentionally not quoted in case there are *multiple*
  147. # minions running with the same configuration.
  148. _su_cmd "$MINION_USER" "kill -TERM $pid 2>/dev/null" || retval=$?
  149. if [ 0 -eq "$retval" ]; then
  150. local endtime=$(($(date '+%s')+$SALTMINION_TIMEOUT))
  151. while _is_running; do
  152. if [ "$endtime" -lt "$(date '+%s')" ]; then
  153. # Try one more time with a big hammer
  154. _su_cmd "$MINION_USER" "kill -KILL $pid 2>/dev/null" || :
  155. sleep $SALTMINION_TICK
  156. if _is_running; then
  157. echo -n "TIMEOUT "
  158. retval=1
  159. fi
  160. break
  161. fi
  162. sleep 1
  163. done
  164. fi
  165. if [ 0 -eq "$retval" ]; then
  166. rm -f "$PID_FILE"
  167. echo -n "OK"
  168. else
  169. echo -n "FAIL"
  170. fi
  171. echo
  172. return $retval
  173. }
  174. status() {
  175. local retval=0
  176. local pid="$(_get_pid)"
  177. if _is_running; then
  178. # Unquote $pid here to display multiple PIDs in one line
  179. echo "$SERVICE:$MINION_USER:$MINION_ID is running:" $pid
  180. else
  181. retval=3
  182. echo "$SERVICE:$MINION_USER:$MINION_ID is stopped."
  183. if [ -e "$PID_FILE" ]; then
  184. echo "$SERVICE:$MINION_USER:$MINION_ID has orphaned pid file: $PID_FILE."
  185. retval=1
  186. fi
  187. fi
  188. return $retval
  189. }
  190. restart() {
  191. # $1 - config dir
  192. stop "$1"
  193. start "$1"
  194. }
  195. main() {
  196. if [ -n "$SALTMINION_DEBUG" ]; then
  197. set -x
  198. ERROR_TO_DEVNULL="&2"
  199. fi
  200. # Check to see if --notrim is a valid netstat option
  201. if ! ( netstat --help 2>&1 | grep -wq '\-\-notrim') ; then
  202. NS_NOTRIM=''
  203. fi
  204. # Pre-filter for unhandled commands
  205. case "$1" in
  206. (start|stop|status|restart|condrestart|try-restart) ;;
  207. (reload)
  208. echo "Can't reload $SERVICE - you must restart it"
  209. exit 3
  210. ;;
  211. (*)
  212. echo "Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload}"
  213. exit 2
  214. ;;
  215. esac
  216. while read MINION_USER CONFIG_DIR; do
  217. if [ -z "$CONFIG_DIR" ]; then
  218. continue
  219. fi
  220. if ! [ -d "$CONFIG_DIR" ]; then
  221. echo "ERROR: non-existent $SERVICE config directory: $CONFIG_DIR"
  222. RETVAL=1
  223. continue
  224. fi
  225. SOCK_DIR="$(_get_salt_config_value sock_dir)"
  226. PID_FILE="$(_get_salt_config_value pidfile)"
  227. LOG_FILE="$(_get_salt_config_value log_file)"
  228. MINION_ID="$(_get_salt_config_value id)"
  229. MINION_ID_HASH="$(_make_id_hash "$MINION_ID")"
  230. if [ \
  231. -z "$SOCK_DIR" \
  232. -o -z "$PID_FILE" \
  233. -o -z "$LOG_FILE" \
  234. -o -z "$MINION_ID" \
  235. -o -z "$MINION_ID_HASH" \
  236. ]; then
  237. echo "ERROR: Unable to look-up config values for $CONFIG_DIR"
  238. RETVAL=1
  239. continue
  240. fi
  241. # See how we were called.
  242. case "$1" in
  243. (start|stop|restart|status)
  244. "$1" || RETVAL=$?
  245. ;;
  246. (condrestart|try-restart)
  247. if ! _is_running; then
  248. RETVAL=7
  249. else
  250. stop
  251. start || RETVAL=$?
  252. fi
  253. ;;
  254. (*)
  255. echo "Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload}"
  256. RETVAL=2
  257. ;;
  258. esac
  259. done <<EOF
  260. $SALTMINION_CONFIGS
  261. EOF
  262. exit $RETVAL
  263. }
  264. if [ "$#" = 0 ]; then
  265. main
  266. else
  267. main "$@"
  268. fi