[service template] Starting new docker when HWSKU change is detected (#946)
Existing dockers has paths mounted according to the HWSKU. When HWSKU changes, these dockers need to be destroyed and recreated with the correct paths mounted.
This commit is contained in:
parent
2e3975d6ed
commit
56ae5edc7f
@ -1,17 +1,56 @@
|
|||||||
#!/bin/bash
|
#!/bin/bash
|
||||||
|
|
||||||
# Obtain our platform and HWSKU as we will mount directories with these names in each docker
|
function getMountPoint()
|
||||||
PLATFORM=`sonic-cfggen -v platform`
|
{
|
||||||
|
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 postStartAction()
|
||||||
|
{
|
||||||
{%- if docker_container_name != "database" %}
|
{%- if docker_container_name != "database" %}
|
||||||
|
:
|
||||||
|
{%- else %}
|
||||||
|
while true; do
|
||||||
|
if [[ "$(docker exec -i database redis-cli ping)" =~ PONG.* ]]; then
|
||||||
|
break
|
||||||
|
fi
|
||||||
|
sleep 1
|
||||||
|
done
|
||||||
|
{%- endif %}
|
||||||
|
}
|
||||||
|
|
||||||
|
# Obtain our platform as we will mount directories with these names in each docker
|
||||||
|
PLATFORM=`sonic-cfggen -v 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"]'`
|
HWSKU=`sonic-cfggen -d -v 'DEVICE_METADATA["localhost"]["hwsku"]'`
|
||||||
{%- endif %}
|
{%- endif %}
|
||||||
|
|
||||||
start() {
|
start() {
|
||||||
docker inspect --type container {{docker_container_name}} &>/dev/null
|
DOCKERCHECK=`docker inspect --type container {{docker_container_name}} 2>/dev/null`
|
||||||
if [ "$?" -eq "0" ]; then
|
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}}
|
docker start {{docker_container_name}}
|
||||||
else
|
postStartAction
|
||||||
|
exit 0
|
||||||
|
fi
|
||||||
|
|
||||||
|
# docker created with a different HWSKU, remove and recreate
|
||||||
|
echo "Removing obsolete {{docker_container_name}} container with HWSKU $DOCKERMOUNT"
|
||||||
|
docker rm {{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}} \
|
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 %}
|
{%- 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 \
|
--log-opt max-size=2M --log-opt max-file=5 \
|
||||||
@ -22,15 +61,8 @@ start() {
|
|||||||
-v /usr/share/sonic/device/$PLATFORM/$HWSKU:/usr/share/sonic/hwsku:ro \
|
-v /usr/share/sonic/device/$PLATFORM/$HWSKU:/usr/share/sonic/hwsku:ro \
|
||||||
{%- endif %}
|
{%- endif %}
|
||||||
--name={{docker_container_name}} {{docker_image_name}}:latest
|
--name={{docker_container_name}} {{docker_image_name}}:latest
|
||||||
fi
|
|
||||||
{%- if docker_container_name == "database" %}
|
postStartAction
|
||||||
while true; do
|
|
||||||
if [[ "$(docker exec -i database redis-cli ping)" =~ PONG.* ]]; then
|
|
||||||
break
|
|
||||||
fi
|
|
||||||
sleep 1
|
|
||||||
done
|
|
||||||
{%- endif %}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
attach() {
|
attach() {
|
||||||
|
Reference in New Issue
Block a user