From 04560880c2acab243ca610897e3630c62bc6d427 Mon Sep 17 00:00:00 2001 From: Benjamin Diaz Date: Tue, 26 Feb 2019 17:25:07 -0300 Subject: [PATCH] Modifies POL deployment to use mysql database Adds script to automatically create database if not existant. Adds generation of db env var to install script. Change-Id: I334ffe2a2f9c2570cf94454948e23f1ce4b5aee7 Signed-off-by: Benjamin Diaz --- docker/POL/Dockerfile | 4 +- docker/POL/scripts/start.sh | 55 +++++++++++++++++++++++++++ installers/docker/docker-compose.yaml | 3 +- installers/full_install_osm.sh | 5 +++ 4 files changed, 65 insertions(+), 2 deletions(-) create mode 100644 docker/POL/scripts/start.sh diff --git a/docker/POL/Dockerfile b/docker/POL/Dockerfile index 8a39a858..148e7421 100644 --- a/docker/POL/Dockerfile +++ b/docker/POL/Dockerfile @@ -47,6 +47,8 @@ ARG COMMON_VERSION RUN apt-get --yes update && apt-get -y install python3-osm-policy-module${POL_VERSION} +COPY scripts/ scripts/ + ENV OSMPOL_MESSAGE_DRIVER kafka ENV OSMPOL_MESSAGE_HOST kafka ENV OSMPOL_MESSAGE_PORT 9092 @@ -58,4 +60,4 @@ ENV OSMPOL_SQL_DATABASE_URI sqlite:///mon_sqlite.db ENV OSMPOL_GLOBAL_LOG_LEVEL INFO -CMD osm-policy-agent +CMD /bin/bash scripts/start.sh diff --git a/docker/POL/scripts/start.sh b/docker/POL/scripts/start.sh new file mode 100644 index 00000000..04d0d34b --- /dev/null +++ b/docker/POL/scripts/start.sh @@ -0,0 +1,55 @@ +DB_EXISTS="" + +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 +} + +function is_db_created() { + db_host=$1 + db_port=$2 + db_user=$3 + db_pswd=$4 + db_name=$5 + + if mysqlshow -h"$db_host" -P"$db_port" -u"$db_user" -p"$db_pswd" | grep -v Wildcard | grep -q $db_name; then + echo "DB $db_name exists" + return 0 + else + echo "DB $db_name does not exist" + return 1 + fi +} + +if [[ $OSMPOL_SQL_DATABASE_URI == *'mysql'* ]]; then + DB_HOST=$(echo $OSMPOL_SQL_DATABASE_URI | sed -r 's|^\w+://.+:.+@(.+):.*$|\1|') + DB_PORT=$(echo $OSMPOL_SQL_DATABASE_URI | sed -r 's|^\w+://.*:([0-9]+).*$|\1|') + DB_USER=$(echo $OSMPOL_SQL_DATABASE_URI | sed -r 's|^\w+://(.+):.+@.+$|\1|') + DB_PASSWORD=$(echo $OSMPOL_SQL_DATABASE_URI | sed -r 's|^.+://.+:(.+)@.*$|\1|') + DB_NAME=$(echo $OSMPOL_SQL_DATABASE_URI | sed -r 's|^\w+://.+:.+@.+:.*/(.+)$|\1|') + + wait_db "$DB_HOST" "$DB_PORT" || exit 1 + + is_db_created "$DB_HOST" "$DB_PORT" "$DB_USER" "$DB_PASSWORD" "$DB_NAME" && DB_EXISTS="Y" + + if [ -z $DB_EXISTS ]; then + mysql -h"$DB_HOST" -P"$DB_PORT" -u"$DB_USER" -p"$DB_PASSWORD" --default_character_set utf8 -e "CREATE DATABASE $DB_NAME" + fi +fi + +osm-policy-agent \ No newline at end of file diff --git a/installers/docker/docker-compose.yaml b/installers/docker/docker-compose.yaml index 3a879add..b1f0cc8c 100644 --- a/installers/docker/docker-compose.yaml +++ b/installers/docker/docker-compose.yaml @@ -148,9 +148,10 @@ services: volumes: - pol_db:/app/database environment: - OSMPOL_SQL_DATABASE_URI: sqlite:///app/database/policy_module.db OSMPOL_MESSAGE_HOST: kafka OSMPOL_MESSAGE_PORT: 9092 + env_file: + - ./pol.env #depends_on: # - kafka # - mon diff --git a/installers/full_install_osm.sh b/installers/full_install_osm.sh index a48bd4d6..6381c7e7 100755 --- a/installers/full_install_osm.sh +++ b/installers/full_install_osm.sh @@ -822,6 +822,11 @@ function generate_docker_env_files() { $WORKDIR_SUDO sed -i "s|OSMMON_VCA_SECRET.*|OSMMON_VCA_SECRET=$OSM_VCA_SECRET|g" $OSM_DOCKER_WORK_DIR/mon.env fi + # POL + if [ ! -f $OSM_DOCKER_WORK_DIR/pol.env ]; then + echo "OSMPOL_SQL_DATABASE_URI=mysql://root:${MYSQL_ROOT_PASSWORD}@mysql:3306/pol" | $WORKDIR_SUDO tee -a $OSM_DOCKER_WORK_DIR/pol.env + fi + echo "Finished generation of docker env files" } -- 2.25.1