Ping VM test from router / DHCP server

source /etc/kolla/admin-openrc.sh
cd /tmp/
 
export OS_ENV=@globals.environment@
export SERVER_ID=$(echo @option.vm@ | sed -e 's/^[[:space:]]*//')
 
 
# search for VM by name
if [ ${#SERVER_ID} -ne 36 ]; then
    RESULT="$(openstack server list --all --name ${SERVER_ID})"
    if [ $(echo "${RESULT}" | wc -l) -eq 5 ]; then
        SERVER_ID=$(echo "${RESULT}" | tail -2 | head -1 | cut -d " " -f2)
    else
        echo "Found several VMs, please choose one from:"
        echo "${RESULT}"
        exit 0
    fi
fi
 
# get port id
PORT_ID=$(openstack port list --server ${SERVER_ID} -c id -f value)
 
# fixme: add support for multiple ports
if [ $(echo "${PORT_ID}" | wc -l) -eq 1 ]; then
    NETWORK_ID=$(openstack port show ${PORT_ID} -c network_id -f value)
    PORT_IP=$(openstack server show ${SERVER_ID} -c addresses -f value | cut -d "=" -f2 | cut -d "," -f1)
 
    # NEW OSC: SECURITY_GROUP_ID=$(openstack port show ${PORT_ID} -c security_group_ids -f json | jq -r 'first(.security_group_ids[])')
    SECURITY_GROUP_IDS=$(openstack port show ${PORT_ID} -c security_group_ids -f json | jq -r .security_group_ids | tr ',' '\n')
 
    # check if ingress icmp security rule exists
    for SECURITY_GROUP_ID in ${SECURITY_GROUP_IDS}; do
        SECURITY_GROUP_RULE_ID=$(openstack security group rule list ${SECURITY_GROUP_ID} --ingress --protocol icmp -c ID -f value)
    done
 
    # check if icmp security group rule found
    if [ -z ${SECURITY_GROUP_RULE_ID} ]; then
        # create security group rule to allow incomming icmp traffic
        echo "Creating new ICMP security group rule"
        TMP_SECURITY_GROUP_RULE_ID=$(openstack security group rule create --protocol icmp ${SECURITY_GROUP_ID} -c id -f value)
        SECURITY_GROUP_RULE_ID=${TMP_SECURITY_GROUP_RULE_ID}
    fi
 
    # ping VM from active router
    ROUTER_PORT_JSON=$(openstack port list --network ${NETWORK_ID} --device-owner network:ha_router_replicated_interface -c binding_host_id -c device_id -c "Fixed IP Addresses" -f json | jq -c 'first')
    if [ ! -z "${ROUTER_PORT_JSON}" ]; then
        ROUTER_HOST=$(echo ${ROUTER_PORT_JSON} | jq -r .binding_host_id)
        ROUTER_DEVICE_ID=$(echo ${ROUTER_PORT_JSON} | jq -r .device_id)
        ROUTER_IP=$(echo ${ROUTER_PORT_JSON} | jq -r '."Fixed IP Addresses"' | cut -d"'" -f2)
 
        echo "# Ping VM from router ${ROUTER_IP} at ${ROUTER_HOST}"
        ssh ${ROUTER_HOST} ip netns exec qrouter-${ROUTER_DEVICE_ID} ping -W 1 -c 5 ${PORT_IP}
 
        echo
    fi
 
    DHCP_PORTS_JSON=$(openstack port list --network ${NETWORK_ID} --device-owner network:dhcp -c binding_host_id -f value --sort-column binding_host_id -c "Fixed IP Addresses" -f json --sort-column binding_host_id | jq -c '.[]')
    IFS=$(echo -en "\n\b")
    for DHCP_PORT_JSON in ${DHCP_PORTS_JSON}; do
        DHCP_HOST=$(echo ${DHCP_PORT_JSON} | jq -r .binding_host_id)
        DHCP_PORT_IP=$(echo ${DHCP_PORT_JSON} | jq -r '."Fixed IP Addresses"' | cut -d"'" -f2)
 
        echo "# Ping VM from DHCP ${DHCP_PORT_IP} at ${DHCP_HOST}"
        ssh ${DHCP_HOST} ip netns exec qdhcp-${NETWORK_ID} ping -W 1 -c 5 ${PORT_IP}
 
        echo
    done
 
    # remove temoprary icmp securitgy grup rule
    if [ ! -z ${TMP_SECURITY_GROUP_RULE_ID} ]; then
        echo "Removing temporatry icmp security group rule ${TMP_SECURITY_GROUP_RULE_ID}"
        openstack security group rule delete ${TMP_SECURITY_GROUP_RULE_ID}
    fi
else
    echo "Multiple ports found"
    echo ${PORT_ID}
fi