Created
February 19, 2016 22:07
-
-
Save salv-orlando/2026ca6548bea888f23c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
set -x | |
OVS_REPO=${OVS_REPO:-http://github.com/openvswitch/ovs.git} | |
OVS_REPO_NAME=$(basename ${OVS_REPO} | cut -f1 -d'.') | |
OVN_REPO_NAME=$OVS_REPO_NAME | |
OVN_REMOTE=${OVN_REMOTE:-tcp:$HOST_IP:6640} | |
OVS_BRANCH=${OVS_BRANCH:-origin/master} | |
USE_SCREEN=${USE_SCREEN:-True} | |
SCREEN_NAME=${SCREEN_NAME:-ovn} | |
SCREEN_SLEEP=${SCREEN_SLEEP:-1} | |
DEST=${DEST:-~} | |
SERVICE_DIR=${DEST}/status | |
SCREEN_LOGDIR=${DEST}/screen-logs | |
LOGDIR=$DEST/logs | |
DATA_DIR=$DEST/data | |
ENABLED_SERVICES=${ENABLED_SERVICES:-"ovn-northd,ovn-controller"} | |
BR_INT=${BR_INT:-"br-int"} | |
source ./functions-common | |
# load_module() - Load module using modprobe module given by argument and dies | |
# on failure | |
# - fatal argument is optional and says whether function should | |
# exit if module can't be loaded | |
function load_module { | |
local module=$1 | |
local fatal=$2 | |
sudo modprobe $module || (echo "FAILED TO LOAD $module") | |
} | |
function is_ovn_service_enabled { | |
ovn_service=$1 | |
is_service_enabled ovn && return 0 | |
is_service_enabled $ovn_service && return 0 | |
return 1 | |
} | |
# compile_ovs() - Compile OVS from source and load needed modules. | |
# Accepts two parameters: | |
# - first one is True, modules are built and installed. | |
# - second optional parameter defines prefix for ovs compilation | |
# Env variables OVS_REPO_NAME, OVS_REPO and OVS_BRANCH must be set | |
function compile_ovs { | |
local _pwd=$(pwd) | |
local build_modules=${1:-True} | |
local prefix=$2 | |
if [ -n "$prefix" ]; then | |
prefix="--prefix=$prefix" | |
fi | |
cd $DEST | |
if [ ! -d $OVS_REPO_NAME ] ; then | |
git clone $OVS_REPO | |
cd $OVS_REPO_NAME | |
git checkout $OVS_BRANCH | |
else | |
cd $OVS_REPO_NAME | |
fi | |
install_package autoconf automake libtool gcc patch make libssl-dev | |
if is_fedora ; then | |
# is_fedora covers Fedora, RHEL, CentOS, etc... | |
install_package kernel-devel | |
fi | |
if [ ! -f configure ] ; then | |
./boot.sh | |
fi | |
if [ ! -f config.status ] || [ configure -nt config.status ] ; then | |
if [[ "$build_modules" == "True" ]]; then | |
./configure $prefix --localstatedir=/var --with-linux=/lib/modules/$(uname -r)/build --sysconfdir=/etc --enable-ssl | |
else | |
./configure $prefix --localstatedir=/var | |
fi | |
fi | |
make -j$[$(nproc) + 1] | |
sudo make install | |
if [[ "$build_modules" == "True" ]]; then | |
sudo make modules_install | |
sudo modprobe -r vport_geneve | |
sudo modprobe -r openvswitch | |
fi | |
load_module openvswitch | |
load_module vport-geneve | |
cd $_pwd | |
} | |
# init_ovn() - Initialize databases, etc. | |
function init_ovn { | |
# clean up from previous (possibly aborted) runs | |
# create required data files | |
# Assumption: this is a dedicated test system and there is nothing important | |
# in the ovn, ovn-nb, or ovs databases. We're going to trash them and | |
# create new ones on each devstack run. | |
base_dir=$DATA_DIR/ovs | |
mkdir -p $base_dir | |
echo "Removing existing OVN databases" | |
for db in ovnsb.db ovnnb.db ; do | |
if [ -f $base_dir/$db ] ; then | |
ovs-appctl -t ovsdb-server ovsdb-server/remove-db $base_dir/$db | |
rm -f $base_dir/$db | |
fi | |
done | |
rm -f $base_dir/.*.db.~lock~ | |
echo "Creating OVN-Southbound and OVN-Northbound Databases" | |
if is_ovn_service_enabled ovn-northd ; then | |
echo "Creating new OVN databases" | |
ovsdb-tool create $base_dir/ovnsb.db $DEST/$OVN_REPO_NAME/ovn/ovn-sb.ovsschema | |
ovsdb-tool create $base_dir/ovnnb.db $DEST/$OVN_REPO_NAME/ovn/ovn-nb.ovsschema | |
echo "Adding OVN databases" | |
sudo ovs-appctl -t ovsdb-server ovsdb-server/add-db $base_dir/ovnsb.db | |
sudo ovs-appctl -t ovsdb-server ovsdb-server/add-db $base_dir/ovnnb.db | |
fi | |
} | |
# install_ovn() - Collect source and prepare | |
function install_ovn { | |
echo "Installing OVN and dependent packages" | |
# If OVS is already installed, remove it, because we're about to re-install | |
# it from source. | |
for package in openvswitch openvswitch-switch openvswitch-common; do | |
if is_package_installed $package ; then | |
uninstall_package $package | |
fi | |
done | |
compile_ovs "True" "/usr" | |
} | |
function start_ovs { | |
echo "Starting OVS" | |
local _pwd=$(pwd) | |
OVSDB_REMOTE="" | |
if is_ovn_service_enabled ovn-northd ; then | |
OVSDB_REMOTE="ptcp:6640:$HOST_IP" | |
fi | |
if is_ubuntu; then | |
# Use debian starter | |
sudo cp $DEST/$OVS_REPO_NAME/debian/openvswitch-switch.init /etc/init.d/openvswitch-switch | |
sudo /etc/init.d/openvswitch-switch start | |
else | |
# Run binaries directly | |
# TODO: systemd support for fedora,rhel,etc | |
local ovsdb_logfile="ovsdb-server.log.${CURRENT_LOG_TIME}" | |
bash -c "cd '$LOGDIR' && touch '$ovsdb_logfile' && ln -sf '$ovsdb_logfile' ovsdb-server.log" | |
if is_ovn_service_enabled ovn-northd || is_ovn_service_enabled ovn-controller; then | |
ovsdb-server --remote=punix:/var/run/openvswitch/db.sock \ | |
--remote=db:Open_vSwitch,Open_vSwitch,manager_options \ | |
--private-key=db:Open_vSwitch,SSL,private_key \ | |
--certificate=db:Open_vSwitch,SSL,certificate \ | |
--bootstrap-ca-cert=db:Open_vSwitch,SSL,ca_cert \ | |
--no-chdir \ | |
--pidfile --detach -vconsole:off \ | |
--log-file=$LOGDIR/ovsdb-server.log $OVSDB_REMOTE \ | |
/etc/openvswitch/conf.db | |
echo -n "Waiting for ovsdb-server to start ... " | |
while ! test -e /var/run/openvswitch/db.sock ; do | |
sleep 1 | |
done | |
echo "done." | |
if is_ovn_service_enabled ovn-controller ; then | |
local ovswd_logfile="ovs-switchd.log.${CURRENT_LOG_TIME}" | |
bash -c "cd '$LOGDIR' && touch '$ovswd_logfile' && ln -sf '$ovswd_logfile' ovs-vswitchd.log" | |
# Bump up the max number of open files ovs-vswitchd can have | |
sudo sh -c "ulimit -n 32000 && exec ovs-vswitchd --pidfile --detach -vconsole:off --log-file=$LOGDIR/ovs-vswitchd.log" | |
fi | |
ovs-vsctl --no-wait init | |
fi | |
fi | |
sudo ovs-appctl -t ovsdb-server ovsdb-server/add-remote $OVSDB_REMOTE | |
if is_ovn_service_enabled ovn-controller ; then | |
ovs-vsctl --no-wait set Open_vSwitch . external-ids:ovn-remote="$OVN_REMOTE" | |
ovs-vsctl --no-wait set Open_vSwitch . external-ids:ovn-encap-type="geneve" | |
ovs-vsctl --no-wait set Open_vSwitch . external-ids:ovn-encap-ip="$HOST_IP" | |
fi | |
cd $_pwd | |
} | |
# start_ovn() - Start running processes, including screen | |
function start_ovn { | |
echo "Starting OVN" | |
if [[ "$USE_SCREEN" == True ]]; then | |
screen -d -m -S $SCREEN_NAME -t shell -s /bin/bash | |
sleep 1 | |
SCREEN_HARDSTATUS='%{= .} %-Lw%{= .}%> %n%f %t*%{= .}%+Lw%< %-=%{g}(%{d}%H/%l%{g})' | |
screen -r $SCREEN_NAME -X hardstatus alwayslastline "$SCREEN_HARDSTATUS" | |
screen -r $SCREEN_NAME -X setenv PROMPT_COMMAND /bin/true | |
fi | |
if is_ovn_service_enabled ovn-northd ; then | |
# TODO (regXboi) ovn-northd doesn't appear to log to console at | |
# all - revisit this after that is fixed | |
run_process ovn-northd "ovn-northd --pidfile --log-file=$LOGDIR/ovn-northd.log" | |
# This makes sure that the console logs have time stamps to | |
# the millisecond, but we need to make sure ovs-appctl has | |
# a pid file to work with, so ... | |
echo -n "Waiting for ovn-northd to start ... " | |
while ! test -e /var/run/openvswitch/ovn-northd.pid ; do | |
sleep 1 | |
done | |
echo "done." | |
sudo ovs-appctl -t ovn-northd vlog/set "PATTERN:CONSOLE:%D{%Y-%m-%dT%H:%M:%S.###Z}|%05N|%c%T|%p|%m" | |
fi | |
if is_ovn_service_enabled ovn-controller ; then | |
# (regXboi) pulling out --log-file to avoid double logging | |
# appears to break devstack, so let's not do that | |
run_process ovn-controller "sudo ovn-controller --pidfile --log-file unix:/var/run/openvswitch/db.sock" | |
# This makes sure that the console logs have time stamps to | |
# the millisecond, but we need to make sure ovs-appctl has | |
# a pid file to work with, so ... | |
echo -n "Waiting for ovn-controller to start ... " | |
while ! test -e /var/run/openvswitch/ovn-controller.pid ; do | |
sleep 1 | |
done | |
echo "done." | |
sudo ovs-appctl -t ovn-controller vlog/set "PATTERN:CONSOLE:%D{%Y-%m-%dT%H:%M:%S.###Z}|%05N|%c%T|%p|%m" | |
fi | |
} | |
# stop_ovn() - Stop running processes (non-screen) | |
function stop_ovn { | |
if is_ovn_service_enabled ovn-controller ; then | |
stop_process ovn-controller | |
if is_ubuntu ; then | |
: | |
else | |
sudo killall ovs-vswitchd | |
fi | |
fi | |
if is_ovn_service_enabled ovn-northd ; then | |
stop_process ovn-northd | |
fi | |
if is_ubuntu ; then | |
sudo service openvswitch-switch stop | |
else | |
sudo killall ovsdb-server | |
fi | |
screen -X -S $SCREEN_NAME quit | |
} | |
# stop_ovs_dp() - Stop OVS datapath | |
function stop_ovs_dp { | |
sudo ovs-dpctl dump-dps | sudo xargs -n1 ovs-dpctl del-dp | |
# sudo rmmod vport_geneve | |
# sudo rmmod openvswitch | |
} | |
function disable_libvirt_apparmor { | |
if ! sudo aa-status --enabled ; then | |
return 0 | |
fi | |
# NOTE(arosen): This is used as a work around to allow newer versions | |
# of libvirt to work with ovs configured ports. See LP#1466631. | |
# requires the apparmor-utils | |
install_package apparmor-utils | |
# disables apparmor for libvirtd | |
sudo aa-complain /etc/apparmor.d/usr.sbin.libvirtd | |
} | |
if [[ "$1" == "start" ]]; then | |
mkdir -p $LOGDIR | |
mkdir -p $SCREEN_LOGDIR | |
mkdir -p $SERVICE_DIR | |
install_ovn | |
start_ovs | |
init_ovn | |
disable_libvirt_apparmor | |
start_ovn | |
grep -lq 'OVN' ~/.bash_profile || echo -e "\n# Enable OVN commands from any node.\nexport OVN_NB_DB=$OVN_REMOTE\nexport OVN_SB_DB=$OVN_REMOTE" >> ~/.bash_profile | |
elif [[ "$1" == "stop" ]]; then | |
stop_ovn | |
stop_ovs_dp | |
fi | |
set +x |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment