Merge branch 'v2.0'
Signed-off-by: tierno <alfonso.tiernosepulveda@telefonica.com>
diff --git a/Dockerfile b/Dockerfile
new file mode 100644
index 0000000..2e05659
--- /dev/null
+++ b/Dockerfile
@@ -0,0 +1,10 @@
+FROM ubuntu:16.04
+
+RUN apt-get update && \
+ DEBIAN_FRONTEND=noninteractive apt-get -y install git build-essential && \
+ DEBIAN_FRONTEND=noninteractive apt-get -y install python python-dev python-all python-stdeb fakeroot pypi2deb && \
+ DEBIAN_FRONTEND=noninteractive apt-get -y install python-pip libmysqlclient-dev libssl-dev libffi-dev libvirt-dev && \
+ DEBIAN_FRONTEND=noninteractive pip install --upgrade pip && \
+ DEBIAN_FRONTEND=noninteractive pip install --upgrade setuptools && \
+ DEBIAN_FRONTEND=noninteractive apt-get -y install python-argcomplete python-jsonschema python-logutils python-mysqldb python-paramiko python-requests python-yaml python-bottle python-libvirt
+
diff --git a/Jenkinsfile b/Jenkinsfile
new file mode 100644
index 0000000..8faa181
--- /dev/null
+++ b/Jenkinsfile
@@ -0,0 +1,38 @@
+pipeline {
+ agent {
+ label "pipeline"
+ }
+ stages {
+ stage("Build") {
+ agent {
+ dockerfile true
+ }
+ steps {
+ sh 'make package'
+ stash name: "deb-files", includes: ".build/*.deb"
+ }
+ }
+ stage("Unittest") {
+ agent {
+ dockerfile true
+ }
+ steps {
+ sh 'echo "UNITTEST"'
+ }
+ }
+ stage("Repo Component") {
+ agent any
+ steps {
+ unstash "deb-files"
+ sh '''
+ mkdir -p pool/openvim
+ mv .build/*.deb pool/openvim/
+ mkdir -p dists/ReleaseOne/unstable/openvim/binary-amd64/
+ apt-ftparchive packages pool/openvim > dists/ReleaseOne/unstable/openvim/binary-amd64/Packages
+ gzip -9fk dists/ReleaseOne/unstable/openvim/binary-amd64/Packages
+ '''
+ archiveArtifacts artifacts: "dists/**,pool/openvim/*.deb"
+ }
+ }
+ }
+}
diff --git a/Makefile b/Makefile
index 8a16ee1..3214870 100644
--- a/Makefile
+++ b/Makefile
@@ -12,7 +12,7 @@
find osm_openvim -name '*.pyo' -delete
prepare_lite:
- pip install --upgrade setuptools
+ pip install --user --upgrade setuptools
mkdir -p build
VER1=$(shell git describe | sed -e 's/^v//' |cut -d- -f1); \
VER2=$(shell git describe | cut -d- -f2); \
@@ -38,7 +38,7 @@
sed -i "s/import osm_openvim; print osm_openvim\.__path__\[0\]/import lib_osm_openvim; print lib_osm_openvim\.__path__\[0\]/g" build/lib_osm_openvim/database_utils/migrate_vim_db.sh
prepare:
- pip install --upgrade setuptools
+ pip install --user --upgrade setuptools
mkdir -p build
VER1=$(shell git describe | sed -e 's/^v//' |cut -d- -f1); \
VER2=$(shell git describe | cut -d- -f2); \
@@ -76,14 +76,14 @@
cd build && cp osm_openvim/scripts/python-osm-openvim.postinst deb_dist/osm-openvim*/debian/
cd build/deb_dist/osm-openvim* && dpkg-buildpackage -rfakeroot -uc -us
mkdir -p .build
- cp build/deb_dist/python-*.deb ./build/
+ cp build/deb_dist/python-*.deb .build/
package_lib: clean prepare_lite
#apt-get install -y python-stdeb
cd build && python setup.py --command-packages=stdeb.command sdist_dsc --with-python2=True
cd build/deb_dist/lib-osm-openvim* && dpkg-buildpackage -rfakeroot -uc -us
mkdir -p .build
- cp build/deb_dist/python-*.deb ./build/
+ cp build/deb_dist/python-*.deb .build/
package: clean_deb package_openvim package_lib
diff --git a/database_utils/install-db-server.sh b/database_utils/install-db-server.sh
index 5a9b9ab..0599b57 100755
--- a/database_utils/install-db-server.sh
+++ b/database_utils/install-db-server.sh
@@ -1,63 +1,133 @@
#!/usr/bin/env bash
+DB_NAME='vim_db'
+DB_ADMIN_USER="root"
+DB_USER="vim"
+DB_PASS="vimpw"
+DB_ADMIN_PASSWD=""
+DB_PORT="3306"
+DB_HOST=""
+DB_HOST_PARAM=""
+QUIET_MODE=""
+FORCEDB=""
+UPDATEDB=""
+NO_PACKAGES=""
+UNINSTALL=""
+
+
function usage(){
echo -e "usage: sudo $0 [OPTIONS]"
- echo -e "Install openvim database server"
+ echo -e "Install openvim database server and the needed packages"
echo -e " OPTIONS"
- echo -e " -U USER: database admin user. 'root' by default. Prompts if needed"
+ echo -e " -U USER: database admin user. '$DB_ADMIN_USER' by default. Prompts if needed"
echo -e " -P PASS: database admin password to be used or installed. Prompts if needed"
- echo -e " -d: d database name, default name vim_db"
- echo -e " -u: database user, default name vim"
- echo -e " -p: database pass, default name vimpw"
+ echo -e " -d: database name, '$DB_NAME' by default"
+ echo -e " -u: database user, '$DB_USER' by default"
+ echo -e " -p: database pass, '$DB_PASS' by default"
+ echo -e " -H: HOST database host. 'localhost' by default"
+ echo -e " -T: PORT database port. '$DB_PORT' by default"
echo -e " -q --quiet: install in unattended mode"
echo -e " -h --help: show this help"
- echo -e " --forcedb: reinstall vim_db DB, deleting previous database if exists and creating a new one"
- echo -e " --no-install-packages: <deprecate> use this option to skip updating and installing the requires packages. This avoid wasting time if you are sure requires packages are present e.g. because of a previous installation"
- echo -e " --unistall: Delete DB, by default vim_db"
+ echo -e " --forcedb: if database exists, it is dropped and a new one is created"
+ echo -e " --updatedb: if database exists, it preserves the content and it is updated to the needed version"
+ echo -e " --no-install-packages: use this option to skip updating and installing the requires packages. This avoid wasting time if you are sure requires packages are present e.g. because of a previous installation"
+ echo -e " --unistall: delete database"
+}
+function install_packages(){
+ [ -x /usr/bin/apt-get ] && apt-get install -y $*
+ [ -x /usr/bin/yum ] && yum install -y $*
+
+ #check properly installed
+ for PACKAGE in $*
+ do
+ PACKAGE_INSTALLED="no"
+ [ -x /usr/bin/apt-get ] && dpkg -l $PACKAGE &>> /dev/null && PACKAGE_INSTALLED="yes"
+ [ -x /usr/bin/yum ] && yum list installed $PACKAGE &>> /dev/null && PACKAGE_INSTALLED="yes"
+ if [ "$PACKAGE_INSTALLED" = "no" ]
+ then
+ echo "failed to install package '$PACKAGE'. Revise network connectivity and try again" >&2
+ exit 1
+ fi
+ done
+}
+function _install_mysql_package(){
+ echo '
+ #################################################################
+ ##### INSTALL REQUIRED PACKAGES #####
+ #################################################################'
+ [ "$_DISTRO" == "Ubuntu" ] && ! install_packages "mysql-server" && exit 1
+ [ "$_DISTRO" == "CentOS" -o "$_DISTRO" == "Red" ] && ! install_packages "mariadb mariadb-server" && exit 1
+
+ if [[ "$_DISTRO" == "Ubuntu" ]]
+ then
+ #start services. By default CentOS does not start services
+ service mysql start >> /dev/null
+ # try to set admin password, ignore if fails
+ [[ -n $DBPASSWD ]] && mysqladmin -u $DB_ADMIN_USER -s password $DB_ADMIN_PASSWD
+ fi
+
+ if [ "$_DISTRO" == "CentOS" -o "$_DISTRO" == "Red" ]
+ then
+ #start services. By default CentOS does not start services
+ service mariadb start
+ service httpd start
+ systemctl enable mariadb
+ systemctl enable httpd
+ read -e -p "Do you want to configure mariadb (recommended if not done before) (Y/n)" KK
+ [ "$KK" != "n" -a "$KK" != "no" ] && mysql_secure_installation
+
+ read -e -p "Do you want to set firewall to grant web access port 80,443 (Y/n)" KK
+ [ "$KK" != "n" -a "$KK" != "no" ] &&
+ firewall-cmd --permanent --zone=public --add-service=http &&
+ firewall-cmd --permanent --zone=public --add-service=https &&
+ firewall-cmd --reload
+ fi
}
function _create_db(){
echo '
#################################################################
- ##### CREATE DATABASE #####
+ ##### CREATE AND INIT DATABASE #####
#################################################################'
- echo -e "\nCreating temporary file for MYSQL installation and initialization"
- TEMPFILE="$(mktemp -q --tmpdir "installopenvim.XXXXXX")"
- trap 'rm -f "$TEMPFILE"' EXIT
- chmod 0600 "$TEMPFILE"
- echo -e "[client]\n user='${DB_ADMIN_USER}'\n password='$DB_ADMIN_PASSWD'">"$TEMPFILE"
-
- if db_exists $DB_NAME $TEMPFILE ; then
- if [[ -n $FORCEDB ]]; then
- DBDELETEPARAM=""
- [[ -n $QUIET_MODE ]] && DBDELETEPARAM="-f"
- mysqladmin --defaults-extra-file=$TEMPFILE -s drop ${DB_NAME} $DBDELETEPARAM || ! echo "Could not delete ${DB_NAME} database" || exit 1
- mysqladmin --defaults-extra-file=$TEMPFILE -s create ${DB_NAME} || ! echo "1 Error creating ${DB_NAME} database" || exit 1
- echo "CREATE USER $DB_USER@'localhost' IDENTIFIED BY '$DB_PASS';" | mysql --defaults-extra-file=$TEMPFILE -s || ! echo "2 Failed while creating user ${DB_USER}"
- echo "GRANT ALL PRIVILEGES ON ${DB_NAME}.* TO $DB_USER@'localhost';" | mysql --defaults-extra-file=$TEMPFILE -s || ! echo "3 Failed while granting privileges to user ${DB_USER} at database ${DB_NAME}" || exit 1
- echo " Database '${DB_NAME}' created, user $DB_USER password '$DB_PASS'"
- else
- echo "Database exists. Use option '--forcedb' to force the deletion of the existing one" && exit 1
- fi
- else
- echo "mysqladmin -u$DB_ADMIN_USER $DBPASSWD_PARAM -s create ${DB_NAME}"
-
- mysqladmin -u$DB_ADMIN_USER $DBPASSWD_PARAM -s create ${DB_NAME} || ! echo "4 Error creating ${DB_NAME} database" || exit 1
- echo "CREATE USER $DB_USER@'localhost' IDENTIFIED BY '$DB_PASS';" | mysql --defaults-extra-file=$TEMPFILE -s || ! echo "Failed while creating user vim at database"
- echo "GRANT ALL PRIVILEGES ON ${DB_NAME}.* TO $DB_USER@'localhost';" | mysql --defaults-extra-file=$TEMPFILE -s || ! echo "Failed giving creating user vim at database" || exit 1
- echo " Database '${DB_NAME}' created, user $DB_USER password '$DB_PASS'"
- fi
+ echo "mysqladmin --defaults-extra-file="$TEMPFILE" -s create ${DB_NAME}"
+ mysqladmin --defaults-extra-file="$TEMPFILE" -s create ${DB_NAME} \
+ || ! echo "Error creating ${DB_NAME} database" >&2 \
+ || exit 1
+ echo "CREATE USER $DB_USER@'localhost' IDENTIFIED BY '$DB_PASS';" | mysql --defaults-extra-file="$TEMPFILE" -s 2>/dev/null \
+ || echo "Warning: User '$DB_USER' cannot be created at database. Probably exist" >&2
+ echo "GRANT ALL PRIVILEGES ON ${DB_NAME}.* TO '$DB_USER'@'localhost';" | mysql --defaults-extra-file="$TEMPFILE" -s \
+ || ! echo "Error: Granting privileges to user '$DB_USER' at database" >&2 \
+ || exit 1
+ echo " Database '${DB_NAME}' created, user '$DB_USER' password '$DB_PASS'"
+ DIRNAME=$(dirname $(readlink -f ${BASH_SOURCE[0]}))
+ ${DIRNAME}/init_vim_db.sh -u"$DB_USER" -p"$DB_PASS" -d"$DB_NAME" -P"$DB_PORT" $DB_HOST_PARAM \
+ || ! echo "Error initializing database '$DB_NAME'" >&2 \
+ || exit 1
}
-function _init_db(){
+function _delete_db(){
+ mysqladmin --defaults-extra-file="$TEMPFILE" -s drop "${DB_NAME}" $DBDELETEPARAM \
+ || ! echo "Error: Could not delete '${DB_NAME}' database" >&2 \
+ || exit 1
+}
+
+function _update_db(){
echo '
#################################################################
- ##### INIT DATABASE #####
+ ##### UPDATE DATABASE #####
#################################################################'
+ echo "CREATE USER $DB_USER@'localhost' IDENTIFIED BY '$DB_PASS';" | mysql --defaults-extra-file="$TEMPFILE" -s 2>/dev/null \
+ || echo "Warning: User '$DB_USER' cannot be created at database. Probably exist" >&2
+ echo "GRANT ALL PRIVILEGES ON ${DB_NAME}.* TO '$DB_USER'@'localhost';" | mysql --defaults-extra-file="$TEMPFILE" -s \
+ || ! echo "Error: Granting privileges to user '$DB_USER' at database" >&2 \
+ || exit 1
+ echo " Granted privileges to user '$DB_USER' password '$DB_PASS' to existing database '${DB_NAME}'"
DIRNAME=$(dirname $(readlink -f ${BASH_SOURCE[0]}))
- su $SUDO_USER -c "${DIRNAME}/init_vim_db.sh -u $DB_USER -p $DB_PASS -d ${DB_NAME}" || ! echo "Failed while initializing database" || exit 1
+ ${DIRNAME}/migrate_vim_db.sh -u"$DB_USER" -p"$DB_PASS" -d"$DB_NAME" -P"$DB_PORT" $DB_HOST_PARAM \
+ || ! echo "Error updating database '$DB_NAME'" >&2 \
+ || exit 1
}
function _uninstall_db(){
@@ -67,36 +137,28 @@
#################################################################'
DBDELETEPARAM=""
[[ -n $QUIET_MODE ]] && DBDELETEPARAM="-f"
- mysqladmin --defaults-extra-file=$TEMPFILE -s drop ${DB_NAME} $DBDELETEPARAM || ! echo "Could not delete ${DB_NAME} database" || exit 1
-
+ _delete_db
}
-function db_exists() {
+
+function db_exists(){ # (db_name, credential_file)
+ # check credentials
+ mysqlshow --defaults-extra-file="$2" >/dev/null || exit 1
RESULT=`mysqlshow --defaults-extra-file="$2" | grep -v Wildcard | grep -o $1`
if [ "$RESULT" == "$1" ]; then
- echo " DB $1 exists"
+ # echo " DB $1 exists"
return 0
fi
- echo " DB $1 does not exist"
+ # echo " DB $1 does not exist"
return 1
}
-DB_NAME='vim_db'
-DB_ADMIN_USER="root"
-DB_USER="vim"
-DB_PASS="vimpw"
-DB_ADMIN_PASSWD=""
-DBPASSWD_PARAM=""
-QUIET_MODE=""
-FORCEDB=""
-NO_PACKAGES=""
-UNINSTALL=""
-while getopts ":U:P:d:u:p:hiq-:" o; do
+
+while getopts ":U:P:d:u:p:H:T:hiq-:" o; do
case "${o}" in
U)
export DB_ADMIN_USER="$OPTARG"
;;
P)
export DB_ADMIN_PASSWD="$OPTARG"
- export DBPASSWD_PARAM="-p$OPTARG"
;;
d)
export DB_NAME="$OPTARG"
@@ -107,6 +169,13 @@
p)
export DB_PASS="$OPTARG"
;;
+ H)
+ export DB_HOST="$OPTARG"
+ export DB_HOST_PARAM="-h$DB_HOST"
+ ;;
+ T)
+ export DB_PORT="$OPTARG"
+ ;;
q)
export QUIET_MODE=yes
export DEBIAN_FRONTEND=noninteractive
@@ -117,6 +186,7 @@
-)
[ "${OPTARG}" == "help" ] && usage && exit 0
[ "${OPTARG}" == "forcedb" ] && FORCEDB="y" && continue
+ [ "${OPTARG}" == "updatedb" ] && UPDATEDB="y" && continue
[ "${OPTARG}" == "quiet" ] && export QUIET_MODE=yes && export DEBIAN_FRONTEND=noninteractive && continue
[ "${OPTARG}" == "no-install-packages" ] && export NO_PACKAGES=yes && continue
[ "${OPTARG}" == "uninstall" ] && UNINSTALL="y" && continue
@@ -137,36 +207,38 @@
;;
esac
done
-
-HERE=$(realpath $(dirname $0))
-OPENVIM_BASEFOLDER=$(dirname $HERE)
-[ "$USER" != "root" ] && echo "Needed root privileges" >&2 && exit -1
-
-if [[ -z "$SUDO_USER" ]] || [[ "$SUDO_USER" = "root" ]]
-then
- export SUDO_USER='root'
+if [ -n "$FORCEDB" ] && [ -n "$UPDATEDB" ] ; then
+ echo "Error: options --forcedb and --updatedb are mutually exclusive" >&2
+ exit 1
fi
-#Discover Linux distribution
-#try redhat type
+# Discover Linux distribution
+# try redhat type
[ -f /etc/redhat-release ] && _DISTRO=$(cat /etc/redhat-release 2>/dev/null | cut -d" " -f1)
-#if not assuming ubuntu type
+# if not assuming ubuntu type
[ -f /etc/redhat-release ] || _DISTRO=$(lsb_release -is 2>/dev/null)
+# Creating temporary file for MYSQL installation and initialization"
+TEMPFILE="$(mktemp -q --tmpdir "installdb.XXXXXX")"
+trap 'rm -f "$TEMPFILE"' EXIT
+chmod 0600 "$TEMPFILE"
+echo -e "[client]\n user='${DB_ADMIN_USER}'\n password='$DB_ADMIN_PASSWD'\n host='$DB_HOST'\n port='$DB_PORT'" > "$TEMPFILE"
+
#check and ask for database user password. Must be done after database installation
-if [[ -n $QUIET_MODE ]]
+if [[ -z $QUIET_MODE ]]
then
echo -e "\nCheking database connection and ask for credentials"
- echo "mysqladmin -s -u$DB_ADMIN_USER $DBPASSWD_PARAM status >/dev/null"
- while ! mysqladmin -s -u$DB_ADMIN_USER $DBPASSWD_PARAM status >/dev/null
+ # echo "mysqladmin --defaults-extra-file=$TEMPFILE -s status >/dev/null"
+ while ! mysqladmin --defaults-extra-file="$TEMPFILE" -s status >/dev/null
do
[ -n "$logintry" ] && echo -e "\nInvalid database credentials!!!. Try again (Ctrl+c to abort)"
[ -z "$logintry" ] && echo -e "\nProvide database credentials"
- read -e -p "database user? ($DB_ADMIN_USER) " DBUSER_
+ read -e -p "database admin user? ($DB_ADMIN_USER) " DBUSER_
[ -n "$DBUSER_" ] && DB_ADMIN_USER=$DBUSER_
- read -e -s -p "database password? (Enter for not using password) " DBPASSWD_
- [ -n "$DBPASSWD_" ] && DB_ADMIN_PASSWD="$DBPASSWD_" && DBPASSWD_PARAM="-p$DBPASSWD_"
- [ -z "$DBPASSWD_" ] && DB_ADMIN_PASSWD="" && DBPASSWD_PARAM=""
+ read -e -s -p "database admin password? (Enter for not using password) " DBPASSWD_
+ [ -n "$DBPASSWD_" ] && DB_ADMIN_PASSWD="$DBPASSWD_"
+ [ -z "$DBPASSWD_" ] && DB_ADMIN_PASSWD=""
+ echo -e "[client]\n user='${DB_ADMIN_USER}'\n password='$DB_ADMIN_PASSWD'\n host='$DB_HOST'\n port='$DB_PORT'" > "$TEMPFILE"
logintry="yes"
done
fi
@@ -177,11 +249,36 @@
exit
fi
-
if [[ -z "$NO_PACKAGES" ]]
then
- _create_db
- _init_db
+ [ "$USER" != "root" ] && echo "Needed root privileges" >&2 && exit 1
+ _install_mysql_package || exit 1
fi
+# Create or update database
+if db_exists $DB_NAME $TEMPFILE ; then
+ if [[ -n $FORCEDB ]] ; then
+ # DBDELETEPARAM=""
+ # [[ -n $QUIET_MODE ]] && DBDELETEPARAM="-f"
+ DBDELETEPARAM="-f"
+ _delete_db
+ _create_db
+ elif [[ -n $UPDATEDB ]] ; then
+ _update_db
+ elif [[ -z $QUIET_MODE ]] ; then
+ echo "database '$DB_NAME' exist. Reinstall it?"
+ read -e -p "Type 'y' to drop and reinstall exiting database (content will be lost), Type 'n' to update existing database (y/N)? " KK_
+ if [ "$KK_" == "yes" ] || [ "$KK_" != "y" ] ; then
+ _delete_db
+ _create_db
+ else
+ _update_db
+ fi
+ else
+ echo "Database '$DB_NAME' exists. Use option '--forcedb' to force the deletion of the existing one, or '--updatedb' to use existing one and update it"
+ exit 1
+ fi
+else
+ _create_db
+fi
diff --git a/osm_openvim/ovim.py b/osm_openvim/ovim.py
index a4108b6..f253912 100755
--- a/osm_openvim/ovim.py
+++ b/osm_openvim/ovim.py
@@ -36,7 +36,7 @@
import threading
import vim_db
import logging
-import imp
+# import imp
import argparse
from netaddr import IPNetwork
from jsonschema import validate as js_v, exceptions as js_e
@@ -371,27 +371,29 @@
module = temp_dict['of_controller']
if module not in ovim.of_module:
- module_info = imp.find_module(module)
- of_conn_module = imp.load_module("OF_conn", *module_info)
- ovim.of_module[module] = of_conn_module
+ for base in ("", "osm_openvim.", "lib_osm_openvim."):
+ try:
+ pkg = __import__(base + module)
+ if base:
+ of_conn_module = getattr(pkg, module)
+ else:
+ of_conn_module = pkg
+ ovim.of_module[module] = of_conn_module
+ self.logger.debug("Module load from {}".format(base + module))
+ break
+ except Exception as e:
+ self.logger.warning("Module {} not found {}".format(base + module, e))
+ else:
+ self.logger.error("Cannot open openflow controller module of type '%s'", module)
+ raise ovimException("Cannot open openflow controller of type module '{}'"
+ "Revise it is installed".format(module),
+ HTTP_Internal_Server_Error)
else:
of_conn_module = ovim.of_module[module]
-
- try:
- return of_conn_module.OF_conn(temp_dict)
- except Exception as e:
- self.logger.error("Cannot open the Openflow controller '%s': %s", type(e).__name__, str(e))
- if module_info and module_info[0]:
- file.close(module_info[0])
- raise ovimException("Cannot open the Openflow controller '{}': '{}'".format(type(e).__name__, str(e)),
- HTTP_Internal_Server_Error)
- except (IOError, ImportError) as e:
- if module_info and module_info[0]:
- file.close(module_info[0])
- self.logger.error("Cannot open openflow controller module '%s'; %s: %s; revise 'of_controller' "
- "field of configuration file.", module, type(e).__name__, str(e))
- raise ovimException("Cannot open openflow controller module '{}'; {}: {}; revise 'of_controller' "
- "field of configuration file.".format(module, type(e).__name__, str(e)),
+ return of_conn_module.OF_conn(temp_dict)
+ except Exception as e:
+ self.logger.error("Cannot open the Openflow controller '%s': %s", type(e).__name__, str(e))
+ raise ovimException("Cannot open the Openflow controller '{}': '{}'".format(type(e).__name__, str(e)),
HTTP_Internal_Server_Error)
def _create_ofc_thread(self, of_conn, ofc_uuid="Default"):