sonic-buildimage/files/build_templates/docker_image_ctl.j2

156 lines
5.4 KiB
Plaintext
Raw Normal View History

#!/bin/bash
function getMountPoint()
{
echo $1 | python -c "import sys, json, os; mnts = [x for x in json.load(sys.stdin)[0]['Mounts'] if x['Destination'] == '/usr/share/sonic/hwsku']; print '' if len(mnts) == 0 else os.path.basename(mnts[0]['Source'])" 2>/dev/null
}
function getBootType()
{
local TYPE
case "$(cat /proc/cmdline)" in
*SONIC_BOOT_TYPE=fast*)
TYPE='fast'
;;
*SONIC_BOOT_TYPE=warm*)
TYPE='warm'
;;
*)
TYPE="normal"
esac
echo $TYPE
}
function postStartAction()
{
{%- if docker_container_name == "database" %}
REDIS_SOCK="/var/run/redis/redis.sock"
until [[ $(/usr/bin/docker exec database redis-cli -s $REDIS_SOCK ping | grep -c PONG) -gt 0 ]]; do
sleep 1;
done
WARM_DIR=/host/warmboot
SUDO="sudo -n"
BOOT_TYPE=`getBootType`
if [[ "$BOOT_TYPE" == "warm" && -d $WARM_DIR ]]; then
function redisLoadAndDelete()
{
DB="$1"
FILENAME="$2"
test -e $FILENAME || { echo "No file $FILENAME" >&2; exit 10; }
$SUDO redis-load -s $REDIS_SOCK -d $DB -e EMPTY $FILENAME || { echo "Failed to redis-load $FILENAME" >&2; exit 11; }
$SUDO rm $FILENAME || exit 12
}
# Load applDB from /host/warm-reboot/appl_db.json
redisLoadAndDelete 0 $WARM_DIR/appl_db.json
# Load configDB from /host/warm-reboot/config_db.json
redisLoadAndDelete 4 $WARM_DIR/config_db.json
# Load stateDB from /host/warm-reboot/state_db.json
redisLoadAndDelete 6 $WARM_DIR/state_db.json
# Load asicDB from /host/warm-reboot/asic_db.json
redisLoadAndDelete 1 $WARM_DIR/asic_db.json
# Load loglevelDB from /host/warm-reboot/loglevel_db.json
redisLoadAndDelete 3 $WARM_DIR/loglevel_db.json
fi
{%- elif docker_container_name == "swss" %}
docker exec swss rm -f /ready # remove cruft
if [[ "$BOOT_TYPE" == "fast" && -d /host/fast-reboot ]]; then
test -e /host/fast-reboot/fdb.json && docker cp /host/fast-reboot/fdb.json swss:/
test -e /host/fast-reboot/arp.json && docker cp /host/fast-reboot/arp.json swss:/
test -e /host/fast-reboot/default_routes.json && docker cp /host/fast-reboot/default_routes.json swss:/
rm -fr /host/fast-reboot
fi
docker exec swss touch /ready # signal swssconfig.sh to go
{%- elif docker_container_name == "pmon" %}
DEVPATH="/usr/share/sonic/device"
REBOOT="platform_reboot"
PSENSOR="/usr/local/bin/platform_sensors.py"
if [ -d ${DEVPATH}/${PLATFORM} ] && [ -f $PSENSOR ]; then
exist=`docker exec -i pmon ls /usr/bin/platform_sensors.py "$@" 2>/dev/null`
if [ -z "$exist" ]; then
docker cp $PSENSOR pmon:/usr/bin/
fi
fi
{%- elif docker_container_name == "snmp" %}
docker exec -i database redis-cli -n 6 HSET 'DEVICE_METADATA|localhost' chassis_serial_number $(decode-syseeprom -s)
{%- else %}
: # nothing
{%- endif %}
}
start() {
# Obtain our platform as we will mount directories with these names in each docker
PLATFORM=`sonic-cfggen -H -v DEVICE_METADATA.localhost.platform`
{%- if docker_container_name == "database" %}
# Don't mount HWSKU in {{docker_container_name}} container.
HWSKU=""
{%- else %}
# Obtain our HWSKU as we will mount directories with these names in each docker
HWSKU=`sonic-cfggen -d -v 'DEVICE_METADATA["localhost"]["hwsku"]'`
{%- endif %}
DOCKERCHECK=`docker inspect --type container {{docker_container_name}} 2>/dev/null`
if [ "$?" -eq "0" ]; then
DOCKERMOUNT=`getMountPoint "$DOCKERCHECK"`
if [ "$DOCKERMOUNT" == "$HWSKU" ]; then
echo "Starting existing {{docker_container_name}} container with HWSKU $HWSKU"
docker start {{docker_container_name}}
postStartAction
exit $?
fi
# docker created with a different HWSKU, remove and recreate
echo "Removing obsolete {{docker_container_name}} container with HWSKU $DOCKERMOUNT"
docker rm -f {{docker_container_name}}
fi
{%- if docker_container_name == "database" %}
echo "Starting new {{docker_container_name}} container"
{%- else %}
echo "Starting new {{docker_container_name}} container with HWSKU $HWSKU"
{%- endif %}
docker run -d {{docker_image_run_opt}} \
{%- if '--log-driver=json-file' in docker_image_run_opt or '--log-driver' not in docker_image_run_opt %}
--log-opt max-size=2M --log-opt max-file=5 \
{%- endif %}
{%- if docker_container_name == "syncd" and sonic_asic_platform == "mellanox" %}
-e SX_SNIFFER_ENABLE \
-e SX_SNIFFER_TARGET \
-e PRM_SNIFFER \
-e PRM_SNIFFER_FILE_PATH \
-v /var/log/mellanox/sniffer:/var/log/mellanox/sniffer:rw \
{%- endif %}
-v /var/run/redis:/var/run/redis:rw \
-v /usr/share/sonic/device/$PLATFORM:/usr/share/sonic/platform:ro \
{%- if docker_container_name != "database" %}
-v /usr/share/sonic/device/$PLATFORM/$HWSKU:/usr/share/sonic/hwsku:ro \
{%- endif %}
--tmpfs /tmp \
--tmpfs /var/tmp \
--name={{docker_container_name}} {{docker_image_name}}:latest || {
echo "Failed to docker run" >&1
exit 4
}
postStartAction
}
attach() {
docker attach --no-stdin {{docker_container_name}}
}
stop() {
docker stop {{docker_container_name}}
}
case "$1" in
start|stop|attach)
$1
;;
*)
echo "Usage: $0 {start|stop|attach}"
exit 1
;;
esac