From: tierno Date: Wed, 12 Sep 2018 14:25:42 +0000 (+0200) Subject: Adding RO-start.sh to deb package for Dockerfile X-Git-Tag: v5.0.0~36 X-Git-Url: https://osm.etsi.org/gitweb/?a=commitdiff_plain;h=refs%2Fchanges%2F70%2F6470%2F3;p=osm%2FRO.git Adding RO-start.sh to deb package for Dockerfile Change-Id: I2f61868a288a8217dbf2a5c0920f736ec547ffcc Signed-off-by: tierno --- diff --git a/docker/Dockerfile-fromdeb b/docker/Dockerfile-fromdeb index de01e39b..73adb2b9 100644 --- a/docker/Dockerfile-fromdeb +++ b/docker/Dockerfile-fromdeb @@ -26,8 +26,6 @@ RUN apt-get update && apt-get install -y curl mysql-client software-properties-c && mkdir -p /bin/RO -COPY docker/scripts/ /bin/RO - VOLUME /opt/openmano/logs EXPOSE 9090 @@ -54,5 +52,5 @@ ENV RO_DB_NAME=mano_db ENV RO_DB_OVIM_NAME=mano_vim_db -CMD /bin/RO/start.sh +CMD RO-start.sh diff --git a/docker/Dockerfile-local b/docker/Dockerfile-local index aad357c3..c073a12c 100644 --- a/docker/Dockerfile-local +++ b/docker/Dockerfile-local @@ -4,8 +4,6 @@ LABEL authors="Gennadiy Dubina, Alfonso Tierno, Gerardo Garcia" COPY . /root/RO -COPY docker/scripts/ /bin/RO - RUN apt-get update && \ DEBIAN_FRONTEND=noninteractive apt-get -y install software-properties-common && \ DEBIAN_FRONTEND=noninteractive add-apt-repository -y cloud-archive:queens && \ @@ -72,5 +70,5 @@ ENV RO_DB_HOST="" \ RO_DB_OVIM_NAME=mano_vim_db \ OPENMANO_TENANT=osm -CMD /bin/RO/start.sh +CMD /root/RO/scripts/RO-start.sh diff --git a/docker/scripts/start.sh b/docker/scripts/start.sh deleted file mode 100755 index 1506e3a3..00000000 --- a/docker/scripts/start.sh +++ /dev/null @@ -1,128 +0,0 @@ -#!/bin/bash - - -[ -z "$RO_DB_OVIM_HOST" ] && export RO_DB_OVIM_HOST="$RO_DB_HOST" -[ -z "$RO_DB_OVIM_ROOT_PASSWORD" ] && export RO_DB_OVIM_ROOT_PASSWORD="$RO_DB_ROOT_PASSWORD" - -function is_db_created() { - db_host=$1 - db_port=$2 - db_user=$3 - db_pswd=$4 - db_name=$5 - - RESULT=`mysqlshow -h"$db_host" -P"$db_port" -u"$db_user" -p"$db_pswd" | grep -v Wildcard | grep -o $db_name` - if [ "$RESULT" == "$db_name" ]; then - - RESULT=`mysqlshow -h"$db_host" -P"$db_port" -u"$db_user" -p"$db_pswd" "$db_name" | grep -v Wildcard | grep schema_version` - #TODO validate version - if [ -n "$RESULT" ]; then - echo " DB $db_name exists and inited" - return 0 - else - echo " DB $db_name exists BUT not inited" - return 1 - fi - fi - echo " DB $db_name does not exist" - return 1 -} - -function configure(){ - #Database parameters - #db_host: localhost - #db_user: mano - #db_passwd: manopw - #db_name: mano_db - # Database ovim parameters - #db_ovim_host: localhost # by default localhost - #db_ovim_user: mano # DB user - #db_ovim_passwd: manopw # DB password - #db_ovim_name: mano_vim_db # Name of the OVIM MANO DB - - - sed -i "s/^db_host:.*/db_host: $RO_DB_HOST/" /etc/osm/openmanod.cfg || return 1 - sed -i "s/^db_user:.*/db_user: $RO_DB_USER/" /etc/osm/openmanod.cfg || return 1 - sed -i "s/^db_passwd:.*/db_passwd: $RO_DB_PASSWORD/" /etc/osm/openmanod.cfg || return 1 - sed -i "s/^db_name:.*/db_name: $RO_DB_NAME/" /etc/osm/openmanod.cfg || return 1 - sed -i "s/^db_ovim_host:.*/db_ovim_host: $RO_DB_OVIM_HOST/" /etc/osm/openmanod.cfg || return 1 - sed -i "s/^db_ovim_user:.*/db_ovim_user: $RO_DB_OVIM_USER/" /etc/osm/openmanod.cfg || return 1 - sed -i "s/^db_ovim_passwd:.*/db_ovim_passwd: $RO_DB_OVIM_PASSWORD/" /etc/osm/openmanod.cfg || return 1 - sed -i "s/^db_ovim_name:.*/db_ovim_name: $RO_DB_OVIM_NAME/" /etc/osm/openmanod.cfg || return 1 - return 0 -} - -max_attempts=120 -function wait_db(){ - db_host=$1 - db_port=$2 - attempt=0 - echo "Wait until $max_attempts seconds for MySQL mano Server ${db_host}:${db_port} " - while ! mysqladmin ping -h"$db_host" -P"$db_port" --silent; do - #wait 120 sec - if [ $attempt -ge $max_attempts ]; then - echo - echo "Can not connect to database ${db_host}:${db_port} during $max_attempts sec" - return 1 - fi - attempt=$[$attempt+1] - echo -n "." - sleep 1 - done - return 0 -} - - -echo "1/4 Apply config" -configure || exit 1 - - -echo "2/4 Wait for db up" -wait_db "$RO_DB_HOST" "$RO_DB_PORT" || exit 1 -[ "$RO_DB_OVIM_HOST" = "$RO_DB_HOST" ] || wait_db "$RO_DB_OVIM_HOST" "$RO_DB_OVIM_PORT" || exit 1 - - -echo "3/4 Init database" -RO_PATH=`python -c 'import osm_ro; print(osm_ro.__path__[0])'` -echo "RO_PATH: $RO_PATH" -if ! is_db_created "$RO_DB_HOST" "$RO_DB_PORT" "$RO_DB_USER" "$RO_DB_PASSWORD" "$RO_DB_NAME" -then - if [ -n "$RO_DB_ROOT_PASSWORD" ] ; then - mysqladmin -h"$RO_DB_HOST" -uroot -p"$RO_DB_ROOT_PASSWORD" create "$RO_DB_NAME" - echo "CREATE USER '${RO_DB_USER}'@'%' IDENTIFIED BY '${RO_DB_PASSWORD}';" | - mysql -h"$RO_DB_HOST" -uroot -p"$RO_DB_ROOT_PASSWORD" || echo "user ${RO_DB_USER} already created?" - echo "GRANT ALL PRIVILEGES ON ${RO_DB_NAME}.* TO '${RO_DB_USER}'@'%';" | - mysql -h"$RO_DB_HOST" -uroot -p"$RO_DB_ROOT_PASSWORD" || echo "user ${RO_DB_USER} already granted?" - fi - ${RO_PATH}/database_utils/init_mano_db.sh -u "$RO_DB_USER" -p "$RO_DB_PASSWORD" -h "$RO_DB_HOST" \ - -P "${RO_DB_PORT}" -d "${RO_DB_NAME}" || exit 1 -else - echo " migrage database version" - ${RO_PATH}/database_utils/migrate_mano_db.sh -u "$RO_DB_USER" -p "$RO_DB_PASSWORD" -h "$RO_DB_HOST" \ - -P "$RO_DB_PORT" -d "$RO_DB_NAME" -fi - -OVIM_PATH=`python -c 'import lib_osm_openvim; print(lib_osm_openvim.__path__[0])'` -echo "OVIM_PATH: $OVIM_PATH" -if ! is_db_created "$RO_DB_OVIM_HOST" "$RO_DB_OVIM_PORT" "$RO_DB_OVIM_USER" "$RO_DB_OVIM_PASSWORD" "$RO_DB_OVIM_NAME" -then - if [ -n "$RO_DB_OVIM_ROOT_PASSWORD" ] ; then - mysqladmin -h"$RO_DB_OVIM_HOST" -uroot -p"$RO_DB_OVIM_ROOT_PASSWORD" create "$RO_DB_OVIM_NAME" - echo "CREATE USER '${RO_DB_OVIM_USER}'@'%' IDENTIFIED BY '${RO_DB_OVIM_PASSWORD}';" | - mysql -h"$RO_DB_OVIM_HOST" -uroot -p"$RO_DB_OVIM_ROOT_PASSWORD" || - echo "user ${RO_DB_OVIM_USER} already created?" - echo "GRANT ALL PRIVILEGES ON ${RO_DB_OVIM_NAME}.* TO '${RO_DB_OVIM_USER}'@'%';" | - mysql -h"$RO_DB_OVIM_HOST" -uroot -p"$RO_DB_OVIM_ROOT_PASSWORD" || - echo "user ${RO_DB_OVIM_USER} already granted?" - fi - ${OVIM_PATH}/database_utils/init_vim_db.sh -u "$RO_DB_OVIM_USER" -p "$RO_DB_OVIM_PASSWORD" -h "$RO_DB_OVIM_HOST" \ - -P "${RO_DB_OVIM_PORT}" -d "${RO_DB_OVIM_NAME}" || exit 1 -else - echo " migrage database version" - ${OVIM_PATH}/database_utils/migrate_vim_db.sh -u "$RO_DB_OVIM_USER" -p "$RO_DB_OVIM_PASSWORD" -h "$RO_DB_OVIM_HOST"\ - -P "$RO_DB_OVIM_PORT" -d "$RO_DB_OVIM_NAME" -fi - - -echo "4/4 Try to start" -/usr/bin/openmanod -c /etc/osm/openmanod.cfg --log-file=/var/log/osm/openmano.log --create-tenant=osm diff --git a/scripts/RO-start.sh b/scripts/RO-start.sh new file mode 100755 index 00000000..afc746a6 --- /dev/null +++ b/scripts/RO-start.sh @@ -0,0 +1,132 @@ +#!/bin/bash + +# This script is intended for launching RO from a docker container. +# It waits for mysql server ready, normally running on a separate container, ... +# then it checks if database is present and creates it if needed. +# Finally it launches RO server. + +[ -z "$RO_DB_OVIM_HOST" ] && export RO_DB_OVIM_HOST="$RO_DB_HOST" +[ -z "$RO_DB_OVIM_ROOT_PASSWORD" ] && export RO_DB_OVIM_ROOT_PASSWORD="$RO_DB_ROOT_PASSWORD" + +function is_db_created() { + db_host=$1 + db_port=$2 + db_user=$3 + db_pswd=$4 + db_name=$5 + + RESULT=`mysqlshow -h"$db_host" -P"$db_port" -u"$db_user" -p"$db_pswd" | grep -v Wildcard | grep -o $db_name` + if [ "$RESULT" == "$db_name" ]; then + + RESULT=`mysqlshow -h"$db_host" -P"$db_port" -u"$db_user" -p"$db_pswd" "$db_name" | grep -v Wildcard | grep schema_version` + #TODO validate version + if [ -n "$RESULT" ]; then + echo " DB $db_name exists and inited" + return 0 + else + echo " DB $db_name exists BUT not inited" + return 1 + fi + fi + echo " DB $db_name does not exist" + return 1 +} + +function configure(){ + #Database parameters + #db_host: localhost + #db_user: mano + #db_passwd: manopw + #db_name: mano_db + # Database ovim parameters + #db_ovim_host: localhost # by default localhost + #db_ovim_user: mano # DB user + #db_ovim_passwd: manopw # DB password + #db_ovim_name: mano_vim_db # Name of the OVIM MANO DB + + + sed -i "s/^db_host:.*/db_host: $RO_DB_HOST/" /etc/osm/openmanod.cfg || return 1 + sed -i "s/^db_user:.*/db_user: $RO_DB_USER/" /etc/osm/openmanod.cfg || return 1 + sed -i "s/^db_passwd:.*/db_passwd: $RO_DB_PASSWORD/" /etc/osm/openmanod.cfg || return 1 + sed -i "s/^db_name:.*/db_name: $RO_DB_NAME/" /etc/osm/openmanod.cfg || return 1 + sed -i "s/^db_ovim_host:.*/db_ovim_host: $RO_DB_OVIM_HOST/" /etc/osm/openmanod.cfg || return 1 + sed -i "s/^db_ovim_user:.*/db_ovim_user: $RO_DB_OVIM_USER/" /etc/osm/openmanod.cfg || return 1 + sed -i "s/^db_ovim_passwd:.*/db_ovim_passwd: $RO_DB_OVIM_PASSWORD/" /etc/osm/openmanod.cfg || return 1 + sed -i "s/^db_ovim_name:.*/db_ovim_name: $RO_DB_OVIM_NAME/" /etc/osm/openmanod.cfg || return 1 + return 0 +} + +max_attempts=120 +function wait_db(){ + db_host=$1 + db_port=$2 + attempt=0 + echo "Wait until $max_attempts seconds for MySQL mano Server ${db_host}:${db_port} " + while ! mysqladmin ping -h"$db_host" -P"$db_port" --silent; do + #wait 120 sec + if [ $attempt -ge $max_attempts ]; then + echo + echo "Can not connect to database ${db_host}:${db_port} during $max_attempts sec" + return 1 + fi + attempt=$[$attempt+1] + echo -n "." + sleep 1 + done + return 0 +} + + +echo "1/4 Apply config" +configure || exit 1 + + +echo "2/4 Wait for db up" +wait_db "$RO_DB_HOST" "$RO_DB_PORT" || exit 1 +[ "$RO_DB_OVIM_HOST" = "$RO_DB_HOST" ] || wait_db "$RO_DB_OVIM_HOST" "$RO_DB_OVIM_PORT" || exit 1 + + +echo "3/4 Init database" +RO_PATH=`python -c 'import osm_ro; print(osm_ro.__path__[0])'` +echo "RO_PATH: $RO_PATH" +if ! is_db_created "$RO_DB_HOST" "$RO_DB_PORT" "$RO_DB_USER" "$RO_DB_PASSWORD" "$RO_DB_NAME" +then + if [ -n "$RO_DB_ROOT_PASSWORD" ] ; then + mysqladmin -h"$RO_DB_HOST" -uroot -p"$RO_DB_ROOT_PASSWORD" create "$RO_DB_NAME" + echo "CREATE USER '${RO_DB_USER}'@'%' IDENTIFIED BY '${RO_DB_PASSWORD}';" | + mysql -h"$RO_DB_HOST" -uroot -p"$RO_DB_ROOT_PASSWORD" || echo "user ${RO_DB_USER} already created?" + echo "GRANT ALL PRIVILEGES ON ${RO_DB_NAME}.* TO '${RO_DB_USER}'@'%';" | + mysql -h"$RO_DB_HOST" -uroot -p"$RO_DB_ROOT_PASSWORD" || echo "user ${RO_DB_USER} already granted?" + fi + ${RO_PATH}/database_utils/init_mano_db.sh -u "$RO_DB_USER" -p "$RO_DB_PASSWORD" -h "$RO_DB_HOST" \ + -P "${RO_DB_PORT}" -d "${RO_DB_NAME}" || exit 1 +else + echo " migrage database version" + ${RO_PATH}/database_utils/migrate_mano_db.sh -u "$RO_DB_USER" -p "$RO_DB_PASSWORD" -h "$RO_DB_HOST" \ + -P "$RO_DB_PORT" -d "$RO_DB_NAME" +fi + +OVIM_PATH=`python -c 'import lib_osm_openvim; print(lib_osm_openvim.__path__[0])'` +echo "OVIM_PATH: $OVIM_PATH" +if ! is_db_created "$RO_DB_OVIM_HOST" "$RO_DB_OVIM_PORT" "$RO_DB_OVIM_USER" "$RO_DB_OVIM_PASSWORD" "$RO_DB_OVIM_NAME" +then + if [ -n "$RO_DB_OVIM_ROOT_PASSWORD" ] ; then + mysqladmin -h"$RO_DB_OVIM_HOST" -uroot -p"$RO_DB_OVIM_ROOT_PASSWORD" create "$RO_DB_OVIM_NAME" + echo "CREATE USER '${RO_DB_OVIM_USER}'@'%' IDENTIFIED BY '${RO_DB_OVIM_PASSWORD}';" | + mysql -h"$RO_DB_OVIM_HOST" -uroot -p"$RO_DB_OVIM_ROOT_PASSWORD" || + echo "user ${RO_DB_OVIM_USER} already created?" + echo "GRANT ALL PRIVILEGES ON ${RO_DB_OVIM_NAME}.* TO '${RO_DB_OVIM_USER}'@'%';" | + mysql -h"$RO_DB_OVIM_HOST" -uroot -p"$RO_DB_OVIM_ROOT_PASSWORD" || + echo "user ${RO_DB_OVIM_USER} already granted?" + fi + ${OVIM_PATH}/database_utils/init_vim_db.sh -u "$RO_DB_OVIM_USER" -p "$RO_DB_OVIM_PASSWORD" -h "$RO_DB_OVIM_HOST" \ + -P "${RO_DB_OVIM_PORT}" -d "${RO_DB_OVIM_NAME}" || exit 1 +else + echo " migrage database version" + ${OVIM_PATH}/database_utils/migrate_vim_db.sh -u "$RO_DB_OVIM_USER" -p "$RO_DB_OVIM_PASSWORD" -h "$RO_DB_OVIM_HOST"\ + -P "$RO_DB_OVIM_PORT" -d "$RO_DB_OVIM_NAME" +fi + + +echo "4/4 Try to start" +/usr/bin/openmanod -c /etc/osm/openmanod.cfg --log-file=/var/log/osm/openmano.log --create-tenant=osm diff --git a/setup.py b/setup.py index 97d7b4b7..05431bd4 100755 --- a/setup.py +++ b/setup.py @@ -60,7 +60,8 @@ setup(name=_name, data_files = [('/etc/osm/', ['osm_ro/openmanod.cfg']), ('/etc/systemd/system/', ['osm_ro/osm-ro.service']), ], - scripts=['openmanod', 'openmano', 'osm_ro/scripts/service-openmano', 'osm_ro/scripts/openmano-report',], + scripts=['openmanod', 'openmano', 'osm_ro/scripts/service-openmano', 'osm_ro/scripts/openmano-report', + 'osm_ro/scripts/RO-start.sh'], install_requires=_requirements, include_package_data=True, setup_requires=['setuptools-version-command'],