Updated salt-minion for Salt 2016.11.2

e9
David Murphy 8 years ago
parent 4275e3cc3b
commit 8e767dc6db

@ -24,301 +24,133 @@
# #
# processname: /usr/bin/salt-minion # processname: /usr/bin/salt-minion
# Allow these to be overridden for tests
: "${SALTMINION_BINDIR:=/usr/bin}"
: "${SALTMINION_SYSCONFDIR:=/etc}"
# Default values (can be overridden in settings file) DEBIAN_VERSION=/etc/debian_version
: "${USER:=$(id -nu)}" SUSE_RELEASE=/etc/SuSE-release
SALTMINION="${SALTMINION_BINDIR}/salt-minion" # Source function library.
SALTCALL="${SALTMINION_BINDIR}/salt-call" if [ -f $DEBIAN_VERSION ]; then
# SALTMINION_CONFIGS are newline-separated entries of: MINION_USER CONFIG_DIR break
: "${SALTMINION_CONFIGS:=" elif [ -f $SUSE_RELEASE -a -r /etc/rc.status ]; then
$USER ${SALTMINION_SYSCONFDIR}/salt . /etc/rc.status
"}" else
SALTMINION_ARGS="" . /etc/rc.d/init.d/functions
SALTMINION_TIMEOUT=30
SALTMINION_TICK=1
SERVICE="salt-minion"
# Read in settings file
if [ -f "${SALTMINION_SYSCONFDIR}/default/salt" ]; then
. "${SALTMINION_SYSCONFDIR}/default/salt"
elif [ -f "${SALTMINION_SYSCONFDIR}/sysconfig/salt" ]; then
. "${SALTMINION_SYSCONFDIR}/sysconfig/salt"
fi fi
RETVAL=0 # Default values (can be overridden below)
NS_NOTRIM="--notrim" SALTMINION=/usr/bin/salt-minion
ERROR_TO_DEVNULL="/dev/null" PYTHON=/usr/bin/python
MINION_ARGS=""
_su_cmd() {
local user="$1"
shift
if [ "X$USER" = "X$user" ]; then
eval $1
else
su -l -c "$1" "$user"
fi
}
_get_pid() {
netstat $NS_NOTRIM -ap --protocol=unix 2>$ERROR_TO_DEVNULL \
| sed -r -e "\|\s${SOCK_DIR}/minion_event_${MINION_ID_HASH}_pub\.ipc$|"'!d; s|/.*||; s/.*\s//;' \
| uniq
}
_is_running() {
[ -n "$(_get_pid)" ]
}
_get_salt_config_value() {
_su_cmd \
"$MINION_USER" \
"\
\"$SALTCALL\" \
-c \"$CONFIG_DIR\" \
--no-color \
--local config.get \
\"$1\" \
" \
2>$ERROR_TO_DEVNULL \
| sed -r -e '2!d; s/^\s*//;'
}
_make_id_hash() { if [ -f /etc/default/salt ]; then
# $1 - minion_id . /etc/default/salt
local hasher='' fi
case "$(_get_salt_config_value hash_type)" in SERVICE=salt-minion
(md5) hasher="md5sum";; PROCESS=salt-minion
(sha1) hasher="sha1sum";;
(sha224) hasher="sha224sum";;
(sha256) hasher="sha256sum";;
(sha384) hasher="sha384sum";;
(sha512) hasher="sha512sum";;
(*) echo "ERROR: No salt hash_type specified";;
esac
if [ -n "$hasher" ]; then
printf "$1" | "$hasher" | cut -c 1-10
fi
}
RETVAL=0
start() { start() {
# $1 - config dir echo -n $"Starting salt-minion daemon: "
local retval=0 if [ -f $SUSE_RELEASE ]; then
startproc -f -p /var/run/$SERVICE.pid $SALTMINION -d $MINION_ARGS
if _is_running; then rc_status -v
echo "Service $SERVICE:$MINION_USER:$MINION_ID already running" elif [ -e $DEBIAN_VERSION ]; then
return 0 if [ -f $LOCKFILE ]; then
fi echo -n "already started, lock file found"
RETVAL=1
echo -n "Starting $SERVICE:$MINION_USER:$MINION_ID daemon: " elif $PYTHON $SALTMINION -d $MINION_ARGS >& /dev/null; then
echo -n "OK"
_su_cmd \ RETVAL=0
"$MINION_USER" \ fi
"\
\"$SALTMINION\" \
-c \"$CONFIG_DIR\" \
-d $SALTMINION_ARGS \
${SALTMINION_DEBUG:+-l debug} \
" \
2>$ERROR_TO_DEVNULL \
|| retval=$?
if [ 0 -eq "$retval" ]; then
local endtime=$(($(date '+%s')+$SALTMINION_TIMEOUT))
while ! _is_running; do
if [ "$endtime" -lt "$(date '+%s')" ]; then
echo -n "TIMEOUT "
retval=1
break
fi
sleep $SALTMINION_TICK
done
fi
if [ 0 -eq "$retval" ]; then
echo -n "OK"
else else
echo -n "FAIL" if [[ ! -z "$(pidofproc -p /var/run/$SERVICE.pid $PROCESS)" ]]; then
if [ -n "$SALTMINION_DEBUG" ]; then RETVAL=$?
printf "\nPROCESSES:\n" >&2 echo -n "already running"
ps wwwaxu | grep '[s]alt-minion' >&2 else
printf "\nSOCKETS:\n" >&2 daemon --check $SERVICE $SALTMINION -d $MINION_ARGS
netstat $NS_NOTRIM -ap --protocol=unix | grep 'salt.*minion' >&2 RETVAL=$?
printf "\nLOG_FILE:\n" >&2 [ $RETVAL -eq 0 ] && touch /var/lock/subsys/$SERVICE
tail -n 20 "$LOG_FILE" >&2 echo
printf "\nENVIRONMENT:\n" >&2 return $RETVAL
env >&2
fi fi
fi fi
RETVAL=$?
echo echo
return $RETVAL
return $retval
} }
stop() { stop() {
# $1 - config dir echo -n $"Stopping salt-minion daemon: "
local retval=0 if [ -f $SUSE_RELEASE ]; then
killproc -TERM $SALTMINION
if ! _is_running; then rc_status -v
echo "Service $SERVICE:$MINION_USER:$MINION_ID is not running" RETVAL=$?
return 0 elif [ -f $DEBIAN_VERSION ]; then
fi # Added this since Debian's start-stop-daemon doesn't support spawned processes
if ps -ef | grep "$PYTHON $SALTMINION" | grep -v grep | awk '{print $2}' | xargs kill &> /dev/null; then
echo -n "Stopping $SERVICE:$MINION_USER:$MINION_ID daemon: " echo -n "OK"
local pid="$(_get_pid)" RETVAL=0
else
# pid below is intentionally not quoted in case there are *multiple* echo -n "Daemon is not started"
# minions running with the same configuration. RETVAL=1
_su_cmd "$MINION_USER" "kill -TERM $pid 2>/dev/null" || retval=$? fi
if [ 0 -eq "$retval" ]; then
local endtime=$(($(date '+%s')+$SALTMINION_TIMEOUT))
while _is_running; do
if [ "$endtime" -lt "$(date '+%s')" ]; then
# Try one more time with a big hammer
_su_cmd "$MINION_USER" "kill -KILL $pid 2>/dev/null" || :
sleep $SALTMINION_TICK
if _is_running; then
echo -n "TIMEOUT "
retval=1
fi
break
fi
sleep 1
done
fi
if [ 0 -eq "$retval" ]; then
rm -f "$PID_FILE"
echo -n "OK"
else
echo -n "FAIL"
fi
echo
return $retval
}
status() {
local retval=0
local pid="$(_get_pid)"
if [ -n "$pid" ]; then
# Unquote $pid here to display multiple PIDs in one line
echo "$SERVICE:$MINION_USER:$MINION_ID is running:" $pid
else else
retval=3 killproc $PROCESS
echo "$SERVICE:$MINION_USER:$MINION_ID is stopped." RETVAL=$?
if [ -e "$PID_FILE" ]; then [ $RETVAL -eq 0 ] && rm -f /var/lock/subsys/$SERVICE
echo "$SERVICE:$MINION_USER:$MINION_ID has orphaned pid file: $PID_FILE." # tidy up any rogue processes:
retval=1 PROCS=`ps -ef | grep "$SALTMINION" | grep -v grep | awk '{print $2}'`
if [ -n "$PROCS" ]; then
kill $PROCS &> /dev/null
sleep 1
PROCS=`ps -ef | grep "$SALTMINION" | grep -v grep | awk '{print $2}'`
if [ -n "$PROCS" ]; then
kill -9 $PROCS &> /dev/null
fi
fi fi
fi fi
return $retval echo
} }
restart() { restart() {
# $1 - config dir stop
stop "$1" start
start "$1"
} }
# See how we were called.
main() { case "$1" in
if [ -n "$SALTMINION_DEBUG" ]; then start|stop|restart)
set -x $1
ERROR_TO_DEVNULL="&2" ;;
fi status)
if [ -f $SUSE_RELEASE ]; then
# Check to see if --notrim is a valid netstat option echo -n "Checking for service salt-minion "
if netstat --notrim 2>&1 >/dev/null | grep -q 'unrecognized'; then checkproc $SALTMINION
NS_NOTRIM='' rc_status -v
fi elif [ -f $DEBIAN_VERSION ]; then
if [ -f $LOCKFILE ]; then
# Pre-filter for unhandled commands RETVAL=0
case "$1" in echo "salt-minion is running."
(start|stop|status|restart|condrestart|try-restart) ;; else
(reload) RETVAL=1
echo "Can't reload $SERVICE - you must restart it" echo "salt-minion is stopped."
exit 3 fi
;; else
(*) status $PROCESS
echo "Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload}" RETVAL=$?
exit 2
;;
esac
while read MINION_USER CONFIG_DIR; do
if [ -z "$CONFIG_DIR" ]; then
continue
fi
if ! [ -d "$CONFIG_DIR" ]; then
echo "ERROR: non-existent $SERVICE config directory: $CONFIG_DIR"
RETVAL=1
continue
fi
SOCK_DIR="$(_get_salt_config_value sock_dir)"
PID_FILE="$(_get_salt_config_value pidfile)"
LOG_FILE="$(_get_salt_config_value log_file)"
MINION_ID="$(_get_salt_config_value id)"
MINION_ID_HASH="$(_make_id_hash "$MINION_ID")"
if [ \
-z "$SOCK_DIR" \
-o -z "$PID_FILE" \
-o -z "$LOG_FILE" \
-o -z "$MINION_ID" \
-o -z "$MINION_ID_HASH" \
]; then
echo "ERROR: Unable to look-up config values for $CONFIG_DIR"
RETVAL=1
continue
fi fi
;;
# See how we were called. condrestart)
case "$1" in [ -f $LOCKFILE ] && restart || :
(start|stop|restart|status) ;;
"$1" || RETVAL=$? reload)
;; echo "can't reload configuration, you have to restart it"
(condrestart|try-restart) RETVAL=1
if ! _is_running; then ;;
RETVAL=7 *)
else echo $"Usage: $0 {start|stop|status|restart|condrestart|reload}"
stop exit 1
start || RETVAL=$? ;;
fi esac
;; exit $RETVAL
(*)
echo "Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload}"
RETVAL=2
;;
esac
done <<EOF
$SALTMINION_CONFIGS
EOF
exit $RETVAL
}
if [ "$#" = 0 ]; then
main
else
main "$@"
fi

Loading…
Cancel
Save