From: tierno Date: Tue, 23 Jan 2018 16:07:46 +0000 (+0100) Subject: Dockerfile update. Allow configure openmanod with ENV X-Git-Tag: v3.0.3~9 X-Git-Url: https://osm.etsi.org/gitweb/?a=commitdiff_plain;h=refs%2Fchanges%2F28%2F5828%2F3;p=osm%2FRO.git Dockerfile update. Allow configure openmanod with ENV Change-Id: I02edb2455735b514e7483ad0a250118f07726f56 Signed-off-by: tierno --- diff --git a/docker/Dockerfile-local b/docker/Dockerfile-local index 3eaa281f..08e3f231 100644 --- a/docker/Dockerfile-local +++ b/docker/Dockerfile-local @@ -1,9 +1,6 @@ from ubuntu:xenial -MAINTAINER Gennadiy Dubina - -# script uses it -ENV USER=root +MAINTAINER Gennadiy Dubina ; Alfonso Tierno #avoid extra information from packages RUN echo 'path-exclude /usr/share/doc/*\n\ @@ -19,32 +16,43 @@ APT::AutoRemove::RecommendsImportant "false";\n\ APT::AutoRemove::SuggestsImportant "false";\n'\ > /etc/apt/apt.conf.d/99_norecommends -#generate lsb_release stub -RUN echo 'if [ "$1" == "-is" ]; then echo "Ubuntu"; else if [ "$1" == "-rs" ]; then echo "16.04"; fi fi' > /usr/bin/lsb_release && chmod +x /usr/bin/lsb_release - -COPY . /usr/local/src/openmano - -RUN apt update && \ - DEBIAN_FRONTEND=noninteractive apt install -fqy software-properties-common make gcc git python python-dev python-pip sudo wget && \ - pip install --upgrade pip && \ - cd /usr/local/src/openmano && \ - make build && \ - cd build && \ - ./scripts/install-openmano.sh -q --noclone --no-db && \ - cd / && \ - rm -rf /usr/local/src/openmano && \ - rm -rf /usr/include/* && \ - rm -rf /root/.cache && \ - apt purge -y make gcc git curl wget python-dev python-pip && \ - apt autoremove -y && \ - apt clean && \ - rm -rf /var/lib/apt/lists/* - -COPY docker/scripts/ /opt/openmano-docker + +RUN apt update && apt install -y curl mysql-client software-properties-common \ + && add-apt-repository -y "deb http://osm-download.etsi.org/repository/osm/debian/ReleaseTHREE stable SO UI RO IM osmclient openvim" \ + && curl "http://osm-download.etsi.org/repository/osm/debian/ReleaseTHREE/OSM%20ETSI%20Release%20Key.gpg" | apt-key add - \ + && apt update \ + && apt install -y python-osm-ro \ + && rm -rf /var/lib/apt/lists/* \ + && mkdir -p /bin/RO + + +COPY docker/scripts/ /bin/RO VOLUME /opt/openmano/logs + EXPOSE 9090 -env DB_USER='' DB_PSWD='' DB_HOST='' DB_PORT=3306 DB_NAME=mano_db +# Two mysql databases are needed (DB and DB_OVIM). Can be hosted on same or separated containers +# These ENV must be provided +ENV RO_DB_HOST="" +ENV RO_DB_OVIM_HOST="" + # if empty RO_DB_HOST is assumed + +# These ENV should be provided first time for creating database. It will create and init only if empty! +ENV RO_DB_ROOT_PASSWORD="" +ENV RO_DB_OVIM_ROOT_PASSWORD="" + # if empty RO_DB_ROOT_PASSWORD is assumed + +# These ENV can be provided, but default values are ok +ENV RO_DB_USER=mano +ENV RO_DB_OVIM_USER=mano +ENV RO_DB_PASSWORD=manopw +ENV RO_DB_OVIM_PASSWORD=manopw +ENV RO_DB_PORT=3306 +ENV RO_DB_OVIM_PORT=3306 +ENV RO_DB_NAME=mano_db +ENV RO_DB_OVIM_NAME=mano_vim_db + + +CMD /bin/RO/start.sh -CMD /opt/openmano-docker/start.sh diff --git a/docker/openmano-compose-3c.yml b/docker/openmano-compose-3c.yml new file mode 100644 index 00000000..cd9a12fc --- /dev/null +++ b/docker/openmano-compose-3c.yml @@ -0,0 +1,46 @@ +version: '3' +services: + osm-ro-db: + image: mysql + container_name: osm-ro-db + restart: always + environment: + - MYSQL_RANDOM_ROOT_PASSWORD=true + - MYSQL_DATABASE=mano_db + - MYSQL_USER=mano + - MYSQL_PASSWORD=manopw + osm-ro-ovim-db: + image: mysql + container_name: osm-ro-ovim-db + restart: always + environment: + - MYSQL_RANDOM_ROOT_PASSWORD=true + - MYSQL_DATABASE=mano_vim_db + - MYSQL_USER=mano + - MYSQL_PASSWORD=manopw + osm-ro: + build: + context: ../ + dockerfile: docker/Dockerfile-local + image: osm/ro + container_name: osm-ro + restart: always + environment: + - RO_DB_USER=mano + - RO_DB_PASSWORD=manopw + - RO_DB_NAME=mano_db + - RO_DB_HOST=osm-ro-db + - RO_DB_OVIM_USER=mano + - RO_DB_OVIM_PASSWORD=manopw + - RO_DB_OVIM_NAME=mano_vim_db + - RO_DB_OVIM_HOST=osm-ro-ovim-db + ports: + - "9090:9090" + volumes: + - /var/log/osm/openmano/logs:/var/log/osm/openmano/logs + depends_on: + - osm-ro-db + - osm-ro-ovim-db + links: + - osm-ro-db + - osm-ro-ovim-db \ No newline at end of file diff --git a/docker/openmano-compose.yml b/docker/openmano-compose.yml index 253e9574..d83b72d0 100644 --- a/docker/openmano-compose.yml +++ b/docker/openmano-compose.yml @@ -5,10 +5,7 @@ services: container_name: osm-ro-db restart: always environment: - - MYSQL_RANDOM_ROOT_PASSWORD=true - - MYSQL_DATABASE=mano_db - - MYSQL_USER=mano - - MYSQL_PASSWORD=manopw + - MYSQL_ROOT_PASSWORD=osm4u osm-ro: build: context: ../ @@ -17,10 +14,8 @@ services: container_name: osm-ro restart: always environment: - - DB_USER=mano - - DB_PSWD=manopw - - DB_NAME=mano_db - - DB_HOST=osm-ro-db + - RO_DB_ROOT_PASSWORD=osm4u + - DB_HOST=osm-ro-db ports: - "9090:9090" volumes: diff --git a/docker/scripts/configure.sh b/docker/scripts/configure.sh deleted file mode 100755 index da7cbba1..00000000 --- a/docker/scripts/configure.sh +++ /dev/null @@ -1,12 +0,0 @@ -#!/bin/bash - -#Database parameters -#db_host: localhost -#db_user: mano -#db_passwd: manopw -#db_name: mano_db - -sed -i "s/^db_host:.*/db_host: $DB_HOST/" /opt/openmano/openmanod.cfg -sed -i "s/^db_user:.*/db_user: $DB_USER/" /opt/openmano/openmanod.cfg -sed -i "s/^db_passwd:.*/db_passwd: $DB_PSWD/" /opt/openmano/openmanod.cfg -sed -i "s/^db_name:.*/db_name: $DB_NAME/" /opt/openmano/openmanod.cfg \ No newline at end of file diff --git a/docker/scripts/start.sh b/docker/scripts/start.sh index 3d70c3f3..486f3a0a 100755 --- a/docker/scripts/start.sh +++ b/docker/scripts/start.sh @@ -1,5 +1,9 @@ #!/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 @@ -24,23 +28,101 @@ function is_db_created() { 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" -/opt/openmano-docker/configure.sh -[ $? -ne 0 ] && exit 1 -echo "2/4 Wait for db" - -/opt/openmano-docker/wait_db.sh -[ $? -ne 0 ] && exit 1 - -echo "3/4 Init database" && \ -is_db_created "$DB_HOST" "$DB_PORT" "$DB_USER" "$DB_PSWD" "$DB_NAME" -if [ $? -ne 0 ]; then - /opt/openmano/database_utils/init_mano_db.sh -u $DB_USER -p $DB_PSWD -h $DB_HOST -P $DB_PORT -d $DB_NAME - [ $? -ne 0 ] && exit 1 +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" - /opt/openmano/database_utils/migrate_mano_db.sh -u $DB_USER -p $DB_PSWD -h $DB_HOST -P $DB_PORT -d $DB_NAME + ${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" -/opt/openmano/openmanod.py -c /opt/openmano/openmanod.cfg --log-file=/opt/openmano/logs/openmano.log \ No newline at end of file +openmanod -c /etc/osm/openmanod.cfg --log-file=/var/log/osm/openmano.log diff --git a/docker/scripts/wait_db.sh b/docker/scripts/wait_db.sh deleted file mode 100755 index f083d76b..00000000 --- a/docker/scripts/wait_db.sh +++ /dev/null @@ -1,14 +0,0 @@ -#!/bin/bash - -attempt=0 -max_attempts=120 -while ! mysqladmin ping -h"$DB_HOST" -P"$DB_PORT" --silent; do - #wait 120 sec - if [ $attempt -ge $max_attempts ]; then - echo "Can not connect to database during $max_attempts sec" - exit 1 - fi - attempt=$[$attempt+1] - echo "Wait for MySQL Server ${DB_HOST}:${DB_PORT} [attempt $attempt/$max_attempts]" - sleep 1 -done \ No newline at end of file diff --git a/openmanod b/openmanod index 22bcacc6..06163891 100755 --- a/openmanod +++ b/openmanod @@ -36,7 +36,7 @@ import time import sys import getopt import yaml -import os.path +from os import getenv as os_getenv, path as os_path from jsonschema import validate as js_v, exceptions as js_e import logging import logging.handlers as log_handlers @@ -48,7 +48,7 @@ import osm_ro __author__ = "Alfonso Tierno, Gerardo Garcia, Pablo Montes" __date__ = "$26-aug-2014 11:09:29$" -__version__ = "0.5.50-r560" +__version__ = "0.5.51-r561" version_date = "Jan 2018" database_version = 27 # expected database schema version @@ -127,15 +127,15 @@ def console_port_iterator(): def usage(): print("Usage: ", sys.argv[0], "[options]") - print( " -v|--version: prints current version") - print( " -c|--config [configuration_file]: loads the configuration file (default: openmanod.cfg)") - print( " -h|--help: shows this help") - print( " -p|--port [port_number]: changes port number and overrides the port number in the configuration file (default: 9090)") - print( " -P|--adminport [port_number]: changes admin port number and overrides the port number in the configuration file (default: 9095)") - #print( " -V|--vnf-repository: changes the path of the vnf-repository and overrides the path in the configuration file") - print( " --log-socket-host HOST: send logs to this host") - print( " --log-socket-port PORT: send logs using this port (default: 9022)") - print( " --log-file FILE: send logs to this file") + print(" -v|--version: prints current version") + print(" -c|--config [configuration_file]: loads the configuration file (default: openmanod.cfg)") + print(" -h|--help: shows this help") + print(" -p|--port [port_number]: changes port number and overrides the port number in the configuration file (default: 9090)") + print(" -P|--adminport [port_number]: changes admin port number and overrides the port number in the configuration file (default: 9095)") + # print( " -V|--vnf-repository: changes the path of the vnf-repository and overrides the path in the configuration file") + print(" --log-socket-host HOST: send logs to this host") + print(" --log-socket-port PORT: send logs using this port (default: 9022)") + print(" --log-file FILE: send logs to this file") return @@ -154,6 +154,18 @@ def set_logging_file(log_file): if __name__=="__main__": + env_config = { + 'db_host': 'RO_DB_HOST', + 'db_name': 'RO_DB_NAME', + 'db_user': 'RO_DB_USER', + 'db_passwd': 'RO_DB_PASSWORD', + 'db_ovim_host': 'RO_DB_OVIM_HOST', + 'db_ovim_name': 'RO_DB_OVIM_NAME', + 'db_ovim_user': 'RO_DB_OVIM_USER', + 'db_ovim_passwd': 'RO_DB_OVIM_PASSWORD', + 'db_port': 'RO_DB_PORT', + 'db_port': 'RO_DB_PORT', + } # Configure logging step 1 hostname = socket.gethostname() # streamformat = "%(levelname)s (%(module)s:%(lineno)d) %(message)s" @@ -211,7 +223,7 @@ if __name__=="__main__": global_config["version"] = __version__ global_config["version_date"] = version_date #print global_config - # Override parameters obtained by command line + # Override parameters obtained by command line on ENV if port: global_config['http_port'] = port if port_admin: @@ -220,6 +232,13 @@ if __name__=="__main__": global_config['log_socket_host'] = log_socket_host if log_socket_port: global_config['log_socket_port'] = log_socket_port + + # override with ENV + for config_key, env_var in env_config.items(): + if os_getenv(env_var): + global_config[config_key] = os_getenv(env_var) + + # if vnf_repository is not None: # global_config['vnf_repository'] = vnf_repository # else: @@ -286,7 +305,7 @@ if __name__=="__main__": mydb = nfvo_db.nfvo_db(); mydb.connect(global_config['db_host'], global_config['db_user'], global_config['db_passwd'], global_config['db_name']) db_path = osm_ro.__path__[0] + "/database_utils" - if not os.path.exists(db_path + "/migrate_mano_db.sh"): + if not os_path.exists(db_path + "/migrate_mano_db.sh"): db_path = osm_ro.__path__[0] + "/../database_utils" try: r = mydb.get_db_version()