additional checks for running servers
authorGiuseppe Lettieri <g.lettieri@iet.unipi.it>
Sat, 8 Sep 2012 16:51:40 +0000 (18:51 +0200)
committerGiuseppe Lettieri <g.lettieri@iet.unipi.it>
Sat, 8 Sep 2012 16:51:40 +0000 (18:51 +0200)
- Added some more is_switch_running calls.
- Added new is_db_running. We need at least
  a running ovsdb-server to actually delete
  a port or a bridge.

planetlab/scripts/sliver-ovs

index aac0d7a..0f6c402 100755 (executable)
@@ -11,6 +11,7 @@ DB_CONF_FILE=/etc/openvswitch/conf.db
 DB_SCHEMA=/usr/share/openvswitch/vswitch.ovsschema
 DB_PID_FILE=/var/run/openvswitch/db.pid
 DB_LOG=/var/log/ovs-db.log
+DB_CTRL_SOCKET=/var/run/openvswitch/db-ctrl.sock
 ##
 DB_SOCKET=/var/run/openvswitch/db.sock
 ##
@@ -33,6 +34,10 @@ function is_switch_running {
     ovs-appctl --target=$SWITCH_SOCKET version >& /dev/null
 }
 
+function is_db_running {
+    ovs-appctl --target=$DB_CTRL_SOCKET version >& /dev/null
+}
+
 function tapname () {
     IP=$1; shift
     echo $(ip addr show to "$IP/32" | perl -ne '/^\s*\d+:\s*([\w-]+):/ && print $1')
@@ -94,6 +99,7 @@ function start_db () {
            --bootstrap-ca-cert=db:SSL,ca_cert \
            --pidfile=$DB_PID_FILE \
            --log-file=$DB_LOG \
+           --unixctl=$DB_CTRL_SOCKET \
            --detach >& /dev/null
     else
        echo 'ovsdb-server appears to be running already, *not* starting'
@@ -108,6 +114,9 @@ function start_switch () {
 
     [[ -n "$@" ]] && error "Usage: $COMMAND start-switch"
 
+    # ensure ovsdb-server is running
+    is_db_running || { echo "ovsdb-server not running" >&2 ; exit 1 ; }
+
     if [ ! -f "$SWITCH_PID_FILE" ] ; then
        ovs-vswitchd \
            --pidfile=$SWITCH_PID_FILE \
@@ -155,7 +164,7 @@ function create_bridge () {
     # check whether the address is already assigned
     TAPNAME=$(tapname $IP)
     if [ ! -z "$TAPNAME" ]; then
-       if ovs-vsctl --db=$DB_SOCKET br-exists "$TAPNAME"; then
+       if ovs-vsctl --db=unix:$DB_SOCKET br-exists "$TAPNAME"; then
            echo $TAPNAME
            exit 0
        fi
@@ -184,6 +193,9 @@ function create_port () {
     port=$1; shift
     [[ -n "$@" ]] || error "$COMMAND create-port <bridge> <port>"
 
+    # ensure ovs-vswitchd is running
+    is_switch_running || { echo "ovs-vswitchd not running" >&2 ; exit 1 ; }
+
     set -e
     if ! ovs-vsctl --db=$DB_SOCKET list-ports "$bridge" | grep -q "^$port\$"; then
        ovs-vsctl --db=$DB_SOCKET add-port "$bridge" "$port" -- set interface "$port" type=tunnel
@@ -200,9 +212,15 @@ function del_bridge () {
     [[ -n "$@" ]] && error "Usage: ${COMMAND} del-bridge <bridge name>"
 
     W=
-    is_switch_running || W="--no-wait"
+    if ! is_switch_running; then
+       # we can delete the bridge even if ovs-vswitchd is not running,
+       # but we need a running ovsdb-server
+       is_db_running || { echo "ovsdb-server not running" >&2; exit 1; }
+       W="--no-wait"
+    fi
 
-    if ovs-vsctl --db=$DB_SOCKET br-exists "$bridge_name"; then
+    set -e
+    if ovs-vsctl --db=$DB_SOCKET $W br-exists "$bridge_name"; then
        ovs-vsctl --db=$DB_SOCKET $W del-br $bridge_name
     fi
     return 0
@@ -213,9 +231,17 @@ function del_port () {
     bridge_name=$1; shift
     [[ -n "$@" ]] && error "Usage: ${COMMAND} del-port <port>"
 
+    W=
+    if ! is_switch_running; then
+       # we can delete the port even if ovs-vswitchd is not running,
+       # but we need a running ovsdb-server
+       is_db_running || { echo "ovsdb-server not running" >&2; exit 1; }
+       W="--no-wait"
+    fi
+
     set -e
-    if ovs-vsctl --db=$DB_SOCKET port-to-br "$1" >/dev/null 2>&1; then
-       ovs-vsctl --db=$DB_SOCKET del-port "$1"
+    if ovs-vsctl --db=$DB_SOCKET $W port-to-br "$1" >/dev/null 2>&1; then
+       ovs-vsctl --db=$DB_SOCKET $W del-port "$1"
     fi
     return 0
 }