lib/quantum
d6767d0d
 # lib/quantum
 # functions - funstions specific to quantum
 
60e9c0ab
 # Dependencies:
 # ``functions`` file
 # ``DEST`` must be defined
 
66afb47c
 # ``stack.sh`` calls the entry points in this order:
 #
 # install_quantum
 # install_quantumclient
 # install_quantum_agent_packages
 # install_quantum_third_party
 # configure_quantum
 # init_quantum
 # configure_quantum_third_party
 # init_quantum_third_party
 # start_quantum_third_party
 # create_nova_conf_quantum
 # start_quantum_service_and_check
 # create_quantum_initial_network
 # setup_quantum_debug
 # start_quantum_agents
 #
 # ``unstack.sh`` calls the entry points in this order:
 #
 # stop_quantum
 
 # Functions in lib/quantum are classified into the following categories:
 #
 # - entry points (called from stack.sh or unstack.sh)
 # - internal functions
 # - quantum exercises
 # - 3rd party programs
 
60e9c0ab
 
 # Quantum Networking
 # ------------------
 
 # Make sure that quantum is enabled in ``ENABLED_SERVICES``.  If you want
 # to run Quantum on this host, make sure that q-svc is also in
 # ``ENABLED_SERVICES``.
 #
 # If you're planning to use the Quantum openvswitch plugin, set
 # ``Q_PLUGIN`` to "openvswitch" and make sure the q-agt service is enabled
 # in ``ENABLED_SERVICES``.  If you're planning to use the Quantum
 # linuxbridge plugin, set ``Q_PLUGIN`` to "linuxbridge" and make sure the
 # q-agt service is enabled in ``ENABLED_SERVICES``.
 #
 # See "Quantum Network Configuration" below for additional variables
 # that must be set in localrc for connectivity across hosts with
 # Quantum.
 #
b3236914
 # With Quantum networking the NETWORK_MANAGER variable is ignored.
60e9c0ab
 
d6767d0d
 # Save trace setting
 XTRACE=$(set +o | grep xtrace)
 set +o xtrace
 
60e9c0ab
 
66afb47c
 # Quantum Network Configuration
 # -----------------------------
60e9c0ab
 
 # Set up default directories
bb7737d1
 QUANTUM_DIR=$DEST/neutron
 QUANTUMCLIENT_DIR=$DEST/python-neutronclient
9343df16
 QUANTUM_AUTH_CACHE_DIR=${QUANTUM_AUTH_CACHE_DIR:-/var/cache/quantum}
5db5bfa2
 
60e9c0ab
 QUANTUM_CONF_DIR=/etc/quantum
 QUANTUM_CONF=$QUANTUM_CONF_DIR/quantum.conf
 export QUANTUM_TEST_CONFIG_FILE=${QUANTUM_TEST_CONFIG_FILE:-"$QUANTUM_CONF_DIR/debug.ini"}
 
 # Default Quantum Plugin
 Q_PLUGIN=${Q_PLUGIN:-openvswitch}
 # Default Quantum Port
 Q_PORT=${Q_PORT:-9696}
 # Default Quantum Host
 Q_HOST=${Q_HOST:-$HOST_IP}
 # Default admin username
 Q_ADMIN_USERNAME=${Q_ADMIN_USERNAME:-quantum}
 # Default auth strategy
 Q_AUTH_STRATEGY=${Q_AUTH_STRATEGY:-keystone}
 # Use namespace or not
 Q_USE_NAMESPACE=${Q_USE_NAMESPACE:-True}
 Q_USE_ROOTWRAP=${Q_USE_ROOTWRAP:-True}
 # Meta data IP
 Q_META_DATA_IP=${Q_META_DATA_IP:-$HOST_IP}
66afb47c
 # Allow Overlapping IP among subnets
d1742feb
 Q_ALLOW_OVERLAPPING_IP=${Q_ALLOW_OVERLAPPING_IP:-True}
60e9c0ab
 # Use quantum-debug command
 Q_USE_DEBUG_COMMAND=${Q_USE_DEBUG_COMMAND:-False}
31c94ab5
 # The name of the default q-l3 router
 Q_ROUTER_NAME=${Q_ROUTER_NAME:-router1}
7a8d8527
 # List of config file names in addition to the main plugin config file
 # See _configure_quantum_common() for details about setting it up
 declare -a Q_PLUGIN_EXTRA_CONF_FILES
60e9c0ab
 
8bc21f64
 if is_service_enabled quantum; then
60e9c0ab
     Q_RR_CONF_FILE=$QUANTUM_CONF_DIR/rootwrap.conf
8bc21f64
     if [[ "$Q_USE_ROOTWRAP" == "False" ]]; then
         Q_RR_COMMAND="sudo"
     else
eb1aa3d5
         QUANTUM_ROOTWRAP=$(get_rootwrap_location quantum)
         Q_RR_COMMAND="sudo $QUANTUM_ROOTWRAP $Q_RR_CONF_FILE"
8bc21f64
     fi
 
66afb47c
     # Provider Network Configurations
     # --------------------------------
 
     # The following variables control the Quantum openvswitch and
     # linuxbridge plugins' allocation of tenant networks and
     # availability of provider networks. If these are not configured
     # in localrc, tenant networks will be local to the host (with no
     # remote connectivity), and no physical resources will be
     # available for the allocation of provider networks.
 
     # To use GRE tunnels for tenant networks, set to True in
     # localrc. GRE tunnels are only supported by the openvswitch
     # plugin, and currently only on Ubuntu.
     ENABLE_TENANT_TUNNELS=${ENABLE_TENANT_TUNNELS:-False}
 
     # If using GRE tunnels for tenant networks, specify the range of
     # tunnel IDs from which tenant networks are allocated. Can be
     # overriden in localrc in necesssary.
     TENANT_TUNNEL_RANGES=${TENANT_TUNNEL_RANGE:-1:1000}
 
     # To use VLANs for tenant networks, set to True in localrc. VLANs
     # are supported by the openvswitch and linuxbridge plugins, each
     # requiring additional configuration described below.
     ENABLE_TENANT_VLANS=${ENABLE_TENANT_VLANS:-False}
 
     # If using VLANs for tenant networks, set in localrc to specify
     # the range of VLAN VIDs from which tenant networks are
     # allocated. An external network switch must be configured to
     # trunk these VLANs between hosts for multi-host connectivity.
     #
     # Example: ``TENANT_VLAN_RANGE=1000:1999``
     TENANT_VLAN_RANGE=${TENANT_VLAN_RANGE:-}
 
     # If using VLANs for tenant networks, or if using flat or VLAN
     # provider networks, set in localrc to the name of the physical
     # network, and also configure OVS_PHYSICAL_BRIDGE for the
     # openvswitch agent or LB_PHYSICAL_INTERFACE for the linuxbridge
     # agent, as described below.
     #
     # Example: ``PHYSICAL_NETWORK=default``
     PHYSICAL_NETWORK=${PHYSICAL_NETWORK:-}
 
     # With the openvswitch plugin, if using VLANs for tenant networks,
     # or if using flat or VLAN provider networks, set in localrc to
     # the name of the OVS bridge to use for the physical network. The
     # bridge will be created if it does not already exist, but a
     # physical interface must be manually added to the bridge as a
     # port for external connectivity.
     #
     # Example: ``OVS_PHYSICAL_BRIDGE=br-eth1``
     OVS_PHYSICAL_BRIDGE=${OVS_PHYSICAL_BRIDGE:-}
 
     # With the linuxbridge plugin, if using VLANs for tenant networks,
     # or if using flat or VLAN provider networks, set in localrc to
     # the name of the network interface to use for the physical
     # network.
     #
     # Example: ``LB_PHYSICAL_INTERFACE=eth1``
     LB_PHYSICAL_INTERFACE=${LB_PHYSICAL_INTERFACE:-}
 
     # With the openvswitch plugin, set to True in localrc to enable
     # provider GRE tunnels when ``ENABLE_TENANT_TUNNELS`` is False.
     #
     # Example: ``OVS_ENABLE_TUNNELING=True``
     OVS_ENABLE_TUNNELING=${OVS_ENABLE_TUNNELING:-$ENABLE_TENANT_TUNNELS}
 fi
60e9c0ab
 
cc6b4435
 
0dd34df4
 # Quantum plugin specific functions
 # ---------------------------------
 # Please refer to lib/quantum_plugins/README.md for details.
 source $TOP_DIR/lib/quantum_plugins/$Q_PLUGIN
 
b7d82842
 # Agent loadbalancer service plugin functions
 # -------------------------------------------
 # Hardcoding for 1 service plugin for now
c07112a2
 source $TOP_DIR/lib/quantum_plugins/services/agent_loadbalancer
b7d82842
 
3452f8eb
 # Use security group or not
 if has_quantum_plugin_security_group; then
     Q_USE_SECGROUP=${Q_USE_SECGROUP:-True}
 else
     Q_USE_SECGROUP=False
 fi
 
cc6b4435
 
 # Functions
 # ---------
60e9c0ab
 
66afb47c
 # configure_quantum()
 # Set common config for all quantum server and agents.
 function configure_quantum() {
     _configure_quantum_common
b0f1c38b
     iniset_rpc_backend quantum $QUANTUM_CONF DEFAULT
66afb47c
 
b7d82842
     # goes before q-svc to init Q_SERVICE_PLUGIN_CLASSES
     if is_service_enabled q-lbaas; then
         _configure_quantum_lbaas
     fi
66afb47c
     if is_service_enabled q-svc; then
         _configure_quantum_service
     fi
     if is_service_enabled q-agt; then
         _configure_quantum_plugin_agent
     fi
     if is_service_enabled q-dhcp; then
         _configure_quantum_dhcp_agent
     fi
     if is_service_enabled q-l3; then
         _configure_quantum_l3_agent
     fi
     if is_service_enabled q-meta; then
         _configure_quantum_metadata_agent
     fi
 
     _configure_quantum_debug_command
 }
 
 function create_nova_conf_quantum() {
9bc47db2
     iniset $NOVA_CONF DEFAULT network_api_class "nova.network.quantumv2.api.API"
     iniset $NOVA_CONF DEFAULT quantum_admin_username "$Q_ADMIN_USERNAME"
     iniset $NOVA_CONF DEFAULT quantum_admin_password "$SERVICE_PASSWORD"
     iniset $NOVA_CONF DEFAULT quantum_admin_auth_url "$KEYSTONE_SERVICE_PROTOCOL://$KEYSTONE_SERVICE_HOST:$KEYSTONE_AUTH_PORT/v2.0"
     iniset $NOVA_CONF DEFAULT quantum_auth_strategy "$Q_AUTH_STRATEGY"
     iniset $NOVA_CONF DEFAULT quantum_admin_tenant_name "$SERVICE_TENANT_NAME"
     iniset $NOVA_CONF DEFAULT quantum_url "http://$Q_HOST:$Q_PORT"
66afb47c
 
3452f8eb
     if [[ "$Q_USE_SECGROUP" == "True" ]]; then
         LIBVIRT_FIREWALL_DRIVER=nova.virt.firewall.NoopFirewallDriver
         iniset $NOVA_CONF DEFAULT security_group_api quantum
     fi
 
0dd34df4
     # set NOVA_VIF_DRIVER and optionally set options in nova_conf
     quantum_plugin_create_nova_conf
 
9bc47db2
     iniset $NOVA_CONF DEFAULT libvirt_vif_driver "$NOVA_VIF_DRIVER"
     iniset $NOVA_CONF DEFAULT linuxnet_interface_driver "$LINUXNET_VIF_DRIVER"
66afb47c
     if is_service_enabled q-meta; then
9bc47db2
         iniset $NOVA_CONF DEFAULT service_quantum_metadata_proxy "True"
66afb47c
     fi
 }
 
 # create_quantum_accounts() - Set up common required quantum accounts
 
 # Tenant               User       Roles
 # ------------------------------------------------------------------
 # service              quantum    admin        # if enabled
 
 # Migrated from keystone_data.sh
 function create_quantum_accounts() {
 
     SERVICE_TENANT=$(keystone tenant-list | awk "/ $SERVICE_TENANT_NAME / { print \$2 }")
     ADMIN_ROLE=$(keystone role-list | awk "/ admin / { print \$2 }")
 
     if [[ "$ENABLED_SERVICES" =~ "q-svc" ]]; then
         QUANTUM_USER=$(keystone user-create \
             --name=quantum \
             --pass="$SERVICE_PASSWORD" \
             --tenant_id $SERVICE_TENANT \
             --email=quantum@example.com \
             | grep " id " | get_field 2)
         keystone user-role-add \
             --tenant_id $SERVICE_TENANT \
             --user_id $QUANTUM_USER \
             --role_id $ADMIN_ROLE
         if [[ "$KEYSTONE_CATALOG_BACKEND" = 'sql' ]]; then
             QUANTUM_SERVICE=$(keystone service-create \
                 --name=quantum \
                 --type=network \
                 --description="Quantum Service" \
                 | grep " id " | get_field 2)
             keystone endpoint-create \
                 --region RegionOne \
                 --service_id $QUANTUM_SERVICE \
                 --publicurl "http://$SERVICE_HOST:9696/" \
                 --adminurl "http://$SERVICE_HOST:9696/" \
                 --internalurl "http://$SERVICE_HOST:9696/"
         fi
     fi
 }
 
 function create_quantum_initial_network() {
     TENANT_ID=$(keystone tenant-list | grep " demo " | get_field 1)
 
     # Create a small network
     # Since quantum command is executed in admin context at this point,
     # ``--tenant_id`` needs to be specified.
37a8d157
     if is_baremetal; then
         sudo ovs-vsctl add-port $OVS_PHYSICAL_BRIDGE $PUBLIC_INTERFACE
         for IP in $(ip addr show dev $PUBLIC_INTERFACE | grep ' inet ' | awk '{print $2}'); do
             sudo ip addr del $IP dev $PUBLIC_INTERFACE
             sudo ip addr add $IP dev $OVS_PHYSICAL_BRIDGE
         done
         NET_ID=$(quantum net-create $PHYSICAL_NETWORK --tenant_id $TENANT_ID --provider:network_type flat --provider:physical_network "$PHYSICAL_NETWORK" | grep ' id ' | get_field 2)
         SUBNET_ID=$(quantum subnet-create --tenant_id $TENANT_ID --ip_version 4 ${ALLOCATION_POOL:+--allocation-pool $ALLOCATION_POOL} --gateway $NETWORK_GATEWAY $NET_ID $FIXED_RANGE | grep ' id ' | get_field 2)
         sudo ifconfig $OVS_PHYSICAL_BRIDGE up
     else
         NET_ID=$(quantum net-create --tenant_id $TENANT_ID "$PRIVATE_NETWORK_NAME" | grep ' id ' | get_field 2)
         SUBNET_ID=$(quantum subnet-create --tenant_id $TENANT_ID --ip_version 4 --gateway $NETWORK_GATEWAY $NET_ID $FIXED_RANGE | grep ' id ' | get_field 2)
     fi
66afb47c
 
555ecd07
     if [[ "$Q_L3_ENABLED" == "True" ]]; then
66afb47c
         # Create a router, and add the private subnet as one of its interfaces
555ecd07
         if [[ "$Q_L3_ROUTER_PER_TENANT" == "True" ]]; then
             # create a tenant-owned router.
31c94ab5
             ROUTER_ID=$(quantum router-create --tenant_id $TENANT_ID $Q_ROUTER_NAME | grep ' id ' | get_field 2)
         else
555ecd07
             # Plugin only supports creating a single router, which should be admin owned.
31c94ab5
             ROUTER_ID=$(quantum router-create $Q_ROUTER_NAME | grep ' id ' | get_field 2)
         fi
66afb47c
         quantum router-interface-add $ROUTER_ID $SUBNET_ID
         # Create an external network, and a subnet. Configure the external network as router gw
         EXT_NET_ID=$(quantum net-create "$PUBLIC_NETWORK_NAME" -- --router:external=True | grep ' id ' | get_field 2)
8396d4f2
         EXT_GW_IP=$(quantum subnet-create --ip_version 4 ${Q_FLOATING_ALLOCATION_POOL:+--allocation-pool $Q_FLOATING_ALLOCATION_POOL} $EXT_NET_ID $FLOATING_RANGE -- --enable_dhcp=False | grep 'gateway_ip' | get_field 2)
66afb47c
         quantum router-gateway-set $ROUTER_ID $EXT_NET_ID
 
555ecd07
         if is_service_enabled q-l3; then
             # logic is specific to using the l3-agent for l3
             if is_quantum_ovs_base_plugin && [[ "$Q_USE_NAMESPACE" = "True" ]]; then
                 CIDR_LEN=${FLOATING_RANGE#*/}
                 sudo ip addr add $EXT_GW_IP/$CIDR_LEN dev $PUBLIC_BRIDGE
                 sudo ip link set $PUBLIC_BRIDGE up
                 ROUTER_GW_IP=`quantum port-list -c fixed_ips -c device_owner | grep router_gateway | awk -F '"' '{ print $8; }'`
                 sudo route add -net $FIXED_RANGE gw $ROUTER_GW_IP
             fi
             if [[ "$Q_USE_NAMESPACE" == "False" ]]; then
                 # Explicitly set router id in l3 agent configuration
                 iniset $Q_L3_CONF_FILE DEFAULT router_id $ROUTER_ID
             fi
66afb47c
         fi
    fi
 }
 
 # init_quantum() - Initialize databases, etc.
 function init_quantum() {
     :
 }
 
 # install_quantum() - Collect source and prepare
 function install_quantum() {
     git_clone $QUANTUM_REPO $QUANTUM_DIR $QUANTUM_BRANCH
fe51a900
     setup_develop $QUANTUM_DIR
66afb47c
 }
 
 # install_quantumclient() - Collect source and prepare
 function install_quantumclient() {
     git_clone $QUANTUMCLIENT_REPO $QUANTUMCLIENT_DIR $QUANTUMCLIENT_BRANCH
fe51a900
     setup_develop $QUANTUMCLIENT_DIR
66afb47c
 }
 
 # install_quantum_agent_packages() - Collect source and prepare
 function install_quantum_agent_packages() {
0dd34df4
     # install packages that is specific to plugin agent
     quantum_plugin_install_agent_packages
66afb47c
 }
 
 # Start running processes, including screen
 function start_quantum_service_and_check() {
7a8d8527
     # build config-file options
     local cfg_file
     local CFG_FILE_OPTIONS="--config-file $QUANTUM_CONF --config-file /$Q_PLUGIN_CONF_FILE"
     for cfg_file in ${Q_PLUGIN_EXTRA_CONF_FILES[@]}; do
          CFG_FILE_OPTIONS+=" --config-file /$cfg_file"
     done
66afb47c
     # Start the Quantum service
7a8d8527
     screen_it q-svc "cd $QUANTUM_DIR && python $QUANTUM_DIR/bin/quantum-server $CFG_FILE_OPTIONS"
66afb47c
     echo "Waiting for Quantum to start..."
0ae742c3
     if ! timeout $SERVICE_TIMEOUT sh -c "while ! http_proxy= wget -q -O- http://$Q_HOST:$Q_PORT; do sleep 1; done"; then
07115eb5
       die $LINENO "Quantum did not start"
66afb47c
     fi
 }
 
 # Start running processes, including screen
 function start_quantum_agents() {
     # Start up the quantum agents if enabled
58ab929d
     screen_it q-agt "cd $QUANTUM_DIR && python $AGENT_BINARY --config-file $QUANTUM_CONF --config-file /$Q_PLUGIN_CONF_FILE"
     screen_it q-dhcp "cd $QUANTUM_DIR && python $AGENT_DHCP_BINARY --config-file $QUANTUM_CONF --config-file=$Q_DHCP_CONF_FILE"
     screen_it q-l3 "cd $QUANTUM_DIR && python $AGENT_L3_BINARY --config-file $QUANTUM_CONF --config-file=$Q_L3_CONF_FILE"
     screen_it q-meta "cd $QUANTUM_DIR && python $AGENT_META_BINARY --config-file $QUANTUM_CONF --config-file=$Q_META_CONF_FILE"
b7d82842
 
     if is_service_enabled q-lbaas; then
58ab929d
         screen_it q-lbaas "cd $QUANTUM_DIR && python $AGENT_LBAAS_BINARY --config-file $QUANTUM_CONF --config-file=$LBAAS_AGENT_CONF_FILENAME"
b7d82842
     fi
66afb47c
 }
 
 # stop_quantum() - Stop running processes (non-screen)
 function stop_quantum() {
     if is_service_enabled q-dhcp; then
         pid=$(ps aux | awk '/[d]nsmasq.+interface=(tap|ns-)/ { print $2 }')
         [ ! -z "$pid" ] && sudo kill -9 $pid
     fi
 }
 
995eb927
 # cleanup_quantum() - Remove residual data files, anything left over from previous
66afb47c
 # runs that a clean run would need to clean up
995eb927
 function cleanup_quantum() {
66afb47c
     :
 }
 
 # _configure_quantum_common()
 # Set common config for all quantum server and agents.
 # This MUST be called before other _configure_quantum_* functions.
 function _configure_quantum_common() {
     # Put config files in ``QUANTUM_CONF_DIR`` for everyone to find
     if [[ ! -d $QUANTUM_CONF_DIR ]]; then
         sudo mkdir -p $QUANTUM_CONF_DIR
     fi
91b8d13e
     sudo chown $STACK_USER $QUANTUM_CONF_DIR
66afb47c
 
     cp $QUANTUM_DIR/etc/quantum.conf $QUANTUM_CONF
 
7a8d8527
     # Set plugin-specific variables Q_DB_NAME, Q_PLUGIN_CLASS.
     # For main plugin config file, set Q_PLUGIN_CONF_PATH, Q_PLUGIN_CONF_FILENAME.
     # For addition plugin config files, set Q_PLUGIN_EXTRA_CONF_PATH,
     # Q_PLUGIN_EXTRA_CONF_FILES.  For example:
     #    Q_PLUGIN_EXTRA_CONF_FILES=(file1, file2)
0dd34df4
     quantum_plugin_configure_common
66afb47c
 
     if [[ $Q_PLUGIN_CONF_PATH == '' || $Q_PLUGIN_CONF_FILENAME == '' || $Q_PLUGIN_CLASS == '' ]]; then
07115eb5
         die $LINENO "Quantum plugin not set.. exiting"
66afb47c
     fi
 
     # If needed, move config file from ``$QUANTUM_DIR/etc/quantum`` to ``QUANTUM_CONF_DIR``
     mkdir -p /$Q_PLUGIN_CONF_PATH
     Q_PLUGIN_CONF_FILE=$Q_PLUGIN_CONF_PATH/$Q_PLUGIN_CONF_FILENAME
     cp $QUANTUM_DIR/$Q_PLUGIN_CONF_FILE /$Q_PLUGIN_CONF_FILE
 
7e79d913
     iniset /$Q_PLUGIN_CONF_FILE DATABASE sql_connection `database_connection_url $Q_DB_NAME`
cf9c10d6
     iniset $QUANTUM_CONF DEFAULT state_path $DATA_DIR/quantum
66afb47c
 
7a8d8527
     # If addition config files are set, make sure their path name is set as well
     if [[ ${#Q_PLUGIN_EXTRA_CONF_FILES[@]} > 0 && $Q_PLUGIN_EXTRA_CONF_PATH == '' ]]; then
         die $LINENO "Quantum additional plugin config not set.. exiting"
     fi
 
     # If additional config files exist, copy them over to quantum configuration
     # directory
     if [[ $Q_PLUGIN_EXTRA_CONF_PATH != '' ]]; then
         mkdir -p /$Q_PLUGIN_EXTRA_CONF_PATH
         local f
         for (( f=0; $f < ${#Q_PLUGIN_EXTRA_CONF_FILES[@]}; f+=1 )); do
             Q_PLUGIN_EXTRA_CONF_FILES[$f]=$Q_PLUGIN_EXTRA_CONF_PATH/${Q_PLUGIN_EXTRA_CONF_FILES[$f]}
             cp $QUANTUM_DIR/${Q_PLUGIN_EXTRA_CONF_FILES[$f]} /${Q_PLUGIN_EXTRA_CONF_FILES[$f]}
         done
     fi
 
66afb47c
     _quantum_setup_rootwrap
 }
 
 function _configure_quantum_debug_command() {
     if [[ "$Q_USE_DEBUG_COMMAND" != "True" ]]; then
         return
     fi
 
     cp $QUANTUM_DIR/etc/l3_agent.ini $QUANTUM_TEST_CONFIG_FILE
 
     iniset $QUANTUM_TEST_CONFIG_FILE DEFAULT verbose False
     iniset $QUANTUM_TEST_CONFIG_FILE DEFAULT debug False
     iniset $QUANTUM_TEST_CONFIG_FILE DEFAULT use_namespaces $Q_USE_NAMESPACE
     iniset $QUANTUM_TEST_CONFIG_FILE DEFAULT root_helper "$Q_RR_COMMAND"
d9ca2b2f
     # Intermediate fix until Quantum patch lands and then line above will
     # be cleaned.
     iniset $QUANTUM_TEST_CONFIG_FILE AGENT root_helper "$Q_RR_COMMAND"
66afb47c
 
     _quantum_setup_keystone $QUANTUM_TEST_CONFIG_FILE DEFAULT set_auth_url
     _quantum_setup_interface_driver $QUANTUM_TEST_CONFIG_FILE
 
0dd34df4
     quantum_plugin_configure_debug_command
66afb47c
 }
 
 function _configure_quantum_dhcp_agent() {
     AGENT_DHCP_BINARY="$QUANTUM_DIR/bin/quantum-dhcp-agent"
     Q_DHCP_CONF_FILE=$QUANTUM_CONF_DIR/dhcp_agent.ini
 
     cp $QUANTUM_DIR/etc/dhcp_agent.ini $Q_DHCP_CONF_FILE
 
     iniset $Q_DHCP_CONF_FILE DEFAULT verbose True
     iniset $Q_DHCP_CONF_FILE DEFAULT debug True
     iniset $Q_DHCP_CONF_FILE DEFAULT use_namespaces $Q_USE_NAMESPACE
     iniset $Q_DHCP_CONF_FILE DEFAULT root_helper "$Q_RR_COMMAND"
 
     _quantum_setup_keystone $Q_DHCP_CONF_FILE DEFAULT set_auth_url
     _quantum_setup_interface_driver $Q_DHCP_CONF_FILE
 
0dd34df4
     quantum_plugin_configure_dhcp_agent
66afb47c
 }
 
 function _configure_quantum_l3_agent() {
555ecd07
     Q_L3_ENABLED=True
     # for l3-agent, only use per tenant router if we have namespaces
     Q_L3_ROUTER_PER_TENANT=$Q_USE_NAMESPACE
66afb47c
     AGENT_L3_BINARY="$QUANTUM_DIR/bin/quantum-l3-agent"
     PUBLIC_BRIDGE=${PUBLIC_BRIDGE:-br-ex}
     Q_L3_CONF_FILE=$QUANTUM_CONF_DIR/l3_agent.ini
 
     cp $QUANTUM_DIR/etc/l3_agent.ini $Q_L3_CONF_FILE
 
     iniset $Q_L3_CONF_FILE DEFAULT verbose True
     iniset $Q_L3_CONF_FILE DEFAULT debug True
     iniset $Q_L3_CONF_FILE DEFAULT use_namespaces $Q_USE_NAMESPACE
     iniset $Q_L3_CONF_FILE DEFAULT root_helper "$Q_RR_COMMAND"
 
     _quantum_setup_keystone $Q_L3_CONF_FILE DEFAULT set_auth_url
     _quantum_setup_interface_driver $Q_L3_CONF_FILE
 
0dd34df4
     quantum_plugin_configure_l3_agent
66afb47c
 }
 
 function _configure_quantum_metadata_agent() {
     AGENT_META_BINARY="$QUANTUM_DIR/bin/quantum-metadata-agent"
     Q_META_CONF_FILE=$QUANTUM_CONF_DIR/metadata_agent.ini
 
     cp $QUANTUM_DIR/etc/metadata_agent.ini $Q_META_CONF_FILE
 
     iniset $Q_META_CONF_FILE DEFAULT verbose True
     iniset $Q_META_CONF_FILE DEFAULT debug True
     iniset $Q_META_CONF_FILE DEFAULT nova_metadata_ip $Q_META_DATA_IP
     iniset $Q_META_CONF_FILE DEFAULT root_helper "$Q_RR_COMMAND"
 
     _quantum_setup_keystone $Q_META_CONF_FILE DEFAULT set_auth_url
 }
 
b7d82842
 function _configure_quantum_lbaas()
 {
     quantum_agent_lbaas_install_agent_packages
     quantum_agent_lbaas_configure_common
     quantum_agent_lbaas_configure_agent
 }
 
66afb47c
 # _configure_quantum_plugin_agent() - Set config files for quantum plugin agent
 # It is called when q-agt is enabled.
 function _configure_quantum_plugin_agent() {
2298ca4f
     # Specify the default root helper prior to agent configuration to
98e18e99
     # ensure that an agent's configuration can override the default
2298ca4f
     iniset /$Q_PLUGIN_CONF_FILE AGENT root_helper "$Q_RR_COMMAND"
 
66afb47c
     # Configure agent for plugin
0dd34df4
     quantum_plugin_configure_plugin_agent
66afb47c
 }
 
 # _configure_quantum_service() - Set config files for quantum service
 # It is called when q-svc is enabled.
 function _configure_quantum_service() {
     Q_API_PASTE_FILE=$QUANTUM_CONF_DIR/api-paste.ini
     Q_POLICY_FILE=$QUANTUM_CONF_DIR/policy.json
 
     cp $QUANTUM_DIR/etc/api-paste.ini $Q_API_PASTE_FILE
     cp $QUANTUM_DIR/etc/policy.json $Q_POLICY_FILE
 
     if is_service_enabled $DATABASE_BACKENDS; then
         recreate_database $Q_DB_NAME utf8
     else
07115eb5
         die $LINENO "A database must be enabled in order to use the $Q_PLUGIN Quantum plugin."
66afb47c
     fi
 
     # Update either configuration file with plugin
     iniset $QUANTUM_CONF DEFAULT core_plugin $Q_PLUGIN_CLASS
 
b7d82842
     if [[ $Q_SERVICE_PLUGIN_CLASSES != '' ]]; then
         iniset $QUANTUM_CONF DEFAULT service_plugins $Q_SERVICE_PLUGIN_CLASSES
     fi
 
66afb47c
     iniset $QUANTUM_CONF DEFAULT verbose True
     iniset $QUANTUM_CONF DEFAULT debug True
3de02e8b
     iniset $QUANTUM_CONF DEFAULT policy_file $Q_POLICY_FILE
66afb47c
     iniset $QUANTUM_CONF DEFAULT allow_overlapping_ips $Q_ALLOW_OVERLAPPING_IP
 
     iniset $QUANTUM_CONF DEFAULT auth_strategy $Q_AUTH_STRATEGY
712feb66
     _quantum_setup_keystone $QUANTUM_CONF keystone_authtoken
     # Comment out keystone authtoken configuration in api-paste.ini
     # It is required to avoid any breakage in Quantum where the sample
     # api-paste.ini has authtoken configurations.
     _quantum_commentout_keystone_authtoken $Q_API_PASTE_FILE filter:authtoken
66afb47c
 
     # Configure plugin
0dd34df4
     quantum_plugin_configure_service
66afb47c
 }
 
 # Utility Functions
 #------------------
 
 # _quantum_setup_rootwrap() - configure Quantum's rootwrap
 function _quantum_setup_rootwrap() {
eb1aa3d5
     if [[ "$Q_USE_ROOTWRAP" == "False" ]]; then
         return
     fi
     # Deploy new rootwrap filters files (owned by root).
     # Wipe any existing rootwrap.d files first
60e9c0ab
     Q_CONF_ROOTWRAP_D=$QUANTUM_CONF_DIR/rootwrap.d
eb1aa3d5
     if [[ -d $Q_CONF_ROOTWRAP_D ]]; then
         sudo rm -rf $Q_CONF_ROOTWRAP_D
     fi
60e9c0ab
     # Deploy filters to $QUANTUM_CONF_DIR/rootwrap.d
eb1aa3d5
     mkdir -p -m 755 $Q_CONF_ROOTWRAP_D
     cp -pr $QUANTUM_DIR/etc/quantum/rootwrap.d/* $Q_CONF_ROOTWRAP_D/
     sudo chown -R root:root $Q_CONF_ROOTWRAP_D
     sudo chmod 644 $Q_CONF_ROOTWRAP_D/*
60e9c0ab
     # Set up rootwrap.conf, pointing to $QUANTUM_CONF_DIR/rootwrap.d
13aab25d
     # location moved in newer versions, prefer new location
     if test -r $QUANTUM_DIR/etc/quantum/rootwrap.conf; then
       sudo cp -p $QUANTUM_DIR/etc/quantum/rootwrap.conf $Q_RR_CONF_FILE
     else
       sudo cp -p $QUANTUM_DIR/etc/rootwrap.conf $Q_RR_CONF_FILE
     fi
eb1aa3d5
     sudo sed -e "s:^filters_path=.*$:filters_path=$Q_CONF_ROOTWRAP_D:" -i $Q_RR_CONF_FILE
     sudo chown root:root $Q_RR_CONF_FILE
     sudo chmod 0644 $Q_RR_CONF_FILE
     # Specify rootwrap.conf as first parameter to quantum-rootwrap
     ROOTWRAP_SUDOER_CMD="$QUANTUM_ROOTWRAP $Q_RR_CONF_FILE *"
 
     # Set up the rootwrap sudoers for quantum
     TEMPFILE=`mktemp`
     echo "$USER ALL=(root) NOPASSWD: $ROOTWRAP_SUDOER_CMD" >$TEMPFILE
     chmod 0440 $TEMPFILE
     sudo chown root:root $TEMPFILE
     sudo mv $TEMPFILE /etc/sudoers.d/quantum-rootwrap
98e18e99
 
     # Update the root_helper
     iniset $QUANTUM_CONF AGENT root_helper "$Q_RR_COMMAND"
eb1aa3d5
 }
 
d6767d0d
 # Configures keystone integration for quantum service and agents
66afb47c
 function _quantum_setup_keystone() {
d6767d0d
     local conf_file=$1
     local section=$2
     local use_auth_url=$3
     if [[ -n $use_auth_url ]]; then
         iniset $conf_file $section auth_url "$KEYSTONE_SERVICE_PROTOCOL://$KEYSTONE_AUTH_HOST:$KEYSTONE_AUTH_PORT/v2.0"
     else
         iniset $conf_file $section auth_host $KEYSTONE_SERVICE_HOST
         iniset $conf_file $section auth_port $KEYSTONE_AUTH_PORT
         iniset $conf_file $section auth_protocol $KEYSTONE_SERVICE_PROTOCOL
     fi
     iniset $conf_file $section admin_tenant_name $SERVICE_TENANT_NAME
     iniset $conf_file $section admin_user $Q_ADMIN_USERNAME
     iniset $conf_file $section admin_password $SERVICE_PASSWORD
5e3deb67
     iniset $conf_file $section signing_dir $QUANTUM_AUTH_CACHE_DIR
     # Create cache dir
     sudo mkdir -p $QUANTUM_AUTH_CACHE_DIR
91b8d13e
     sudo chown $STACK_USER $QUANTUM_AUTH_CACHE_DIR
23431f35
     rm -f $QUANTUM_AUTH_CACHE_DIR/*
d6767d0d
 }
 
712feb66
 function _quantum_commentout_keystone_authtoken() {
     local conf_file=$1
     local section=$2
 
     inicomment $conf_file $section auth_host
     inicomment $conf_file $section auth_port
     inicomment $conf_file $section auth_protocol
     inicomment $conf_file $section auth_url
 
     inicomment $conf_file $section admin_tenant_name
     inicomment $conf_file $section admin_user
     inicomment $conf_file $section admin_password
     inicomment $conf_file $section signing_dir
 }
 
66afb47c
 function _quantum_setup_interface_driver() {
0dd34df4
     quantum_plugin_setup_interface_driver $1
602cf9bd
 }
 
66afb47c
 # Functions for Quantum Exercises
 #--------------------------------
 
 function delete_probe() {
     local from_net="$1"
     net_id=`_get_net_id $from_net`
     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}'`
     quantum-debug --os-tenant-name admin --os-username admin probe-delete $probe_id
 }
 
 function setup_quantum_debug() {
     if [[ "$Q_USE_DEBUG_COMMAND" == "True" ]]; then
         public_net_id=`_get_net_id $PUBLIC_NETWORK_NAME`
3452f8eb
         quantum-debug --os-tenant-name admin --os-username admin --os-password $ADMIN_PASSWORD probe-create --device-owner compute $public_net_id
66afb47c
         private_net_id=`_get_net_id $PRIVATE_NETWORK_NAME`
3452f8eb
         quantum-debug --os-tenant-name admin --os-username admin --os-password $ADMIN_PASSWORD probe-create --device-owner compute $private_net_id
602cf9bd
     fi
66afb47c
 }
 
 function teardown_quantum_debug() {
     delete_probe $PUBLIC_NETWORK_NAME
     delete_probe $PRIVATE_NETWORK_NAME
602cf9bd
 }
 
5db5bfa2
 function _get_net_id() {
     quantum --os-tenant-name admin --os-username admin --os-password $ADMIN_PASSWORD net-list | grep $1 | awk '{print $2}'
 }
 
 function _get_probe_cmd_prefix() {
     local from_net="$1"
     net_id=`_get_net_id $from_net`
     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`
8bc21f64
     echo "$Q_RR_COMMAND ip netns exec qprobe-$probe_id"
5db5bfa2
 }
 
 function _ping_check_quantum() {
     local from_net=$1
     local ip=$2
     local timeout_sec=$3
     local expected=${4:-"True"}
     local check_command=""
     probe_cmd=`_get_probe_cmd_prefix $from_net`
     if [[ "$expected" = "True" ]]; then
8bc21f64
         check_command="while ! $probe_cmd ping -w 1 -c 1 $ip; do sleep 1; done"
5db5bfa2
     else
8bc21f64
         check_command="while $probe_cmd ping -w 1 -c 1 $ip; do sleep 1; done"
5db5bfa2
     fi
     if ! timeout $timeout_sec sh -c "$check_command"; then
         if [[ "$expected" = "True" ]]; then
07115eb5
             die $LINENO "[Fail] Couldn't ping server"
5db5bfa2
         else
07115eb5
             die $LINENO "[Fail] Could ping server"
5db5bfa2
         fi
     fi
 }
 
 # ssh check
 function _ssh_check_quantum() {
     local from_net=$1
     local key_file=$2
     local ip=$3
     local user=$4
     local timeout_sec=$5
     local probe_cmd = ""
     probe_cmd=`_get_probe_cmd_prefix $from_net`
cc6b4435
     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
07115eb5
         die $LINENO "server didn't become ssh-able!"
5db5bfa2
     fi
 }
 
66afb47c
 # Quantum 3rd party programs
 #---------------------------
0dd34df4
 # please refer to lib/quantum_thirdparty/README.md for details
 QUANTUM_THIRD_PARTIES=""
 for f in $TOP_DIR/lib/quantum_thirdparty/*; do
      third_party=$(basename $f)
      if is_service_enabled $third_party; then
          source $TOP_DIR/lib/quantum_thirdparty/$third_party
          QUANTUM_THIRD_PARTIES="$QUANTUM_THIRD_PARTIES,$third_party"
      fi
66afb47c
 done
 
0dd34df4
 function _quantum_third_party_do() {
66afb47c
     for third_party in ${QUANTUM_THIRD_PARTIES//,/ }; do
0dd34df4
         ${1}_${third_party}
66afb47c
     done
5db5bfa2
 }
 
0dd34df4
 # configure_quantum_third_party() - Set config files, create data dirs, etc
 function configure_quantum_third_party() {
     _quantum_third_party_do configure
 }
 
66afb47c
 # init_quantum_third_party() - Initialize databases, etc.
 function init_quantum_third_party() {
0dd34df4
     _quantum_third_party_do init
66afb47c
 }
 
 # install_quantum_third_party() - Collect source and prepare
 function install_quantum_third_party() {
0dd34df4
     _quantum_third_party_do install
66afb47c
 }
 
 # start_quantum_third_party() - Start running processes, including screen
 function start_quantum_third_party() {
0dd34df4
     _quantum_third_party_do start
5db5bfa2
 }
 
66afb47c
 # stop_quantum_third_party - Stop running processes (non-screen)
 function stop_quantum_third_party() {
0dd34df4
     _quantum_third_party_do stop
66afb47c
 }
 
 
d6767d0d
 # Restore xtrace
 $XTRACE
584d90ec
 
 # Local variables:
 # mode: shell-script
 # End: