#!/bin/ksh93 typeset -r VERSION='1.0' FPROG=${.sh.file} PROG=${FPROG##*/} SDIR=${FPROG%/*} function showUsage { [[ -n $1 ]] && X='-?' || X='--man' getopts -a ${PROG} "${ print ${USAGE} ; }" OPT $X } function checkDB { [[ ${DB_ADAPTER} == 'sqlite3' ]] || return 0 typeset X X=${ grep '^Group=' /lib/systemd/system/greenlight:${SOPT[INSTANCE]}; } X="${X#*=}" [[ -z $X ]] && X='webservd' chgrp "$X" "${DB_NAME}" chmod g+w ${DB_NAME} } function checkEnv { [[ -z ${SOPT[INSTANCE]} ]] && SOPT[INSTANCE]='bbb' if [[ -z ${SOPT[REPO]} ]]; then if ! cd ~webadm/webapp 2>/dev/null ; then print -u2 "Unable to 'cd ~webadm/webapp' - exiting." return 1 fi elif ! cd ${SOPT[REPO]} 2>/dev/null ; then print -u2 "Unable to 'cd ${SOPT[REPO]}' - exiting." return 1 fi if [[ -e ${SOPT[REPO]}/${SOPT[INSTANCE]}.env ]]; then . ${SOPT[REPO]}/${SOPT[INSTANCE]}.env else print -u2 "Missing '${SOPT[REPO]}/${SOPT[INSTANCE]}.env' - exiting." return 2 fi } function doMain { [[ -z $1 ]] && showUsage 1 && return 0 case "$1" in start) checkEnv || return $? bin/puma --control-url=${CTRL_SOCKET} \ -C config/puma.rb --control-token= ;; stop|restart|status|stats|halt|phased-restart) checkEnv || return $? bin/pumactl --control-url=${CTRL_SOCKET} "$1" ;; checkDB) checkDB ;; fixSocket) chacl u::rwx,g::rwx,o::r--,g:staff:rwx,m::rwx ${CTRL_SOCKET} ;; *) print -u2 "Unknown operand '$1'." showUsage 1 ;; esac } unset SOPT; typeset -A SOPT USAGE="[-?${VERSION}"' ] [-copyright?Copyright (c) 2020 Jens Elkner. All rights reserved.] [-license?CDDL 1.0] [+NAME?'"${PROG}"' - Greenlight service management.] [+DESCRIPTION?Script to manage a Greenlight service instance. It is usually used by systemd to control several Greenlight instances alias services. The operand checkDB tries to change the group of the DB file to the same value of \bGroup=\b in the \b/lib/systemd/system/greenlight:\aINAME\a file, and to make it readable for this group, which defaults to \bwebservd\b. The \bfixSocket\b operand just tries to apply an ACL command to the puma control socket, so that users of the group \bstaff\b are able to control the server as well. All other operands are passed to puma as is and will cause the related action.] [h:help?Print this help and exit.] [F:functions?Print a list of all functions available and exit.] [T:trace]:[functionList?A comma separated list of functions of this script to trace (convinience for troubleshooting).] [i:instance]:[name?The \aname\a of the instance to manage. Default: \bbbb\b] [r:repo]:[path?The \apath\a where the greenlight repository got cloned to, and which contains the instance related settings file \aINAME\a\b.env\b. Default: \b~webadm/webapp\b] \n\n[start|stop|restart|status|stats|halt|phased-restart|checkDB|fixSocket] ' X="${ print ${USAGE} ; }" while getopts "${X}" OPT ; do case ${OPT} in h) showUsage ; exit 0 ;; T) if [[ ${OPTARG} == 'ALL' ]]; then typeset -ft ${ typeset +f ; } else typeset -ft ${OPTARG//,/ } fi ;; F) typeset +f && exit 0 ;; i) SOPT[INSTANCE]="${OPTARG}" ;; r) SOPT[REPO]="${OPTARG}" ;; *) showUsage 1 ; exit 1 ;; esac done X=$((OPTIND-1)) shift $X && OPTIND=1 unset X doMain "$@"