From b187fb5b629e3ef032d5915a89267f5266f5d373 Mon Sep 17 00:00:00 2001 From: Brent Eagles Date: Fri, 27 Mar 2020 14:39:12 -0230 Subject: [PATCH] Using podman/docker ps to look for running sidecars We've been running into numerous issues with searching on patterns in the command so using the container CLI to match seems better. Change-Id: I13633ab4d5ebfc7f52b7215523c310fe72a5487e Closes-Bug: #1869384 --- .../templates/service_sync.j2 | 27 +++++++++++++++---- 1 file changed, 22 insertions(+), 5 deletions(-) diff --git a/tripleo_ansible/roles/tripleo_systemd_wrapper/templates/service_sync.j2 b/tripleo_ansible/roles/tripleo_systemd_wrapper/templates/service_sync.j2 index 5ce5af03f..f05974978 100644 --- a/tripleo_ansible/roles/tripleo_systemd_wrapper/templates/service_sync.j2 +++ b/tripleo_ansible/roles/tripleo_systemd_wrapper/templates/service_sync.j2 @@ -6,7 +6,8 @@ set -x function start_service { local NETNS=$1 shift - local NAME="{{ tripleo_systemd_wrapper_service_name }}-${NETNS}" + local NAME=$1 + shift local CLI='{{ tripleo_systemd_wrapper_container_cli }}' local CMD="{{ tripleo_systemd_wrapper_service_dir }}/{{ tripleo_systemd_wrapper_service_name }}/command" local CONTAINER_CMD="ip netns exec ${NETNS} ${CMD}" @@ -39,10 +40,26 @@ flock "$lock_fd" IFS=$'\n' for LINE in $(cat {{ tripleo_systemd_wrapper_service_dir }}/{{ tripleo_systemd_wrapper_service_name }}/processes); do NETNS=$(echo $LINE | awk '{ print $1 }') - IFS=$' ' ARGS=$(echo $LINE | sed -e "s|$NETNS ||" | xargs) - # TODO(emilien) investigate if we should rather run docker/podman ps instead of ps on the host - if ! ps -e -o pid,command | grep "$(echo $NETNS | sed 's|^[^-]*\-||')" | egrep -v "grep | netns exec" &> /dev/null; then - start_service $NETNS $ARGS + CONTAINER_NAME="{{ tripleo_systemd_wrapper_service_name }}-${NETNS}" + CLI='{{ tripleo_systemd_wrapper_container_cli }}' + + # We do a filter to create a short list and then have to exact name match the results. The reason is that + # containers that partially match the filter will show up in the list. This is a little unlikely but there + # is nothing to prevent that. + start_container=yes + container_list=`$CLI ps --format={% raw %}"{{.Names}}"{% endraw %} --filter="name=$CONTAINER_NAME"` + for name in ${container_list}; + do + if [ "x$name" = "x$CONTAINER_NAME"]; + then + start_container=no + break + fi + done + if [ "$start_container" = "yes" ]; + then + IFS=$' ' ARGS=$(echo $LINE | sed -e "s|$NETNS ||" | xargs) + start_service $NETNS $CONTAINER_NAME $ARGS fi done # truncate the file so we don't start them again