blob: cb683398e129741025c9a568a8393b82f8c6a30c [file] [log] [blame]
Salvatore Orlandod6767d02012-08-31 04:55:20 -07001# lib/quantum
2# functions - funstions specific to quantum
3
4# Save trace setting
5XTRACE=$(set +o | grep xtrace)
6set +o xtrace
7
Nachi Ueno8bc21f62012-11-19 22:04:28 -08008QUANTUM_DIR=$DEST/quantum
Nachi Ueno5db5bfa2012-10-29 11:25:29 -07009export QUANTUM_TEST_CONFIG_FILE=${QUANTUM_TEST_CONFIG_FILE:-"/etc/quantum/debug.ini"}
Gary Kotton9343df12012-11-28 10:05:53 +000010QUANTUM_AUTH_CACHE_DIR=${QUANTUM_AUTH_CACHE_DIR:-/var/cache/quantum}
Nachi Ueno5db5bfa2012-10-29 11:25:29 -070011
Nachi Ueno8bc21f62012-11-19 22:04:28 -080012if is_service_enabled quantum; then
13 Q_CONF_FILE=/etc/quantum/quantum.conf
14 Q_RR_CONF_FILE=/etc/quantum/rootwrap.conf
15 if [[ "$Q_USE_ROOTWRAP" == "False" ]]; then
16 Q_RR_COMMAND="sudo"
17 else
Nachi Uenoeb1aa3d2012-12-06 11:55:29 -080018 QUANTUM_ROOTWRAP=$(get_rootwrap_location quantum)
19 Q_RR_COMMAND="sudo $QUANTUM_ROOTWRAP $Q_RR_CONF_FILE"
Nachi Ueno8bc21f62012-11-19 22:04:28 -080020 fi
21fi
22
Nachi Uenoeb1aa3d2012-12-06 11:55:29 -080023# configure_quantum_rootwrap() - configure Quantum's rootwrap
24function configure_quantum_rootwrap() {
25 if [[ "$Q_USE_ROOTWRAP" == "False" ]]; then
26 return
27 fi
28 # Deploy new rootwrap filters files (owned by root).
29 # Wipe any existing rootwrap.d files first
30 Q_CONF_ROOTWRAP_D=/etc/quantum/rootwrap.d
31 if [[ -d $Q_CONF_ROOTWRAP_D ]]; then
32 sudo rm -rf $Q_CONF_ROOTWRAP_D
33 fi
34 # Deploy filters to /etc/quantum/rootwrap.d
35 mkdir -p -m 755 $Q_CONF_ROOTWRAP_D
36 cp -pr $QUANTUM_DIR/etc/quantum/rootwrap.d/* $Q_CONF_ROOTWRAP_D/
37 sudo chown -R root:root $Q_CONF_ROOTWRAP_D
38 sudo chmod 644 $Q_CONF_ROOTWRAP_D/*
39 # Set up rootwrap.conf, pointing to /etc/quantum/rootwrap.d
40 sudo cp -p $QUANTUM_DIR/etc/rootwrap.conf $Q_RR_CONF_FILE
41 sudo sed -e "s:^filters_path=.*$:filters_path=$Q_CONF_ROOTWRAP_D:" -i $Q_RR_CONF_FILE
42 sudo chown root:root $Q_RR_CONF_FILE
43 sudo chmod 0644 $Q_RR_CONF_FILE
44 # Specify rootwrap.conf as first parameter to quantum-rootwrap
45 ROOTWRAP_SUDOER_CMD="$QUANTUM_ROOTWRAP $Q_RR_CONF_FILE *"
46
47 # Set up the rootwrap sudoers for quantum
48 TEMPFILE=`mktemp`
49 echo "$USER ALL=(root) NOPASSWD: $ROOTWRAP_SUDOER_CMD" >$TEMPFILE
50 chmod 0440 $TEMPFILE
51 sudo chown root:root $TEMPFILE
52 sudo mv $TEMPFILE /etc/sudoers.d/quantum-rootwrap
53}
54
55
Salvatore Orlandod6767d02012-08-31 04:55:20 -070056# Configures keystone integration for quantum service and agents
57function quantum_setup_keystone() {
58 local conf_file=$1
59 local section=$2
60 local use_auth_url=$3
61 if [[ -n $use_auth_url ]]; then
62 iniset $conf_file $section auth_url "$KEYSTONE_SERVICE_PROTOCOL://$KEYSTONE_AUTH_HOST:$KEYSTONE_AUTH_PORT/v2.0"
63 else
64 iniset $conf_file $section auth_host $KEYSTONE_SERVICE_HOST
65 iniset $conf_file $section auth_port $KEYSTONE_AUTH_PORT
66 iniset $conf_file $section auth_protocol $KEYSTONE_SERVICE_PROTOCOL
67 fi
68 iniset $conf_file $section admin_tenant_name $SERVICE_TENANT_NAME
69 iniset $conf_file $section admin_user $Q_ADMIN_USERNAME
70 iniset $conf_file $section admin_password $SERVICE_PASSWORD
Gary Kotton9343df12012-11-28 10:05:53 +000071 if [[ "$KEYSTONE_TOKEN_FORMAT" == "PKI" ]]; then
72 iniset $conf_file $section signing_dir $QUANTUM_AUTH_CACHE_DIR
73 # Create cache dir
74 sudo mkdir -p $QUANTUM_AUTH_CACHE_DIR
75 sudo chown `whoami` $QUANTUM_AUTH_CACHE_DIR
76 fi
Salvatore Orlandod6767d02012-08-31 04:55:20 -070077}
78
79function quantum_setup_ovs_bridge() {
80 local bridge=$1
81 for PORT in `sudo ovs-vsctl --no-wait list-ports $bridge`; do
82 if [[ "$PORT" =~ tap* ]]; then echo `sudo ip link delete $PORT` > /dev/null; fi
83 sudo ovs-vsctl --no-wait del-port $bridge $PORT
84 done
85 sudo ovs-vsctl --no-wait -- --if-exists del-br $bridge
86 sudo ovs-vsctl --no-wait add-br $bridge
87 sudo ovs-vsctl --no-wait br-set-external-id $bridge bridge-id $bridge
88}
89
Yoshihiro Kaneko602cf9b2012-07-23 06:27:36 +000090function quantum_setup_external_bridge() {
91 local bridge=$1
92 # Create it if it does not exist
93 sudo ovs-vsctl --no-wait -- --may-exist add-br $bridge
94 # remove internal ports
95 for PORT in `sudo ovs-vsctl --no-wait list-ports $bridge`; do
96 TYPE=$(sudo ovs-vsctl get interface $PORT type)
97 if [[ "$TYPE" == "internal" ]]; then
98 echo `sudo ip link delete $PORT` > /dev/null
99 sudo ovs-vsctl --no-wait del-port $bridge $PORT
100 fi
101 done
102 # ensure no IP is configured on the public bridge
103 sudo ip addr flush dev $bridge
104}
105
106function is_quantum_ovs_base_plugin() {
Dean Troyer5a4148d2012-10-23 15:47:01 -0500107 local plugin=$1
Yoshihiro Kaneko602cf9b2012-07-23 06:27:36 +0000108 if [[ ",openvswitch,ryu," =~ ,${plugin}, ]]; then
109 return 0
110 fi
111 return 1
112}
113
Nachi Ueno5db5bfa2012-10-29 11:25:29 -0700114function _get_net_id() {
115 quantum --os-tenant-name admin --os-username admin --os-password $ADMIN_PASSWORD net-list | grep $1 | awk '{print $2}'
116}
117
118function _get_probe_cmd_prefix() {
119 local from_net="$1"
120 net_id=`_get_net_id $from_net`
121 probe_id=`quantum-debug --os-tenant-name admin --os-username admin --os-password $ADMIN_PASSWORD probe-list -c id -c network_id | grep $net_id | awk '{print $2}' | head -n 1`
Nachi Ueno8bc21f62012-11-19 22:04:28 -0800122 echo "$Q_RR_COMMAND ip netns exec qprobe-$probe_id"
Nachi Ueno5db5bfa2012-10-29 11:25:29 -0700123}
124
125function delete_probe() {
126 local from_net="$1"
127 net_id=`_get_net_id $from_net`
128 probe_id=`quantum-debug --os-tenant-name admin --os-username admin --os-password $ADMIN_PASSWORD probe-list -c id -c network_id | grep $net_id | awk '{print $2}'`
129 quantum-debug --os-tenant-name admin --os-username admin probe-delete $probe_id
130}
131
132function _ping_check_quantum() {
133 local from_net=$1
134 local ip=$2
135 local timeout_sec=$3
136 local expected=${4:-"True"}
137 local check_command=""
138 probe_cmd=`_get_probe_cmd_prefix $from_net`
139 if [[ "$expected" = "True" ]]; then
Nachi Ueno8bc21f62012-11-19 22:04:28 -0800140 check_command="while ! $probe_cmd ping -w 1 -c 1 $ip; do sleep 1; done"
Nachi Ueno5db5bfa2012-10-29 11:25:29 -0700141 else
Nachi Ueno8bc21f62012-11-19 22:04:28 -0800142 check_command="while $probe_cmd ping -w 1 -c 1 $ip; do sleep 1; done"
Nachi Ueno5db5bfa2012-10-29 11:25:29 -0700143 fi
144 if ! timeout $timeout_sec sh -c "$check_command"; then
145 if [[ "$expected" = "True" ]]; then
146 echo "[Fail] Couldn't ping server"
147 else
148 echo "[Fail] Could ping server"
149 fi
150 exit 1
151 fi
152}
153
154# ssh check
155function _ssh_check_quantum() {
156 local from_net=$1
157 local key_file=$2
158 local ip=$3
159 local user=$4
160 local timeout_sec=$5
161 local probe_cmd = ""
162 probe_cmd=`_get_probe_cmd_prefix $from_net`
163 if ! timeout $timeout_sec sh -c "while ! $probe_cmd ssh -o StrictHostKeyChecking=no -i $key_file ${user}@$ip echo success ; do sleep 1; done"; then
164 echo "server didn't become ssh-able!"
165 exit 1
166 fi
167}
168
169function setup_quantum() {
170 public_net_id=`_get_net_id $PUBLIC_NETWORK_NAME`
171 quantum-debug --os-tenant-name admin --os-username admin --os-password $ADMIN_PASSWORD probe-create $public_net_id
172 private_net_id=`_get_net_id $PRIVATE_NETWORK_NAME`
173 quantum-debug --os-tenant-name admin --os-username admin --os-password $ADMIN_PASSWORD probe-create $private_net_id
174}
175
176function teardown_quantum() {
177 delete_probe $PUBLIC_NETWORK_NAME
178 delete_probe $PRIVATE_NETWORK_NAME
179}
180
Salvatore Orlandod6767d02012-08-31 04:55:20 -0700181# Restore xtrace
182$XTRACE