Log directly to syslog from os services

We currently have a horrible hack for upstart where we run our services
with stdout and stderr piped into logger. There are lots of issues with
this, such as upstart has a hard time knowing which PID to track.
Conveniently, python logging knows how to syslog, so why not just use
that!

This changes the default behaviour of os-svc-daemon to create a python
logging.conf so services log directly to syslog.

This also changes the default upstart respawn limiting to be 2 fails in
10 secs. This is becauce making upstart monitor the actual service
daemon was causing some services to respawn loop due to time it took
them to fail.

Closes-Bug: #1385346

Change-Id: Ibd491881d79160ad1457526d51512f5fc1cf9ea5
This commit is contained in:
Gregory Haynes 2014-12-03 12:13:39 -08:00
parent 7f40375cd8
commit 5fc2040aa5
3 changed files with 58 additions and 19 deletions

View File

@ -8,11 +8,13 @@ usage() {
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 "environment variables. By default this will create a python logging"
echo "configuration file in /etc/os-logging/servicename.conf"
echo ""
echo " -a Use alternate svc-map instead of map-services"
echo " -h Show help and exit"
echo " -p Print the job file instead of writing to disk"
echo " -l Create neither a python logging.conf nor pass --log-config-append argument to command."
echo " -d [NAME] Specify the name of the runtime directory, which will be"
echo " /var/run/[NAME]"
echo " -s POSTSTART post_start will be added to the upstart job. Ignored with systemd."
@ -32,6 +34,7 @@ INSTALLDIR=
RUNAS=${RUNAS:-""}
RUNCMD=${RUNCMD:-""}
ENV=${ENV:-""}
DISABLE_LOGGING_CONF=
CREATE_DIR_NAME=${CREATE_DIR_NAME:-""}
# The default helps avoid race with daemon listening. http://pad.lv/1179766
POSTSTART=${POSTSTART:-$DEFAULT_POSTSTART}
@ -53,7 +56,7 @@ OUTPUT=print_to_file
APPEND=append_to_file
nshift=0
while getopts "aphd:s:n:i:u:c:e:" opt; do
while getopts "aplhd:s:n:i:u:c:e:" opt; do
case "$opt" in
n) SERVICENAME=$OPTARG;;
i) INSTALLDIR=$OPTARG;;
@ -63,6 +66,7 @@ while getopts "aphd:s:n:i:u:c:e:" opt; do
e) ENV=$OPTARG;;
a) MAPPING_COMMAND=svc-map;;
p) OUTPUT=print_only; APPEND=print_only;;
l) DISABLE_LOGGING_CONF="1";;
d) CREATE_DIR_NAME=$OPTARG;;
h) usage; exit 0;;
\?) usage; exit 1;;
@ -92,6 +96,33 @@ if [ -z "$INSTALLDIR" ]; then
INSTALLDIR="/opt/stack/venvs/$RUNAS"
fi
if [ -z "$DISABLE_LOGGING_CONF" ]; then
# Set up service-specific logging config
LOGGING_CONFIG="/etc/os-logging/$SERVICENAME.config"
mkdir /etc/os-logging || true
$OUTPUT $LOGGING_CONFIG <<EOF
[loggers]
keys=root
[handlers]
keys=syslog
[formatters]
keys=normal
[logger_root]
handlers=syslog
[handler_syslog]
class=handlers.SysLogHandler
args=('/dev/log', handlers.SysLogHandler.LOG_USER)
formatter=normal
[formatter_normal]
format=$SERVICENAME: %(asctime)s %(levelname)s %(message)s
EOF
fi
function install_upstart {
local name=$1
local install_dir=$2
@ -109,6 +140,10 @@ env }
fi
local target_file="/etc/init/$name.conf"
if [ -z "$DISABLE_LOGGING_CONF" ]; then
args="--log-config-append $LOGGING_CONFIG $args"
fi
$OUTPUT $target_file <<EOF
start on runlevel [2345]
stop on runlevel [016]
@ -128,13 +163,13 @@ EOF
respawn
# the default post-start of 1 second sleep delays respawning enough to
# not hit the default of 10 times in 5 seconds. Make it 2 times in 5s.
respawn limit 2 5
# not hit the default of 10 times in 5 seconds. Make it 2 times in 10s.
respawn limit 2 10
# We're logging to syslog
console none
exec start-stop-daemon --start -c $user --exec $install_dir/bin/$cmd -- $args 2>&1 | logger -t $name
exec start-stop-daemon --start -c $user --exec $install_dir/bin/$cmd -- $args
post-start $POSTSTART
EOF
@ -156,6 +191,10 @@ function install_systemd {
Environment=}
fi
if [ -z "$DISABLE_LOGGING_CONF" ]; then
args="--log-config-append $LOGGING_CONFIG $args"
fi
$OUTPUT /lib/systemd/system/$name.service <<EOF
[Unit]
Description=$name Service

View File

@ -10,6 +10,6 @@ $SWIFT_VENV_DIR/bin/pip install keystonemiddleware
$SWIFT_VENV_DIR/bin/pip install python-swiftclient
$SWIFT_VENV_DIR/bin/pip install pycrypto
os-svc-daemon -i "$SWIFT_VENV_DIR" swift-proxy swift swift-proxy-server "/etc/swift/proxy-server.conf"
os-svc-daemon -l -i "$SWIFT_VENV_DIR" swift-proxy swift swift-proxy-server "/etc/swift/proxy-server.conf"
ln -s $SWIFT_VENV_DIR/bin/swift-recon /usr/local/bin/swift-recon

View File

@ -2,20 +2,20 @@
set -eux
os-svc-daemon -i "$SWIFT_VENV_DIR" swift-account swift swift-account-server /etc/swift/account-server.conf
os-svc-daemon -i "$SWIFT_VENV_DIR" swift-account-auditor swift swift-account-auditor /etc/swift/account-server.conf
os-svc-daemon -i "$SWIFT_VENV_DIR" swift-account-reaper swift swift-account-reaper /etc/swift/account-server.conf
os-svc-daemon -i "$SWIFT_VENV_DIR" swift-account-replicator swift swift-account-replicator /etc/swift/account-server.conf
os-svc-daemon -l -i "$SWIFT_VENV_DIR" swift-account swift swift-account-server /etc/swift/account-server.conf
os-svc-daemon -l -i "$SWIFT_VENV_DIR" swift-account-auditor swift swift-account-auditor /etc/swift/account-server.conf
os-svc-daemon -l -i "$SWIFT_VENV_DIR" swift-account-reaper swift swift-account-reaper /etc/swift/account-server.conf
os-svc-daemon -l -i "$SWIFT_VENV_DIR" swift-account-replicator swift swift-account-replicator /etc/swift/account-server.conf
os-svc-daemon -i "$SWIFT_VENV_DIR" swift-container swift swift-container-server /etc/swift/container-server.conf
os-svc-daemon -i "$SWIFT_VENV_DIR" swift-container-auditor swift swift-container-auditor /etc/swift/container-server.conf
os-svc-daemon -i "$SWIFT_VENV_DIR" swift-container-replicator swift swift-container-replicator /etc/swift/container-server.conf
os-svc-daemon -i "$SWIFT_VENV_DIR" swift-container-sync swift swift-container-sync /etc/swift/container-server.conf
os-svc-daemon -i "$SWIFT_VENV_DIR" swift-container-updater swift swift-container-updater /etc/swift/container-server.conf
os-svc-daemon -l -i "$SWIFT_VENV_DIR" swift-container swift swift-container-server /etc/swift/container-server.conf
os-svc-daemon -l -i "$SWIFT_VENV_DIR" swift-container-auditor swift swift-container-auditor /etc/swift/container-server.conf
os-svc-daemon -l -i "$SWIFT_VENV_DIR" swift-container-replicator swift swift-container-replicator /etc/swift/container-server.conf
os-svc-daemon -l -i "$SWIFT_VENV_DIR" swift-container-sync swift swift-container-sync /etc/swift/container-server.conf
os-svc-daemon -l -i "$SWIFT_VENV_DIR" swift-container-updater swift swift-container-updater /etc/swift/container-server.conf
os-svc-daemon -i "$SWIFT_VENV_DIR" swift-object swift swift-object-server /etc/swift/object-server.conf
os-svc-daemon -i "$SWIFT_VENV_DIR" swift-object-auditor swift swift-object-auditor /etc/swift/object-server.conf
os-svc-daemon -i "$SWIFT_VENV_DIR" swift-object-replicator swift swift-object-replicator /etc/swift/object-server.conf
os-svc-daemon -i "$SWIFT_VENV_DIR" swift-object-updater swift swift-object-updater /etc/swift/object-server.conf
os-svc-daemon -l -i "$SWIFT_VENV_DIR" swift-object swift swift-object-server /etc/swift/object-server.conf
os-svc-daemon -l -i "$SWIFT_VENV_DIR" swift-object-auditor swift swift-object-auditor /etc/swift/object-server.conf
os-svc-daemon -l -i "$SWIFT_VENV_DIR" swift-object-replicator swift swift-object-replicator /etc/swift/object-server.conf
os-svc-daemon -l -i "$SWIFT_VENV_DIR" swift-object-updater swift swift-object-updater /etc/swift/object-server.conf
ln -s $SWIFT_VENV_DIR/bin/swift-recon-cron /usr/local/bin/swift-recon-cron