Created
February 27, 2015 09:28
-
-
Save codeskyblue/3a884c41242290d479b9 to your computer and use it in GitHub Desktop.
help tool for supervisorctl
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# | |
# help tool of supervisorctl | |
# | |
# reference: http://supervisord.org/ | |
RUNUSER=hzsunshx | |
CONFDIR="/etc/supervisor/conf.d/" | |
CRED='\033[0;31m' | |
CGREEN='\033[0;32m' | |
CEND='\033[0m' | |
die(){ | |
echo -e "${CRED}""$@""${CEND}" | |
exit 1 | |
} | |
status(){ | |
echo ---------------- supervisor status -------------------- | |
supervisorctl status "$@" | | |
awk '$2 == "RUNNING" {printf "\033[0;32m"; print; printf "\033[0m"} $2 != "RUNNING" {print}' | |
} | |
usage(){ | |
cat <<EOF | |
Usage: msv COMMAND [arg...] | |
Commands: | |
help Show help info | |
test Test if program can be started | |
gen Generate new supervisor.conf file | |
add Add supervisor.conf to conf.d, if no conf file found, will run gen before. | |
del Remove from supervisor | |
EOF | |
status $(basename $PWD) | |
exit 0 | |
} | |
test -d "$CONFDIR" || die "dir not found: ${CONFDIR}" | |
test $(id -u) -eq 0 || die "root privilege needed" | |
test $# -eq 0 && usage | |
COMMAND=$1 | |
shift | |
DIRECTORY=$PWD | |
NAME= | |
TARGET= | |
RUNUSER=root | |
_SHIFT=0 | |
parse_args(){ | |
while getopts ":u:d:" o; do | |
case "${o}" in | |
u) #user | |
RUNUSER=${OPTARG} | |
;; | |
d) | |
DIRECTORY=${OPTARG} | |
;; | |
*) | |
usage | |
;; | |
esac | |
done | |
_SHIFT=$((OPTIND-1)) | |
shift $_SHIFT | |
if test $# -eq 0 | |
then | |
NAME=$(basename $PWD) | |
echo "Use name: $NAME" | |
else | |
NAME=$1 | |
fi | |
TARGET=${CONFDIR}/$NAME-supervisor.conf | |
} | |
# <command>: show command info | |
run_help(){ | |
cmd=${1:-other} | |
HEADER="msv ${cmd}" | |
case "${cmd}" in | |
help|gen|test) | |
grep -B 10 "^run_$cmd" $0 | grep "^#" | sed 's/#//' | sed "1,1i$HEADER" | |
;; | |
*) | |
supervisorctl help "$@" | |
;; | |
esac | |
exit 1 | |
} | |
run_test(){ | |
env -i bash start.sh | |
} | |
# [-u USER] [-d DIR] <name> | |
run_gen(){ | |
echo "generate supervisor.conf" | |
cat > supervisor.conf <<EOF | |
[program:$NAME] | |
directory=$DIRECTORY | |
command=bash start.sh | |
autostart=true | |
autorestart=true | |
redirect_stderr=true | |
user=$RUNUSER | |
stopsignal=TERM | |
stopasgroup=true | |
EOF | |
chown $RUNUSER:$RUNUSER supervisor.conf | |
} | |
run_add(){ | |
test -f start.sh || die "start.sh is needed in $PWD" | |
test -f $TARGET && die "[program:$NAME] already exist in supervisord" | |
test -f supervisor.conf || run_gen | |
mv supervisor.conf ${TARGET} | |
supervisorctl update | |
ln -s ${TARGET} $PWD/supervisor.conf | |
status $NAME | |
} | |
run_del(){ | |
test -L supervisor.conf || die "no link file found, wired !!!" | |
NAME=$(cat supervisor.conf | grep -E -o "\[program:(\w+)" | cut -d: -f2) | |
test -z "$NAME" && die "msv fatal: fail to get program name" | |
supervisorctl stop $NAME | |
set -e | |
supervisorctl remove $NAME | |
rm supervisor.conf | |
mv ${TARGET} supervisor.conf | |
} | |
# | |
# main | |
# | |
parse_args "$@" | |
case "$COMMAND" in | |
del|test|help) | |
run_${COMMAND} "$@" | |
;; | |
gen|add) | |
shift $_SHIFT | |
run_${COMMAND} "$@" | |
;; | |
*) | |
supervisorctl "$COMMAND" "$@" | |
;; | |
esac |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment