Sean Dague | af9bf86 | 2015-04-16 08:58:32 -0400 | [diff] [blame] | 1 | #!/bin/bash |
| 2 | # |
| 3 | # Copyright 2015 Hewlett-Packard Development Company, L.P. |
| 4 | # |
| 5 | # Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 6 | # not use this file except in compliance with the License. You may obtain |
| 7 | # a copy of the License at |
| 8 | # |
| 9 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | # |
| 11 | # Unless required by applicable law or agreed to in writing, software |
| 12 | # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 13 | # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 14 | # License for the specific language governing permissions and limitations |
| 15 | # under the License. |
| 16 | |
| 17 | # Ping a neutron guest using a network namespace probe |
| 18 | |
| 19 | set -o errexit |
| 20 | set -o pipefail |
| 21 | |
| 22 | TOP_DIR=$(cd $(dirname "$0")/.. && pwd) |
| 23 | |
| 24 | # This *must* be run as the admin tenant |
| 25 | source $TOP_DIR/openrc admin admin |
| 26 | |
| 27 | function usage { |
| 28 | cat - <<EOF |
| 29 | ping_neutron.sh <net_name> [ping args] |
| 30 | |
| 31 | This provides a wrapper to ping neutron guests that are on isolated |
| 32 | tenant networks that the caller can't normally reach. It does so by |
Brian Haley | 6764eab | 2023-05-12 16:34:08 -0400 | [diff] [blame] | 33 | using either the DHCP or Metadata network namespace to support both |
| 34 | ML2/OVS and OVN. |
Sean Dague | af9bf86 | 2015-04-16 08:58:32 -0400 | [diff] [blame] | 35 | |
| 36 | It takes arguments like ping, except the first arg must be the network |
| 37 | name. |
| 38 | |
| 39 | Note: in environments with duplicate network names, the results are |
| 40 | non deterministic. |
| 41 | |
| 42 | This should *really* be in the neutron cli. |
| 43 | |
| 44 | EOF |
| 45 | exit 1 |
| 46 | } |
| 47 | |
Brian Haley | 6764eab | 2023-05-12 16:34:08 -0400 | [diff] [blame] | 48 | # BUG: with duplicate network names, this fails pretty hard since it |
| 49 | # will just pick the first match. |
| 50 | function _get_net_id { |
| 51 | openstack --os-cloud devstack-admin --os-region-name="$REGION_NAME" --os-project-name admin --os-username admin --os-password $ADMIN_PASSWORD network list | grep $1 | head -n 1 | awk '{print $2}' |
| 52 | } |
| 53 | |
Sean Dague | af9bf86 | 2015-04-16 08:58:32 -0400 | [diff] [blame] | 54 | NET_NAME=$1 |
| 55 | |
| 56 | if [[ -z "$NET_NAME" ]]; then |
| 57 | echo "Error: net_name is required" |
| 58 | usage |
| 59 | fi |
| 60 | |
Stanislaw Szydlo | aa81e2e | 2015-07-27 10:54:44 +0200 | [diff] [blame] | 61 | REMAINING_ARGS="${@:2}" |
Sean Dague | af9bf86 | 2015-04-16 08:58:32 -0400 | [diff] [blame] | 62 | |
Brian Haley | 6764eab | 2023-05-12 16:34:08 -0400 | [diff] [blame] | 63 | NET_ID=`_get_net_id $NET_NAME` |
| 64 | NET_NS=$(ip netns list | grep "$NET_ID" | head -n 1) |
Sean Dague | af9bf86 | 2015-04-16 08:58:32 -0400 | [diff] [blame] | 65 | |
| 66 | # This runs a command inside the specific netns |
Brian Haley | 6764eab | 2023-05-12 16:34:08 -0400 | [diff] [blame] | 67 | NET_NS_CMD="ip netns exec $NET_NS" |
Sean Dague | af9bf86 | 2015-04-16 08:58:32 -0400 | [diff] [blame] | 68 | |
Stanislaw Szydlo | aa81e2e | 2015-07-27 10:54:44 +0200 | [diff] [blame] | 69 | PING_CMD="sudo $NET_NS_CMD ping $REMAINING_ARGS" |
Sean Dague | af9bf86 | 2015-04-16 08:58:32 -0400 | [diff] [blame] | 70 | echo "Running $PING_CMD" |
| 71 | $PING_CMD |