-
Notifications
You must be signed in to change notification settings - Fork 5
/
tools.sh
executable file
·61 lines (53 loc) · 1.94 KB
/
tools.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#!/bin/bash -e
# Common functions
function route_to_subnetpool {
# Neutron no longer sets route to the created net:
# https://github.com/openstack-dev/devstack/commit/1493bdeba24674f6634160d51b8081c571df4017
# Add/replace it here for ease of use
local ROUTER=$(openstack router list -f value -c ID)
# No router
if [ -z "${ROUTER}" ]; then
return
fi
# No namespace (different node or OVN deployment)
if ! sudo ip netns list | grep -q qrouter-"${ROUTER}"; then
return
fi
local NET_GATEWAY=$(sudo ip netns exec qrouter-"${ROUTER}" ip -4 route get 8.8.8.8 | head -n1 | awk '{print $7}')
# Filter IPv6 pool out
local SUBNET_POOL=$(openstack subnet pool list -f value -c Prefixes | grep -v : | sed -e "s/.*'\(.*\)'.*/\1/")
sudo ip route replace "${SUBNET_POOL}" via "${NET_GATEWAY}"
}
function ssh_command {
if [ ! -z "${SSH_COMMAND}" ]; then
return
fi
# For OVN deployment, get namespace
PRIVATE_METADATA=$(openstack network show private -f value -c id)
SSH_OPTIONS="-o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null -o PubkeyAcceptedKeyTypes=+ssh-rsa"
if sudo ip netns list | grep -q ovnmeta-"${PRIVATE_METADATA}"; then
SSH_COMMAND="sudo ip netns exec ovnmeta-${PRIVATE_METADATA} ssh ${SSH_OPTIONS} -i ${HOME}/.ssh/id_rsa"
else
SSH_COMMAND="ssh ${SSH_OPTIONS}"
fi
}
function wait_for_ssh {
ssh_command
# Try to connect to SSH
local SSH_TARGET=${1}
local RETRIES=${2:-5}
for i in $(seq ${RETRIES})
do
STATUS=$(${SSH_COMMAND} -o "ConnectTimeout=10" "${SSH_TARGET}" echo ok 2> /dev/null || true)
if [ "${STATUS}" == "ok" ]
then
return 0
fi
done
return 1
}
function basic_web_server {
ssh_command
local SSH_TARGET=${1}
${SSH_COMMAND} "${SSH_TARGET}" 'while true; do echo -e "HTTP/1.0 200 OK\r\n\r\nWelcome to $(hostname)" | sudo nc -l -p 80 ; done&' 2> /dev/null
}