Add post-start option to os-svc-daemon.

We also migrate os-svc-daemon to supporting switches to modify its
behavior and be more flexible as it grows. The old positional style is
still supported but will be deprecated.

Each service will have a unique way to verify that it has started. The
default 'exec sleep 1' will be sufficient for some, but not all. Quantum
is the first example, which can take more than 5 seconds to open a
listening socket.

Due to a lack of knowledge and testing on systemd, it will simply print
out a warning about the issue until we have a better answer.

Change-Id: I8d0e0842dfa66b2e32b942270ed071e593d3865e
This commit is contained in:
Clint Byrum 2013-05-16 10:16:24 -07:00
parent acd6f161f5
commit b969532109
2 changed files with 80 additions and 13 deletions

View File

@ -4,7 +4,7 @@ Command line utilities to simplify instalation of OpenStack services.
Given a git repo url, pip-install the repo and all of its python dependencies into a virtualenv under /opt/stack/venvs.
## os-svc-daemon
Given a system service command line and run-as user, generate and install system service start script.
Given a system service command line and run-as user, generate and install system service start script. See output of `os-svc-daemon -h` for online help.
## example usage
@ -13,6 +13,6 @@ Given a system service command line and run-as user, generate and install system
os-svc-install -u nova -n nova-all -c 'nova-all --someoption' -r https://github.com/openstack/nova.git
# install a system-start script for nova-api
os-svc-daemon nova-api nova /opt/stack/venvs/nova/bin/nova-api --config-dir /etc/nova
os-svc-daemon -n nova-api -u nova -c /opt/stack/venvs/nova/bin/nova-api -- --config-dir /etc/nova
```

View File

@ -1,6 +1,70 @@
#!/bin/bash
set -eu
DEFAULT_POSTSTART="exec sleep 1"
usage() {
echo "Usage: os-svc-daemon [ -ph ] [ -s POSTSTART ] -n SERVICENAME -u RUNAS -c RUNCMD -- [arg [arg...]]"
echo ""
echo "SERVICENAME, RUNAS, RUNCMD, and POSTSTART can be set via the"
echo "environment as well. Command line arguments will override"
echo "environment variables."
echo ""
echo " -h Show help and exit"
echo " -p Print the job file instead of writing to disk"
echo " -s POSTSTART post_start will be added to the upstart job. Ignored with systemd."
echo " default: $DEFAULT_POSTSTART"
echo " -n SERVICENAME Name of job/service file."
echo " -u RUNAS User to run main executable as."
echo " -c RUNCMD Command to execute. Must stay in foreground."
echo " arg... Arguments will be passed to COMMAND"
echo ""
}
# Can be set in environment now
SERVICENAME=${SERVICENAME:-""}
RUNAS=${RUNAS:-""}
RUNCMD=${RUNCMD:-""}
# The default helps avoid race with daemon listening. http://pad.lv/1179766
POSTSTART=${POSTSTART:-$DEFAULT_POSTSTART}
print_only() {
cat
}
print_to_file() {
cat > $1
}
OUTPUT=print_to_file
nshift=0
while getopts "phs:n:u:c:" opt; do
case "$opt" in
n) SERVICENAME=$OPTARG;;
u) RUNAS=$OPTARG;;
c) RUNCMD=$OPTARG;;
s) POSTSTART=$OPTARG;;
p) OUTPUT=print_only;;
h) usage; exit 0;;
\?) usage; exit 1;;
:) usage; exit 1;;
esac
done
shift $(($OPTIND-1))
if [ -z "$SERVICENAME" ] || [ -z "$RUNAS" ] || [ -z "$RUNCMD" ] ; then
if [ $# -lt 3 ] ; then
usage
exit 1
fi
fi
# Compatibility with old style passing w/o switches
[ -n "$SERVICENAME" ] || { SERVICENAME=$1 ; shift; }
[ -n "$RUNAS" ] || { RUNAS=$1 ; shift; }
[ -n "$RUNCMD" ] || { RUNCMD=$1 ; shift; }
function install_upstart {
local name=$1
@ -8,7 +72,7 @@ function install_upstart {
local cmd=$3
shift; shift; shift
local args=$*
cat > /etc/init/$name.conf <<EOF
$OUTPUT /etc/init/$name.conf <<EOF
start on runlevel [2345]
stop on runlevel [016]
@ -21,8 +85,7 @@ respawn
exec start-stop-daemon --start -c $user --exec /opt/stack/venvs/$user/bin/$cmd -- $args
# Help avoid race with daemon listening. http://pad.lv/1179766
post-start exec sleep 1
post-start $POSTSTART
EOF
}
@ -32,7 +95,13 @@ function install_systemd {
local cmd=$3
shift; shift; shift
local args=$*
cat > /etc/systemd/system/$name.service <<EOF
if [ $print_only -eq 1 ] ; then
out=cat
else
out="cat > /etc/systemd/system/$name.service"
fi
$OUTPUT /etc/systemd/system/$name.service <<EOF
[Unit]
Description=$name Service
@ -46,14 +115,12 @@ Alias=$name.service
EOF
}
if [ $# -lt 3 ]; then
echo "Usage: os-svc-daemon DAEMON_NAME USER COMMAND [ARG[ARG[...]]]"
exit 1
fi
# TODO: SysV init fallback support
if [ -d /etc/init ]; then
install_upstart $*
install_upstart $SERVICENAME $RUNAS $RUNCMD $*
elif [ -d /etc/systemd/system ]; then
install_systemd $*
if [ "$POSTSTART" != "$DEFAULT_POSTSTART" ] ; then
echo "WARNING: post start is ignored with systemd." >&2
fi
install_systemd $SERVICENAME $RUNAS $RUNCMD $*
fi