| # Copyright 2012 The Chromium OS Authors. All rights reserved. |
| # Use of this source code is governed by a BSD-style license that can be |
| # found in the LICENSE file. |
| |
| description "Start servod" |
| author "chromium-os-dev@chromium.org" |
| |
| respawn |
| |
| # If you change the respawn limit, change the log rotation, below. |
| respawn limit 3 10 |
| |
| # This upstart job takes in 4 args, PORT, BOARD, SERIAL and CONFIG. |
| # * Default PORT is 9999. |
| # * The config file is determined by the port, and contains settings for |
| # BOARD (mandatory) and SERIAL (optional, unless there's more than one |
| # servo) and CONFIG (optional) used for passing an extra overlay file |
| # to load. |
| # * Parameters are taken from the config file. |
| # * Parameters on the `start` command will initialize/replace settings in the |
| # config file. |
| |
| env PORT=9999 |
| env BOARD= |
| env SERIAL= |
| env CONFIG= |
| env CONFIG_FILE_DIR="/var/lib/servod" |
| instance $PORT |
| |
| pre-start script |
| mkdir -p /var/lib/servod |
| for CMD in iptables ip6tables ; do |
| $CMD -A INPUT -p tcp --dport $PORT -j ACCEPT || |
| logger -t "${UPSTART_JOB}" "Failed to configure $CMD." |
| done |
| |
| CONFIG_FILE=$CONFIG_FILE_DIR/config_$PORT |
| |
| . /usr/share/cros/servod_utils.sh |
| # We'll want to update the config file with all the args passed in. |
| update_config $CONFIG_FILE BOARD $BOARD |
| update_config $CONFIG_FILE SERIAL $SERIAL |
| update_config $CONFIG_FILE CONFIG $CONFIG |
| |
| end script |
| |
| script |
| LOG=/var/log/servod_$PORT.log |
| |
| # We set OLD to ".4" to correspond to the respawn limit of 3 |
| # above. That way, if we die from the respawn limit, we'll have |
| # logs from all failed respawns, plus the original log that |
| # presumably contains the first failure. |
| OLD=.4 |
| for NEW in .3 .2 .1 ''; do |
| if [ -f $LOG$NEW ]; then |
| mv -f $LOG$NEW $LOG$OLD |
| fi |
| OLD=$NEW |
| done |
| exec >$LOG 2>&1 |
| |
| CONFIG_FILE=$CONFIG_FILE_DIR/config_$PORT |
| if [ ! -f $CONFIG_FILE ]; then |
| logger -t "$UPSTART_JOB" "No configuration file ($CONFIG_FILE); terminating" |
| stop |
| exit 0 |
| fi |
| |
| # Config file must have the board and serial (in multi-servo environment) |
| # declared like so: |
| # BOARD=veyron_minnie |
| # SERIAL=1234 |
| . $CONFIG_FILE |
| if [ -z "$BOARD" ]; then |
| logger -t "$UPSTART_JOB" "No board specified; terminating" |
| stop |
| exit 0 |
| fi |
| |
| SERIAL_FLAG="" |
| SERIAL_MSG="" |
| if [ -z "$SERIAL" ]; then |
| logger -t "$UPSTART_JOB" "No serial specified" |
| else |
| SERIAL_FLAG="--serialname ${SERIAL}" |
| SERIAL_MSG="using servo serial $SERIAL" |
| fi |
| |
| logger -t "$UPSTART_JOB" \ |
| "Launching servod for $BOARD on port $PORT $SERIAL_MSG" |
| BOARD_FLAG="--board ${BOARD}" |
| PORT_FLAG="--port ${PORT}" |
| |
| if [ "$DEBUG" = "1" ]; then |
| DEBUG_FLAG="--debug" |
| else |
| DEBUG_FLAG="" |
| fi |
| |
| CONFIG_FLAG="" |
| if [ ! -z "$CONFIG" ]; then |
| CONFIG_FLAG="--config ${CONFIG}" |
| fi |
| |
| exec servod --host 0.0.0.0 $BOARD_FLAG $SERIAL_FLAG $PORT_FLAG $DEBUG_FLAG $CONFIG_FLAG |
| end script |
| |
| post-stop script |
| for CMD in iptables ip6tables ; do |
| $CMD -D INPUT -p tcp --dport $PORT -j ACCEPT || true |
| done |
| end script |