aboutsummaryrefslogtreecommitdiff
path: root/bin/s
diff options
context:
space:
mode:
Diffstat (limited to 'bin/s')
-rwxr-xr-xbin/s103
1 files changed, 69 insertions, 34 deletions
diff --git a/bin/s b/bin/s
index 960cb35..88066f3 100755
--- a/bin/s
+++ b/bin/s
@@ -1,4 +1,18 @@
-#!/usr/bin/mksh
+#!/bin/sh
+
+usage() {
+ cat <<EOUSAGE
+Usage: s [-u <user>] [-w <sec>] <COMMAND> [SERVICE...]
+COMMANDS
+ enable
+ disable
+ up
+ down
+ reload
+ restart
+EOUSAGE
+ exit 1
+}
fatal() {
msg "$@" >&2
@@ -6,41 +20,54 @@ fatal() {
}
msg() {
- printf "$@"
+ printf "$@\n"
}
sv_enable() {
- [ ! -e "${SVDIR}${1}" ] && fatal "service '${1}' not found.\n"
- [ -e "${SERVICEDIR}${1}" ] && fatal "service '${1}' already enabled.\n"
- ln -sfv "${SVDIR}${1}" "${SERVICEDIR}${1}"
}
sv_disable() {
- [ ! -e "${SVDIR}${1}" ] && fatal "service '${1}' not found."
- [ ! -e "${SERVICEDIR}${1}" ] && fatal "service '${1}' already disabled.\n"
- rm -rfv "${SERVICEDIR}${1}"
}
-sv_list() {
- sv s "${SVDIR}"* 2>/dev/null
+sv_status_wants() {
+ dd if="$1/supervise/status" bs=1 skip=17 count=1 2>/dev/null
}
-sv_deps() {
- for service in "$@"; do
- # TODO: check if service exists
- case $(cat "${SVDIR}${service}/supervise/stat") in
- run*)
- /home/duncan/bin/s check "${service}" >/dev/null || wait+=" $service"
+sv_list() {
+ for dir in "$1/"*; do
+ status="disabled"
+ color="$COLOR_FG_GREY"
+ wants=""
+ name="${dir##*/}"
+ [ -e "${dir}/supervise/stat" ] && status=$(cat "${dir}/supervise/stat")
+ [ "$status" != "disabled" ] && wants=$(sv_status_wants "${dir}")
+ case "$status" in
+ run)
+ if [ "$wants" = "u" ]; then
+ color="$COLOR_FG_GREEN"
+ else
+ color="$COLOR_FG_RED"
+ fi
;;
- down*)
- /home/duncan/bin/s u "${service}" >/dev/null && wait+=" $service"
+ down)
+ if [ "$wants" = "d" ]; then
+ color="$COLOR_FG_GREEN"
+ else
+ color="$COLOR_FG_RED"
+ fi
;;
esac
+ printf "%-20s\t$color%-10s$COLOR_FG\n" "$name" "$status"
done
- [ -z "$wait" ] || fatal "wait for dependencies:$wait.\n"
- exit 0
}
+: ${SVCMD:=$(command -v sv)}
+
+: ${COLOR_FG:="\e[0;37;40m"}
+: ${COLOR_FG_RED="\e[0;31;40m"}
+: ${COLOR_FG_GREEN="\e[0;32;40m"}
+: ${COLOR_FG_GREY:="\e[1;30;40m"}
+
while getopts "u:w:v" opt; do
case "$opt" in
u) SVUSER="$OPTARG" ;;
@@ -49,25 +76,33 @@ while getopts "u:w:v" opt; do
done
shift $(($OPTIND - 1))
-: ${SVUSER:=$USER}
-
-case "$SVUSER" in
+case "${SVUSER:=$USER}" in
root)
- SVDIR="/etc/sv/"
- SERVICEDIR="/var/service/"
+ : ${ETCSVDIR:="/etc/sv"}
+ : ${SVDIR:="/var/service"}
;;
*)
- HOMEDIR=$(getent passwd "$SVUSER" | cut -d':' -f6)
- SVDIR="${HOMEDIR}/sv/"
- SERVICEDIR="${HOMEDIR}/service/"
+ : ${ETCSVDIR:="${HOME}/sv"}
+ : ${SVDIR:="${HOME}/service"}
;;
esac
+[ -z "$1" ] && usage
case "$1" in
- ls|list) sv_list "$2" ;;
- enable) sv_enable "$2" ;;
- disable) sv_disable "$2" ;;
- deps) shift 1; sv_deps "$@" ;;
- tree) shift 1; sv_tree "$SVDIR" ;;
- *) SVDIR="$SVDIR" sv "$@" ;;
+ ls|list) sv_list "${ETCSVDIR}" ;;
+ en)
+ [ -z "$2" ] && usage
+ [ -d "${ETCSVDIR}/$2" ] && fatal "service '$2' does not exist"
+ [ -h "$2" ] && fatal "service '$2' is already enabled"
+ ln -sfv "${ETCSVDIR}/${2}" "${SVDIR}/${2}"
+ ;;
+ dis)
+ [ -z "$2" ] && usage
+ [ -d "${ETCSVDIR}/$2" ] && fatal "service '$2' does not exist"
+ [ ! -h "$2" ] && fatal "service '$2' is not enabled"
+ rm -v "${SVDIR}/$2"
+ ;;
+ x|e|X|E|D|T|c|u|d|o|t|p|h|a|i|k|q|1|2|s|r|f|restart)
+ SVDIR="${SVDIR}" "${SVCMD}" "$@"
+ ;;
esac